comparison browser_side/panels.py @ 78:12680e220b35

browser side: refactoring: menu is now in its own module
author Goffi <goffi@goffi.org>
date Sun, 19 Jun 2011 01:33:29 +0200
parents 4b4c0b9e2533
children 68d360caeecb
comparison
equal deleted inserted replaced
77:975e6be24e11 78:12680e220b35
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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.FlowPanel import FlowPanel
25 from pyjamas.ui.AbsolutePanel import AbsolutePanel 24 from pyjamas.ui.AbsolutePanel import AbsolutePanel
26 from pyjamas.ui.VerticalPanel import VerticalPanel 25 from pyjamas.ui.VerticalPanel import VerticalPanel
27 from pyjamas.ui.HorizontalPanel import HorizontalPanel 26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
28 from pyjamas.ui.ScrollPanel import ScrollPanel 27 from pyjamas.ui.ScrollPanel import ScrollPanel
29 from pyjamas.ui.TabPanel import TabPanel 28 from pyjamas.ui.TabPanel import TabPanel
30 from pyjamas.ui.HTMLPanel import HTMLPanel 29 from pyjamas.ui.HTMLPanel import HTMLPanel
31 from pyjamas.ui.Grid import Grid 30 from pyjamas.ui.Grid import Grid
32 from pyjamas.ui.DialogBox import DialogBox
33 from pyjamas.ui.TextArea import TextArea 31 from pyjamas.ui.TextArea import TextArea
34 from pyjamas.ui.MenuBar import MenuBar
35 from pyjamas.ui.MenuBarPopupPanel import MenuBarPopupPanel
36 from pyjamas.ui.MenuItem import MenuItem
37 from pyjamas.ui.ListBox import ListBox
38 from pyjamas.ui.Label import Label 32 from pyjamas.ui.Label import Label
39 from pyjamas.ui.TextBox import TextBox
40 from pyjamas.ui.Button import Button
41 from pyjamas.ui.HTML import HTML 33 from pyjamas.ui.HTML import HTML
42 from pyjamas.ui.Frame import Frame
43 from pyjamas.ui.DropWidget import DropWidget 34 from pyjamas.ui.DropWidget import DropWidget
44 from pyjamas.ui.ClickListener import ClickHandler 35 from pyjamas.ui.ClickListener import ClickHandler
45 from pyjamas.ui.KeyboardListener import KEY_ENTER 36 from pyjamas.ui.KeyboardListener import KEY_ENTER
46 from pyjamas.Timer import Timer 37 from pyjamas.Timer import Timer
47 from pyjamas import Window 38 from pyjamas import Window
48 from pyjamas import DOM 39 from pyjamas import DOM
49 from __pyjamas__ import JS, doc 40 from card_game import CardPanel
50 41 from menu import Menu
51 from pyjamas.dnd import makeDraggable
52 from pyjamas.ui.DragWidget import DragWidget, DragContainer
53 from card_game import CardPanel
54 from jid import JID 42 from jid import JID
55 from tools import html_sanitize 43 from tools import html_sanitize
56 from datetime import datetime 44 from datetime import datetime
57 from time import time 45 from time import time
58 import dialog 46 import dialog
59 import re 47 from __pyjamas__ import doc
60
61 class MenuCmd:
62
63 def __init__(self, object, handler):
64 self._object = object
65 self._handler = handler
66
67 def execute(self):
68 handler = getattr(self._object, self._handler)
69 handler()
70
71 class LiberviaMenuBar(MenuBar):
72
73 def __init__(self):
74 MenuBar.__init__(self, vertical=False)
75
76 def doItemAction(self, item, fireCommand):
77 MenuBar.doItemAction(self, item, fireCommand)
78 if item == self.items[-1] and self.popup:
79 print "DEBUG"
80 print self.popup.getOffsetWidth()
81 self.popup.setPopupPosition(Window.getClientWidth() -
82 self.popup.getOffsetWidth() - 22,
83 self.getAbsoluteTop() +
84 self.getOffsetHeight() - 1)
85 self.popup.addStyleName('menuLastPopup')
86
87
88 class Menu(SimplePanel):
89
90 def __init__(self, host):
91 self.host = host
92 SimplePanel.__init__(self)
93 self.setStyleName('menuContainer')
94
95 menu_general = MenuBar(vertical=True)
96 menu_general.addItem("Social contract", MenuCmd(self, "onSocialContract"))
97 menu_general.addItem("About", MenuCmd(self, "onAbout"))
98
99 menu_contacts = MenuBar(vertical=True)
100 menu_contacts.addItem("add contact", MenuCmd(self, "onAddContact"))
101 menu_contacts.addItem("update contact", MenuCmd(self, "onUpdateContact"))
102 menu_contacts.addItem("remove contact", MenuCmd(self, "onRemoveContact"))
103
104 menu_group = MenuBar(vertical=True)
105 menu_group.addItem("join room", MenuCmd(self, "onJoinRoom"))
106
107 menu_games = MenuBar(vertical=True)
108 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
109 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
110
111 menubar = LiberviaMenuBar()
112 menubar.addItem(MenuItem("General", menu_general))
113 menubar.addItem(MenuItem("Contacts", menu_contacts))
114 menubar.addItem(MenuItem("Groups", menu_group))
115 _separator = MenuItem('', None)
116 _separator.setStyleName('menuSeparator')
117 menubar.addItem(_separator, None)
118 menubar.addItem(MenuItem("Games", True, menu_games))
119 self.add(menubar)
120
121 #General menu
122 def onSocialContract(self):
123 _frame = Frame('contrat_social.html')
124 _frame.setStyleName('infoFrame')
125 _dialog = dialog.GenericDialog("Contrat Social", _frame)
126 _dialog.setSize('80%', '80%')
127 _dialog.show()
128
129 def onAbout(self):
130 _about = HTML("""<b>Libervia</b>, a Salut à Toi project<br />
131 <br />
132 You can contact the author at <a href="mailto:goffi@goffi.org">goffi@goffi.org</a><br />
133 Blog available (mainly in french) at <a href="http://www.goffi.org" target="_blank">http://www.goffi.org</a><br />
134 Project page: <a href="http://wiki.goffi.org/wiki/Salut_à_Toi"target="_blank">http://wiki.goffi.org/wiki/Salut_à_Toi</a><br />
135 <br />
136 Any help welcome :)
137 <p style='font-size:small;text-align:center'>This project is dedicated to Roger Poisson</p>
138 """)
139 _dialog = dialog.GenericDialog("About", _about)
140 _dialog.show()
141
142 #Contact menu
143 def onAddContact(self):
144 """Q&D contact addition"""
145 _dialog = None
146 edit = TextBox()
147
148 def addContactCb(sender):
149 if not re.match(r'^.+@.+\..+',edit.getText(), re.IGNORECASE):
150 Window.alert('You must enter a valid contact JID (like "contact@libervia.org")')
151 _dialog.show()
152 else:
153 self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() )
154
155 label = Label("new contact identifier (JID):")
156 edit.setText('@libervia.org')
157 edit.setWidth('100%')
158 _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [], addContactCb)
159 _dialog.setHTML('Adding contact')
160 _dialog.show()
161
162 def onUpdateContact(self):
163 _dialog = None
164 _contacts_list = ListBox()
165
166 def updateContactCb(sender):
167 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
168 self.host.bridge.call('updateContact', None, _jid, '', _dialog.getSelectedGroups())
169
170 def onContactChange(_list):
171 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
172 groups = self.host.contact_panel.getContactGroups(_jid)
173 _dialog.setGroupsSelected(groups)
174
175 for contact in self.host.contact_panel.getContacts():
176 _contacts_list.addItem(contact)
177 _contacts_list.addChangeListener(onContactChange)
178 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
179 _selected_groups = self.host.contact_panel.getContactGroups(_jid)
180 _dialog = dialog.GroupSelector([Label('Which contact do you want to update ?'), _contacts_list],
181 self.host.contact_panel.getGroups(), _selected_groups, updateContactCb)
182 _dialog.setHTML('Updating contact')
183 _dialog.show()
184
185 def onRemoveContact(self):
186 _dialog = None
187 _contacts_list = ListBox()
188
189 def secondConfirmCb(confirm):
190 if confirm:
191 for contact in _contacts_list.getSelectedValues():
192 self.host.bridge.call('delContact', None, contact)
193 else:
194 _dialog.show()
195
196 def dialogCb(confirm):
197 if confirm:
198 html_list = ''.join(['<li>%s</li>' % html_sanitize(contact) for contact in _contacts_list.getSelectedValues()])
199 html_body = "Are you sure to remove the following contacts from your contact list ?<ul>%s</ul>" % html_list
200 dialog.ConfirmDialog(secondConfirmCb, html_body).show()
201
202 for contact in self.host.contact_panel.getContacts():
203 _contacts_list.addItem(contact)
204 _dialog = dialog.GenericConfirmDialog([_contacts_list], dialogCb, "Who do you want to remove from your contacts ?")
205 _dialog.show()
206
207 #Group menu
208 def onJoinRoom(self):
209 _dialog = None
210 _edit = None
211
212 def onOK(sender):
213 if not _edit.getText():
214 Window.alert('You must enter a room jid in the form libervia@conference.libervia.org')
215 if self.host.whoami:
216 nick = self.host.whoami.node
217 self.host.bridge.call('joinMUC', None, _edit.getText(), nick)
218 _dialog.hide()
219
220 def onCancel(sender):
221 _dialog.hide()
222
223 _main_panel = VerticalPanel()
224 _label = Label("Discussion room:")
225 _edit = TextBox()
226 _edit.setText('libervia@conference.libervia.org')
227 hpanel = HorizontalPanel()
228 hpanel.add(_label)
229 hpanel.add(_edit)
230 _main_panel.add(hpanel)
231 button_panel = HorizontalPanel()
232 button_panel.add(Button("Join", onOK))
233 button_panel.add(Button("Cancel", onCancel))
234 _main_panel.add(button_panel)
235 _dialog = DialogBox(centered=True)
236 _dialog.setHTML('Group discussions')
237 _dialog.setWidget(_main_panel)
238 _dialog.show()
239
240
241 #Game menu
242
243 def onTarotGame(self):
244 #Window.alert("Tarot selected")
245 #self.host.tab_panel.add(EmptyPanel(self.host), "Tarot")
246 def onPlayersSelected(other_players):
247 self.host.bridge.call('launchTarotGame', None, other_players)
248 dialog.ContactsChooser(self.host, onPlayersSelected, 3, text="Please select 3 other players").getContacts()
249
250 def onXiangqiGame(self):
251 Window.alert("A Xiangqi game is planed, but not available yet")
252 48
253 class DropCell(DropWidget): 49 class DropCell(DropWidget):
254 """Cell in the middle grid which replace itself with the dropped widget on DnD""" 50 """Cell in the middle grid which replace itself with the dropped widget on DnD"""
255 51
256 def __init__(self): 52 def __init__(self):