comparison browser_side/base_panels.py @ 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
comparison
equal deleted inserted replaced
403:ec6f7581b453 404:c393e7dc9ae6
438 438
439 439
440 class SimpleTextEditor(BaseTextEditor, FocusHandler, KeyboardHandler, ClickHandler): 440 class SimpleTextEditor(BaseTextEditor, FocusHandler, KeyboardHandler, ClickHandler):
441 """Base class for manage a simple text editor.""" 441 """Base class for manage a simple text editor."""
442 442
443 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False): 443 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
444 """ 444 """
445 @param content 445 @param content
446 @param modifiedCb 446 @param modifiedCb
447 @param afterEditCb 447 @param afterEditCb
448 @param single_line: set to True to manage a single line editor. In that 448 @param options: dict with the following value:
449 case the edition will be terminated when <enter> or <escape> is pressed. 449 - no_xhtml: set to True to clean any xhtml content.
450 @param listen_focus: set to True to terminate the edition when the 450 - enhance_display: if True, the display text will be enhanced with addURLToText
451 focus is lost. Leave to None in order to use single_line's value. 451 - listen_keyboard: set to True to terminate the edition with <enter> or <escape>.
452 @param listen_click: set to True to start the edition when you click on the widget. 452 - listen_focus: set to True to terminate the edition when the focus is lost.
453 @param enhance_display: if True, the display text will be enhanced with addURLToText 453 - listen_click: set to True to start the edition when you click on the widget.
454 """ 454 """
455 self.__single_line = single_line 455 self.options = {'no_xhtml': False,
456 self.__enhance_display = enhance_display 456 'enhance_display': True,
457 self.__listen_focus = single_line if listen_focus is None else listen_focus 457 'listen_keyboard': True,
458 self.__listen_click = listen_click 458 'listen_focus': False,
459 'listen_click': False
460 }
461 if options:
462 self.options.update(options)
459 self.__shift_down = False 463 self.__shift_down = False
460 if self.__listen_focus: 464 if self.options['listen_focus']:
461 FocusHandler.__init__(self) 465 FocusHandler.__init__(self)
462 if self.__listen_click: 466 if self.options['listen_click']:
463 ClickHandler.__init__(self) 467 ClickHandler.__init__(self)
464 KeyboardHandler.__init__(self) 468 KeyboardHandler.__init__(self)
465 strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text) 469 strproc = lambda text: html_sanitize(html_strip(text)) if self.options['no_xhtml'] else html_strip(text)
466 BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb) 470 BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb)
467 self.textarea = self.display = None 471 self.textarea = self.display = None
468 472
469 def setContent(self, content=None): 473 def setContent(self, content=None):
470 BaseTextEditor.setContent(self, content) 474 BaseTextEditor.setContent(self, content)
473 raise NotImplementedError 477 raise NotImplementedError
474 478
475 def edit(self, edit, abort=False, sync=False): 479 def edit(self, edit, abort=False, sync=False):
476 BaseTextEditor.edit(self, edit) 480 BaseTextEditor.edit(self, edit)
477 if edit: 481 if edit:
478 if self.__listen_focus and self not in self.textarea._focusListeners: 482 if self.options['listen_focus'] and self not in self.textarea._focusListeners:
479 self.textarea.addFocusListener(self) 483 self.textarea.addFocusListener(self)
480 if self.__listen_click: 484 if self.options['listen_click']:
481 self.display.clearClickListener() 485 self.display.clearClickListener()
482 if self not in self.textarea._keyboardListeners: 486 if self not in self.textarea._keyboardListeners:
483 self.textarea.addKeyboardListener(self) 487 self.textarea.addKeyboardListener(self)
484 else: 488 else:
485 self.setDisplayContent() 489 self.setDisplayContent()
486 if self.__listen_focus: 490 if self.options['listen_focus']:
487 try: 491 try:
488 self.textarea.removeFocusListener(self) 492 self.textarea.removeFocusListener(self)
489 except ValueError: 493 except ValueError:
490 pass 494 pass
491 if self.__listen_click and self not in self.display._clickListeners: 495 if self.options['listen_click'] and self not in self.display._clickListeners:
492 self.display.addClickListener(self) 496 self.display.addClickListener(self)
493 try: 497 try:
494 self.textarea.removeKeyboardListener(self) 498 self.textarea.removeKeyboardListener(self)
495 except ValueError: 499 except ValueError:
496 pass 500 pass
497 501
498 def setDisplayContent(self): 502 def setDisplayContent(self):
499 text = addURLToImage(self._original_content['text']) 503 text = self._original_content['text']
500 self.display.setHTML(addURLToText(text) if self.__enhance_display else text) 504 if not self.options['no_xhtml']:
505 text = addURLToImage(text)
506 if self.options['enhance_display']:
507 text = addURLToText(text)
508 self.display.setHTML(text)
501 509
502 def setFocus(self, focus): 510 def setFocus(self, focus):
503 raise NotImplementedError 511 raise NotImplementedError
504 512
505 def onKeyDown(self, sender, keycode, modifiers): 513 def onKeyDown(self, sender, keycode, modifiers):
506 for listener in self.edit_listeners: 514 for listener in self.edit_listeners:
507 listener(self.textarea, keycode) 515 listener(self.textarea, keycode)
508 if not self.__single_line: 516 if not self.options['listen_keyboard']:
509 return 517 return
510 if keycode == KEY_SHIFT or self.__shift_down: # allow input a new line with <shift> + <enter> 518 if keycode == KEY_SHIFT or self.__shift_down: # allow input a new line with <shift> + <enter>
511 self.__shift_down = True 519 self.__shift_down = True
512 return 520 return
513 if keycode in (KEY_ENTER, KEY_ESCAPE): # finish the edition 521 if keycode in (KEY_ENTER, KEY_ESCAPE): # finish the edition
514 self.textarea.setFocus(False) 522 self.textarea.setFocus(False)
515 if not self.__listen_focus: 523 if not self.options['listen_focus']:
516 self.edit(False) 524 self.edit(False)
517 525
518 def onKeyUp(self, sender, keycode, modifiers): 526 def onKeyUp(self, sender, keycode, modifiers):
519 if keycode == KEY_SHIFT: 527 if keycode == KEY_SHIFT:
520 self.__shift_down = False 528 self.__shift_down = False
521 529
522 def onLostFocus(self, sender): 530 def onLostFocus(self, sender):
523 """Finish the edition when focus is lost""" 531 """Finish the edition when focus is lost"""
524 if self.__listen_focus: 532 if self.options['listen_focus']:
525 self.edit(False) 533 self.edit(False)
526 534
527 def onClick(self, sender=None): 535 def onClick(self, sender=None):
528 """Start the edition when the widget is clicked""" 536 """Start the edition when the widget is clicked"""
529 if self.__listen_click: 537 if self.options['listen_click']:
530 self.edit(True) 538 self.edit(True)
531 539
532 def onBrowserEvent(self, event): 540 def onBrowserEvent(self, event):
533 if self.__listen_focus: 541 if self.options['listen_focus']:
534 FocusHandler.onBrowserEvent(self, event) 542 FocusHandler.onBrowserEvent(self, event)
535 if self.__listen_click: 543 if self.options['listen_click']:
536 ClickHandler.onBrowserEvent(self, event) 544 ClickHandler.onBrowserEvent(self, event)
537 KeyboardHandler.onBrowserEvent(self, event) 545 KeyboardHandler.onBrowserEvent(self, event)
538 546
539 547
540 class HTMLTextEditor(SimpleTextEditor, HTML, FocusHandler, KeyboardHandler): 548 class HTMLTextEditor(SimpleTextEditor, HTML, FocusHandler, KeyboardHandler):
541 """Manage a simple text editor with the HTML 5 "contenteditable" property.""" 549 """Manage a simple text editor with the HTML 5 "contenteditable" property."""
542 550
543 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False): 551 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
544 HTML.__init__(self) 552 HTML.__init__(self)
545 SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, single_line, enhance_display, listen_focus, listen_click) 553 SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, options)
546 self.textarea = self.display = self 554 self.textarea = self.display = self
547 555
548 def getContent(self): 556 def getContent(self):
549 text = DOM.getInnerHTML(self.getElement()) 557 text = DOM.getInnerHTML(self.getElement())
550 return {'text': self.strproc(text) if text else ''} 558 return {'text': self.strproc(text) if text else ''}
563 571
564 572
565 class LightTextEditor(SimpleTextEditor, SimplePanel, FocusHandler, KeyboardHandler): 573 class LightTextEditor(SimpleTextEditor, SimplePanel, FocusHandler, KeyboardHandler):
566 """Manage a simple text editor with a TextArea for editing, HTML for display.""" 574 """Manage a simple text editor with a TextArea for editing, HTML for display."""
567 575
568 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None, listen_click=False): 576 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, options=None):
569 SimplePanel.__init__(self) 577 SimplePanel.__init__(self)
570 SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, single_line, enhance_display, listen_focus, listen_click) 578 SimpleTextEditor.__init__(self, content, modifiedCb, afterEditCb, options)
571 self.textarea = TextArea() 579 self.textarea = TextArea()
572 self.display = HTML() 580 self.display = HTML()
573 581
574 def getContent(self): 582 def getContent(self):
575 text = self.textarea.getText() 583 text = self.textarea.getText()