comparison browser_side/menu.py @ 387:933bce4cb816

browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
author souliane <souliane@mailoo.org>
date Tue, 25 Feb 2014 07:56:05 +0100
parents 603fa314880e
children c86d7a8d2c1e
comparison
equal deleted inserted replaced
386:87e1194e55d6 387:933bce4cb816
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import pyjd # this is dummy in pyjs 20 import pyjd # this is dummy in pyjs
21 from pyjamas.ui.SimplePanel import SimplePanel 21 from pyjamas.ui.SimplePanel import SimplePanel
22 from pyjamas.ui.VerticalPanel import VerticalPanel
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel
24 from pyjamas.ui.DialogBox import DialogBox
25 from pyjamas.ui.FormPanel import FormPanel
26 from pyjamas.ui.FileUpload import FileUpload
27 from pyjamas.ui.MenuBar import MenuBar 22 from pyjamas.ui.MenuBar import MenuBar
28 from pyjamas.ui.MenuItem import MenuItem 23 from pyjamas.ui.MenuItem import MenuItem
29 from pyjamas.ui.ListBox import ListBox 24 from pyjamas.ui.ListBox import ListBox
30 from pyjamas.ui.Label import Label 25 from pyjamas.ui.Label import Label
31 from pyjamas.ui.TextBox import TextBox 26 from pyjamas.ui.TextBox import TextBox
32 from pyjamas.ui.Button import Button
33 from pyjamas.ui.HTML import HTML 27 from pyjamas.ui.HTML import HTML
34 from pyjamas.ui.Frame import Frame 28 from pyjamas.ui.Frame import Frame
35 from pyjamas import Window 29 from pyjamas import Window
36 from jid import JID 30 from jid import JID
37 from html_tools import html_sanitize 31 from html_tools import html_sanitize
38 from file_tools import FilterFileUpload 32 from file_tools import FileUploadPanel
39 from xmlui import XMLUI 33 from xmlui import XMLUI
40 import panels 34 import panels
41 import dialog 35 import dialog
42 from contact_group import ContactGroupEditor 36 from contact_group import ContactGroupEditor
43 import re 37 import re
80 self.getAbsoluteTop() + 74 self.getAbsoluteTop() +
81 self.getOffsetHeight() - 1) 75 self.getOffsetHeight() - 1)
82 self.popup.addStyleName('menuLastPopup') 76 self.popup.addStyleName('menuLastPopup')
83 77
84 78
85 class AvatarUpload(FormPanel): 79 class AvatarUpload(FileUploadPanel):
86 80 def __init__(self):
87 def __init__(self, close_cb=None): 81 texts = {'ok_button': 'Upload avatar',
88 FormPanel.__init__(self) 82 'body': 'Please select an image to show as your avatar...<br>Your picture must be a square and will be resized to 64x64 pixels if necessary.',
89 self.close_cb = close_cb 83 'errback': "Can't open image... did you actually submit an image?",
90 self.setEncoding(FormPanel.ENCODING_MULTIPART) 84 'body_errback': 'Please select another image file.',
91 self.setMethod(FormPanel.METHOD_POST) 85 'callback': "Your new profile picture has been set!"}
92 self.setAction("upload_avatar") 86 FileUploadPanel.__init__(self, 'upload_avatar', 'avatar_path', 2, texts)
93 self.vPanel = VerticalPanel()
94 self.message = HTML('Please select an image to show as your avatar...<br>Your picture must be a square and will be resized to 64x64 pixels if necessary')
95 self.vPanel.add(self.message)
96
97 hPanel = HorizontalPanel()
98 hPanel.setSpacing(5)
99 self.file_upload = FilterFileUpload("avatar_path", 2)
100 self.vPanel.add(self.file_upload)
101
102 hPanel.add(Button("Cancel", getattr(self, "onCloseBtnClick")))
103 self.upload_btn = Button("Upload avatar", getattr(self, "onSubmitBtnClick"))
104 hPanel.add(self.upload_btn)
105
106 self.status = Label()
107 hPanel.add(self.status)
108
109 self.vPanel.add(hPanel)
110
111 self.add(self.vPanel)
112 self.addFormHandler(self)
113
114 def setCloseCb(self, close_cb):
115 self.close_cb = close_cb
116
117 def onCloseBtnClick(self):
118 if self.close_cb:
119 self.close_cb()
120 else:
121 print ("WARNING: no close method defined")
122
123 def onSubmitBtnClick(self):
124 if not self.file_upload.check():
125 return
126 self.message.setHTML('<strong>Submitting, please wait...</strong>')
127 self.upload_btn.setEnabled(False)
128 self.submit()
129
130 def onSubmit(self, event):
131 pass
132
133 def onSubmitComplete(self, event):
134 result = event.getResults()
135 if result != "OK":
136 Window.alert("Can't open image... did you actually submit an image?")
137 self.message.setHTML('Please select another image file')
138 self.upload_btn.setEnabled(True)
139 else:
140 Window.alert("Your new profile picture has been set!")
141 self.close_cb()
142 87
143 88
144 class Menu(SimplePanel): 89 class Menu(SimplePanel):
145 90
146 def __init__(self, host): 91 def __init__(self, host):
153 menus_dict = {} 98 menus_dict = {}
154 menus_order = [] 99 menus_order = []
155 100
156 def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd): 101 def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd):
157 """ add a menu to menu_dict """ 102 """ add a menu to menu_dict """
158 print "addMenu:",menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd 103 print "addMenu:", menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd
159 try: 104 try:
160 menu_bar = menus_dict[menu_name] 105 menu_bar = menus_dict[menu_name]
161 except KeyError: 106 except KeyError:
162 menu_bar = menus_dict[menu_name] = MenuBar(vertical=True) 107 menu_bar = menus_dict[menu_name] = MenuBar(vertical=True)
163 menus_order.append((menu_name, menu_name_i18n, icon)) 108 menus_order.append((menu_name, menu_name_i18n, icon))
189 print "WARNING: skipping menu with a path of lenght 1 [%s]" % path[0] 134 print "WARNING: skipping menu with a path of lenght 1 [%s]" % path[0]
190 continue 135 continue
191 item_name_i18n = ' | '.join(path_i18n[1:]) 136 item_name_i18n = ' | '.join(path_i18n[1:])
192 addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id)) 137 addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id))
193 138
194 menus_order.append(None) # we add separator 139 menus_order.append(None) # we add separator
195 140
196 addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract")) 141 addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract"))
197 addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout")) 142 addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout"))
198 addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters")) 143 addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters"))
199 144
256 def addContactCb(sender): 201 def addContactCb(sender):
257 if not re.match(r'^.+@.+\..+', edit.getText(), re.IGNORECASE): 202 if not re.match(r'^.+@.+\..+', edit.getText(), re.IGNORECASE):
258 Window.alert('You must enter a valid contact JID (like "contact@%s")' % self.host._defaultDomain) 203 Window.alert('You must enter a valid contact JID (like "contact@%s")' % self.host._defaultDomain)
259 _dialog.show() 204 _dialog.show()
260 else: 205 else:
261 self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() ) 206 self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups())
262 207
263 label = Label("New contact identifier (JID):") 208 label = Label("New contact identifier (JID):")
264 edit.setText('@%s' % self.host._defaultDomain) 209 edit.setText('@%s' % self.host._defaultDomain)
265 edit.setWidth('100%') 210 edit.setWidth('100%')
266 _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [], 211 _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [],