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.7KB

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