comparison browser_side/base_panels.py @ 395:98cd5387d291

browser_side: new microblogs are not using the rich text editor by default: - a button allows to switch from raw to rich text, and vice versa - raw text microblogs are not submitted anymore when the focus is lost, only when <enter> is pressed
author souliane <souliane@mailoo.org>
date Mon, 10 Mar 2014 22:39:26 +0100
parents 30d03d9f07e4
children 487dd238ab88
comparison
equal deleted inserted replaced
394:ee61b0765d6c 395:98cd5387d291
340 def getContent(self): 340 def getContent(self):
341 """Get the current edited or editable content. 341 """Get the current edited or editable content.
342 @return: dict with at least a 'text' key 342 @return: dict with at least a 'text' key
343 """ 343 """
344 raise NotImplementedError 344 raise NotImplementedError
345
346 def setOriginalContent(self, content):
347 """Use this method with care! Content initialization should normally be
348 done with self.setContent. This method exists to let you trick the editor,
349 e.g. for self.modified to return True also when nothing has been modified.
350 @param content: dict
351 """
352 self._original_content = content
353
354 def getOriginalContent(self):
355 """
356 @return the original content before modification (dict)
357 """
358 return self._original_content
345 359
346 def modified(self, content=None): 360 def modified(self, content=None):
347 """Check if the content has been modified. 361 """Check if the content has been modified.
348 Remark: we don't use the direct comparison because we want to ignore empty elements 362 Remark: we don't use the direct comparison because we want to ignore empty elements
349 @content: content to be check against the original content or None to use the current content 363 @content: content to be check against the original content or None to use the current content
422 436
423 437
424 class LightTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler): 438 class LightTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler):
425 """Manage a simple text editor whith the HTML 5 "contenteditable" property.""" 439 """Manage a simple text editor whith the HTML 5 "contenteditable" property."""
426 440
427 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True): 441 def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None):
428 """ 442 """
429 @param content 443 @param content
430 @param modifiedCb 444 @param modifiedCb
431 @param afterEditCb 445 @param afterEditCb
432 @param single_line: set to True to manage a single line editor. In that case 446 @param single_line: set to True to manage a single line editor. In that
433 the edition would be terminated when the focus is lost or <enter> key is pressed. 447 case the edition will be terminated when the <enter> key is pressed.
448 @param listen_focus: set to True to terminate the edition when the
449 focus is lost. Leave to None in order to use single_line's value.
434 @param enhance_display: if True, the display text will be enhanced with addURLToText 450 @param enhance_display: if True, the display text will be enhanced with addURLToText
435 """ 451 """
436 HTML.__init__(self) 452 HTML.__init__(self)
437 if single_line: 453 self.__single_line = single_line
454 self.__enhance_display = enhance_display
455 self.__listen_focus = single_line if listen_focus is None else listen_focus
456 if self.__listen_focus:
438 FocusHandler.__init__(self) 457 FocusHandler.__init__(self)
439 KeyboardHandler.__init__(self) 458 KeyboardHandler.__init__(self)
440 self.__single_line = single_line
441 self.__enhance_display = enhance_display
442 strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text) 459 strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text)
443 BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb) 460 BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb)
444 461
445 def setContent(self, content=None): 462 def setContent(self, content=None):
446 BaseTextEditor.setContent(self, content) 463 BaseTextEditor.setContent(self, content)
453 if edit: 470 if edit:
454 self.setHTML(self._original_content['text']) 471 self.setHTML(self._original_content['text'])
455 self.getElement().setAttribute('contenteditable', 'true' if edit else 'false') 472 self.getElement().setAttribute('contenteditable', 'true' if edit else 'false')
456 BaseTextEditor.edit(self, edit) 473 BaseTextEditor.edit(self, edit)
457 if edit: 474 if edit:
458 if self.__single_line: 475 if self.__listen_focus:
459 self.addFocusListener(self) 476 self.addFocusListener(self)
460 self.addKeyboardListener(self) 477 self.addKeyboardListener(self)
461 else: 478 else:
462 self.setDisplayContent() 479 self.setDisplayContent()
463 if self.__single_line: 480 if self.__listen_focus:
464 if self in self._focusListeners: 481 if self in self._focusListeners:
465 self.removeFocusListener(self) 482 self.removeFocusListener(self)
466 if self in self._keyboardListeners: 483 if self in self._keyboardListeners:
467 self.removeKeyboardListener(self) 484 self.removeKeyboardListener(self)
468 485
481 listener(self, keycode) 498 listener(self, keycode)
482 499
483 def onKeyPress(self, sender, keycode, modifiers): 500 def onKeyPress(self, sender, keycode, modifiers):
484 if not self.__single_line: 501 if not self.__single_line:
485 return 502 return
486 if keycode == KEY_ENTER: 503 if keycode == KEY_ENTER: # finish the edition
487 self.setFocus(False) # finish the edition 504 self.setFocus(False)
505 if not self.__listen_focus:
506 self.edit(False)
488 507
489 def onLostFocus(self, sender): 508 def onLostFocus(self, sender):
490 """Finish the edition when focus is lost""" 509 """Finish the edition when focus is lost"""
491 self.edit(False) 510 self.edit(False)