xwt
112 分钟以前 8a5fc169c691543f60109b2b3a4e000762f247c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
  loadImageAsync,
  ObjectKeys,
  noop
} from './util'
 
let imageCache = {}
 
// el: {
//     state,
//     src,
//     error,
//     loading
// }
 
export default class ReactiveListener {
  constructor ({ el, src, error, loading, bindType, $parent, options, elRenderer }) {
    this.el = el
    this.src = src
    this.error = error
    this.loading = loading
    this.bindType = bindType
    this.attempt = 0
 
    this.naturalHeight = 0
    this.naturalWidth = 0
 
    this.options = options
 
    this.rect = null
 
    this.$parent = $parent
    this.elRenderer = elRenderer
 
    this.performanceData = {
      init: Date.now(),
      loadStart: 0,
      loadEnd: 0
    }
 
    this.filter()
    this.initState()
    this.render('loading', false)
  }
 
  /*
   * init listener state
   * @return
   */
  initState () {
    this.el.dataset.src = this.src
    this.state = {
      error: false,
      loaded: false,
      rendered: false
    }
  }
 
  /*
   * record performance
   * @return
   */
  record (event) {
    this.performanceData[event] = Date.now()
  }
 
  /*
   * update image listener data
   * @param  {String} image uri
   * @param  {String} loading image uri
   * @param  {String} error image uri
   * @return
   */
  update ({ src, loading, error }) {
    const oldSrc = this.src
    this.src = src
    this.loading = loading
    this.error = error
    this.filter()
    if (oldSrc !== this.src) {
      this.attempt = 0
      this.initState()
    }
  }
 
  /*
   * get el node rect
   * @return
   */
  getRect () {
    this.rect = this.el.getBoundingClientRect()
  }
 
  /*
   *  check el is in view
   * @return {Boolean} el is in view
   */
  checkInView () {
    this.getRect()
    return (this.rect.top < window.innerHeight * this.options.preLoad && this.rect.bottom > this.options.preLoadTop) &&
            (this.rect.left < window.innerWidth * this.options.preLoad && this.rect.right > 0)
  }
 
  /*
   * listener filter
   */
  filter () {
    ObjectKeys(this.options.filter).map(key => {
      this.options.filter[key](this, this.options)
    })
  }
 
  /*
   * render loading first
   * @params cb:Function
   * @return
   */
  renderLoading (cb) {
    loadImageAsync({
      src: this.loading
    }, data => {
      this.render('loading', false)
      cb()
    }, () => {
      // handler `loading image` load failed
      cb()
      if (!this.options.silent) console.warn(`VueLazyload log: load failed with loading image(${this.loading})`)
    })
  }
 
  /*
   * try load image and  render it
   * @return
   */
  load (onFinish = noop) {
    if ((this.attempt > this.options.attempt - 1) && this.state.error) {
      if (!this.options.silent) console.log(`VueLazyload log: ${this.src} tried too more than ${this.options.attempt} times`)
      onFinish()
      return
    }
 
    if (this.state.loaded || imageCache[this.src]) {
      this.state.loaded = true
      onFinish()
      return this.render('loaded', true)
    }
 
    this.renderLoading(() => {
      this.attempt++
 
      this.record('loadStart')
 
      loadImageAsync({
        src: this.src
      }, data => {
        this.naturalHeight = data.naturalHeight
        this.naturalWidth = data.naturalWidth
        this.state.loaded = true
        this.state.error = false
        this.record('loadEnd')
        this.render('loaded', false)
        imageCache[this.src] = 1
        onFinish()
      }, err => {
        !this.options.silent && console.error(err)
        this.state.error = true
        this.state.loaded = false
        this.render('error', false)
      })
    })
  }
 
  /*
   * render image
   * @param  {String} state to render // ['loading', 'src', 'error']
   * @param  {String} is form cache
   * @return
   */
  render (state, cache) {
    this.elRenderer(this, state, cache)
  }
 
  /*
   * output performance data
   * @return {Object} performance data
   */
  performance () {
    let state = 'loading'
    let time = 0
 
    if (this.state.loaded) {
      state = 'loaded'
      time = (this.performanceData.loadEnd - this.performanceData.loadStart) / 1000
    }
 
    if (this.state.error) state = 'error'
 
    return {
      src: this.src,
      state,
      time
    }
  }
 
  /*
   * destroy
   * @return
   */
  destroy () {
    this.el = null
    this.src = null
    this.error = null
    this.loading = null
    this.bindType = null
    this.attempt = 0
  }
}