diff src/browser/sat_browser/richtext.py @ 738:caad07bdb659

browser_side: simplify RichTextEditor initialization
author souliane <souliane@mailoo.org>
date Thu, 19 Nov 2015 13:14:01 +0100
parents 398b54bd97f0
children b6510fd9ae15
line wrap: on
line diff
--- a/src/browser/sat_browser/richtext.py	Thu Nov 19 12:08:58 2015 +0100
+++ b/src/browser/sat_browser/richtext.py	Thu Nov 19 13:14:01 2015 +0100
@@ -25,83 +25,64 @@
 from pyjamas.ui.TextArea import TextArea
 from pyjamas.ui.Button import Button
 from pyjamas.ui.CheckBox import CheckBox
-from pyjamas.ui.DialogBox import DialogBox
 from pyjamas.ui.Label import Label
-from pyjamas.ui.HTML import HTML
 from pyjamas.ui.FlexTable import FlexTable
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
+from pyjamas.ui.KeyboardListener import KeyboardHandler
 from pyjamas import Window
-from pyjamas.ui.KeyboardListener import KeyboardHandler
 from __pyjamas__ import doc
 
 from constants import Const as C
 import dialog
 import base_panel
 import editor_widget
-import list_manager
 import html_tools
-import blog
-import chat
 
 
 class RichTextEditor(editor_widget.BaseTextEditor, FlexTable):
     """Panel for the rich text editor."""
 
-    def __init__(self, host, content=None, modifiedCb=None, afterEditCb=None, options=None, style=None):
+    STYLE = {'main': 'richTextEditor',
+             'title': 'richTextTitle',
+             'toolbar': 'richTextToolbar',
+             'textarea': 'richTextArea'
+             }
+
+    def __init__(self, host, content=None, modifiedCb=None, afterEditCb=None, options=None):
         """
-        @param host: the SatWebFrontend instance
-        @param content: dict with at least a 'text' key
-        @param modifiedCb: method to be called when the text has been modified
-        @param afterEditCb: method to be called when the edition is done
-        @param options: list of UI options (see self.readOptions)
+
+        @param host (SatWebFrontend): host instance
+        @param content (dict): dict with at least a 'text' key
+        @param modifiedCb (callable): to be called when the text has been modified
+        @param afterEditCb (callable): to be called when the edition is done
+        @param options (list[unicode]): UI options ("read_only", "update_msg")
         """
         FlexTable.__init__(self) # FIXME
         self.host = host
-        self._debug = False  # TODO: don't forget to set  it False before commit
         self.wysiwyg = False
-        self.__readOptions(options)
-        self.style = {'main': 'richTextEditor',
-                      'title': 'richTextTitle',
-                      'toolbar': 'richTextToolbar',
-                      'textarea': 'richTextArea'}
-        if isinstance(style, dict):
-            self.style.update(style)
-        self._prepareUI()
-        editor_widget.BaseTextEditor.__init__(self, content, None, modifiedCb, afterEditCb)
-
-    def __readOptions(self, options):
-        """Set the internal flags according to the given options."""
-        if options is None:
-            options = []
         self.read_only = 'read_only' in options
         self.update_msg = 'update_msg' in options
-        self.no_title = 'no_title' in options or self.read_only
-        self.no_command = 'no_command' in options or self.read_only
 
-    def _prepareUI(self, y_offset=0):
-        """Prepare the UI to host title panel, toolbar, text area...
-        @param y_offset: Y offset to start from (extra rows on top)"""
-        if not self.read_only:
-            self.title_offset = y_offset
-            self.toolbar_offset = self.title_offset + (0 if self.no_title else 1)
-            self.content_offset = self.toolbar_offset + (len(composition.RICH_SYNTAXES) if self._debug else 1)
-            self.command_offset = self.content_offset + 1
-        else:
-            self.title_offset = self.toolbar_offset = self.content_offset = y_offset
-            self.command_offset = self.content_offset + 1
-        # FlexTable.__init__(self, rowspan=self.command_offset + (0 if self.no_command else 1), colspan=2) # FIXME
-        self.addStyleName(self.style['main'])
+        indices = (-1, -1, 0, -1) if self.read_only else (0, 1, 2, 3)
+        self.title_offset, self.toolbar_offset, self.content_offset, self.command_offset = indices
+        self.addStyleName(self.STYLE['main'])
+
+        editor_widget.BaseTextEditor.__init__(self, content, None, modifiedCb, afterEditCb)
 
     def addEditListener(self, listener):
         """Add a method to be called whenever the text is edited.
-        @param listener: method taking two arguments: sender, keycode"""
+
+        @param listener: method taking two arguments: sender, keycode
+        """
         editor_widget.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"""
+        """Refresh the UI for edition/display mode.
+
+        @param edit: set to True to display the edition mode
+        """
         if edit is None:
             edit = hasattr(self, 'textarea') and self.textarea.getVisible()
 
@@ -111,13 +92,15 @@
 
         if hasattr(self, 'toolbar'):
             self.toolbar.setVisible(False)
+
         if not hasattr(self, 'display'):
             self.display = editor_widget.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'):
             self.textarea = EditTextArea(self)  # for edition mode
-            self.textarea.addStyleName(self.style['textarea'])
+            self.textarea.addStyleName(self.STYLE['textarea'])
 
         self.getFlexCellFormatter().setColSpan(self.content_offset, 0, 2)
         if edit and not self.wysiwyg:
@@ -128,13 +111,13 @@
         if not edit:
             return
 
-        if not self.no_title and not hasattr(self, 'title_panel'):
+        if not self.read_only and not hasattr(self, 'title_panel'):
             self.title_panel = base_panel.TitlePanel()
-            self.title_panel.addStyleName(self.style['title'])
+            self.title_panel.addStyleName(self.STYLE['title'])
             self.getFlexCellFormatter().setColSpan(self.title_offset, 0, 2)
             self.setWidget(self.title_offset, 0, self.title_panel)
 
-        if not self.no_command and not hasattr(self, 'command'):
+        if not self.read_only and not hasattr(self, 'command'):
             self.command = HorizontalPanel()
             self.command.addStyleName("marginAuto")
             self.command.add(Button("Cancel", lambda: self.edit(True, True)))
@@ -152,24 +135,21 @@
         if hasattr(self, "toolbar") and self.toolbar.syntax == syntax:
             self.toolbar.setVisible(True)
             return
-        count = 0
-        for syntax in composition.RICH_SYNTAXES.keys() if self._debug else [syntax]:
-            self.toolbar = HorizontalPanel()
-            self.toolbar.syntax = syntax
-            self.toolbar.addStyleName(self.style['toolbar'])
-            for key in composition.RICH_SYNTAXES[syntax].keys():
-                self.addToolbarButton(syntax, key)
-            self.wysiwyg_button = CheckBox(_('preview'))
-            wysiywgCb = lambda sender: self.setWysiwyg(sender.getChecked())
-            self.wysiwyg_button.addClickListener(wysiywgCb)
-            self.toolbar.add(self.wysiwyg_button)
-            self.syntax_label = Label(_("Syntax: %s") % syntax)
-            self.syntax_label.addStyleName("richTextSyntaxLabel")
-            self.toolbar.add(self.syntax_label)
-            self.toolbar.setCellWidth(self.syntax_label, "100%")
-            self.getFlexCellFormatter().setColSpan(self.toolbar_offset + count, 0, 2)
-            self.setWidget(self.toolbar_offset + count, 0, self.toolbar)
-            count += 1
+        self.toolbar = HorizontalPanel()
+        self.toolbar.syntax = syntax
+        self.toolbar.addStyleName(self.STYLE['toolbar'])
+        for key in composition.RICH_SYNTAXES[syntax].keys():
+            self.addToolbarButton(syntax, key)
+        self.wysiwyg_button = CheckBox(_('preview'))
+        wysiywgCb = lambda sender: self.setWysiwyg(sender.getChecked())
+        self.wysiwyg_button.addClickListener(wysiywgCb)
+        self.toolbar.add(self.wysiwyg_button)
+        self.syntax_label = Label(_("Syntax: %s") % syntax)
+        self.syntax_label.addStyleName("richTextSyntaxLabel")
+        self.toolbar.add(self.syntax_label)
+        self.toolbar.setCellWidth(self.syntax_label, "100%")
+        self.getFlexCellFormatter().setColSpan(self.toolbar_offset, 0, 2)
+        self.setWidget(self.toolbar_offset, 0, self.toolbar)
 
     def setWysiwyg(self, wysiwyg, init=False):
         """Toggle the edition mode between rich content syntax and wysiwyg.