comparison browser_side/richtext.py @ 352:2610443b05a2

browser_side: misc improvements for RichTextEditor to prepare Wysiwyg edition
author souliane <souliane@mailoo.org>
date Thu, 13 Feb 2014 01:17:07 +0100
parents c943fd54c90e
children ddb909ab5cbc
comparison
equal deleted inserted replaced
351:c943fd54c90e 352:2610443b05a2
23 from pyjamas.ui.Label import Label 23 from pyjamas.ui.Label import Label
24 from pyjamas.ui.HTML import HTML 24 from pyjamas.ui.HTML import HTML
25 from pyjamas.ui.FlexTable import FlexTable 25 from pyjamas.ui.FlexTable import FlexTable
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel 26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
27 27
28 from constants import Const
28 from dialog import ConfirmDialog, InfoDialog 29 from dialog import ConfirmDialog, InfoDialog
29 from base_panels import TitlePanel, BaseTextEditor, LightTextEditor 30 from base_panels import TitlePanel, BaseTextEditor, LightTextEditor
30 from list_manager import ListManager 31 from list_manager import ListManager
31 import panels 32 import panels
32 33
45 @param afterEditCb: method to be called when the edition is done 46 @param afterEditCb: method to be called when the edition is done
46 @param options: list of UI options (see self.readOptions) 47 @param options: list of UI options (see self.readOptions)
47 """ 48 """
48 self.host = host 49 self.host = host
49 self._debug = False # TODO: don't forget to set it False before commit 50 self._debug = False # TODO: don't forget to set it False before commit
51 self.wysiwyg = False
50 self.__readOptions(options) 52 self.__readOptions(options)
51 self.style = {'main': 'richTextEditor', 53 self.style = {'main': 'richTextEditor',
52 'title': 'richTextTitle', 54 'title': 'richTextTitle',
53 'toolbar': 'richTextToolbaar', 55 'toolbar': 'richTextToolbaar',
54 'textarea': 'richTextArea'} 56 'textarea': 'richTextArea'}
85 87
86 for widget in ['title_panel', 'command']: 88 for widget in ['title_panel', 'command']:
87 if hasattr(self, widget): 89 if hasattr(self, widget):
88 getattr(self, widget).setVisible(edit) 90 getattr(self, widget).setVisible(edit)
89 91
92 if hasattr(self, 'toolbar'):
93 self.toolbar.setVisible(False)
94 if not hasattr(self, 'display'):
95 self.display = LightTextEditor() # for display mode
96 if not self.read_only and not hasattr(self, 'textarea'):
97 self.textarea = TextArea() # for edition mode
98 self.textarea.addStyleName(self.style['textarea'])
99
90 self.getFlexCellFormatter().setColSpan(self.content_offset, 0, 2) 100 self.getFlexCellFormatter().setColSpan(self.content_offset, 0, 2)
91 if edit: 101 if edit and not self.wysiwyg:
92 if not hasattr(self, 'textarea'):
93 self.textarea = TextArea() # for edition mode
94 self.textarea.addStyleName(self.style['textarea'])
95 self.textarea.setWidth('100%') # CSS width doesn't do it, don't know why 102 self.textarea.setWidth('100%') # CSS width doesn't do it, don't know why
96 self.setWidget(self.content_offset, 0, self.textarea) 103 self.setWidget(self.content_offset, 0, self.textarea)
97 else: 104 else:
98 if hasattr(self, 'toolbar'):
99 self.toolbar.setVisible(False)
100 if not hasattr(self, 'display'):
101 self.display = LightTextEditor() # for display mode
102 self.setWidget(self.content_offset, 0, self.display) 105 self.setWidget(self.content_offset, 0, self.display)
106 if not edit:
103 return 107 return
104 108
105 if not self.no_title and not hasattr(self, 'title_panel'): 109 if not self.no_title and not hasattr(self, 'title_panel'):
106 self.title_panel = TitlePanel() 110 self.title_panel = TitlePanel()
107 self.title_panel.addStyleName(self.style['title']) 111 self.title_panel.addStyleName(self.style['title'])
123 have change his setting since the last time.""" 127 have change his setting since the last time."""
124 if syntax is None or syntax not in composition.RICH_SYNTAXES.keys(): 128 if syntax is None or syntax not in composition.RICH_SYNTAXES.keys():
125 syntax = composition.RICH_SYNTAXES.keys()[0] 129 syntax = composition.RICH_SYNTAXES.keys()[0]
126 if hasattr(self, "toolbar") and self.toolbar.syntax == syntax: 130 if hasattr(self, "toolbar") and self.toolbar.syntax == syntax:
127 self.toolbar.setVisible(True) 131 self.toolbar.setVisible(True)
132 return
128 count = 0 133 count = 0
129 for syntax in composition.RICH_SYNTAXES.keys() if self._debug else [syntax]: 134 for syntax in composition.RICH_SYNTAXES.keys() if self._debug else [syntax]:
130 self.toolbar = HorizontalPanel() 135 self.toolbar = HorizontalPanel()
131 self.toolbar.syntax = syntax 136 self.toolbar.syntax = syntax
132 self.toolbar.addStyleName(self.style['toolbar']) 137 self.toolbar.addStyleName(self.style['toolbar'])
201 content.update({'syntax': syntax}) 206 content.update({'syntax': syntax})
202 self.textarea.setText(content['text']) 207 self.textarea.setText(content['text'])
203 if hasattr(self, 'title_panel') and 'title' in content: 208 if hasattr(self, 'title_panel') and 'title' in content:
204 self.title_panel.setText(content['title']) 209 self.title_panel.setText(content['title'])
205 self.title_panel.setStackVisible(0, content['title'] != '') 210 self.title_panel.setStackVisible(0, content['title'] != '')
211 self.setToolBar(syntax)
206 if content['text'] and content['syntax'] != syntax: 212 if content['text'] and content['syntax'] != syntax:
207 self.host.bridge.call('syntaxConvert', syntaxConvertCb, content['text'], content['syntax']) 213 self.host.bridge.call('syntaxConvert', syntaxConvertCb, content['text'], content['syntax'])
208 else: 214 else:
209 syntaxConvertCb() 215 syntaxConvertCb()
210 self.setToolBar(syntax) 216 self.host.bridge.call('asyncGetParamA', getParamCb, composition.PARAM_NAME_SYNTAX, composition.PARAM_KEY_COMPOSITION)
211 self.host.bridge.call('asyncGetParamA', getParamCb, composition.PARAM_NAME_SYNTAX, composition.PARAM_KEY_COMPOSITION) or self.setToolBar(None)
212 else: 217 else:
213 if not self.initialized: 218 if not self.initialized:
214 # set the display text in XHTML only during init because a new MicroblogEntry instance is created after each modification 219 # set the display text in XHTML only during init because a new MicroblogEntry instance is created after each modification
215 text = content['text'] 220 text = content['text']
216 if 'title' in content and content['title']: 221 if 'title' in content and content['title']:
251 if hasattr(host.richtext, 'popup'): 256 if hasattr(host.richtext, 'popup'):
252 host.richtext.popup.hide() 257 host.richtext.popup.hide()
253 else: 258 else:
254 host.richtext.setVisible(False) 259 host.richtext.setVisible(False)
255 callback() 260 callback()
256 host.richtext.initialized = False
257 options = ['no_title'] 261 options = ['no_title']
258 style = {'main': 'richMessageEditor', 'textarea': 'richMessageArea'} 262 style = {'main': 'richMessageEditor', 'textarea': 'richMessageArea'}
259 host.richtext = RichMessageEditor(host, None, modifiedCb, afterEditCb, options, style) 263 host.richtext = RichMessageEditor(host, None, modifiedCb, afterEditCb, options, style)
260 264
261 def add(widget, parent): 265 def add(widget, parent):
264 widget.removeFromParent() 268 widget.removeFromParent()
265 parent.add(widget) 269 parent.add(widget)
266 else: 270 else:
267 parent.add(widget) 271 parent.add(widget)
268 widget.setVisible(True) 272 widget.setVisible(True)
273 widget.initialized = False # fake a new creation
269 widget.edit(True) 274 widget.edit(True)
270 275
271 if parent is None: 276 if parent is None:
272 if not hasattr(host.richtext, 'popup'): 277 if not hasattr(host.richtext, 'popup'):
273 host.richtext.popup = DialogBox(autoHide=False, centered=True) 278 host.richtext.popup = DialogBox(autoHide=False, centered=True)
326 def __syncToUniBox(self, recipients=None, emptyText=False): 331 def __syncToUniBox(self, recipients=None, emptyText=False):
327 """Synchronize to unibox if a maximum of one recipient is set. 332 """Synchronize to unibox if a maximum of one recipient is set.
328 @return True if the sync could be done, False otherwise""" 333 @return True if the sync could be done, False otherwise"""
329 if not self.host.uni_box: 334 if not self.host.uni_box:
330 return 335 return
331 setText = lambda: self.host.uni_box.setText("" if emptyText else self.textarea.getText()) 336 setText = lambda: self.host.uni_box.setText("" if emptyText else self.getContent()['text'])
332 if not hasattr(self, 'recipient'): 337 if not hasattr(self, 'recipient'):
333 setText() 338 setText()
334 return True 339 return True
335 if recipients is None: 340 if recipients is None:
336 recipients = self.recipient.getContacts() 341 recipients = self.recipient.getContacts()
385 if recipient.startswith("@"): 390 if recipient.startswith("@"):
386 targets.append(("PUBLIC", None, addr) if recipient == "@@" else ("GROUP", recipient[1:], addr)) 391 targets.append(("PUBLIC", None, addr) if recipient == "@@" else ("GROUP", recipient[1:], addr))
387 else: 392 else:
388 targets.append(("chat", recipient, addr)) 393 targets.append(("chat", recipient, addr))
389 # check that we actually have a message target and data 394 # check that we actually have a message target and data
390 text = self.textarea.getText() 395 content = self.getContent()
391 if text == "" or len(targets) == 0: 396 if content['text'] == "" or len(targets) == 0:
392 InfoDialog("Missing information", 397 InfoDialog("Missing information",
393 "Some information are missing and the message hasn't been sent.", Width="400px").center() 398 "Some information are missing and the message hasn't been sent.", Width="400px").center()
394 return None 399 return None
395 self.__syncToUniBox(recipients, emptyText=True) 400 self.__syncToUniBox(recipients, emptyText=True)
396 extra = {'content_rich': text} 401 extra = {'content_rich': content['text']}
397 if hasattr(self, 'title_panel'): 402 if hasattr(self, 'title_panel'):
398 extra.update({'title': self.title_panel.getText()}) 403 extra.update({'title': content['title']})
399 self.host.send(targets, text, extra=extra) 404 self.host.send(targets, content['text'], extra=extra)
400 return True 405 return True
401 406
402 407
403 class RecipientManager(ListManager): 408 class RecipientManager(ListManager):
404 """A manager for sub-panels to set the recipients for each recipient type.""" 409 """A manager for sub-panels to set the recipients for each recipient type."""