<s> from pyjamas . ui . HTML import HTML <EOL> from pyjamas . ui . TextBox import TextBox <EOL> from pyjamas . ui . Label import Label <EOL> from pyjamas . ui . Button import Button <EOL> from pyjamas . ui . VerticalPanel import VerticalPanel <EOL> from pyjamas . ui . HorizontalPanel import HorizontalPanel <EOL> from pyjamas . ui . DisclosurePanel import DisclosurePanel <EOL> from pyjamas . ui . Grid import Grid <EOL> from pyjamas . ui . Composite import Composite <EOL> from pyjamas . Timer import Timer <EOL> from pyjamas import DOM <EOL> import json <EOL> from __pyjamas__ import doc , wnd <EOL> class Photos ( Composite ) : <EOL> def __init__ ( self ) : <EOL> Composite . __init__ ( self ) <EOL> self . albums = [ ] <EOL> self . photos = [ ] <EOL> self . grid = Grid ( <NUM_LIT:4> , <NUM_LIT:4> , CellPadding = <NUM_LIT:4> , CellSpacing = <NUM_LIT:4> ) <EOL> self . grid . addTableListener ( self ) <EOL> self . drill = <NUM_LIT:0> <EOL> self . pos = <NUM_LIT:0> <EOL> self . up = Button ( "<STR_LIT>" , self ) <EOL> self . next = Button ( "<STR_LIT>" , self ) <EOL> self . prev = Button ( "<STR_LIT>" , self ) <EOL> self . timer = Timer ( notify = self ) <EOL> self . userid = "<STR_LIT>" <EOL> self . album_url = "<STR_LIT>" + self . userid + "<STR_LIT>" <EOL> self . doRESTQuery ( self . album_url , self . timer ) <EOL> self . vp = VerticalPanel ( ) <EOL> self . disclosure = DisclosurePanel ( "<STR_LIT>" ) <EOL> self . disclosure . add ( HTML ( '''<STR_LIT>''' ) ) <EOL> self . IDPanel = HorizontalPanel ( ) <EOL> self . IDPanel . add ( Label ( "<STR_LIT>" ) ) <EOL> self . IDButton = Button ( "<STR_LIT>" , self ) <EOL> self . IDBox = TextBox ( ) <EOL> self . IDBox . setText ( self . userid ) <EOL> self . IDPanel . add ( self . IDBox ) <EOL> self . IDPanel . add ( self . IDButton ) <EOL> self . vp . add ( self . IDPanel ) <EOL> self . vp . add ( self . disclosure ) <EOL> self . vp . add ( self . grid ) <EOL> self . initWidget ( self . vp ) <EOL> def doRESTQuery ( self , url , timer ) : <EOL> """<STR_LIT>""" <EOL> new_script = DOM . createElement ( "<STR_LIT>" ) <EOL> DOM . setElemAttribute ( new_script , "<STR_LIT:src>" , url ) <EOL> DOM . setElemAttribute ( new_script , "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> doc ( ) . body . appendChild ( new_script ) <EOL> self . timer . schedule ( <NUM_LIT:100> ) <EOL> def onCellClicked ( self , sender , row , col ) : <EOL> if self . drill == <NUM_LIT:0> : <EOL> self . drill += <NUM_LIT:1> <EOL> self . vp . clear ( ) <EOL> self . grid . clear ( ) <EOL> self . vp . add ( self . up ) <EOL> self . vp . add ( self . grid ) <EOL> gridcols = self . grid . getColumnCount ( ) <EOL> album = self . albums [ row + col + ( row * ( gridcols - <NUM_LIT:1> ) ) ] <EOL> url = "<STR_LIT>" + self . userid + "<STR_LIT>" + album [ "<STR_LIT:id>" ] + "<STR_LIT>" <EOL> self . doRESTQuery ( url , self . timer ) <EOL> elif self . drill == <NUM_LIT:1> : <EOL> self . drill += <NUM_LIT:1> <EOL> gridcols = self . grid . getColumnCount ( ) <EOL> self . pos = row + col + ( row * ( gridcols - <NUM_LIT:1> ) ) <EOL> photo = self . photos [ self . pos ] <EOL> self . vp . clear ( ) <EOL> self . fullsize = HTML ( '<STR_LIT>' + photo [ "<STR_LIT>" ] + '<STR_LIT>' ) <EOL> hp = HorizontalPanel ( ) <EOL> hp . add ( self . up ) <EOL> hp . add ( self . prev ) <EOL> hp . add ( self . next ) <EOL> hp . setSpacing ( <NUM_LIT:8> ) <EOL> self . vp . add ( hp ) <EOL> self . vp . add ( self . fullsize ) <EOL> def onClick ( self , sender ) : <EOL> if sender == self . IDButton : <EOL> self . userid = self . IDBox . getText ( ) <EOL> if self . userid == "<STR_LIT>" or self . userid . isdigit ( ) : <EOL> return <EOL> self . drill = <NUM_LIT:0> <EOL> self . album_url = "<STR_LIT>" + self . userid + "<STR_LIT>" <EOL> self . grid . clear ( ) <EOL> self . doRESTQuery ( self . album_url , self . timer ) <EOL> else : <EOL> if self . drill == <NUM_LIT:2> : <EOL> if sender == self . up : <EOL> self . drill = <NUM_LIT:1> <EOL> self . vp . clear ( ) <EOL> self . vp . add ( self . up ) <EOL> self . vp . add ( self . grid ) <EOL> self . fillGrid ( self . photos , "<STR_LIT>" ) <EOL> else : <EOL> if sender == self . next : <EOL> if self . pos >= len ( self . photos ) : <EOL> return <EOL> self . pos += <NUM_LIT:1> <EOL> elif sender == self . prev : <EOL> if self . pos < <NUM_LIT:1> : <EOL> return <EOL> self . pos -= <NUM_LIT:1> <EOL> photo = self . photos [ self . pos ] <EOL> self . fullsize . setHTML ( '<STR_LIT>' + photo [ "<STR_LIT>" ] + '<STR_LIT>' ) <EOL> elif self . drill == <NUM_LIT:1> : <EOL> self . drill = <NUM_LIT:0> <EOL> self . vp . clear ( ) <EOL> self . vp . add ( self . IDPanel ) <EOL> self . vp . add ( self . disclosure ) <EOL> self . vp . add ( self . grid ) <EOL> self . fillGrid ( self . albums , "<STR_LIT>" ) <EOL> def onTimer ( self , timer ) : <EOL> fd = doc ( ) . getElementById ( "<STR_LIT>" ) <EOL> receiver = fd . innerHTML <EOL> if receiver == '<STR_LIT>' : <EOL> self . timer . schedule ( <NUM_LIT:1000> ) <EOL> return <EOL> fd . innerHTML = '<STR_LIT>' <EOL> if self . drill == <NUM_LIT:0> : <EOL> self . parseAlbums ( receiver ) <EOL> self . fillGrid ( self . albums , "<STR_LIT>" ) <EOL> elif self . drill == <NUM_LIT:1> : <EOL> self . parsePhotos ( receiver ) <EOL> self . fillGrid ( self . photos , "<STR_LIT>" ) <EOL> def fillGrid ( self , items , type ) : <EOL> self . grid . clear ( ) <EOL> cols = self . grid . getColumnCount ( ) <EOL> self . grid . resizeRows ( ( len ( items ) / cols ) + <NUM_LIT:1> ) <EOL> rows = self . grid . getRowCount ( ) <EOL> for i in range ( len ( items ) ) : <EOL> vp = VerticalPanel ( ) <EOL> if type == '<STR_LIT>' : <EOL> vp . add ( items [ i ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> vp . add ( items [ i ] [ '<STR_LIT>' ] ) <EOL> vp . add ( items [ i ] [ '<STR_LIT:title>' ] ) <EOL> self . grid . setWidget ( int ( i / cols ) , i % cols , vp ) <EOL> def parsePhotos ( self , items ) : <EOL> photo_list = json . loads ( items ) <EOL> self . photos = [ ] <EOL> for ph in photo_list : <EOL> aphoto = { } <EOL> aphoto [ '<STR_LIT>' ] = HTML ( '<STR_LIT>' + ph [ u"<STR_LIT>" ] [ u"<STR_LIT>" ] [ <NUM_LIT:1> ] [ u"<STR_LIT:url>" ] + '<STR_LIT>' ) <EOL> aphoto [ '<STR_LIT>' ] = ph [ u"<STR_LIT>" ] [ u"<STR_LIT>" ] [ <NUM_LIT:0> ] [ u"<STR_LIT:url>" ] <EOL> self . photos . append ( aphoto ) <EOL> def parseAlbums ( self , items ) : <EOL> album_list = json . loads ( items ) <EOL> self . albums = [ ] <EOL> for al in album_list : <EOL> analbum = { } <EOL> analbum [ '<STR_LIT:title>' ] = HTML ( al [ u"<STR_LIT:title>" ] [ u"<STR_LIT>" ] ) <EOL> analbum [ '<STR_LIT>' ] = HTML ( '<STR_LIT>' + al [ u"<STR_LIT>" ] [ u"<STR_LIT>" ] [ <NUM_LIT:0> ] [ u"<STR_LIT:url>" ] + '<STR_LIT>' ) <EOL> url = al [ u"<STR_LIT:id>" ] [ u"<STR_LIT>" ] <EOL> analbum [ '<STR_LIT:id>' ] = url . split ( u'<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( u'<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . albums . append ( analbum ) </s>
