-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaploader.js
106 lines (91 loc) · 2.29 KB
/
yaploader.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
/**
* Constructor for Preloader Class
* @class
* @classdesc Preloads required assets
* @param {PreloadItems} items The items to preload.
*/
function Preloader(items) {
var _Items = items;
var _LoadedCount = 0;
this.preloadComplete = null;
var _PreloadedItems = new Array();
/**
* Starts the preload process.
*/
this.preload = function()
{
var _this = this;
if (_Items != null)
{
for (var i=0; i< _Items.length; i++)
{
var item = _Items[i];
switch (item.type)
{
case "image":
var image = new Image();
$(image).on('load', {id: item.identifier, imageRef: image},function (event) {
_this.itemLoaded(event.data.id, event.data.imageRef);
});
image.src = item.src;
break;
case "audio":
var audio = new Audio();
$(audio).on('canplaythrough', {id: item.identifier, audioRef: audio},function (event) {
_this.itemLoaded(event.data.id, event.data.audioRef);
});
audio.src = item.src;
break;
case "video":
var video = new Video();
$(video).on('canplaythrough', {id: item.identifier, videoRef: audio},function (event) {
_this.itemLoaded(event.data.id, event.data.videoRef);
});
video.src = item.src;
break;
}
}
} else {
//Nothing to load so fire onload complete
this.onPreLoadComplete();
}
}
/**
* Once an item has loaded used to get item
* @param {string} id The item to get
*/
this.getItem = function(id) {
return _PreloadedItems[id];
}
/**
* Called when an item has loaded
* @param {string} id The id of the item that has loaded.
* @param {Object} item The item that has loaded.
*/
this.itemLoaded = function(id, item) {
_LoadedCount++;
_PreloadedItems[id] = item;
this.onProgressChanged();
if (_LoadedCount == _Items.length)
{
this.onPreLoadComplete();
}
}
/**
* Fires an event when all required assets have loaded completly
*/
this.onPreLoadComplete = function() {
if (this.preloadComplete !=null) {
this.preloadComplete(true);
}
}
/**
* Fires an optional progress event. Useful for a progress bar.
*/
this.onProgressChanged= function() {
if (this.progressChanged !=null) {
var percentDone = (_LoadedCount/ _Items.length) * 100;
this.progressChanged(percentDone);
}
}
}