From 0ea2b16f3199e1dc9f746f688d7530e6e3d94570 Mon Sep 17 00:00:00 2001 From: pixel Date: Wed, 16 Jan 2008 14:23:49 +0000 Subject: Adding the generic image preloading javascript to the htmllib. --- lib/htmllib.lua | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/htmllib.lua b/lib/htmllib.lua index 9603b4f..ab10530 100644 --- a/lib/htmllib.lua +++ b/lib/htmllib.lua @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: htmllib.lua,v 1.1 2008-01-15 18:20:23 pixel Exp $ */ +/* $Id: htmllib.lua,v 1.2 2008-01-16 14:23:49 pixel Exp $ */ ]]-- @@ -64,7 +64,6 @@ function removeAllOptions(selectbox) } } - function addOption(selectbox, value, text ) { selectbox = getObjById(selectbox); @@ -75,6 +74,67 @@ function addOption(selectbox, value, text ) selectbox.options.add(optn); } +//============================================================================= +// Image Preloader +function ImagePreloader(images, callback) { + // store the callback + this.callback = callback; + + // initialize internal state. + this.nLoaded = 0; + this.nProcessed = 0; + this.aImages = new Array; + + // record the number of images. + this.nImages = images.length; + + // for each image, call preload() + for (var i = 0; i < images.length; i++) + this.preload(images[i]); +} + +ImagePreloader.prototype.preload = function(image) { + // create new Image object and add to array + var oImage = new Image; + this.aImages.push(oImage); + + // set up event handlers for the Image object + oImage.onload = ImagePreloader.prototype.onload; + oImage.onerror = ImagePreloader.prototype.onerror; + oImage.onabort = ImagePreloader.prototype.onabort; + + // assign pointer back to this. + oImage.oImagePreloader = this; + oImage.bLoaded = false; + oImage.source = image; + + // assign the .src property of the Image object + oImage.src = image; +} + +ImagePreloader.prototype.onComplete = function() { + this.nProcessed++; + if (this.nProcessed == this.nImages) + this.callback(this.aImages); +} + +ImagePreloader.prototype.onload = function() { + this.bLoaded = true; + this.oImagePreloader.nLoaded++; + this.oImagePreloader.onComplete(); +} + +ImagePreloader.prototype.onerror = function() { + this.bError = true; + this.oImagePreloader.onComplete(); +} + +ImagePreloader.prototype.onabort = function() { + this.bAbort = true; + this.oImagePreloader.onComplete(); +} +//============================================================================= + ///////////////////////////////////////////////////////////////////// /*------------------------------------------------------------------- AKX GetVariables -- cgit v1.2.3