diff 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
line wrap: on
line diff
--- a/browser_side/recipients.py	Tue Oct 15 13:39:21 2013 +0200
+++ b/browser_side/recipients.py	Tue Oct 15 13:36:51 2013 +0200
@@ -23,7 +23,6 @@
 from pyjamas.ui.Button import Button
 from pyjamas.ui.ListBox import ListBox
 from pyjamas.ui.FlowPanel import FlowPanel
-from pyjamas.ui.PopupPanel import PopupPanel
 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
 from pyjamas.ui.Label import Label
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
@@ -36,10 +35,11 @@
 from pyjamas.ui.DragWidget import DragWidget
 from pyjamas.Timer import Timer
 from pyjamas import DOM
+import panels
 
 # Map the recipient types to their properties. For convenience, the key
 # value is copied during the initialization to its associated sub-map,
-# stored in the value of a new entry which uses "key" as its key.
+# stored in the value of a new entry which uses "title" as its key.
 RECIPIENT_TYPES = {"To": {"desc": "Direct recipients", "optional": False},
                    "Cc": {"desc": "Carbon copies", "optional": True},
                    "Bcc": {"desc": "Blind carbon copies", "optional": True}}
@@ -72,18 +72,24 @@
         # mark a change to sort the list before it's used
         self.__remaining_list_sorted = True
 
+        self.recipient_menu = panels.PopupMenuPanel(entries=RECIPIENT_TYPES,
+                                                    hide=lambda sender, key: self.__children[key]["panel"].isVisible(),
+                                                    callback=self.setRecipientPanelVisible,
+                                                    item_style="recipientTypeItem")
+
     def createWidgets(self):
         """Fill the parent grid with all the widgets but
         only show those for non optional recipient types."""
         self.__children = {}
         for key in RECIPIENT_TYPES:
             # copy the key to its associated sub-map
-            RECIPIENT_TYPES[key]["key"] = key
+            RECIPIENT_TYPES[key]["title"] = key
             self._addChild(RECIPIENT_TYPES[key])
 
     def _addChild(self, entry):
         """Add a button and FlowPanel for the corresponding map entry."""
-        button = Button("%s: " % entry["key"], self.selectRecipientType)
+        button = Button("%s: " % entry["title"])
+        self.recipient_menu.registerClickSender(button)
         button.addStyleName("recipientTypeItem")
         button.setTitle(entry["desc"])
         button.setVisible(not entry["optional"])
@@ -93,9 +99,9 @@
         _child = RecipientTypePanel(self, entry)
         self._parent.setWidget(len(self.__children), 1, _child)
 
-        self.__children[entry["key"]] = {}
-        self.__children[entry["key"]]["button"] = button
-        self.__children[entry["key"]]["panel"] = _child
+        self.__children[entry["title"]] = {}
+        self.__children[entry["title"]]["button"] = button
+        self.__children[entry["title"]]["panel"] = _child
 
     def _refresh(self):
         """Set visible the sub-panels that are non optional or non empty, hide the rest."""
@@ -106,7 +112,8 @@
             if len(_map[key]) > 0 or not RECIPIENT_TYPES[key]["optional"]:
                 self.setRecipientPanelVisible(key, True)
 
-    def setRecipientPanelVisible(self, key, visible=True):
+    def setRecipientPanelVisible(self, key, visible=True, sender=None):
+        """Do not remove the "sender" param as it is needed for the context menu."""
         self.__children[key]["button"].setVisible(visible)
         self.__children[key]["panel"].setVisible(visible)
 
@@ -138,33 +145,10 @@
     def selectRecipients(self):
         """Display the recipients chooser dialog. This has been implemented while
         prototyping and is currently not used. Left for an eventual later use.
-        Replaced by self.selectRecipientType.
+        Replaced by the popup menu which allows to add a panel for Cc or Bcc.
         """
         RecipientChooserPanel(self)
 
-    def selectRecipientType(self, sender):
-        """Display a context menu to add a new recipient type."""
-        self.context_menu = VerticalPanel()
-        self.context_menu.setStyleName("recipientTypeMenu")
-        popup = PopupPanel(autoHide=True)
-
-        for key in RECIPIENT_TYPES:
-            if self.__children[key]["panel"].isVisible():
-                continue
-
-            def showPanel(sender):
-                self.setRecipientPanelVisible(sender.getText())
-                popup.hide(autoClosed=True)
-
-            item = Button(key, showPanel)
-            item.setStyleName("recipientTypeItem")
-            item.setTitle(RECIPIENT_TYPES[key]["desc"])
-            self.context_menu.add(item)
-
-        popup.add(self.context_menu)
-        popup.setPopupPosition(sender.getAbsoluteLeft() + sender.getOffsetWidth(), sender.getAbsoluteTop())
-        popup.show()
-
     def setRecipients(self, _map={}):
         """Set the recipients for each recipient types."""
         for key in RECIPIENT_TYPES:
@@ -358,10 +342,10 @@
         textbox.setText(recipient)
         self.add(textbox)
         try:
-            textbox.setVisibleLength(len(recipient))
+            textbox.setVisibleLength(len(str(recipient)))
         except:
             #TODO: . how come could this happen?! len(recipient) is sometimes 0 but recipient is not empty
-            print "len(recipient) returns %d where recipient == %s..." % (len(recipient), recipient)
+            print "len(recipient) returns %d where recipient == %s..." % (len(str(recipient)), str(recipient))
         self._parent.removeFromRemainingList(recipient)
 
         remove_btn = Button(REMOVE_BUTTON, Visible=False)