|
| 1 | +import { LazyOptions, LazyBinding, LazyElement } from '../types/index.js' |
| 2 | + |
| 3 | +export class LazyCore { |
| 4 | + private useNative: boolean |
| 5 | + private rootMargin: string |
| 6 | + private type: 'loading' | 'observer' | 'none' = 'loading' |
| 7 | + private io?: IntersectionObserver |
| 8 | + |
| 9 | + constructor(options: LazyOptions) { |
| 10 | + this.useNative = options?.useNative ?? true |
| 11 | + this.rootMargin = options?.rootMargin ?? '200px' |
| 12 | + this.init() |
| 13 | + } |
| 14 | + |
| 15 | + private init() { |
| 16 | + if (this.useNative && 'loading' in HTMLImageElement.prototype) { |
| 17 | + this.type = 'loading' |
| 18 | + } else if (window.IntersectionObserver) { |
| 19 | + this.type = 'observer' |
| 20 | + this.setObserver() |
| 21 | + } else { |
| 22 | + this.type = 'none' |
| 23 | + error('Your browser does not support IntersectionObserver. https://github.com/tolking/vue-lazy-loading#Browser Support') |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + bind(el: Element, binding: LazyBinding) { |
| 28 | + !el.hasAttribute('loading') && el.setAttribute('loading', 'lazy') |
| 29 | + this.update(el, binding) |
| 30 | + } |
| 31 | + |
| 32 | + update(el: Element, { oldValue, value, arg }: LazyBinding) { |
| 33 | + if (oldValue === value) return |
| 34 | + if (arg) { |
| 35 | + switch (arg) { |
| 36 | + case 'bg': |
| 37 | + case 'background': |
| 38 | + if ((el as LazyElement).style.backgroundImage) { |
| 39 | + (el as LazyElement).style.backgroundImage = '' |
| 40 | + } |
| 41 | + if (this.type === 'loading' || this.type === 'observer') { |
| 42 | + if (!this.io) { |
| 43 | + this.setObserver() |
| 44 | + } |
| 45 | + el.setAttribute('data-bg', value) |
| 46 | + this.io?.observe(el) |
| 47 | + } else { |
| 48 | + (el as LazyElement).style.backgroundImage = `url(${value})` |
| 49 | + } |
| 50 | + break; |
| 51 | + case 'set': |
| 52 | + case 'srcset': |
| 53 | + el.hasAttribute('srcset') && el.removeAttribute('srcset') |
| 54 | + if (this.type === 'loading') { |
| 55 | + el.setAttribute('srcset', value) |
| 56 | + } else if (this.type === 'observer') { |
| 57 | + el.setAttribute('data-srcset', value) |
| 58 | + this.io?.observe(el) |
| 59 | + } else { |
| 60 | + el.setAttribute('srcset', value) |
| 61 | + } |
| 62 | + break |
| 63 | + default: |
| 64 | + error('One of [v-lazy="URL", v-lazy:bg="URL", v-lazy:background="URL", v-lazy:set="URL"]') |
| 65 | + break; |
| 66 | + } |
| 67 | + } else { |
| 68 | + el.hasAttribute('src') && el.removeAttribute('src') |
| 69 | + if (this.type === 'loading') { |
| 70 | + el.setAttribute('src', value) |
| 71 | + } else if (this.type === 'observer') { |
| 72 | + el.setAttribute('data-src', value) |
| 73 | + this.io?.observe(el) |
| 74 | + } else { |
| 75 | + el.setAttribute('src', value) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + unbind(el: Element) { |
| 81 | + if (this.type === 'observer') { |
| 82 | + this.io?.unobserve(el) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private setObserver() { |
| 87 | + this.io = new IntersectionObserver(entries => { |
| 88 | + entries.forEach(item => { |
| 89 | + if (item.isIntersecting) { |
| 90 | + const src = (item.target as LazyElement).dataset?.src |
| 91 | + const srcset = (item.target as LazyElement).dataset?.srcset |
| 92 | + const bg = (item.target as LazyElement).dataset?.bg |
| 93 | + |
| 94 | + if (src) { |
| 95 | + (item.target as LazyElement).src = src |
| 96 | + } |
| 97 | + if (srcset) { |
| 98 | + (item.target as LazyElement).srcset = srcset |
| 99 | + } |
| 100 | + if (bg) { |
| 101 | + (item.target as LazyElement).style.backgroundImage = `url(${bg})` |
| 102 | + } |
| 103 | + this.io?.unobserve(item.target) |
| 104 | + } |
| 105 | + }) |
| 106 | + }, { |
| 107 | + rootMargin: this.rootMargin |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export function getVueVersion(Vue: any) { |
| 113 | + return Number(Vue.version.split('.')[0]) |
| 114 | +} |
| 115 | + |
| 116 | +export function error(msg: string) { |
| 117 | + process.env.NODE_ENV === 'development' && |
| 118 | + console.error('[vue-lazy-loading error]: ' + msg); |
| 119 | +} |
0 commit comments