forked from bennadel/JavaScript-Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.htm
377 lines (250 loc) · 9.12 KB
/
index.htm
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<title>
Preloading Images In AngularJS With Promises
</title>
</head>
<body ng-controller="AppController">
<h1>
Preloading Images In AngularJS With Promises
</h1>
<div ng-switch="isLoading">
<!-- BEGIN: Loading View. -->
<p ng-switch-when="true">
Your images are being loaded... {{ percentLoaded }}%
</p>
<!-- END: Loading View. -->
<!-- BEGIN: Results View. -->
<div
ng-switch-when="false"
ng-switch="isSuccessful">
<p ng-switch-when="true">
<img
ng-repeat="src in imageLocations"
ng-src="{{ src }}"
style="width: 100px ; margin-right: 10px ;"
/>
</p>
<p ng-switch-when="false">
<strong>Oops</strong>: One of your images failed to load :(
</p>
</div>
<!-- END: Results View. -->
</div>
<!-- Load scripts. -->
<script type="text/javascript" src="../../vendor/jquery/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="../../vendor/angularjs/angular-1.2.min.js"></script>
<script type="text/javascript">
// Create an application module for our demo.
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I control the root of the application.
app.controller(
"AppController",
function( $scope, preloader ) {
// I keep track of the state of the loading images.
$scope.isLoading = true;
$scope.isSuccessful = false;
$scope.percentLoaded = 0;
// I am the image SRC values to preload and display./
// --
// NOTE: "cache" attribute is to prevent images from caching in the
// browser (for the sake of the demo).
$scope.imageLocations = [
( "./ahhh.jpg?v=1&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=2&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=3&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=4&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=5&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=6&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=7&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=8&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=9&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=10&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=11&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=12&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=13&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=14&cache=" + ( new Date() ).getTime() ),
( "./ahhh.jpg?v=15&cache=" + ( new Date() ).getTime() ),
];
// Preload the images; then, update display when returned.
preloader.preloadImages( $scope.imageLocations ).then(
function handleResolve( imageLocations ) {
// Loading was successful.
$scope.isLoading = false;
$scope.isSuccessful = true;
console.info( "Preload Successful" );
},
function handleReject( imageLocation ) {
// Loading failed on at least one image.
$scope.isLoading = false;
$scope.isSuccessful = false;
console.error( "Image Failed", imageLocation );
console.info( "Preload Failure" );
},
function handleNotify( event ) {
$scope.percentLoaded = event.percent;
console.info( "Percent loaded:", event.percent );
}
);
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I provide a utility class for preloading image objects.
app.factory(
"preloader",
function( $q, $rootScope ) {
// I manage the preloading of image objects. Accepts an array of image URLs.
function Preloader( imageLocations ) {
// I am the image SRC values to preload.
this.imageLocations = imageLocations;
// As the images load, we'll need to keep track of the load/error
// counts when announing the progress on the loading.
this.imageCount = this.imageLocations.length;
this.loadCount = 0;
this.errorCount = 0;
// I am the possible states that the preloader can be in.
this.states = {
PENDING: 1,
LOADING: 2,
RESOLVED: 3,
REJECTED: 4
};
// I keep track of the current state of the preloader.
this.state = this.states.PENDING;
// When loading the images, a promise will be returned to indicate
// when the loading has completed (and / or progressed).
this.deferred = $q.defer();
this.promise = this.deferred.promise;
}
// ---
// STATIC METHODS.
// ---
// I reload the given images [Array] and return a promise. The promise
// will be resolved with the array of image locations.
Preloader.preloadImages = function( imageLocations ) {
var preloader = new Preloader( imageLocations );
return( preloader.load() );
};
// ---
// INSTANCE METHODS.
// ---
Preloader.prototype = {
// Best practice for "instnceof" operator.
constructor: Preloader,
// ---
// PUBLIC METHODS.
// ---
// I determine if the preloader has started loading images yet.
isInitiated: function isInitiated() {
return( this.state !== this.states.PENDING );
},
// I determine if the preloader has failed to load all of the images.
isRejected: function isRejected() {
return( this.state === this.states.REJECTED );
},
// I determine if the preloader has successfully loaded all of the images.
isResolved: function isResolved() {
return( this.state === this.states.RESOLVED );
},
// I initiate the preload of the images. Returns a promise.
load: function load() {
// If the images are already loading, return the existing promise.
if ( this.isInitiated() ) {
return( this.promise );
}
this.state = this.states.LOADING;
for ( var i = 0 ; i < this.imageCount ; i++ ) {
this.loadImageLocation( this.imageLocations[ i ] );
}
// Return the deferred promise for the load event.
return( this.promise );
},
// ---
// PRIVATE METHODS.
// ---
// I handle the load-failure of the given image location.
handleImageError: function handleImageError( imageLocation ) {
this.errorCount++;
// If the preload action has already failed, ignore further action.
if ( this.isRejected() ) {
return;
}
this.state = this.states.REJECTED;
this.deferred.reject( imageLocation );
},
// I handle the load-success of the given image location.
handleImageLoad: function handleImageLoad( imageLocation ) {
this.loadCount++;
// If the preload action has already failed, ignore further action.
if ( this.isRejected() ) {
return;
}
// Notify the progress of the overall deferred. This is different
// than Resolving the deferred - you can call notify many times
// before the ultimate resolution (or rejection) of the deferred.
this.deferred.notify({
percent: Math.ceil( this.loadCount / this.imageCount * 100 ),
imageLocation: imageLocation
});
// If all of the images have loaded, we can resolve the deferred
// value that we returned to the calling context.
if ( this.loadCount === this.imageCount ) {
this.state = this.states.RESOLVED;
this.deferred.resolve( this.imageLocations );
}
},
// I load the given image location and then wire the load / error
// events back into the preloader instance.
// --
// NOTE: The load/error events trigger a $digest.
loadImageLocation: function loadImageLocation( imageLocation ) {
var preloader = this;
// When it comes to creating the image object, it is critical that
// we bind the event handlers BEFORE we actually set the image
// source. Failure to do so will prevent the events from proper
// triggering in some browsers.
var image = $( new Image() )
.load(
function( event ) {
// Since the load event is asynchronous, we have to
// tell AngularJS that something changed.
$rootScope.$apply(
function() {
preloader.handleImageLoad( event.target.src );
// Clean up object reference to help with the
// garbage collection in the closure.
preloader = image = event = null;
}
);
}
)
.error(
function( event ) {
// Since the load event is asynchronous, we have to
// tell AngularJS that something changed.
$rootScope.$apply(
function() {
preloader.handleImageError( event.target.src );
// Clean up object reference to help with the
// garbage collection in the closure.
preloader = image = event = null;
}
);
}
)
.prop( "src", imageLocation )
;
}
};
// Return the factory instance.
return( Preloader );
}
);
</script>
</body>
</html>