comparison src/browser/sat_browser/base_widget.py @ 647:e0021d571eef frontends_multi_profiles

browser side: moved ContactBox, ContactsPanel, ContactLabeland ContactMenuBar to base_widget/base_panels so they can be reused outside of contact_list module
author Goffi <goffi@goffi.org>
date Thu, 26 Feb 2015 13:10:46 +0100
parents 7113d40533d6
children 6d3142b782c3
comparison
equal deleted inserted replaced
646:9972a24592b0 647:e0021d571eef
32 from pyjamas.ui.FlexTable import FlexTable 32 from pyjamas.ui.FlexTable import FlexTable
33 from pyjamas.ui.TabPanel import TabPanel 33 from pyjamas.ui.TabPanel import TabPanel
34 from pyjamas.ui.HTMLPanel import HTMLPanel 34 from pyjamas.ui.HTMLPanel import HTMLPanel
35 from pyjamas.ui.Label import Label 35 from pyjamas.ui.Label import Label
36 from pyjamas.ui.HTML import HTML 36 from pyjamas.ui.HTML import HTML
37 from pyjamas.ui.Image import Image
37 from pyjamas.ui.Button import Button 38 from pyjamas.ui.Button import Button
38 from pyjamas.ui.Widget import Widget 39 from pyjamas.ui.Widget import Widget
39 from pyjamas.ui.DragWidget import DragWidget 40 from pyjamas.ui.DragWidget import DragWidget
40 from pyjamas.ui.DropWidget import DropWidget 41 from pyjamas.ui.DropWidget import DropWidget
41 from pyjamas.ui.ClickListener import ClickHandler 42 from pyjamas.ui.ClickListener import ClickHandler
42 from pyjamas.ui import HasAlignment 43 from pyjamas.ui import HasAlignment
43 from pyjamas import DOM 44 from pyjamas import DOM
44 from pyjamas import Window 45 from pyjamas import Window
46 from constants import Const as C
45 47
46 from __pyjamas__ import doc 48 from __pyjamas__ import doc
47 49
48 import dialog 50 import dialog
49 import base_menu 51 import base_menu
52 import html_tools
53
54 unicode = str # XXX: pyjama doesn't manage unicode
50 55
51 56
52 class NoLiberviaWidgetException(Exception): 57 class NoLiberviaWidgetException(Exception):
53 pass 58 pass
54 59
890 """ Called when a child WidgetsPanel is empty and need to be removed """ 895 """ Called when a child WidgetsPanel is empty and need to be removed """
891 widget_index = self.getWidgetIndex(panel) 896 widget_index = self.getWidgetIndex(panel)
892 self.remove(panel) 897 self.remove(panel)
893 widgets_count = self.getWidgetCount() 898 widgets_count = self.getWidgetCount()
894 self.selectTab(widget_index if widget_index < widgets_count else widgets_count - 1) 899 self.selectTab(widget_index if widget_index < widgets_count else widgets_count - 1)
900
901
902 class ContactLabel(HTML):
903 """Display a contact in HTML, selecting best display (jid/nick/etc)"""
904
905 def __init__(self, host, jid_):
906 # TODO: add a listener for nick changes
907 HTML.__init__(self)
908 self.host = host
909 self.jid = jid_.bare
910 self.nick = self.host.contact_lists[C.PROF_KEY_NONE].getCache(self.jid, "nick")
911 self.alert = False
912 self.refresh()
913 self.setStyleName('contactLabel')
914
915 def refresh(self):
916 alert_html = "<strong>(*)</strong>&nbsp;" if self.alert else ""
917 contact_html = html_tools.html_sanitize(self.nick or unicode(self.jid))
918 html = "%(alert)s%(contact)s" % {'alert': alert_html,
919 'contact': contact_html}
920 self.setHTML(html)
921
922 def updateNick(self, new_nick):
923 """Change the current nick
924
925 @param new_nick(unicode): new nick to use
926 """
927 self.nick = new_nick
928 self.refresh()
929
930 def setAlert(self, alert):
931 """Show a visual indicator
932
933 @param alert: True if alert must be shown
934 """
935 self.alert = alert
936 self.refresh()
937
938
939 class ContactMenuBar(WidgetMenuBar):
940
941 def onBrowserEvent(self, event):
942 WidgetMenuBar.onBrowserEvent(self, event)
943 event.stopPropagation() # prevent opening the chat dialog
944
945 @classmethod
946 def getCategoryHTML(cls, menu_name_i18n, type_):
947 return '<img src="%s"/>' % C.DEFAULT_AVATAR_URL
948
949 def setUrl(self, url):
950 """Set the URL of the contact avatar."""
951 self.items[0].setHTML('<img src="%s" />' % url)
952
953
954 class ContactBox(VerticalPanel, ClickHandler, DragLabel):
955
956 def __init__(self, parent, jid_):
957 """
958 @param parent (ContactPanel): ContactPanel hosting this box
959 @param jid_ (jid.JID): contact JID
960 """
961 VerticalPanel.__init__(self, StyleName='contactBox', VerticalAlignment='middle')
962 ClickHandler.__init__(self)
963 DragLabel.__init__(self, jid_, "CONTACT", parent.host)
964 self.jid = jid_.bare
965 self.label = ContactLabel(parent.host, self.jid)
966 self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image()
967 self.updateAvatar(parent.host.getAvatarURL(self.jid))
968 self.add(self.avatar)
969 self.add(self.label)
970 self.addClickListener(self)
971
972 def addMenus(self, menu_bar):
973 menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid)})
974 menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid)})
975
976 def setAlert(self, alert):
977 """Show a visual indicator
978
979 @param alert: True if alert indicator show be shown"""
980 self.label.setAlert(alert)
981
982 def updateAvatar(self, url):
983 """Update the avatar.
984
985 @param url (unicode): image url
986 """
987 self.avatar.setUrl(url)
988
989 def updateNick(self, new_nick):
990 """Update the nickname.
991
992 @param new_nick (unicode): new nickname to use
993 """
994 self.label.updateNick(new_nick)
995
996 def onClick(self, sender):
997 try:
998 self.parent.onClick(self.jid)
999 except AttributeError:
1000 pass
1001 else:
1002 self.setAlert(False)