changeset 404:c393e7dc9ae6

browser_side: refactorize SimpleTextEditor init parameters
author souliane <souliane@mailoo.org>
date Sat, 15 Mar 2014 00:21:04 +0100
parents ec6f7581b453
children 41b8b96f2248
files browser_side/base_panels.py browser_side/panels.py browser_side/richtext.py
diffstat 3 files changed, 41 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_panels.py	Fri Mar 14 21:00:13 2014 +0100
+++ b/browser_side/base_panels.py	Sat Mar 15 00:21:04 2014 +0100
@@ -440,29 +440,33 @@
 class SimpleTextEditor(BaseTextEditor, FocusHandler, KeyboardHandler, ClickHandler):
     """Base class for manage a simple text editor."""
 
-    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False):
+    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
         """
         @param content
         @param modifiedCb
         @param afterEditCb
-        @param single_line: set to True to manage a single line editor. In that
-        case the edition will be terminated when <enter> or <escape> is pressed.
-        @param listen_focus: set to True to terminate the edition when the
-        focus is lost. Leave to None in order to use single_line's value.
-        @param listen_click: set to True to start the edition when you click on the widget.
-        @param enhance_display: if True, the display text will be enhanced with addURLToText
+        @param options: dict with the following value:
+        - no_xhtml: set to True to clean any xhtml content.
+        - enhance_display: if True, the display text will be enhanced with addURLToText
+        - listen_keyboard: set to True to terminate the edition with <enter> or <escape>.
+        - listen_focus: set to True to terminate the edition when the focus is lost.
+        - listen_click: set to True to start the edition when you click on the widget.
         """
-        self.__single_line = single_line
-        self.__enhance_display = enhance_display
-        self.__listen_focus = single_line if listen_focus is None else listen_focus
-        self.__listen_click = listen_click
+        self.options = {'no_xhtml': False,
+                        'enhance_display': True,
+                        'listen_keyboard': True,
+                        'listen_focus': False,
+                        'listen_click': False
+                        }
+        if options:
+            self.options.update(options)
         self.__shift_down = False
-        if self.__listen_focus:
+        if self.options['listen_focus']:
             FocusHandler.__init__(self)
-        if self.__listen_click:
+        if self.options['listen_click']:
             ClickHandler.__init__(self)
         KeyboardHandler.__init__(self)
-        strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text)
+        strproc = lambda text: html_sanitize(html_strip(text)) if self.options['no_xhtml'] else html_strip(text)
         BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb)
         self.textarea = self.display = None
 
@@ -475,20 +479,20 @@
     def edit(self, edit, abort=False, sync=False):
         BaseTextEditor.edit(self, edit)
         if edit:
-            if self.__listen_focus and self not in self.textarea._focusListeners:
+            if self.options['listen_focus'] and self not in self.textarea._focusListeners:
                 self.textarea.addFocusListener(self)
-            if self.__listen_click:
+            if self.options['listen_click']:
                 self.display.clearClickListener()
             if self not in self.textarea._keyboardListeners:
                 self.textarea.addKeyboardListener(self)
         else:
             self.setDisplayContent()
-            if self.__listen_focus:
+            if self.options['listen_focus']:
                 try:
                     self.textarea.removeFocusListener(self)
                 except ValueError:
                     pass
-            if self.__listen_click and self not in self.display._clickListeners:
+            if self.options['listen_click'] and self not in self.display._clickListeners:
                 self.display.addClickListener(self)
             try:
                 self.textarea.removeKeyboardListener(self)
@@ -496,8 +500,12 @@
                 pass
 
     def setDisplayContent(self):
-        text = addURLToImage(self._original_content['text'])
-        self.display.setHTML(addURLToText(text) if self.__enhance_display else text)
+        text = self._original_content['text']
+        if not self.options['no_xhtml']:
+            text = addURLToImage(text)
+        if self.options['enhance_display']:
+            text = addURLToText(text)
+        self.display.setHTML(text)
 
     def setFocus(self, focus):
         raise NotImplementedError
@@ -505,14 +513,14 @@
     def onKeyDown(self, sender, keycode, modifiers):
         for listener in self.edit_listeners:
             listener(self.textarea, keycode)
-        if not self.__single_line:
+        if not self.options['listen_keyboard']:
             return
         if keycode == KEY_SHIFT or self.__shift_down:  # allow input a new line with <shift> + <enter>
             self.__shift_down = True
             return
         if keycode in (KEY_ENTER, KEY_ESCAPE):  # finish the edition
             self.textarea.setFocus(False)
-            if not self.__listen_focus:
+            if not self.options['listen_focus']:
                 self.edit(False)
 
     def onKeyUp(self, sender, keycode, modifiers):
@@ -521,18 +529,18 @@
 
     def onLostFocus(self, sender):
         """Finish the edition when focus is lost"""
-        if self.__listen_focus:
+        if self.options['listen_focus']:
             self.edit(False)
 
     def onClick(self, sender=None):
         """Start the edition when the widget is clicked"""
-        if self.__listen_click:
+        if self.options['listen_click']:
             self.edit(True)
 
     def onBrowserEvent(self, event):
-        if self.__listen_focus:
+        if self.options['listen_focus']:
             FocusHandler.onBrowserEvent(self, event)
-        if self.__listen_click:
+        if self.options['listen_click']:
             ClickHandler.onBrowserEvent(self, event)
         KeyboardHandler.onBrowserEvent(self, event)
 
@@ -540,9 +548,9 @@
 class HTMLTextEditor(SimpleTextEditor, HTML, FocusHandler, KeyboardHandler):
     """Manage a simple text editor with the HTML 5 "contenteditable" property."""
 
-    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False):
+    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
         HTML.__init__(self)
-        SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, single_line, enhance_display, listen_focus, listen_click)
+        SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, options)
         self.textarea = self.display = self
 
     def getContent(self):
@@ -565,9 +573,9 @@
 class LightTextEditor(SimpleTextEditor, SimplePanel, FocusHandler, KeyboardHandler):
     """Manage a simple text editor with a TextArea for editing, HTML for display."""
 
-    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False):
+    def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
         SimplePanel.__init__(self)
-        SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, single_line, enhance_display, listen_focus, listen_click)
+        SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, options)
         self.textarea = TextArea()
         self.display = HTML()
 
--- a/browser_side/panels.py	Fri Mar 14 21:00:13 2014 +0100
+++ b/browser_side/panels.py	Sat Mar 15 00:21:04 2014 +0100
@@ -526,7 +526,7 @@
                 options = [] if self.empty else ['update_msg']
             self.bubble = richtext.RichTextEditor(self._blog_panel.host, content, self.__modifiedCb, self.__afterEditCb, options)
         else:  # assume raw text message have no title
-            self.bubble = LightTextEditor(content, self.__modifiedCb, self.__afterEditCb, single_line=True, listen_focus=False)
+            self.bubble = LightTextEditor(content, self.__modifiedCb, self.__afterEditCb, options={'no_xhtml': True})
         self.bubble.setStyleName("bubble")
         try:
             self.toggle_syntax_button.removeFromParent()
@@ -951,7 +951,7 @@
     def __init__(self, host, status=''):
         self.host = host
         modifiedCb = lambda content: self.host.bridge.call('setStatus', None, self.host.status_panel.presence, content['text']) or True
-        HTMLTextEditor.__init__(self, {'text': status}, modifiedCb, None, single_line=True, listen_click=True)
+        HTMLTextEditor.__init__(self, {'text': status}, modifiedCb, options={'no_xhtml': True, 'listen_focus': True, 'listen_click': True})
         self.edit(False)
         self.setStyleName('statusPanel')
 
--- a/browser_side/richtext.py	Fri Mar 14 21:00:13 2014 +0100
+++ b/browser_side/richtext.py	Sat Mar 15 00:21:04 2014 +0100
@@ -107,7 +107,7 @@
         if hasattr(self, 'toolbar'):
             self.toolbar.setVisible(False)
         if not hasattr(self, 'display'):
-            self.display = HTMLTextEditor(enhance_display=False)  # for display mode
+            self.display = HTMLTextEditor(options={'enhance_display': False, 'listen_keyboard': False})  # for display mode
             for listener in self.edit_listeners:
                 self.display.addEditListener(listener)
         if not self.read_only and not hasattr(self, 'textarea'):