Package pyjamas :: Package Canvas :: Module ImageLoader
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.Canvas.ImageLoader

  1  """ 
  2  * Copyright 2008 Google Inc. 
  3  * 
  4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  5  * use this file except in compliance with the License. You may obtain a copy of 
  6  * the License at 
  7  * 
  8  * http:#www.apache.org/licenses/LICENSE-2.0 
  9  * 
 10  * Unless required by applicable law or agreed to in writing, software 
 11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  * License for the specific language governing permissions and limitations under 
 14  * the License. 
 15  """ 
 16   
 17  from pyjamas import DOM 
 18  from pyjamas.ui.Image import Image 
 19  from pyjamas.ui import Event 
 20   
 21  """* 
 22  * Static internal collection of ImageLoader instances. 
 23  * ImageLoader is not instantiable externally. 
 24  """ 
 25  imageLoaders = [] 
 26   
 27   
 28  """* 
 29  * Provides a mechanism for deferred execution of a callback 
 30  * method once all specified Images are loaded. 
 31  """ 
32 -class ImageLoader:
33 34
35 - def __init__(self):
36 37 self.images = [] 38 self.callBack = None 39 self.loadedImages = 0 40 self.totalImages = 0
41 42 43 """* 44 * Stores the ImageElement reference so that when all the images report 45 * an onload, we can return the array of all the ImageElements. 46 * @param img 47 """
48 - def addHandle(self, img):
49 self.totalImages += 1 50 self.images.append(img)
51 52 53 """* 54 * Invokes the onImagesLoaded method in the CallBack if all the 55 * images are loaded AND we have a CallBack specified. 56 * 57 * Called from the JSNI onload event handler. 58 """
59 - def dispatchIfComplete(self):
60 if self.callBack is not None and self.isAllLoaded(): 61 self.callBack.onImagesLoaded(self.images) 62 # remove the image loader 63 imageLoaders.remove(self)
64 65 66 67 """* 68 * Sets the callback object for the ImageLoader. 69 * Once this is set, we may invoke the callback once all images that 70 * need to be loaded report in from their onload event handlers. 71 * 72 * @param cb 73 """
74 - def finalize(self, cb):
75 self.callBack = cb
76 77
78 - def incrementLoadedImages(self):
79 self.loadedImages += 1
80 81
82 - def isAllLoaded(self):
83 return (self.loadedImages == self.totalImages)
84 85 86 """* 87 * Returns a handle to an img object. Ties back to the ImageLoader instance 88 """
89 - def prepareImage(self, url):
90 img = Image() 91 img.__isLoaded = False 92 img.addLoadListener(self) 93 # normally, event listeners are only set up when the widget 94 # is attached to part of the DOM (see Widget.onAttach). but, 95 # in this case, we want a load even _even though_ the Image 96 # widget is not yet attached (and quite likely won't be). 97 DOM.setEventListener(img.getElement(), img) 98 DOM.sinkEvents(img.getElement(), Event.ONLOAD) 99 return img
100
101 - def onLoad(self, img):
102 103 if not img.__isLoaded: 104 105 # __isLoaded should be set for the first time here. 106 # if for some reason img fires a second onload event 107 # we do not want to execute the following again (hence the guard) 108 img.__isLoaded = True; 109 self.incrementLoadedImages(); 110 img.removeLoadListener(self) 111 112 # we call this function each time onload fires 113 # It will see if we are ready to invoke the callback 114 self.dispatchIfComplete(); 115 116 return img;
117 118 119 120 """* 121 * Takes in an array of url Strings corresponding to the images needed to 122 * be loaded. The onImagesLoaded() method in the specified CallBack 123 * object is invoked with an array of ImageElements corresponding to 124 * the original input array of url Strings once all the images report 125 * an onload event. 126 * 127 * @param urls Array of urls for the images that need to be loaded 128 * @param cb CallBack object 129 """
130 -def loadImages(urls, cb):
131 il = ImageLoader() 132 for i in range(len(urls)): 133 il.addHandle(il.prepareImage(urls[i])) 134 135 il.finalize(cb) 136 imageLoaders.append(il) 137 # Go ahead and fetch the images now 138 for i in range(len(urls)): 139 il.images[i].setUrl(urls[i])
140