Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VImg.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import '../../../src/stylus/components/_images.styl';
  2. // Components
  3. import VResponsive from '../VResponsive';
  4. // Utils
  5. import { consoleError, consoleWarn } from '../../util/console';
  6. /* @vue/component */
  7. export default VResponsive.extend({
  8. name: 'v-img',
  9. props: {
  10. alt: String,
  11. contain: Boolean,
  12. src: {
  13. type: [String, Object],
  14. default: ''
  15. },
  16. gradient: String,
  17. lazySrc: String,
  18. srcset: String,
  19. sizes: String,
  20. position: {
  21. type: String,
  22. default: 'center center'
  23. },
  24. transition: {
  25. type: [Boolean, String],
  26. default: 'fade-transition'
  27. }
  28. },
  29. data: function data() {
  30. return {
  31. currentSrc: '',
  32. image: null,
  33. isLoading: true,
  34. calculatedAspectRatio: undefined
  35. };
  36. },
  37. computed: {
  38. computedAspectRatio: function computedAspectRatio() {
  39. return this.normalisedSrc.aspect;
  40. },
  41. normalisedSrc: function normalisedSrc() {
  42. return typeof this.src === 'string' ? {
  43. src: this.src,
  44. srcset: this.srcset,
  45. lazySrc: this.lazySrc,
  46. aspect: Number(this.aspectRatio || this.calculatedAspectRatio)
  47. } : {
  48. src: this.src.src,
  49. srcset: this.srcset || this.src.srcset,
  50. lazySrc: this.lazySrc || this.src.lazySrc,
  51. aspect: Number(this.aspectRatio || this.src.aspect || this.calculatedAspectRatio)
  52. };
  53. },
  54. __cachedImage: function __cachedImage() {
  55. if (!(this.normalisedSrc.src || this.normalisedSrc.lazySrc)) return [];
  56. var backgroundImage = [];
  57. var src = this.isLoading ? this.normalisedSrc.lazySrc : this.currentSrc;
  58. if (this.gradient) backgroundImage.push('linear-gradient(' + this.gradient + ')');
  59. if (src) backgroundImage.push('url("' + src + '")');
  60. var image = this.$createElement('div', {
  61. staticClass: 'v-image__image',
  62. class: {
  63. 'v-image__image--preload': this.isLoading,
  64. 'v-image__image--contain': this.contain,
  65. 'v-image__image--cover': !this.contain
  66. },
  67. style: {
  68. backgroundImage: backgroundImage.join(', '),
  69. backgroundPosition: this.position
  70. },
  71. key: +this.isLoading
  72. });
  73. if (!this.transition) return image;
  74. return this.$createElement('transition', {
  75. attrs: {
  76. name: this.transition,
  77. mode: 'in-out'
  78. }
  79. }, [image]);
  80. }
  81. },
  82. watch: {
  83. src: function src() {
  84. if (!this.isLoading) this.init();else this.loadImage();
  85. },
  86. '$vuetify.breakpoint.width': 'getSrc'
  87. },
  88. mounted: function mounted() {
  89. this.init();
  90. },
  91. methods: {
  92. init: function init() {
  93. if (this.normalisedSrc.lazySrc) {
  94. var lazyImg = new Image();
  95. lazyImg.src = this.normalisedSrc.lazySrc;
  96. this.pollForSize(lazyImg, null);
  97. }
  98. /* istanbul ignore else */
  99. if (this.normalisedSrc.src) this.loadImage();
  100. },
  101. onLoad: function onLoad() {
  102. this.getSrc();
  103. this.isLoading = false;
  104. this.$emit('load', this.src);
  105. },
  106. onError: function onError() {
  107. consoleError('Image load failed\n\n' + ('src: ' + this.normalisedSrc.src), this);
  108. this.$emit('error', this.src);
  109. },
  110. getSrc: function getSrc() {
  111. /* istanbul ignore else */
  112. if (this.image) this.currentSrc = this.image.currentSrc || this.image.src;
  113. },
  114. loadImage: function loadImage() {
  115. var _this = this;
  116. var image = new Image();
  117. this.image = image;
  118. image.onload = function () {
  119. /* istanbul ignore if */
  120. if (image.decode) {
  121. image.decode().catch(function (err) {
  122. consoleWarn('Failed to decode image, trying to render anyway\n\n' + ('src: ' + _this.normalisedSrc.src) + (err.message ? '\nOriginal error: ' + err.message : ''), _this);
  123. }).then(_this.onLoad);
  124. } else {
  125. _this.onLoad();
  126. }
  127. };
  128. image.onerror = this.onError;
  129. image.src = this.normalisedSrc.src;
  130. this.sizes && (image.sizes = this.sizes);
  131. this.normalisedSrc.srcset && (image.srcset = this.normalisedSrc.srcset);
  132. this.aspectRatio || this.pollForSize(image);
  133. this.getSrc();
  134. },
  135. pollForSize: function pollForSize(img) {
  136. var _this2 = this;
  137. var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
  138. var poll = function poll() {
  139. var naturalHeight = img.naturalHeight,
  140. naturalWidth = img.naturalWidth;
  141. if (naturalHeight || naturalWidth) {
  142. _this2.calculatedAspectRatio = naturalWidth / naturalHeight;
  143. } else {
  144. timeout != null && setTimeout(poll, timeout);
  145. }
  146. };
  147. poll();
  148. },
  149. __genPlaceholder: function __genPlaceholder() {
  150. if (this.$slots.placeholder) {
  151. var placeholder = this.isLoading ? [this.$createElement('div', {
  152. staticClass: 'v-image__placeholder'
  153. }, this.$slots.placeholder)] : [];
  154. if (!this.transition) return placeholder[0];
  155. return this.$createElement('transition', {
  156. attrs: { name: this.transition }
  157. }, placeholder);
  158. }
  159. }
  160. },
  161. render: function render(h) {
  162. var node = VResponsive.options.render.call(this, h);
  163. node.data.staticClass += ' v-image';
  164. node.data.attrs = {
  165. role: this.alt ? 'img' : undefined,
  166. 'aria-label': this.alt
  167. };
  168. node.children = [this.__cachedSizer, this.__cachedImage, this.__genPlaceholder(), this.genContent()];
  169. return h(node.tag, node.data, node.children);
  170. }
  171. });
  172. //# sourceMappingURL=VImg.js.map