Permalink
Browse files

gallery-2010.05.05-19-39 caridy gallery-preload

  • Loading branch information...
1 parent bd3399d commit ca0cc05cc02e9d958c69f4dba569f7dec3f0a490 YUI Builder committed May 5, 2010
Showing with 116 additions and 0 deletions.
  1. +26 −0 src/gallery-preload/build.properties
  2. +7 −0 src/gallery-preload/build.xml
  3. +83 −0 src/gallery-preload/js/gallery-preload.js
@@ -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=
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- YUI 3 Gallery Component Build File -->
+<project name="preload" default="local">
+ <description>preload Build File</description>
+ <property file="build.properties" />
+ <import file="${builddir}/3.x/bootstrap.xml" description="Default Build Properties and Targets" />
+</project>
@@ -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;
+};

0 comments on commit ca0cc05

Please sign in to comment.