PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/addons/RichTextEditor.py

http://pyjamas.googlecode.com/
Python | 131 lines | 128 code | 0 blank | 3 comment | 1 complexity | 3a7ce446004d28b6580dfb8d9a077aca MD5 | raw file
  1"""

  2    A rich text editor using FCKeditor.

  3    

  4    Pass "-j fckeditor/fckeditor.js" to build.py in order to include the FCKeditor

  5    javascript.

  6"""
  7import Window
  8import DOM
  9from __pyjamas__ import console
 10from BoundMethod import BoundMethod
 11from ui import Widget
 12
 13from __pyjamas__ import JS
 14
 15def createFCK(name):
 16    JS("""

 17    return new FCKeditor(name);

 18    """)
 19
 20JS("""

 21    $wnd.FCKeditor_OnComplete = function(editorInstance )

 22    {

 23        pyjsObject = $doc.getElementById(editorInstance.Name.substr(3)).__listener;

 24        console.log("pyjsObject is %o", pyjsObject);

 25        if(pyjsObject)

 26            pyjsObject.onFCKLoaded(editorInstance);

 27    }

 28""")
 29class RichTextEditor(Widget):
 30    def __init__(self, initialValue="", target="", method="POST"):
 31        Widget.__init__(self);
 32        self.id = "rte"+hash(self)
 33        fck = createFCK("fck"+self.id)
 34        fck.Height = "600px"
 35        self.setElement(DOM.createForm())
 36        DOM.setAttribute(self.element, "method", "POST")
 37        DOM.setAttribute(self.element, "target", target)
 38        JS("""

 39        var rte = this;

 40        this.element.onsubmit = function() {

 41            $wnd.setTimeout(function() { rte.onSave.call(rte) }, 0);

 42            return false;

 43        }

 44        """)
 45        self.setID(self.id)
 46        self.addStyleName("gwt-RichTextEditor")
 47        fck.Value = initialValue
 48        fck.BasePath = "fckeditor/"
 49        fck.Config.CustomConfigurationsPath = "../../fckconfig.js"
 50        fck.pyjsObject = self
 51        self.loaded = False
 52        self.saveListeners = []
 53        self.pendingHTML = None
 54        html = fck.CreateHtml()
 55        #console.log("fck html = %s", html)

 56        html = html
 57        DOM.setInnerHTML(self.getElement(), html)
 58    
 59    def addSaveListener(self, listener):
 60        """

 61        When the user clicks the save button, your listener will be notified.

 62        

 63        Either pass a function (e.g. a BoundMethod) or an object with an onSave()

 64        method.  Either will be passed the RichtTextEditor instance.

 65        """
 66        self.saveListeners.append(listener)
 67        
 68    def removeSaveListener(self, listener):
 69        """

 70        Remove a previously added listener

 71        """
 72        self.saveListeners.remove(listener)
 73    
 74    def onFCKLoaded(self, fck):
 75        """

 76        Called when the FCK editor has loaded, and it is ready for use

 77        """
 78        self.loaded = True
 79        self.fck = fck
 80        fck.Events.AttachEvent('OnSelectionChange', BoundMethod(self, self.onSelectionChange))
 81        fck.Events.AttachEvent('OnBlur', BoundMethod(self, self.onBlur))
 82        fck.Events.AttachEvent('OnFocus', BoundMethod(self, self.onFocus))
 83        fck.Events.AttachEvent('OnPaste', BoundMethod(self, self.onPaste))
 84        if self.pendingHTML:
 85            fck.SetHTML(self.pendingHTML)
 86            self.pendingHTML = None
 87            
 88    def onSelectionChange(self, sender):
 89        pass#console.log("onSelectionChange!")

 90        
 91    def onBlur(self, sender):
 92        pass#console.log("onBlur!")

 93        
 94    def onFocus(self, sender):
 95        pass#console.log("onFocus!")

 96        
 97    def onPaste(self, sender):
 98        pass#console.log("onPaste!")

 99        
100    def onSave(self):
101        """

102        Handle the save click and pass it onto the listeners

103        """
104        console.log("onSave() in %s", Window.getLocation().getHref())
105        for listener in self.saveListeners:
106            if listener.onSave: listener.onSave(self)
107            else: listener(self)
108        return False
109    
110    def setHTML(self, html):
111        """

112        Call this to change the html showing in the editor

113        """
114        if self.loaded:
115            self.fck.SetHTML(html);
116        else:
117            self.pendingHTML = html
118    
119    def getHTML(self):
120        """

121        Call this to retrieve the HTML showing in the editor (e.g. to save/preview it)

122        """
123        return self.fck.GetXHTML(True)
124
125    def getDOM(self):
126        return self.fck.EditorDocument()
127    
128    def getWindow(self):
129        return self.fck.EditorWindow()
130    
131