changeset 671:2201ff543a05 frontends_multi_profiles

browser_side: replace ContactsPanel and ContactBox init "parent" attribute with "host" + menus bound to the avatar can be parameterized
author souliane <souliane@mailoo.org>
date Mon, 09 Mar 2015 16:30:06 +0100
parents a80c13249f5d
children b39a9eddfe56
files src/browser/public/libervia.css src/browser/sat_browser/chat.py src/browser/sat_browser/contact_list.py src/browser/sat_browser/contact_panel.py src/browser/sat_browser/contact_widget.py
diffstat 5 files changed, 58 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/public/libervia.css	Mon Mar 09 16:27:20 2015 +0100
+++ b/src/browser/public/libervia.css	Mon Mar 09 16:30:06 2015 +0100
@@ -499,7 +499,7 @@
     background: #EDEDED;
 }
 
-.contactBox img {
+.contactBox img, .muc_contact img {
     width: 32px;
     height: 32px;
     border-radius: 5px;
--- a/src/browser/sat_browser/chat.py	Mon Mar 09 16:27:20 2015 +0100
+++ b/src/browser/sat_browser/chat.py	Mon Mar 09 16:30:06 2015 +0100
@@ -96,7 +96,10 @@
         chat_area = HorizontalPanel()
         chat_area.setStyleName('chatArea')
         if type_ == C.CHAT_GROUP:
-            self.occupants_panel = contact_panel.ContactsPanel(self, contacts_style="muc_contact", contacts_display=('resource',))
+            self.occupants_panel = contact_panel.ContactsPanel(host,
+                                                               contacts_style="muc_contact",
+                                                               contacts_menus=(C.MENU_JID_CONTEXT),
+                                                               contacts_display=('resource',))
             self.occupants_panel.setStyleName("occupantsPanel")
             chat_area.add(self.occupants_panel)
         self._body.add(chat_area)
--- a/src/browser/sat_browser/contact_list.py	Mon Mar 09 16:27:20 2015 +0100
+++ b/src/browser/sat_browser/contact_list.py	Mon Mar 09 16:30:06 2015 +0100
@@ -136,10 +136,13 @@
 class ContactsPanel(contact_panel.ContactsPanel):
     """The contact list that is displayed on the left side."""
 
-    def __init__(self, parent):
+    def __init__(self, host):
+
         def on_click(contact_jid):
             self.host.displayWidget(chat.Chat, contact_jid, type_=C.CHAT_ONE2ONE)
-        contact_panel.ContactsPanel.__init__(self, parent, on_click=on_click, handle_menu=True)
+
+        contact_panel.ContactsPanel.__init__(self, host, contacts_click=on_click,
+                                             contacts_menus=(C.MENU_JID_CONTEXT, C.MENU_ROSTER_JID_CONTEXT))
 
     def setState(self, jid_, type_, state):
         """Change the appearance of the contact, according to the state
@@ -185,7 +188,7 @@
         self.vPanel = VerticalPanel()
         _title = ContactTitleLabel(host, 'Contacts')
         DOM.setStyleAttribute(_title.getElement(), "cursor", "pointer")
-        self._contacts_panel = ContactsPanel(self)
+        self._contacts_panel = ContactsPanel(host)
         self._contacts_panel.setStyleName('contactPanel') # FIXME: style doesn't exists !
         self._group_panel = GroupPanel(self)
 
--- a/src/browser/sat_browser/contact_panel.py	Mon Mar 09 16:27:20 2015 +0100
+++ b/src/browser/sat_browser/contact_panel.py	Mon Mar 09 16:30:06 2015 +0100
@@ -82,22 +82,27 @@
     Special features like popup menu panel or changing the contact states must be done in a sub-class.
     """
 
-    def __init__(self, parent, on_click=None, handle_menu=True, contacts_style=None, contacts_display=C.CONTACT_DEFAULT_DISPLAY):
+    def __init__(self, host, contacts_click=None, contacts_style=None, contacts_menus=True, contacts_display=C.CONTACT_DEFAULT_DISPLAY):
         """
-        @param on_click (callable): click callback (used if ContactBox is created)
-        @param handle_menu (bool): if True, bind a popup menu to the avatar (used if ContactBox is created)
-        """ # FIXME
+
+        @param host (SatWebFrontend): host instance
+        @param contacts_click (callable): click callback for the contact boxes
+        @param contacts_style (unicode): CSS style name for the contact boxes
+        @param contacts_menus (tuple): define the menu types that fit this
+            contact panel, with values from the menus type constants.
+        @param contacts_display (tuple): prioritize the display methods of the
+            contact's label with values in ("jid", "nick", "bare", "resource")
+        """
         VerticalPanel.__init__(self)
-        self._parent = parent
-        self.host = parent.host
-        self._contacts = {} # entity jid to ContactBox map
+        self.host = host
+        self._contacts = {}  # entity jid to ContactBox map
         self.click_listener = None
-        self.handle_menu = handle_menu
 
-        if on_click is not None:
-            self.onClick = on_click
+        if contacts_click is not None:
+            self.onClick = contacts_click
 
-        self.contacts_style=contacts_style
+        self.contacts_style = contacts_style
+        self.contacts_menus = contacts_menus
         self.contacts_display = contacts_display
 
     def setList(self, jids):
@@ -134,7 +139,10 @@
         try:
             return self._contacts[contact_jid.bare]
         except KeyError:
-            box = contact_widget.ContactBox(self, contact_jid, style_name=self.contacts_style, display=self.contacts_display)
+            box = contact_widget.ContactBox(self.host, contact_jid,
+                                            style_name=self.contacts_style,
+                                            menu_types=self.contacts_menus,
+                                            display=self.contacts_display)
             self._contacts[contact_jid.bare] = box
             return box
 
--- a/src/browser/sat_browser/contact_widget.py	Mon Mar 09 16:27:20 2015 +0100
+++ b/src/browser/sat_browser/contact_widget.py	Mon Mar 09 16:30:06 2015 +0100
@@ -38,6 +38,13 @@
     """Display a contact in HTML, selecting best display (jid/nick/etc)"""
 
     def __init__(self, host, jid_, display=C.CONTACT_DEFAULT_DISPLAY):
+        """
+
+        @param host (SatWebFrontend): host instance
+        @param jid_ (jid.JID): contact JID
+        @param display (tuple): prioritize the display methods of the contact's
+            label with values in ("jid", "nick", "bare", "resource").
+        """
         # TODO: add a listener for nick changes
         HTML.__init__(self)
         self.host = host
@@ -107,25 +114,36 @@
 
 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel):
 
-    def __init__(self, parent, jid_, style_name=None, display=C.CONTACT_DEFAULT_DISPLAY):
+    def __init__(self, host, jid_, style_name=None, menu_types=None, display=C.CONTACT_DEFAULT_DISPLAY):
         """
-        @param parent (ContactPanel): ContactPanel hosting this box
+        @param host (SatWebFrontend): host instance
         @param jid_ (jid.JID): contact JID
+        @param style_name (unicode): CSS style name
+        @param menu_types (tuple): define the menu types that fit this
+            contact panel, with values from the menus type constants.
+        @param contacts_display (tuple): prioritize the display methods of the
+            contact's label with values in ("jid", "nick", "bare", "resource").
+
         """
         VerticalPanel.__init__(self, StyleName=style_name or 'contactBox', VerticalAlignment='middle')
         ClickHandler.__init__(self)
-        libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", parent.host)
+        libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", host)
         self.jid = jid_
-        self.label = ContactLabel(parent.host, self.jid, display=display)
-        self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image()
-        self.updateAvatar(parent.host.getAvatarURL(self.jid.bare))
+        self.label = ContactLabel(host, self.jid, display=display)
+        self.menu_types = menu_types
+        self.avatar = ContactMenuBar(self, host) if menu_types else Image()
+        try:  # FIXME: dirty hack to force using an Image when the menu is actually empty
+            self.avatar.items[0]
+        except IndexError:
+            self.avatar = Image()
+        self.updateAvatar(host.getAvatarURL(self.jid.bare))
         self.add(self.avatar)
         self.add(self.label)
         self.addClickListener(self)
 
     def addMenus(self, menu_bar):
-        menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid.bare)})
-        menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid.bare)})
+        for menu_type in self.menu_types:
+            menu_bar.addCachedMenus(menu_type, {'jid': unicode(self.jid.bare)})
 
     def setAlert(self, alert):
         """Show a visual indicator
@@ -150,7 +168,7 @@
     def onClick(self, sender):
         try:
             self.parent.onClick(self.jid.bare)
-        except AttributeError:
+        except (AttributeError, TypeError):
             pass
         else:
             self.setAlert(False)