Mercurial > libervia-web
comparison browser_side/base_panels.py @ 401:ea03f898067f
browser_side: LightTextEditor renamed to HTMLTextEditor, new class LightTextEditor based on TextArea
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 13 Mar 2014 13:15:48 +0100 |
parents | 487dd238ab88 |
children | d7e78cb78dfc |
comparison
equal
deleted
inserted
replaced
400:487dd238ab88 | 401:ea03f898067f |
---|---|
22 from pyjamas.ui.VerticalPanel import VerticalPanel | 22 from pyjamas.ui.VerticalPanel import VerticalPanel |
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel | 23 from pyjamas.ui.HorizontalPanel import HorizontalPanel |
24 from pyjamas.ui.HTMLPanel import HTMLPanel | 24 from pyjamas.ui.HTMLPanel import HTMLPanel |
25 from pyjamas.ui.Button import Button | 25 from pyjamas.ui.Button import Button |
26 from pyjamas.ui.HTML import HTML | 26 from pyjamas.ui.HTML import HTML |
27 from pyjamas.ui.SimplePanel import SimplePanel | |
27 from pyjamas.ui.PopupPanel import PopupPanel | 28 from pyjamas.ui.PopupPanel import PopupPanel |
28 from pyjamas.ui.StackPanel import StackPanel | 29 from pyjamas.ui.StackPanel import StackPanel |
29 from pyjamas.ui.TextArea import TextArea | 30 from pyjamas.ui.TextArea import TextArea |
30 from pyjamas.ui.Event import BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT | 31 from pyjamas.ui.Event import BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT |
31 from pyjamas.ui.KeyboardListener import KEY_ENTER, KEY_ESCAPE, KeyboardHandler | 32 from pyjamas.ui.KeyboardListener import KEY_ENTER, KEY_ESCAPE, KeyboardHandler |
433 """Add a method to be called whenever the text is edited. | 434 """Add a method to be called whenever the text is edited. |
434 @param listener: method taking two arguments: sender, keycode""" | 435 @param listener: method taking two arguments: sender, keycode""" |
435 self.edit_listeners.append(listener) | 436 self.edit_listeners.append(listener) |
436 | 437 |
437 | 438 |
438 class LightTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler): | 439 class HTMLTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler): |
439 """Manage a simple text editor whith the HTML 5 "contenteditable" property.""" | 440 """Manage a simple text editor with the HTML 5 "contenteditable" property.""" |
440 | 441 |
441 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None): | 442 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None): |
442 """ | 443 """ |
443 @param content | 444 @param content |
444 @param modifiedCb | 445 @param modifiedCb |
507 self.edit(False) | 508 self.edit(False) |
508 | 509 |
509 def onLostFocus(self, sender): | 510 def onLostFocus(self, sender): |
510 """Finish the edition when focus is lost""" | 511 """Finish the edition when focus is lost""" |
511 self.edit(False) | 512 self.edit(False) |
513 | |
514 | |
515 class LightTextEditor(BaseTextEditor, SimplePanel, FocusHandler, KeyboardHandler): | |
516 """Manage a simple text editor with a TextArea for editing, HTML for display.""" | |
517 | |
518 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None): | |
519 """ | |
520 @param content | |
521 @param modifiedCb | |
522 @param afterEditCb | |
523 @param single_line: set to True to manage a single line editor. In that | |
524 case the edition will be terminated when <enter> or <escape> is pressed. | |
525 @param listen_focus: set to True to terminate the edition when the | |
526 focus is lost. Leave to None in order to use single_line's value. | |
527 @param enhance_display: if True, the display text will be enhanced with addURLToText | |
528 """ | |
529 SimplePanel.__init__(self) | |
530 self.__single_line = single_line | |
531 self.__enhance_display = enhance_display | |
532 self.__listen_focus = single_line if listen_focus is None else listen_focus | |
533 if self.__listen_focus: | |
534 FocusHandler.__init__(self) | |
535 KeyboardHandler.__init__(self) | |
536 strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text) | |
537 BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb) | |
538 self.textarea = TextArea() | |
539 self.display = HTML() | |
540 | |
541 def setContent(self, content=None): | |
542 BaseTextEditor.setContent(self, content) | |
543 | |
544 def getContent(self): | |
545 text = self.textarea.getText() | |
546 return {'text': self.strproc(text) if text else ''} | |
547 | |
548 def edit(self, edit, abort=False, sync=False): | |
549 if edit: | |
550 self.textarea.setText(self._original_content['text']) | |
551 self.setWidget(self.textarea if edit else self.display) | |
552 BaseTextEditor.edit(self, edit) | |
553 if edit: | |
554 if self.__listen_focus: | |
555 self.textarea.addFocusListener(self) | |
556 self.textarea.addKeyboardListener(self) | |
557 else: | |
558 self.setDisplayContent() | |
559 if self.__listen_focus: | |
560 if self in self._focusListeners: | |
561 self.textarea.removeFocusListener(self) | |
562 if self in self._keyboardListeners: | |
563 self.textarea.removeKeyboardListener(self) | |
564 | |
565 def setDisplayContent(self): | |
566 text = addURLToImage(self._original_content['text']) | |
567 self.display.setHTML(addURLToText(text) if self.__enhance_display else text) | |
568 | |
569 def setFocus(self, focus): | |
570 self.textarea.setFocus(focus) | |
571 | |
572 def onKeyDown(self, sender, keycode, modifiers): | |
573 for listener in self.edit_listeners: | |
574 listener(self.textarea, keycode) | |
575 | |
576 def onKeyPress(self, sender, keycode, modifiers): | |
577 if not self.__single_line: | |
578 return | |
579 # XXX: it seems that pyjamas never catches the escape key | |
580 if keycode in (KEY_ENTER, KEY_ESCAPE): # finish the edition | |
581 self.textarea.setFocus(False) | |
582 if not self.__listen_focus: | |
583 self.edit(False) | |
584 | |
585 def onLostFocus(self, sender): | |
586 """Finish the edition when focus is lost""" | |
587 self.edit(False) |