annotate browser_side/dialog.py @ 43:a7ff1e6f1229

browser_side: prevent default for escape key (cancel HTTPRequest in FF)
author Goffi <goffi@goffi.org>
date Wed, 25 May 2011 14:21:48 +0200
parents 71a9cc9b9d57
children 153de5d461a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
3
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
7
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
12
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
17
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
21
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from pyjamas.ui.VerticalPanel import VerticalPanel
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel
43
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
24 from pyjamas.ui.PopupPanel import PopupPanel
26
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from pyjamas.ui.DialogBox import DialogBox
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from pyjamas.ui.ListBox import ListBox
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from pyjamas.ui.Button import Button
41
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
28 from pyjamas.ui.HTML import HTML
43
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
29 from pyjamas.ui.KeyboardListener import KEY_ESCAPE
26
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
30
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
31 class ContactsChooser(DialogBox):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
32
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def __init__(self, host, callback, nb_contact=None, text='Please select contacts'):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
35 ContactsChooser allow to select one or several connected contacts
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
36 @param host: SatWebFrontend instance
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @param callback: method to call when contacts have been choosed
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
38 @param nb_contact: number of contacts that have to be selected, None for no limit
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.host = host
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.callback = callback
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.nb_contact = nb_contact
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
43 DialogBox.__init__(self, centered=True)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
44
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
45 content = VerticalPanel()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
46 content.setWidth('100%')
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.contacts_list = ListBox()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.contacts_list.setVisibleItemCount(10)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.contacts_list.setMultipleSelect(True)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.contacts_list.setWidth("100%")
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.contacts_list.setStyleName('contactsChooser')
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.contacts_list.addChangeListener(self.onChange)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
53 content.add(self.contacts_list)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
54 button_panel = HorizontalPanel()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.choose_button = Button("Choose", self.onChoose)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.choose_button.setEnabled(False)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
57 button_panel.add(self.choose_button)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
58 button_panel.add(Button("Cancel", self.onCancel))
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
59 content.add(button_panel)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.setHTML(text)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.setWidget(content)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
62
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def onChange(self):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if self.nb_contact:
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if len(self.contacts_list.getSelectedValues()) == self.nb_contact:
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.choose_button.setEnabled(True)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
67 else:
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.choose_button.setEnabled(False)
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
69
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def getContacts(self):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
71 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
72 Actually ask to choose the contacts
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.contacts_list.clear()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
75 for contact in self.host.contact_panel.getConnected():
32
6b8da70b0799 browser_side: contactsChooser now discard MUC rooms
Goffi <goffi@goffi.org>
parents: 26
diff changeset
76 if contact not in [room.bare for room in self.host.room_list]:
6b8da70b0799 browser_side: contactsChooser now discard MUC rooms
Goffi <goffi@goffi.org>
parents: 26
diff changeset
77 self.contacts_list.addItem(contact)
26
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.show()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
79
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def onChoose(self):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.hide()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.callback(self.contacts_list.getSelectedValues())
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
83
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def onCancel(self):
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.hide()
824516b247e6 browser side: added ContactsChooser dialog
Goffi <goffi@goffi.org>
parents:
diff changeset
86
39
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
87 class ConfirmDialog(DialogBox):
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
88
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
89 def __init__(self, callback, text='Are you sure ?'):
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
90 """
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
91 Dialog to confirm an action
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
92 @param callback: method to call when contacts have been choosed
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
93 """
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
94 self.callback = callback
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
95 DialogBox.__init__(self, centered=True)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
96
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
97 content = VerticalPanel()
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
98 content.setWidth('100%')
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
99 button_panel = HorizontalPanel()
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
100 self.confirm_button = Button("OK", self.onConfirm)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
101 button_panel.add(self.confirm_button)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
102 button_panel.add(Button("Cancel", self.onCancel))
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
103 content.add(button_panel)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
104 self.setHTML(text)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
105 self.setWidget(content)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
106
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
107 def onConfirm(self):
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
108 self.hide()
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
109 self.callback(True)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
110
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
111 def onCancel(self):
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
112 self.hide()
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
113 self.callback(False)
305e81c7a32c Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents: 32
diff changeset
114
41
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
115 class SimpleDialog(DialogBox):
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
116
42
71a9cc9b9d57 Browser side: Tarot game
Goffi <goffi@goffi.org>
parents: 41
diff changeset
117 def __init__(self, title, body, callback = None):
41
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
118 """Simple notice dialog box
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
119 @param title: HTML put in the header
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
120 @param body: HTML put in the body"""
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
121 DialogBox.__init__(self, centered=True)
42
71a9cc9b9d57 Browser side: Tarot game
Goffi <goffi@goffi.org>
parents: 41
diff changeset
122 self.callback = callback
41
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
123 _body = VerticalPanel()
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
124 _body.setSpacing(4)
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
125 _body.add(HTML(body))
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
126 _body.add(Button("Close", self.onClose))
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
127 _body.setStyleName("dialogBody")
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
128 self.setHTML(title)
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
129 self.setWidget(_body)
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
130
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
131 def onClose(self):
7782a786b2f0 Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents: 39
diff changeset
132 self.hide()
42
71a9cc9b9d57 Browser side: Tarot game
Goffi <goffi@goffi.org>
parents: 41
diff changeset
133 if self.callback:
71a9cc9b9d57 Browser side: Tarot game
Goffi <goffi@goffi.org>
parents: 41
diff changeset
134 self.callback()
43
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
135
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
136 class PopupPanelWrapper(PopupPanel):
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
137 """This wrapper catch Escape event to avoid request cancellation by Firefox"""
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
138
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
139 def onEventPreview(self, event):
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
140 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE:
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
141 #needed to prevent request cancellation in Firefox
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
142 event.preventDefault()
a7ff1e6f1229 browser_side: prevent default for escape key (cancel HTTPRequest in FF)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
143 return PopupPanel.onEventPreview(self, event)