diff browser_side/base_panels.py @ 347:f1ba38043d78

browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
author souliane <souliane@mailoo.org>
date Fri, 07 Feb 2014 20:14:11 +0100
parents 82f9e92379b0
children f488692c4903
line wrap: on
line diff
--- a/browser_side/base_panels.py	Fri Feb 07 20:08:28 2014 +0100
+++ b/browser_side/base_panels.py	Fri Feb 07 20:14:11 2014 +0100
@@ -28,12 +28,14 @@
 from pyjamas.ui.StackPanel import StackPanel
 from pyjamas.ui.TextArea import TextArea
 from pyjamas.ui.Event import BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT
+from pyjamas.ui.KeyboardListener import KEY_ENTER, KeyboardHandler
+from pyjamas.ui.FocusListener import FocusHandler
 from pyjamas import DOM
 
 from datetime import datetime
 from time import time
 
-from tools import html_sanitize, inlineRoot
+from tools import html_sanitize, html_clean, inlineRoot
 
 from sat_frontends.tools.strings import addURLToText
 from sat.core.i18n import _
@@ -285,3 +287,56 @@
 
     def setText(self, text):
         self.text_area.setText(text)
+
+
+class LightTextEditor(HTML, FocusHandler, KeyboardHandler):
+
+    def __init__(self, content='', single_line=False, callback=None):
+        HTML.__init__(self)
+        if single_line:
+            FocusHandler.__init__(self)
+            KeyboardHandler.__init__(self)
+        self.__single_line = single_line
+        self.__callback = callback
+        self.setContent(content)
+
+    def setContent(self, content=''):
+        self.__original_content = html_clean(content) if self.__single_line else content.strip()
+        self.setHTML('<div>%s</div>' % self.__original_content)
+
+    def getContent(self):
+        content = DOM.getInnerHTML(self.getElement().firstChild)
+        return html_clean(content) if self.__single_line else content.strip()
+
+    def modified(self, content=None):
+        if content is None:
+            content = self.getContent()
+        return content != self.__original_content
+
+    def edit(self, edit):
+        self.getElement().setAttribute('contenteditable', 'true' if edit else 'false')
+        if edit:
+            self.setFocus(True)
+            if self.__single_line:
+                self.addFocusListener(self)
+                self.addKeyboardListener(self)
+        else:
+            if self.__single_line:
+                self.removeFocusListener(self)
+                self.removeKeyboardListener(self)
+            content = self.getContent()
+            if self.modified(content) and self.__callback:
+                self.__callback(content)
+
+    def setFocus(self, focus):
+        if focus:
+            self.getElement().focus()
+        else:
+            self.getElement().blur()
+
+    def onKeyDown(self, sender, keycode, modifiers):
+        if keycode == KEY_ENTER:
+            self.setFocus(False)
+
+    def onLostFocus(self, sender):
+        self.edit(False)