diff libervia.py @ 16:099c05a0dcab

browser side: microblog panel improvments - panels can now be changed by DnD - contacts panel's title can be used by DnD for the global microblog panel - microblogs now appear in reverse order (from bottom to top) - MicroblogPanel now use a ScrollPanel and 100% space - user's message now appear in correct group when using groupblog - MagicBox renamed to UniBox
author Goffi <goffi@goffi.org>
date Fri, 15 Apr 2011 02:01:34 +0200
parents 9bf8ed012adc
children c725b702e927
line wrap: on
line diff
--- a/libervia.py	Sat Apr 09 13:14:39 2011 +0200
+++ b/libervia.py	Fri Apr 15 02:01:34 2011 +0200
@@ -25,6 +25,7 @@
 from pyjamas.ui.VerticalPanel import VerticalPanel
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
 from pyjamas.ui.HTMLPanel import HTMLPanel
+from pyjamas.ui.ScrollPanel import ScrollPanel
 from pyjamas.ui.Grid import Grid
 from pyjamas.ui.Label import Label
 from pyjamas.ui import HasAlignment
@@ -119,7 +120,7 @@
     def onXiangqiGame(self):
         Window.alert("Xiangqi selected")
 
-class MagicBox(AutoCompleteTextBox):
+class UniBox(AutoCompleteTextBox):
 
     def __init__(self, host):
         AutoCompleteTextBox.__init__(self)
@@ -138,28 +139,31 @@
         #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this
         return AutoCompleteTextBox.complete(self)
 
-class EmptyPanel(DropWidget, SimplePanel):
-    """Empty dropable panel"""
-
-    def __init__(self, host, data):
-        SimplePanel.__init__(self)
-        self.host = host
-        self.data = data
-        _panel = HTMLPanel("&nbsp;")
-        self.add(_panel)
-        self.setHeight('100%')
+class DropCell(DropWidget):
+    """Cell in the middle grid which replace itself with the dropped widget on DnD"""
+    
+    def __init__(self):
         DropWidget.__init__(self)
     
     def onDragEnter(self, event):
+        print "drag enter"
         self.addStyleName('dragover')
         DOM.eventPreventDefault(event)
 
     def onDragLeave(self, event):
+        print "drag leave"
         self.removeStyleName('dragover')
 
     def onDragOver(self, event):
         DOM.eventPreventDefault(event)
 
+    def _getCellAndRow(self, grid, event):
+        """Return cell and row index where the event is occuring"""
+        cell = grid.getEventTargetCell(event)
+        row = DOM.getParent(cell)
+        return (row.rowIndex, cell.cellIndex)
+
+
     def onDrop(self, event):
         print "Empty Panel: onDrop"
         dt = event.dataTransfer
@@ -174,14 +178,34 @@
             item='&nbsp;'
             item_type = None
         DOM.eventPreventDefault(event)
-        _mblog = MicroblogPanel(self.host, item)
         if item_type=="GROUP":
+            _mblog = MicroblogPanel(self.host, item)
             _mblog.setAcceptedGroup(item)
-        self.host.mpanels.insert(0,_mblog)
-        self.host.middle_panel.changePanel(self.data,self.host.mpanels[0])
+        elif item_type=="CONTACT":
+            _mblog = MicroblogPanel(self.host, accept_all=True)
+        self.host.mpanels.remove(self)
+        self.host.mpanels.append(_mblog)
+        print "DEBUG"
+        grid = self.getParent()
+        row_idx, cell_idx = self._getCellAndRow(grid, event)
+        self.removeFromParent()
+        grid.setWidget(row_idx, cell_idx, _mblog)
+        print "index:", row_idx, cell_idx
+        #FIXME: delete object ? Check the right way with pyjamas
+        #self.host.middle_panel.changePanel(self.data,self.host.mpanels[0])
+        
 
+class EmptyPanel(DropCell, SimplePanel):
+    """Empty dropable panel"""
 
-
+    def __init__(self, host):
+        SimplePanel.__init__(self)
+        self.host = host
+        _panel = HTMLPanel("&nbsp;")
+        self.add(_panel)
+        self.setHeight('100%')
+        DropCell.__init__(self)
+    
 class MicroblogEntry(SimplePanel):
 
     def __init__(self, body, author, timestamp):
@@ -198,19 +222,27 @@
         self.add(panel)
 
 
-class MicroblogPanel(VerticalPanel):
+class MicroblogPanel(DropCell, ScrollPanel):
 
     def __init__(self,host, title='&nbsp;', accept_all=False):
+        """Panel used to show microblog
+        @param title: title of the panel
+        @param accept_all: if true, show every message, without filtering jids"""
         self.host = host
         self.accept_all = accept_all
         title=title.replace('<','&lt;').replace('>','&gt;')
-        VerticalPanel.__init__(self)
         self.accepted_groups = []
         _class = ['mb_panel_header']
         if title == '&nbsp;':
             _class.append('empty_header')
-        self.add(HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),title)))
+        ScrollPanel.__init__(self)
+        self.vpanel = VerticalPanel()
+        self.vpanel.add(HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),title)))
+        self.vpanel.setWidth('100%')
+        self.setHeight('100%')
         self.setStyleName('microblogPanel')
+        self.add(self.vpanel)
+        DropCell.__init__(self)
 
     def addEntry(self, text, author=None, timestamp=None):
         """Add an entry to the panel
@@ -218,7 +250,7 @@
         @param author: who wrote the entry
         @param date: when the entry was written"""
         _entry = MicroblogEntry(text, author, timestamp)
-        self.add(_entry)
+        self.vpanel.insert(_entry,1)
 
     def setAcceptedGroup(self, group):
         """Set the group which can be displayed in this panel
@@ -239,6 +271,7 @@
             if self.host.contactPanel.isContactInGroup(group, jid):
                 return True
         return False
+    
 
 
 class MiddlePannel(HorizontalPanel):
@@ -271,18 +304,18 @@
         self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
 
         menu = Menu()
-        magic_box = host.magicBox
+        uni_box = host.uniBox
         self.middle_panel = MiddlePannel(self.host)
         self.middle_panel.setWidth('100%')
 
         self.add(menu)
-        self.add(magic_box)
+        self.add(uni_box)
         self.add(self.middle_panel)
         
         self.setCellHeight(menu, "5%")
-        self.setCellHeight(magic_box, "5%")
-        self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
-        self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
+        self.setCellHeight(uni_box, "5%")
+        self.setCellVerticalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
+        self.setCellHorizontalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
         self.setCellHeight(self.middle_panel, "90%")
         self.setCellWidth(self.middle_panel, "100%")
 
@@ -293,15 +326,15 @@
     def onModuleLoad(self):
         self.bridge = BridgeCall()
         self.bridge_signals = BridgeSignals()
-        self.magicBox = MagicBox(self)
-        self.magicBox.addKey("@@: ")
+        self.uniBox = UniBox(self)
+        self.uniBox.addKey("@@: ")
         self.contactPanel = ContactPanel(self)
         self.panel = MainPanel(self)
         self.middle_panel = self.panel.middle_panel
-        self.mpanels = [MicroblogPanel(self, accept_all=True)]
-        self.middle_panel.changePanel(1,self.mpanels[0])
-        self.middle_panel.changePanel(0,EmptyPanel(self, 0))
-        self.middle_panel.changePanel(2,EmptyPanel(self, 2))
+        self.mpanels = [EmptyPanel(self), MicroblogPanel(self, accept_all=True), EmptyPanel(self)]
+        self.middle_panel.changePanel(0,self.mpanels[0])
+        self.middle_panel.changePanel(1,self.mpanels[1])
+        self.middle_panel.changePanel(2,self.mpanels[2])
         self._dialog = None
         RootPanel().add(self.panel)
         self._register = RegisterCall()
@@ -349,8 +382,14 @@
             if not data.has_key('content'):
                 print ("WARNING: No content found in microblog data")
                 return
+            print dir('')
+            if data.has_key('groups'):
+                _groups = set(data['groups'].split() if data['groups'] else [])
+            else:
+                _groups=None
+            print "_groups=",_groups
             for panel in self.mpanels:
-                if isinstance(panel,MicroblogPanel) and panel.isJidAccepted(sender):
+                if isinstance(panel,MicroblogPanel) and (panel.isJidAccepted(sender) or _groups == None or _groups.intersection(panel.accepted_groups)):
                     print "sender:",sender
                     content = data['content']
                     author = data.get('author')