<s> from pyjamas import DOM <EOL> from pyjamas import Factory <EOL> from pyjamas . ui . Widget import Widget <EOL> from pyjamas . ui import Event <EOL> from pyjamas . ui . MouseListener import MouseHandler <EOL> from pyjamas . ui . ClickListener import ClickHandler <EOL> prefetchImages = { } <EOL> class Image ( Widget , MouseHandler , ClickHandler ) : <EOL> _props = [ ( "<STR_LIT:url>" , "<STR_LIT>" , "<STR_LIT>" , None ) , <EOL> ] <EOL> def __init__ ( self , url = "<STR_LIT>" , ** kwargs ) : <EOL> if not kwargs . has_key ( '<STR_LIT>' ) : kwargs [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> if url : kwargs [ '<STR_LIT>' ] = url <EOL> self . setElement ( kwargs . pop ( '<STR_LIT>' , None ) or DOM . createImg ( ) ) <EOL> Widget . __init__ ( self , ** kwargs ) <EOL> MouseHandler . __init__ ( self ) <EOL> ClickHandler . __init__ ( self ) <EOL> self . sinkEvents ( Event . ONLOAD | Event . ONERROR ) <EOL> self . loadListeners = [ ] <EOL> @ classmethod <EOL> def _getProps ( self ) : <EOL> return Widget . _getProps ( ) + self . _props <EOL> def addLoadListener ( self , listener ) : <EOL> self . loadListeners . append ( listener ) <EOL> def removeLoadListener ( self , listener ) : <EOL> self . loadListeners . remove ( listener ) <EOL> def getUrl ( self ) : <EOL> return DOM . getAttribute ( self . getElement ( ) , "<STR_LIT:src>" ) <EOL> def onBrowserEvent ( self , event ) : <EOL> Widget . onBrowserEvent ( self , event ) <EOL> type = DOM . eventGetType ( event ) <EOL> if type == "<STR_LIT>" : <EOL> for listener in self . loadListeners : <EOL> listener . onLoad ( self ) <EOL> elif type == "<STR_LIT:error>" : <EOL> for listener in self . loadListeners : <EOL> listener . onError ( self ) <EOL> def prefetch ( self , url ) : <EOL> img = DOM . createImg ( ) <EOL> DOM . setElemAttribute ( img , "<STR_LIT:src>" , url ) <EOL> prefetchImages [ url ] = img <EOL> def setUrl ( self , url ) : <EOL> DOM . setElemAttribute ( self . getElement ( ) , "<STR_LIT:src>" , url ) <EOL> Factory . registerClass ( '<STR_LIT>' , '<STR_LIT>' , Image ) </s>
