changeset 362:019e1e706e74

browser_side: display the popup notifying the message's recipient when you edit the blog entry's bubble.
author souliane <souliane@mailoo.org>
date Wed, 19 Feb 2014 16:38:13 +0100
parents f4efffb9627c
children 187126b63170
files browser_side/base_panels.py browser_side/panels.py browser_side/richtext.py
diffstat 3 files changed, 59 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_panels.py	Wed Feb 19 14:17:03 2014 +0100
+++ b/browser_side/base_panels.py	Wed Feb 19 16:38:13 2014 +0100
@@ -321,6 +321,7 @@
         self.__modifiedCb = modifiedCb
         self._afterEditCb = afterEditCb
         self.initialized = False
+        self.edit_listeners = []
         self.setContent(content)
 
     def setContent(self, content=None):
@@ -414,6 +415,11 @@
     def abortEdition(self, content):
         return True
 
+    def addEditListener(self, listener):
+        """Add a method to be called whenever the text is edited.
+        @param listener: method taking two arguments: sender, keycode"""
+        self.edit_listeners.append(listener)
+
 
 class LightTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler):
     """Manage a simple text editor whith the HTML 5 "contenteditable" property."""
@@ -430,7 +436,7 @@
         HTML.__init__(self)
         if single_line:
             FocusHandler.__init__(self)
-            KeyboardHandler.__init__(self)
+        KeyboardHandler.__init__(self)
         self.__single_line = single_line
         self.__enhance_display = enhance_display
         strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text)
@@ -451,14 +457,14 @@
         if edit:
             if self.__single_line:
                 self.addFocusListener(self)
-                self.addKeyboardListener(self)
+            self.addKeyboardListener(self)
         else:
             self.setDisplayContent()
             if self.__single_line:
                 if self in self._focusListeners:
                     self.removeFocusListener(self)
-                if self in self._keyboardListeners:
-                    self.removeKeyboardListener(self)
+            if self in self._keyboardListeners:
+                self.removeKeyboardListener(self)
 
     def setDisplayContent(self):
         text = addURLToImage(self._original_content['text'])
@@ -470,7 +476,13 @@
         else:
             self.getElement().blur()
 
+    def onKeyDown(self, sender, keycode, modifiers):
+        for listener in self.edit_listeners:
+            listener(self, keycode)
+
     def onKeyPress(self, sender, keycode, modifiers):
+        if not self.__single_line:
+            return
         if keycode == KEY_ENTER:
             self.setFocus(False)  # finish the edition
 
--- a/browser_side/panels.py	Wed Feb 19 14:17:03 2014 +0100
+++ b/browser_side/panels.py	Wed Feb 19 16:38:13 2014 +0100
@@ -232,7 +232,6 @@
     def onKeyPress(self, sender, keycode, modifiers):
         _txt = self.getText()
         target_hook, type_, msg = self._getTarget(_txt)
-        self.host.showWarning(type_, msg)
 
         if keycode == KEY_ENTER:
             if _txt:
@@ -242,6 +241,8 @@
                     self.host._updateInputHistory(_txt)
                     self.setText('')
             self.host.showWarning(None, None)
+        else:
+            self.host.showWarning(type_, msg)
         MessageBox.onKeyPress(self, sender, keycode, modifiers)
 
     def getTargetAndData(self):
@@ -529,6 +530,13 @@
         self.bubble.setStyleName("bubble")
         self.entry_dialog.add(self.bubble)
         self.bubble.edit(False)
+        self.bubble.addEditListener(self.__showWarning)
+
+    def __showWarning(self, sender, keycode):
+        if keycode == KEY_ENTER:
+            self._blog_panel.host.showWarning(None, None)
+        else:
+            self._blog_panel.host.showWarning(*self._blog_panel.getWarningData(self.type == 'comment'))
 
     def _delete(self, empty=False):
         """Ask confirmation for deletion.
@@ -647,13 +655,18 @@
         entity.sort()  # sort() do not return the sorted list: do it here, not on the "return" line
         return self.accepted_groups == entity
 
-    def getWarningData(self):
-        if self.selected_entry:
-            if not self.selected_entry.comments:
+    def getWarningData(self, comment=None):
+        """
+        @param comment: True if the composed message is a comment. If None, consider we are
+        composing from the unibox and guess the message type from self.selected_entry
+        @return: a couple (type, msg) for calling self.host.showWarning"""
+        if comment is None:  # composing from the unibox
+            if self.selected_entry and not self.selected_entry.comments:
                 print ("ERROR: an item without comment is selected")
                 return ("NONE", None)
+            comment = self.selected_entry is not None
+        if comment:
             return ("PUBLIC", "This is a <span class='warningTarget'>comment</span> and keep the initial post visibility, so it is potentialy public")
-
         elif not self._accepted_groups:
             # we have a meta MicroblogPanel, we publish publicly
             return ("PUBLIC", self.warning_msg_public)
--- a/browser_side/richtext.py	Wed Feb 19 14:17:03 2014 +0100
+++ b/browser_side/richtext.py	Wed Feb 19 16:38:13 2014 +0100
@@ -26,6 +26,7 @@
 from pyjamas.ui.FlexTable import FlexTable
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
 from pyjamas import Window
+from pyjamas.ui.KeyboardListener import KeyboardHandler
 from __pyjamas__ import doc
 
 from constants import Const
@@ -85,6 +86,13 @@
         FlexTable.__init__(self, self.command_offset + (0 if self.no_command else 1), 2)
         self.addStyleName(self.style['main'])
 
+    def addEditListener(self, listener):
+        """Add a method to be called whenever the text is edited.
+        @param listener: method taking two arguments: sender, keycode"""
+        BaseTextEditor.addEditListener(self, listener)
+        if hasattr(self, 'display'):
+            self.display.addEditListener(listener)
+
     def refresh(self, edit=None):
         """Refresh the UI for edition/display mode
         @param edit: set to True to display the edition mode"""
@@ -99,8 +107,10 @@
             self.toolbar.setVisible(False)
         if not hasattr(self, 'display'):
             self.display = LightTextEditor(enhance_display=False)  # for display mode
+            for listener in self.edit_listeners:
+                self.display.addEditListener(listener)
         if not self.read_only and not hasattr(self, 'textarea'):
-            self.textarea = TextArea()  # for edition mode
+            self.textarea = EditTextArea(self)  # for edition mode
             self.textarea.addStyleName(self.style['textarea'])
 
         self.getFlexCellFormatter().setColSpan(self.content_offset, 0, 2)
@@ -219,6 +229,7 @@
                                   + text[cursor_pos + selection_length:])
             self.textarea.setCursorPos(cursor_pos + len(data[0]) + len(middle_text))
             self.textarea.setFocus(True)
+            self.textarea.onKeyDown()
 
         def wysiwygCb():
             """Callback for a toolbar button while wysiwyg mode is enabled."""
@@ -236,6 +247,7 @@
                     execCommand(data[0], data[2])
             else:
                 execCommand(data, False, '')
+            self.textarea.onKeyDown()
 
         button.addClickListener(lambda: wysiwygCb() if self.wysiwyg else buttonCb())
 
@@ -494,3 +506,15 @@
         self.registerPopupMenuPanel(entries=composition.RECIPIENT_TYPES,
                                     hide=lambda sender, key: self.__children[key]["panel"].isVisible(),
                                     callback=self.setContactPanelVisible)
+
+
+class EditTextArea(TextArea, KeyboardHandler):
+    def __init__(self, _parent):
+        TextArea.__init__(self)
+        self._parent = _parent
+        KeyboardHandler.__init__(self)
+        self.addKeyboardListener(self)
+
+    def onKeyDown(self, sender=None, keycode=None, modifiers=None):
+        for listener in self._parent.edit_listeners:
+            listener(self, keycode)