forked from yui/yui3-gallery
/
gallery-preload.js
83 lines (69 loc) · 2.54 KB
/
gallery-preload.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
/**
* Y.preload() function for YUI3
* Preload images, CSS and JavaScript files without executing them
* Port of Stoyan Stefanov's Script – http://www.phpied.com/preload-cssjavascript-without-execution/
* Note that since this script relies on YUI Loader, the preloading process will not start until YUI has finished loading.
*
* @module gallery-preload
*/
var _idleQueue = [];
/**
* Preload images, CSS and JavaScript files without executing them
* @namespace Y
* @class preload
* @static
* @param {String|Array} files Collection of files to be loaded
* @return {YUI} Y instance for chaining
*/
Y.preload = function(files) {
var o, ie = Y.UA.ie, doc = Y.config.doc;
// If the first argument is not an array, we can assume the user is trying to load one
// single file or a list of files like: Y.preload ('file1.js', 'file2.css');
files = (Y.Lang.isArray(files)?files:Y.Array(arguments, 0, true));
Y.log("Preloading files: " + files.join(', '), "info", "Y.preload");
Y.Array.each(files, function (f) {
if (ie) {
(new Image()).src = f;
} else {
o = doc.createElement('object');
o.data = f;
o.width = o.height = 0;
doc.body.appendChild(o);
}
});
return Y;
};
/**
* Wait until the user become idle to preload files without executing them. It uses
* Idle Timer Module (by Nicholas) to monitor user actions.
* @namespace Y
* @class preloadOnIdle
* @static
* @param {Array} files Collection of files to be loaded
* @param {int} t (Optional) A new value for the timeout period in ms.
* @return {YUI} Y instance for chaining
*/
Y.preloadOnIdle = function(files, t) {
// Loading Idle Timer Module (by Nicholas) on-demand to avoid setting it
// as a dependency when most of the users will not use it...
Y.log("Loading Idle Timer on-demand to preloading files on idle.", "info", "Y.preloadOnIdle");
// adding the set of files into the queue
_idleQueue.push (files);
Y.use('gallery-idletimer', function(Y) {
Y.IdleTimer.subscribe("idle", function(){
// collecting the first file from the list
var fs = files.shift();
if (fs) {
Y.log("User is idle, lets preload a file", "info", "Y.preloadOnIdle");
Y.preload(fs);
} else {
Y.log("No more files pending for preload, stopping the IdleTimer", "info", "Y.preloadOnIdle");
Y.IdleTimer.stop();
}
});
//start the timer with a default timeout of 30s
Y.log("Starting the timer: "+t, "info", "Y.preloadOnIdle");
Y.IdleTimer.start(t);
});
return Y;
};