From 8ba072a1a928e391809aca1b114b926ea7d51cba Mon Sep 17 00:00:00 2001
From: thatcher <thatcher.christopher@gmail.com>
Date: Tue, 27 Dec 2011 18:17:24 -0500
Subject: [PATCH] removed ImageLoader abstraction (loadImage is noe method of
 Drawer) since its constructor was only called once and the resulting object
 was kept psuedo-private.

---
 build.xml          |   1 -
 openseadragon.js   | 154 ++++++++++++++++++++++-----------------------
 src/drawer.js      |  79 ++++++++++++++++++++++-
 src/imageloader.js |  74 ----------------------
 4 files changed, 152 insertions(+), 156 deletions(-)

diff --git a/build.xml b/build.xml
index 34f3182b..cbdda4ec 100644
--- a/build.xml
+++ b/build.xml
@@ -28,7 +28,6 @@
             <file name="src/strings.js" />
             <file name="src/point.js" />
             <file name="src/profiler.js" />
-            <file name="src/imageloader.js" />
             <file name="src/tilesource.js" />
             <file name="src/dzitilesource.js" />
             <file name="src/button.js" />
diff --git a/openseadragon.js b/openseadragon.js
index 50ce6167..6fb0ac0a 100644
--- a/openseadragon.js
+++ b/openseadragon.js
@@ -2389,81 +2389,6 @@ $.Profiler.prototype = {
 
 }( OpenSeadragon ));
 
-(function( $ ){
-
-    //TODO: make TIMEOUT configurable
-    var TIMEOUT = 5000;
-    
-$.ImageLoader = function( imageLoaderLimit ) {
-    this.downloading = 0;
-    this.imageLoaderLimit = imageLoaderLimit;
-};
-
-$.ImageLoader.prototype = {
-    loadImage: function(src, callback) {
-        var _this = this,
-            loading = false,
-            image,
-            jobid,
-            complete;
-
-        if ( !this.imageLoaderLimit || this.downloading < this.imageLoaderLimit ) {
-            
-            this.downloading++;
-
-            image = new Image();
-
-            complete = function( imagesrc ){
-                _this.downloading--;
-                if (typeof ( callback ) == "function") {
-                    try {
-                        callback( image );
-                    } catch ( e ) {
-                        $.Debug.error(
-                            e.name + " while executing " + src +" callback: " + e.message, 
-                            e
-                        );
-                    }
-                }
-            };
-
-            image.onload = function(){
-                finish( image, complete, true );
-            };
-
-            image.onabort = image.onerror = function(){
-                finish( image, complete, false );
-            };
-
-            jobid = window.setTimeout( function(){
-                finish( image, complete, false, jobid );
-            }, TIMEOUT );
-
-            loading   = true;
-            image.src = src;
-        }
-
-        return loading;
-    }
-};
-
-function finish( image, callback, successful, jobid ){
-
-    image.onload = null;
-    image.onabort = null;
-    image.onerror = null;
-
-    if ( jobid ) {
-        window.clearTimeout( jobid );
-    }
-    window.setTimeout( function() {
-        callback( image.src, successful ? image : null);
-    }, 1 );
-
-};
-
-}( OpenSeadragon ));
-
 (function( $ ){
 
 $.TileSource = function(width, height, tileSize, tileOverlap, minLevel, maxLevel) {
@@ -3398,6 +3323,9 @@ $.Tile.prototype = {
 var QUOTA = 100;    // the max number of images we should keep in memory
 var MIN_PIXEL_RATIO = 0.5;  // the most shrunk a tile should be
 
+//TODO: make TIMEOUT configurable
+var TIMEOUT = 5000;
+
 var browser = $.Utils.getBrowser();
 var browserVer = $.Utils.getBrowserVersion();
 
@@ -3419,7 +3347,9 @@ $.Drawer = function(source, viewport, elmt) {
     this._source = source;
     this.config = this._viewport.config;
 
-    this._imageLoader = new $.ImageLoader(this.config.imageLoaderLimit);
+    this.downloading = 0;
+    this.imageLoaderLimit = this.config.imageLoaderLimit;
+
     this._profiler = new $.Profiler();
 
     this._minLevel = source.minLevel;
@@ -3515,8 +3445,15 @@ $.Drawer.prototype = {
     },
 
     _loadTile: function(tile, time) {
-        tile.loading = this._imageLoader.loadImage(tile.url,
-                    $.Utils.createCallback(null, $.delegate(this, this._onTileLoad), tile, time));
+        tile.loading = this.loadImage(
+            tile.url,
+            $.Utils.createCallback(
+                null, 
+                $.delegate(this, this._onTileLoad), 
+                tile, 
+                time
+            )
+        );
     },
 
     _onTileLoad: function(tile, time, image) {
@@ -3966,9 +3903,70 @@ $.Drawer.prototype = {
     },
 
     idle: function() {
+    },
+
+    loadImage: function(src, callback) {
+        var _this = this,
+            loading = false,
+            image,
+            jobid,
+            complete;
+
+        if ( !this.imageLoaderLimit || this.downloading < this.imageLoaderLimit ) {
+            
+            this.downloading++;
+
+            image = new Image();
+
+            complete = function( imagesrc ){
+                _this.downloading--;
+                if (typeof ( callback ) == "function") {
+                    try {
+                        callback( image );
+                    } catch ( e ) {
+                        $.Debug.error(
+                            e.name + " while executing " + src +" callback: " + e.message, 
+                            e
+                        );
+                    }
+                }
+            };
+
+            image.onload = function(){
+                finishLoadingImage( image, complete, true );
+            };
+
+            image.onabort = image.onerror = function(){
+                finishLoadingImage( image, complete, false );
+            };
+
+            jobid = window.setTimeout( function(){
+                finishLoadingImage( image, complete, false, jobid );
+            }, TIMEOUT );
+
+            loading   = true;
+            image.src = src;
+        }
+
+        return loading;
     }
 };
 
+function finishLoadingImage( image, callback, successful, jobid ){
+
+    image.onload = null;
+    image.onabort = null;
+    image.onerror = null;
+
+    if ( jobid ) {
+        window.clearTimeout( jobid );
+    }
+    window.setTimeout( function() {
+        callback( image.src, successful ? image : null);
+    }, 1 );
+
+};
+
 }( OpenSeadragon ));
 
 (function( $ ){
diff --git a/src/drawer.js b/src/drawer.js
index 52b975c9..1a3e83fc 100644
--- a/src/drawer.js
+++ b/src/drawer.js
@@ -4,6 +4,9 @@
 var QUOTA = 100;    // the max number of images we should keep in memory
 var MIN_PIXEL_RATIO = 0.5;  // the most shrunk a tile should be
 
+//TODO: make TIMEOUT configurable
+var TIMEOUT = 5000;
+
 var browser = $.Utils.getBrowser();
 var browserVer = $.Utils.getBrowserVersion();
 
@@ -25,7 +28,9 @@ $.Drawer = function(source, viewport, elmt) {
     this._source = source;
     this.config = this._viewport.config;
 
-    this._imageLoader = new $.ImageLoader(this.config.imageLoaderLimit);
+    this.downloading = 0;
+    this.imageLoaderLimit = this.config.imageLoaderLimit;
+
     this._profiler = new $.Profiler();
 
     this._minLevel = source.minLevel;
@@ -121,8 +126,15 @@ $.Drawer.prototype = {
     },
 
     _loadTile: function(tile, time) {
-        tile.loading = this._imageLoader.loadImage(tile.url,
-                    $.Utils.createCallback(null, $.delegate(this, this._onTileLoad), tile, time));
+        tile.loading = this.loadImage(
+            tile.url,
+            $.Utils.createCallback(
+                null, 
+                $.delegate(this, this._onTileLoad), 
+                tile, 
+                time
+            )
+        );
     },
 
     _onTileLoad: function(tile, time, image) {
@@ -572,7 +584,68 @@ $.Drawer.prototype = {
     },
 
     idle: function() {
+    },
+
+    loadImage: function(src, callback) {
+        var _this = this,
+            loading = false,
+            image,
+            jobid,
+            complete;
+
+        if ( !this.imageLoaderLimit || this.downloading < this.imageLoaderLimit ) {
+            
+            this.downloading++;
+
+            image = new Image();
+
+            complete = function( imagesrc ){
+                _this.downloading--;
+                if (typeof ( callback ) == "function") {
+                    try {
+                        callback( image );
+                    } catch ( e ) {
+                        $.Debug.error(
+                            e.name + " while executing " + src +" callback: " + e.message, 
+                            e
+                        );
+                    }
+                }
+            };
+
+            image.onload = function(){
+                finishLoadingImage( image, complete, true );
+            };
+
+            image.onabort = image.onerror = function(){
+                finishLoadingImage( image, complete, false );
+            };
+
+            jobid = window.setTimeout( function(){
+                finishLoadingImage( image, complete, false, jobid );
+            }, TIMEOUT );
+
+            loading   = true;
+            image.src = src;
+        }
+
+        return loading;
     }
 };
 
+function finishLoadingImage( image, callback, successful, jobid ){
+
+    image.onload = null;
+    image.onabort = null;
+    image.onerror = null;
+
+    if ( jobid ) {
+        window.clearTimeout( jobid );
+    }
+    window.setTimeout( function() {
+        callback( image.src, successful ? image : null);
+    }, 1 );
+
+};
+
 }( OpenSeadragon ));
diff --git a/src/imageloader.js b/src/imageloader.js
index 2cfae0cd..8b137891 100644
--- a/src/imageloader.js
+++ b/src/imageloader.js
@@ -1,75 +1 @@
 
-(function( $ ){
-
-    //TODO: make TIMEOUT configurable
-    var TIMEOUT = 5000;
-    
-$.ImageLoader = function( imageLoaderLimit ) {
-    this.downloading = 0;
-    this.imageLoaderLimit = imageLoaderLimit;
-};
-
-$.ImageLoader.prototype = {
-    loadImage: function(src, callback) {
-        var _this = this,
-            loading = false,
-            image,
-            jobid,
-            complete;
-
-        if ( !this.imageLoaderLimit || this.downloading < this.imageLoaderLimit ) {
-            
-            this.downloading++;
-
-            image = new Image();
-
-            complete = function( imagesrc ){
-                _this.downloading--;
-                if (typeof ( callback ) == "function") {
-                    try {
-                        callback( image );
-                    } catch ( e ) {
-                        $.Debug.error(
-                            e.name + " while executing " + src +" callback: " + e.message, 
-                            e
-                        );
-                    }
-                }
-            };
-
-            image.onload = function(){
-                finish( image, complete, true );
-            };
-
-            image.onabort = image.onerror = function(){
-                finish( image, complete, false );
-            };
-
-            jobid = window.setTimeout( function(){
-                finish( image, complete, false, jobid );
-            }, TIMEOUT );
-
-            loading   = true;
-            image.src = src;
-        }
-
-        return loading;
-    }
-};
-
-function finish( image, callback, successful, jobid ){
-
-    image.onload = null;
-    image.onabort = null;
-    image.onerror = null;
-
-    if ( jobid ) {
-        window.clearTimeout( jobid );
-    }
-    window.setTimeout( function() {
-        callback( image.src, successful ? image : null);
-    }, 1 );
-
-};
-
-}( OpenSeadragon ));