Mercurial > libervia-web
comparison browser_side/recipients.py @ 241:86055ccf69c3
browser_side: added class PopupMenuPanel to manage more complex context menu
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 15 Oct 2013 13:36:51 +0200 |
parents | 0ed09cc0566f |
children |
comparison
equal
deleted
inserted
replaced
240:a565ce2facc0 | 241:86055ccf69c3 |
---|---|
21 | 21 |
22 from pyjamas.ui.Grid import Grid | 22 from pyjamas.ui.Grid import Grid |
23 from pyjamas.ui.Button import Button | 23 from pyjamas.ui.Button import Button |
24 from pyjamas.ui.ListBox import ListBox | 24 from pyjamas.ui.ListBox import ListBox |
25 from pyjamas.ui.FlowPanel import FlowPanel | 25 from pyjamas.ui.FlowPanel import FlowPanel |
26 from pyjamas.ui.PopupPanel import PopupPanel | |
27 from pyjamas.ui.AutoComplete import AutoCompleteTextBox | 26 from pyjamas.ui.AutoComplete import AutoCompleteTextBox |
28 from pyjamas.ui.Label import Label | 27 from pyjamas.ui.Label import Label |
29 from pyjamas.ui.HorizontalPanel import HorizontalPanel | 28 from pyjamas.ui.HorizontalPanel import HorizontalPanel |
30 from pyjamas.ui.VerticalPanel import VerticalPanel | 29 from pyjamas.ui.VerticalPanel import VerticalPanel |
31 from pyjamas.ui.DialogBox import DialogBox | 30 from pyjamas.ui.DialogBox import DialogBox |
34 from pyjamas.ui.FocusListener import FocusHandler | 33 from pyjamas.ui.FocusListener import FocusHandler |
35 from pyjamas.ui.DropWidget import DropWidget | 34 from pyjamas.ui.DropWidget import DropWidget |
36 from pyjamas.ui.DragWidget import DragWidget | 35 from pyjamas.ui.DragWidget import DragWidget |
37 from pyjamas.Timer import Timer | 36 from pyjamas.Timer import Timer |
38 from pyjamas import DOM | 37 from pyjamas import DOM |
38 import panels | |
39 | 39 |
40 # Map the recipient types to their properties. For convenience, the key | 40 # Map the recipient types to their properties. For convenience, the key |
41 # value is copied during the initialization to its associated sub-map, | 41 # value is copied during the initialization to its associated sub-map, |
42 # stored in the value of a new entry which uses "key" as its key. | 42 # stored in the value of a new entry which uses "title" as its key. |
43 RECIPIENT_TYPES = {"To": {"desc": "Direct recipients", "optional": False}, | 43 RECIPIENT_TYPES = {"To": {"desc": "Direct recipients", "optional": False}, |
44 "Cc": {"desc": "Carbon copies", "optional": True}, | 44 "Cc": {"desc": "Carbon copies", "optional": True}, |
45 "Bcc": {"desc": "Blind carbon copies", "optional": True}} | 45 "Bcc": {"desc": "Blind carbon copies", "optional": True}} |
46 | 46 |
47 # HTML content for the removal button (image or text) | 47 # HTML content for the removal button (image or text) |
70 self.__remaining_list = [] | 70 self.__remaining_list = [] |
71 self.__remaining_list.extend(self.__list) | 71 self.__remaining_list.extend(self.__list) |
72 # mark a change to sort the list before it's used | 72 # mark a change to sort the list before it's used |
73 self.__remaining_list_sorted = True | 73 self.__remaining_list_sorted = True |
74 | 74 |
75 self.recipient_menu = panels.PopupMenuPanel(entries=RECIPIENT_TYPES, | |
76 hide=lambda sender, key: self.__children[key]["panel"].isVisible(), | |
77 callback=self.setRecipientPanelVisible, | |
78 item_style="recipientTypeItem") | |
79 | |
75 def createWidgets(self): | 80 def createWidgets(self): |
76 """Fill the parent grid with all the widgets but | 81 """Fill the parent grid with all the widgets but |
77 only show those for non optional recipient types.""" | 82 only show those for non optional recipient types.""" |
78 self.__children = {} | 83 self.__children = {} |
79 for key in RECIPIENT_TYPES: | 84 for key in RECIPIENT_TYPES: |
80 # copy the key to its associated sub-map | 85 # copy the key to its associated sub-map |
81 RECIPIENT_TYPES[key]["key"] = key | 86 RECIPIENT_TYPES[key]["title"] = key |
82 self._addChild(RECIPIENT_TYPES[key]) | 87 self._addChild(RECIPIENT_TYPES[key]) |
83 | 88 |
84 def _addChild(self, entry): | 89 def _addChild(self, entry): |
85 """Add a button and FlowPanel for the corresponding map entry.""" | 90 """Add a button and FlowPanel for the corresponding map entry.""" |
86 button = Button("%s: " % entry["key"], self.selectRecipientType) | 91 button = Button("%s: " % entry["title"]) |
92 self.recipient_menu.registerClickSender(button) | |
87 button.addStyleName("recipientTypeItem") | 93 button.addStyleName("recipientTypeItem") |
88 button.setTitle(entry["desc"]) | 94 button.setTitle(entry["desc"]) |
89 button.setVisible(not entry["optional"]) | 95 button.setVisible(not entry["optional"]) |
90 self._parent.setWidget(len(self.__children), 0, button) | 96 self._parent.setWidget(len(self.__children), 0, button) |
91 self._parent.getCellFormatter().setStyleName(len(self.__children), 0, "recipientButtonCell") | 97 self._parent.getCellFormatter().setStyleName(len(self.__children), 0, "recipientButtonCell") |
92 | 98 |
93 _child = RecipientTypePanel(self, entry) | 99 _child = RecipientTypePanel(self, entry) |
94 self._parent.setWidget(len(self.__children), 1, _child) | 100 self._parent.setWidget(len(self.__children), 1, _child) |
95 | 101 |
96 self.__children[entry["key"]] = {} | 102 self.__children[entry["title"]] = {} |
97 self.__children[entry["key"]]["button"] = button | 103 self.__children[entry["title"]]["button"] = button |
98 self.__children[entry["key"]]["panel"] = _child | 104 self.__children[entry["title"]]["panel"] = _child |
99 | 105 |
100 def _refresh(self): | 106 def _refresh(self): |
101 """Set visible the sub-panels that are non optional or non empty, hide the rest.""" | 107 """Set visible the sub-panels that are non optional or non empty, hide the rest.""" |
102 for key in self.__children: | 108 for key in self.__children: |
103 self.setRecipientPanelVisible(key, False) | 109 self.setRecipientPanelVisible(key, False) |
104 _map = self.getRecipients() | 110 _map = self.getRecipients() |
105 for key in _map: | 111 for key in _map: |
106 if len(_map[key]) > 0 or not RECIPIENT_TYPES[key]["optional"]: | 112 if len(_map[key]) > 0 or not RECIPIENT_TYPES[key]["optional"]: |
107 self.setRecipientPanelVisible(key, True) | 113 self.setRecipientPanelVisible(key, True) |
108 | 114 |
109 def setRecipientPanelVisible(self, key, visible=True): | 115 def setRecipientPanelVisible(self, key, visible=True, sender=None): |
116 """Do not remove the "sender" param as it is needed for the context menu.""" | |
110 self.__children[key]["button"].setVisible(visible) | 117 self.__children[key]["button"].setVisible(visible) |
111 self.__children[key]["panel"].setVisible(visible) | 118 self.__children[key]["panel"].setVisible(visible) |
112 | 119 |
113 def getList(self): | 120 def getList(self): |
114 """Return the full list of potential recipients.""" | 121 """Return the full list of potential recipients.""" |
136 self.__sort_remaining_list = True | 143 self.__sort_remaining_list = True |
137 | 144 |
138 def selectRecipients(self): | 145 def selectRecipients(self): |
139 """Display the recipients chooser dialog. This has been implemented while | 146 """Display the recipients chooser dialog. This has been implemented while |
140 prototyping and is currently not used. Left for an eventual later use. | 147 prototyping and is currently not used. Left for an eventual later use. |
141 Replaced by self.selectRecipientType. | 148 Replaced by the popup menu which allows to add a panel for Cc or Bcc. |
142 """ | 149 """ |
143 RecipientChooserPanel(self) | 150 RecipientChooserPanel(self) |
144 | |
145 def selectRecipientType(self, sender): | |
146 """Display a context menu to add a new recipient type.""" | |
147 self.context_menu = VerticalPanel() | |
148 self.context_menu.setStyleName("recipientTypeMenu") | |
149 popup = PopupPanel(autoHide=True) | |
150 | |
151 for key in RECIPIENT_TYPES: | |
152 if self.__children[key]["panel"].isVisible(): | |
153 continue | |
154 | |
155 def showPanel(sender): | |
156 self.setRecipientPanelVisible(sender.getText()) | |
157 popup.hide(autoClosed=True) | |
158 | |
159 item = Button(key, showPanel) | |
160 item.setStyleName("recipientTypeItem") | |
161 item.setTitle(RECIPIENT_TYPES[key]["desc"]) | |
162 self.context_menu.add(item) | |
163 | |
164 popup.add(self.context_menu) | |
165 popup.setPopupPosition(sender.getAbsoluteLeft() + sender.getOffsetWidth(), sender.getAbsoluteTop()) | |
166 popup.show() | |
167 | 151 |
168 def setRecipients(self, _map={}): | 152 def setRecipients(self, _map={}): |
169 """Set the recipients for each recipient types.""" | 153 """Set the recipients for each recipient types.""" |
170 for key in RECIPIENT_TYPES: | 154 for key in RECIPIENT_TYPES: |
171 if key in _map: | 155 if key in _map: |
356 textbox = DragAutoCompleteTextBox() | 340 textbox = DragAutoCompleteTextBox() |
357 textbox.addStyleName("recipientTextBox") | 341 textbox.addStyleName("recipientTextBox") |
358 textbox.setText(recipient) | 342 textbox.setText(recipient) |
359 self.add(textbox) | 343 self.add(textbox) |
360 try: | 344 try: |
361 textbox.setVisibleLength(len(recipient)) | 345 textbox.setVisibleLength(len(str(recipient))) |
362 except: | 346 except: |
363 #TODO: . how come could this happen?! len(recipient) is sometimes 0 but recipient is not empty | 347 #TODO: . how come could this happen?! len(recipient) is sometimes 0 but recipient is not empty |
364 print "len(recipient) returns %d where recipient == %s..." % (len(recipient), recipient) | 348 print "len(recipient) returns %d where recipient == %s..." % (len(str(recipient)), str(recipient)) |
365 self._parent.removeFromRemainingList(recipient) | 349 self._parent.removeFromRemainingList(recipient) |
366 | 350 |
367 remove_btn = Button(REMOVE_BUTTON, Visible=False) | 351 remove_btn = Button(REMOVE_BUTTON, Visible=False) |
368 remove_btn.addStyleName("recipientRemoveButton") | 352 remove_btn.addStyleName("recipientRemoveButton") |
369 | 353 |