|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | class ImgLoader {
|
8 |
| - /** |
9 |
| - * 初始化方法,在页面的 onLoad 方法中调用,传入 Page 对象及图片加载完成的默认回调 |
10 |
| - */ |
11 |
| - constructor(pageContext, defaultCallback) { |
12 |
| - this.page = pageContext |
13 |
| - this.defaultCallback = defaultCallback || function(){} |
14 |
| - this.callbacks = {} |
15 |
| - this.imgInfo = {} |
| 8 | + /** |
| 9 | + * 初始化方法,在页面的 onLoad 方法中调用,传入 Page 对象及图片加载完成的默认回调 |
| 10 | + */ |
| 11 | + constructor(pageContext, defaultCallback) { |
| 12 | + this.page = pageContext |
| 13 | + this.defaultCallback = defaultCallback || function(){} |
| 14 | + this.callbacks = {} |
| 15 | + this.imgInfo = {} |
16 | 16 |
|
17 |
| - this.page.data.imgLoadList = [] //下载队列 |
18 |
| - this.page._imgOnLoad = this._imgOnLoad.bind(this) |
19 |
| - this.page._imgOnLoadError = this._imgOnLoadError.bind(this) |
20 |
| - } |
| 17 | + this.page.data.imgLoadList = [] //下载队列 |
| 18 | + this.page._imgOnLoad = this._imgOnLoad.bind(this) |
| 19 | + this.page._imgOnLoadError = this._imgOnLoadError.bind(this) |
| 20 | + } |
21 | 21 |
|
22 |
| - /** |
23 |
| - * 加载图片 |
24 |
| - * |
25 |
| - * @param {String} src 图片地址 |
26 |
| - * @param {Function} callback 加载完成后的回调(可选),第一个参数个错误信息,第二个为图片信息 |
27 |
| - */ |
28 |
| - load(src, callback) { |
29 |
| - if (!src) return; |
| 22 | + /** |
| 23 | + * 加载图片 |
| 24 | + * |
| 25 | + * @param {String} src 图片地址 |
| 26 | + * @param {Function} callback 加载完成后的回调(可选),第一个参数个错误信息,第二个为图片信息 |
| 27 | + */ |
| 28 | + load(src, callback) { |
| 29 | + if (!src) return; |
30 | 30 |
|
31 |
| - let list = this.page.data.imgLoadList, |
32 |
| - imgInfo = this.imgInfo[src] |
| 31 | + let list = this.page.data.imgLoadList, |
| 32 | + imgInfo = this.imgInfo[src] |
33 | 33 |
|
34 |
| - if (callback) |
35 |
| - this.callbacks[src] = callback |
| 34 | + if (callback) |
| 35 | + this.callbacks[src] = callback |
36 | 36 |
|
37 |
| - //已经加载成功过的,直接回调 |
38 |
| - if (imgInfo) { |
39 |
| - this._runCallback(null, { |
40 |
| - src: src, |
41 |
| - width: imgInfo.width, |
42 |
| - height: imgInfo.height |
43 |
| - }) |
44 |
| - |
45 |
| - //新的未在下载队列中的 |
46 |
| - } else if (list.indexOf(src) == -1) { |
47 |
| - list.push(src) |
48 |
| - this.page.setData({ 'imgLoadList': list }) |
49 |
| - } |
50 |
| - } |
| 37 | + //已经加载成功过的,直接回调 |
| 38 | + if (imgInfo) { |
| 39 | + this._runCallback(null, { |
| 40 | + src: src, |
| 41 | + width: imgInfo.width, |
| 42 | + height: imgInfo.height |
| 43 | + }) |
| 44 | + |
| 45 | + //新的未在下载队列中的 |
| 46 | + } else if (list.indexOf(src) == -1) { |
| 47 | + list.push(src) |
| 48 | + this.page.setData({ 'imgLoadList': list }) |
| 49 | + } |
| 50 | + } |
51 | 51 |
|
52 |
| - _imgOnLoad(ev) { |
53 |
| - let src = ev.currentTarget.dataset.src, |
54 |
| - width = ev.detail.width, |
55 |
| - height = ev.detail.height |
| 52 | + _imgOnLoad(ev) { |
| 53 | + let src = ev.currentTarget.dataset.src, |
| 54 | + width = ev.detail.width, |
| 55 | + height = ev.detail.height |
56 | 56 |
|
57 |
| - //记录已下载图片的尺寸信息 |
58 |
| - this.imgInfo[src] = { width, height } |
59 |
| - this._removeFromLoadList(src) |
| 57 | + //记录已下载图片的尺寸信息 |
| 58 | + this.imgInfo[src] = { width, height } |
| 59 | + this._removeFromLoadList(src) |
60 | 60 |
|
61 |
| - this._runCallback(null, { src, width, height }) |
62 |
| - } |
| 61 | + this._runCallback(null, { src, width, height }) |
| 62 | + } |
63 | 63 |
|
64 |
| - _imgOnLoadError(ev) { |
65 |
| - let src = ev.currentTarget.dataset.src |
66 |
| - this._removeFromLoadList(src) |
67 |
| - this._runCallback('Loading failed', { src }) |
68 |
| - } |
| 64 | + _imgOnLoadError(ev) { |
| 65 | + let src = ev.currentTarget.dataset.src |
| 66 | + this._removeFromLoadList(src) |
| 67 | + this._runCallback('Loading failed', { src }) |
| 68 | + } |
69 | 69 |
|
70 |
| - //将图片从下载队列中移除 |
71 |
| - _removeFromLoadList(src) { |
72 |
| - let list = this.page.data.imgLoadList |
73 |
| - list.splice(list.indexOf(src), 1) |
74 |
| - this.page.setData({ 'imgLoadList': list }) |
75 |
| - } |
| 70 | + //将图片从下载队列中移除 |
| 71 | + _removeFromLoadList(src) { |
| 72 | + let list = this.page.data.imgLoadList |
| 73 | + list.splice(list.indexOf(src), 1) |
| 74 | + this.page.setData({ 'imgLoadList': list }) |
| 75 | + } |
76 | 76 |
|
77 |
| - //执行回调 |
78 |
| - _runCallback(err, data) { |
79 |
| - let callback = this.callbacks[data.src] || this.defaultCallback |
80 |
| - callback(err, data) |
81 |
| - delete this.callbacks[data.src] |
82 |
| - } |
| 77 | + //执行回调 |
| 78 | + _runCallback(err, data) { |
| 79 | + let callback = this.callbacks[data.src] || this.defaultCallback |
| 80 | + callback(err, data) |
| 81 | + delete this.callbacks[data.src] |
| 82 | + } |
83 | 83 | }
|
84 | 84 |
|
85 | 85 | module.exports = ImgLoader
|
0 commit comments