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

Source Code for Module pyjamas.Canvas.ImageLoaderhulahop

  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 JS 
 18   
 19  from pyjamas import DOM 
 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 - def _onload(self, form, event, something):
87 if not self.__formAction: 88 return 89 self._listener.onFrameLoad()
90
91 - def _onsubmit(self, form, event, something):
92 print form, event, something 93 try: 94 event = get_main_frame().gobject_wrap(event) # webkit HACK! 95 form = get_main_frame().gobject_wrap(form) # webkit HACK! 96 except: 97 pass 98 99 if self.iframe: 100 self.__formAction = form.action 101 return self._listener.onFormSubmit()
102 103 # FormPanelImpl.hookEvents
104 - def hookEvents(self, iframe, form, listener):
105 # TODO: might have to fix this, use DOM.set_listener() 106 self._listener = listener 107 if iframe: 108 wf = mf = get_main_frame() 109 self._onload_listener = mf.addEventListener(iframe, "load", 110 self._onload) 111 112 self._onsubmit_listener = mf.addEventListener(form, "onsubmit", 113 self._onsubmit)
114 115 """* 116 * Returns a handle to an img object. Ties back to the ImageLoader instance 117 """
118 - def prepareImage(self, url):
119 img = Image() 120 JS(""" 121 // if( callback specified ) 122 // do nothing 123 124 var __this = this; 125 126 img.onload = function() { 127 if(!img.__isLoaded) { 128 129 // __isLoaded should be set for the first time here. 130 // if for some reason img fires a second onload event 131 // we do not want to execute the following again (hence the guard) 132 img.__isLoaded = true; 133 __this.incrementLoadedImages(); 134 img.onload = null; 135 136 // we call this function each time onload fires 137 // It will see if we are ready to invoke the callback 138 __this.dispatchIfComplete(); 139 } else { 140 // we invoke the callback since we are already loaded 141 __this.dispatchIfComplete(); 142 } 143 } 144 145 return img; 146 """)
147 148 149
150 -def init():
151 global imageLoadArray 152 imageLoadArray = {}
153