From ca0cc05cc02e9d958c69f4dba569f7dec3f0a490 Mon Sep 17 00:00:00 2001 From: YUI Builder Date: Wed, 5 May 2010 12:40:16 -0700 Subject: [PATCH] gallery-2010.05.05-19-39 caridy gallery-preload --- src/gallery-preload/build.properties | 26 +++++++ src/gallery-preload/build.xml | 7 ++ src/gallery-preload/js/gallery-preload.js | 83 +++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/gallery-preload/build.properties create mode 100644 src/gallery-preload/build.xml create mode 100644 src/gallery-preload/js/gallery-preload.js diff --git a/src/gallery-preload/build.properties b/src/gallery-preload/build.properties new file mode 100644 index 0000000000..e4787757f4 --- /dev/null +++ b/src/gallery-preload/build.properties @@ -0,0 +1,26 @@ +# preload Build Properties + +# As long as the 'builder' project is cloned to the default folder +# next to the 'yui3-gallery' project folder, the 'builddir' property does not +# need to be changed +# +# If the 'builder' project is checked out to an alternate location, this +# property should be updated to point to the checkout location. +builddir=../../../builder/componentbuild + +# The name of the component. E.g. event, attribute, widget +component=gallery-preload + +# The list of files which should be concatenated to create the component. +# NOTE: For a css component (e.g. cssfonts, cssgrids etc.) use component.cssfiles instead. +# component.jsfiles=preload.js, preloadHelperClass.js, preloadSubComponentClass.js +component.jsfiles=gallery-preload.js + +# The list of modules this component requires. Used to set up the Y.add module call for YUI 3. +component.requires=yui + +# The list of modules this component supersedes. Used to set up the Y.add module call for YUI 3. +component.supersedes= + +# The list of modules that are optional for this module. Used to set up the Y.add module call for YUI 3. +component.optional= diff --git a/src/gallery-preload/build.xml b/src/gallery-preload/build.xml new file mode 100644 index 0000000000..7b6d9bcc6d --- /dev/null +++ b/src/gallery-preload/build.xml @@ -0,0 +1,7 @@ + + + + preload Build File + + + \ No newline at end of file diff --git a/src/gallery-preload/js/gallery-preload.js b/src/gallery-preload/js/gallery-preload.js new file mode 100644 index 0000000000..7d6377bef6 --- /dev/null +++ b/src/gallery-preload/js/gallery-preload.js @@ -0,0 +1,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; +}; \ No newline at end of file