-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageLoader.js
162 lines (114 loc) · 3.85 KB
/
ImageLoader.js
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
import Controller from "properjs-controller";
export default class ImageLoader extends Controller {
constructor ( options ) {
super();
this._executor = options.executor ? options.executor.bind( this ) : this.executor.bind( this );
this._elements = options.elements || []; // allow empty?
this._property = options.property || "data-src";
this._loadType = options.loadType || "async";
this._numLoaded = 0;
this._num2Load = this._elements.length;
this._transitionDelay = options.transitionDelay || 0;
this._transitionDuration = options.transitionDuration || 400;
this._resolved = false;
if ( !this._num2Load ) {
this.resolve();
} else if ( this._loadType === "sync" ) {
this.sync();
} else {
this.async();
}
}
executor ( el ) {
const bounds = el.getBoundingClientRect();
return (bounds.top < window.innerHeight && bounds.bottom > 0);
}
async () {
this.go(() => {
if ( this._resolved ) {
this.stop();
} else {
this.handle();
}
});
}
sync () {
const syncLoad = () => {
const elem = this._elements[ this._numLoaded ];
this._numLoaded++;
this.load( elem, ( error ) => {
if ( !error && !this._resolved ) {
syncLoad();
}
});
};
syncLoad();
}
load ( element, callback ) {
let image = null;
let timeout = null;
const isFunc = (typeof callback === "function");
const isImage = (element.nodeName === "IMG");
const source = element.getAttribute( this._property );
if ( isImage ) {
image = element;
} else {
image = new Image();
}
element.setAttribute( "data-imageloader", false );
timeout = setTimeout(() => {
clearTimeout( timeout );
image.onload = () => {
this.fire( "load", element );
if ( !isImage ) {
element.style.backgroundImage = `url(${source})`;
image = null;
}
element.setAttribute( "data-imageloader", true );
timeout = setTimeout(() => {
clearTimeout( timeout );
if ( (this._numLoaded === this._num2Load) && !this._resolved ) {
this.resolve( true );
} else if ( isFunc ) {
// Errors first
callback( false );
}
}, this._transitionDuration );
};
image.onerror = () => {
this.fire( "error", element );
if ( (this._numLoaded === this._num2Load) && !this._resolved ) {
this.resolve( true );
} else if ( isFunc ) {
// Errors first
callback( true );
}
};
image.src = source;
}, this._transitionDelay );
return this;
}
handle () {
const elems = this.getNotLoaded();
const len = elems.length;
for ( let i = 0; i < len; i++ ) {
if ( this._executor( elems[ i ] ) ) {
this._numLoaded++;
this.load( elems[ i ] );
}
}
}
getNotLoaded () {
const elems = [];
for ( var i = 0; i < this._elements.length; i++ ) {
if ( !this._elements[ i ].getAttribute( "data-imageloader" ) ) {
elems.push( this._elements[ i ] );
}
}
return elems;
}
resolve () {
this._resolved = true;
this.fire( "done" );
}
}