comparison libervia.py @ 18:795d144fc1d2

moved panels and menu in a separate file
author Goffi <goffi@goffi.org>
date Fri, 15 Apr 2011 15:30:31 +0200
parents c725b702e927
children e8e3704eb97f
comparison
equal deleted inserted replaced
17:c725b702e927 18:795d144fc1d2
20 """ 20 """
21 21
22 import pyjd # this is dummy in pyjs 22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.SimplePanel import SimplePanel 23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.RootPanel import RootPanel 24 from pyjamas.ui.RootPanel import RootPanel
25 from pyjamas.ui.VerticalPanel import VerticalPanel
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
27 from pyjamas.ui.HTMLPanel import HTMLPanel
28 from pyjamas.ui.ScrollPanel import ScrollPanel
29 from pyjamas.ui.Grid import Grid
30 from pyjamas.ui.Label import Label
31 from pyjamas.ui import HasAlignment
32 from pyjamas.ui.MenuBar import MenuBar
33 from pyjamas.ui.MenuItem import MenuItem
34 from pyjamas.ui.AutoComplete import AutoCompleteTextBox 25 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
35 from pyjamas.ui.DropWidget import DropWidget
36 from pyjamas import Window 26 from pyjamas import Window
37 from pyjamas.JSONService import JSONProxy 27 from pyjamas.JSONService import JSONProxy
38 from pyjamas.ui.KeyboardListener import KEY_ENTER 28 from pyjamas.ui.KeyboardListener import KEY_ENTER
39 from browser_side.register import RegisterPanel, RegisterBox 29 from browser_side.register import RegisterPanel, RegisterBox
40 from pyjamas import DOM
41 from browser_side.contact import ContactPanel 30 from browser_side.contact import ContactPanel
42 from datetime import datetime 31 from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel
43 32
44 33
45 class LiberviaJsonProxy(JSONProxy): 34 class LiberviaJsonProxy(JSONProxy):
46 def __init__(self, *args, **kwargs): 35 def __init__(self, *args, **kwargs):
47 JSONProxy.__init__(self, *args, **kwargs) 36 JSONProxy.__init__(self, *args, **kwargs)
82 class BridgeSignals(LiberviaJsonProxy): 71 class BridgeSignals(LiberviaJsonProxy):
83 def __init__(self): 72 def __init__(self):
84 LiberviaJsonProxy.__init__(self, "/json_signal_api", 73 LiberviaJsonProxy.__init__(self, "/json_signal_api",
85 ["getSignals"]) 74 ["getSignals"])
86 75
87 class MenuCmd:
88
89 def __init__(self, object, handler):
90 self._object = object
91 self._handler = handler
92
93 def execute(self):
94 handler = getattr(self._object, self._handler)
95 handler()
96
97 class Menu(SimplePanel):
98
99 def __init__(self):
100 SimplePanel.__init__(self)
101
102 menu_general = MenuBar(vertical=True)
103 menu_general.addItem("Properties", MenuCmd(self, "onProperties"))
104
105 menu_games = MenuBar(vertical=True)
106 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
107 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
108
109 menubar = MenuBar(vertical=False)
110 menubar.addItem(MenuItem("General", menu_general))
111 menubar.addItem(MenuItem("Games", True, menu_games))
112 self.add(menubar)
113
114 def onProperties(self):
115 Window.alert("Properties selected")
116
117 def onTarotGame(self):
118 Window.alert("Tarot selected")
119
120 def onXiangqiGame(self):
121 Window.alert("Xiangqi selected")
122 76
123 class UniBox(AutoCompleteTextBox): 77 class UniBox(AutoCompleteTextBox):
124 78
125 def __init__(self, host): 79 def __init__(self, host):
126 AutoCompleteTextBox.__init__(self) 80 AutoCompleteTextBox.__init__(self)
137 def complete(self): 91 def complete(self):
138 #self.visible=False #XXX: self.visible is not unset in pyjamas when ENTER is pressed and a completion is done 92 #self.visible=False #XXX: self.visible is not unset in pyjamas when ENTER is pressed and a completion is done
139 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this 93 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this
140 return AutoCompleteTextBox.complete(self) 94 return AutoCompleteTextBox.complete(self)
141 95
142 class DropCell(DropWidget):
143 """Cell in the middle grid which replace itself with the dropped widget on DnD"""
144
145 def __init__(self):
146 DropWidget.__init__(self)
147
148 def onDragEnter(self, event):
149 print "drag enter"
150 self.addStyleName('dragover')
151 DOM.eventPreventDefault(event)
152
153 def onDragLeave(self, event):
154 print "drag leave"
155 self.removeStyleName('dragover')
156
157 def onDragOver(self, event):
158 DOM.eventPreventDefault(event)
159
160 def _getCellAndRow(self, grid, event):
161 """Return cell and row index where the event is occuring"""
162 cell = grid.getEventTargetCell(event)
163 row = DOM.getParent(cell)
164 return (row.rowIndex, cell.cellIndex)
165
166
167 def onDrop(self, event):
168 print "Empty Panel: onDrop"
169 dt = event.dataTransfer
170 #'text', 'text/plain', and 'Text' are equivalent.
171 try:
172 item = dt.getData("text/plain")
173 item_type = dt.getData("type")
174 print "message: %s" % item
175 print "type: %s" % item_type
176 except:
177 print "no message found"
178 item='&nbsp;'
179 item_type = None
180 DOM.eventPreventDefault(event)
181 if item_type=="GROUP":
182 _mblog = MicroblogPanel(self.host, item)
183 _mblog.setAcceptedGroup(item)
184 elif item_type=="CONTACT":
185 _mblog = MicroblogPanel(self.host, accept_all=True)
186 self.host.mpanels.remove(self)
187 self.host.mpanels.append(_mblog)
188 print "DEBUG"
189 grid = self.getParent()
190 row_idx, cell_idx = self._getCellAndRow(grid, event)
191 self.removeFromParent()
192 grid.setWidget(row_idx, cell_idx, _mblog)
193 print "index:", row_idx, cell_idx
194 #FIXME: delete object ? Check the right way with pyjamas
195 #self.host.middle_panel.changePanel(self.data,self.host.mpanels[0])
196
197
198 class EmptyPanel(DropCell, SimplePanel):
199 """Empty dropable panel"""
200
201 def __init__(self, host):
202 SimplePanel.__init__(self)
203 self.host = host
204 _panel = HTMLPanel("&nbsp;")
205 self.add(_panel)
206 self.setHeight('100%')
207 DropCell.__init__(self)
208
209 class MicroblogEntry(SimplePanel):
210
211 def __init__(self, body, author, timestamp):
212 SimplePanel.__init__(self)
213
214 _datetime = datetime.fromtimestamp(timestamp)
215
216 panel = HTMLPanel("<div class='mb_entry_header'><span class='mb_entry_author'>%(author)s</span> on <span class='mb_entry_timestamp'>%(timestamp)s</span></div><div class='mb_entry_body'>%(body)s</div>" %
217 {"author": author,
218 "timestamp": _datetime,
219 "body": body}
220 )
221 panel.setStyleName('microblogEntry')
222 self.add(panel)
223
224
225 class MicroblogPanel(DropCell, ScrollPanel):
226
227 def __init__(self,host, title='&nbsp;', accept_all=False):
228 """Panel used to show microblog
229 @param title: title of the panel
230 @param accept_all: if true, show every message, without filtering jids"""
231 self.host = host
232 self.accept_all = accept_all
233 title=title.replace('<','&lt;').replace('>','&gt;')
234 self.accepted_groups = []
235 _class = ['mb_panel_header']
236 if title == '&nbsp;':
237 _class.append('empty_header')
238 ScrollPanel.__init__(self)
239 self.vpanel = VerticalPanel()
240 self.vpanel.add(HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),title)))
241 self.vpanel.setWidth('100%')
242 self.setHeight('100%')
243 self.setStyleName('microblogPanel')
244 self.add(self.vpanel)
245 DropCell.__init__(self)
246
247 def addEntry(self, text, author=None, timestamp=None):
248 """Add an entry to the panel
249 @param text: main text of the entry
250 @param author: who wrote the entry
251 @param date: when the entry was written"""
252 _entry = MicroblogEntry(text, author, timestamp)
253 self.vpanel.insert(_entry,1)
254
255 def setAcceptedGroup(self, group):
256 """Set the group which can be displayed in this panel
257 @param group: string of the group, or list of string
258 """
259 if isinstance(group, list):
260 self.accepted_groups.extend(group)
261 else:
262 self.accepted_groups.append(group)
263
264 def isJidAccepted(self, jid):
265 """Tell if a jid is actepted and show in this panel
266 @param jid: jid
267 @return: True if the jid is accepted"""
268 if self.accept_all:
269 return True
270 for group in self.accepted_groups:
271 if self.host.contactPanel.isContactInGroup(group, jid):
272 return True
273 return False
274
275
276
277 class MiddlePannel(HorizontalPanel):
278
279 def __init__(self, host):
280 self.host=host
281 HorizontalPanel.__init__(self)
282 self._left = self.host.contactPanel
283 self._right = Grid(1,3)
284 self._right.setWidth('100%')
285 self._right.setHeight('100%')
286 self.add(self._left)
287 self.setCellWidth(self._left, "15%")
288 self.add(self._right)
289 self.setCellWidth(self._right, "85%")
290 self.setHeight('100%')
291
292 def changePanel(self, idx, panel):
293 print "panel:",panel
294 print "idx:",idx
295 self._right.setWidget(0,idx,panel)
296
297 class MainPanel(VerticalPanel):
298
299 def __init__(self, host):
300 self.host=host
301 VerticalPanel.__init__(self)
302
303 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
304 self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
305
306 menu = Menu()
307 uni_box = host.uniBox
308 self.middle_panel = MiddlePannel(self.host)
309 self.middle_panel.setWidth('100%')
310
311 self.add(menu)
312 self.add(uni_box)
313 self.add(self.middle_panel)
314
315 self.setCellHeight(menu, "5%")
316 self.setCellHeight(uni_box, "5%")
317 self.setCellVerticalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
318 self.setCellHorizontalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
319 self.setCellHeight(self.middle_panel, "90%")
320 self.setCellWidth(self.middle_panel, "100%")
321
322 self.setWidth("100%")
323 self.setHeight("100%")
324 96
325 class SatWebFrontend: 97 class SatWebFrontend:
326 def onModuleLoad(self): 98 def onModuleLoad(self):
327 self.bridge = BridgeCall() 99 self.bridge = BridgeCall()
328 self.bridge_signals = BridgeSignals() 100 self.bridge_signals = BridgeSignals()