def onEditContentClick(self, sender):
     if self.editor:
         editor = self.editor
         self.editor = None
         ComplexPanel.remove(self, editor)
         self.editButton.setText("edit " + unescape(self.templateName))
     else:
         self.editor = RichTextEditor(self.originalText)
         self.editor.addSaveListener(self)
         ComplexPanel.insert(self, self.editor, self.getElement(),
                             len(self.children))
         self.editButton.setText("close editor")
Example #2
0
    def __init__(self, sink):
        Composite.__init__(self)

        self.remote = sink.remote

        panel = VerticalPanel(Width="100%", Spacing=8)

        self.view = Button("View", self)
        self.newpage = Button("New", self)
        self.todoId = None
        self.todoTextName = TextBox()
        self.todoTextName.addKeyboardListener(self)

        self.todoTextArea = RichTextEditor(basePath="/fckeditor/")
        self.todoTextArea.setWidth("100%")
        self.todoTextArea.addSaveListener(self)

        self.todoList = ListBox()
        self.todoList.setVisibleItemCount(7)
        self.todoList.setWidth("200px")
        self.todoList.addClickListener(self)

        self.fDialogButton = Button("Upload Files", self)

        self.status = HTML()

        panel.add(HTML("Status:"))
        panel.add(self.status)
        panel.add(self.fDialogButton)
        panel.add(Label("Create New Page (doesn't save current one!):"))
        panel.add(self.newpage)
        panel.add(Label("Add/Edit New Page:"))
        panel.add(self.todoTextName)
        panel.add(Label("Click to Load and Edit (doesn't save current one!):"))
        panel.add(self.todoList)
        panel.add(self.view)
        panel.add(
            Label(
                "New Page HTML.  Click 'save' icon to save.  (pagename is editable as well)"
            ))
        panel.add(self.todoTextArea)

        self.setWidget(panel)

        self.remote.getPages(self)
 def onEditContentClick(self, sender):
     if self.editor:
         editor = self.editor
         self.editor = None
         ComplexPanel.remove(self, editor)
         self.editButton.setText("edit "+unescape(self.templateName))
     else:
         self.editor = RichTextEditor(self.originalText)
         self.editor.addSaveListener(self)
         ComplexPanel.insert(self, self.editor, self.getElement(), len(self.children))
         self.editButton.setText("close editor")
Example #4
0
    def __init__(self, sink):
        Composite.__init__(self)

        self.remote = sink.remote

        panel = VerticalPanel(Width="100%", Spacing=8)

        self.view = Button("View", self)
        self.newpage = Button("New", self)
        self.todoId = None
        self.todoTextName = TextBox()
        self.todoTextName.addKeyboardListener(self)

        self.todoTextArea = RichTextEditor(basePath="/fckeditor/")
        self.todoTextArea.setWidth("100%")
        self.todoTextArea.addSaveListener(self)

        self.todoList = ListBox()
        self.todoList.setVisibleItemCount(7)
        self.todoList.setWidth("200px")
        self.todoList.addClickListener(self)

        self.fDialogButton = Button("Upload Files", self)
        
        self.status = HTML()

        panel.add(HTML("Status:"))
        panel.add(self.status)
        panel.add(self.fDialogButton)
        panel.add(Label("Create New Page (doesn't save current one!):"))
        panel.add(self.newpage)
        panel.add(Label("Add/Edit New Page:"))
        panel.add(self.todoTextName)
        panel.add(Label("Click to Load and Edit (doesn't save current one!):"))
        panel.add(self.todoList)
        panel.add(self.view)
        panel.add(Label("New Page HTML.  Click 'save' icon to save.  (pagename is editable as well)"))
        panel.add(self.todoTextArea)

        self.setWidget(panel)

        self.remote.getPages(self)
class TemplatePanel(ComplexPanel):
    """
    Panel which allows you to attach or insert widgets into a
    pre-defined template.

    We don't do any caching of our own, since the browser will do
    caching for us, and probably more efficiently.
    """
    templateRoot = ""
    # XXX What is this text expression supposed to do? It won't be
    # added to the docstring as it is.
    """Set staticRoot to change the base path of all the templates
    that are loaded; templateRoot should have a trailing slash"""

    def __init__(self, templateName, allowEdit=False):
        ComplexPanel.__init__(self)
        self.loaded = False # Set after widgets are attached
        self.widgetsAttached = False
        self.id = None
        self.templateName = None
        self.title = None
        self.elementsById = {}
        self.metaTags = {}
        self.body = None
        self.links = []
        self.forms = []
        self.metaTagList = []
        self.loadListeners = []
        self.toAttach = []
        self.toInsert = []
        self.setElement(DOM.createDiv())
        self.editor = None
        self.allowEdit = allowEdit
        if templateName:
            self.loadTemplate(templateName)

    def getTemplatePath(self, templateName):
        return self.templateRoot+'tpl/'+templateName+'.html'

    def loadTemplate(self, templateName):
        self.templateName = templateName
        self.id = templateName + str(hash(self))
        self.httpReq = HTTPRequest()
        self.httpReq.asyncGet(self.getTemplatePath(templateName),
                              TemplateLoader(self))

    def getCurrentTemplate(self):
        """Return the template that is currently loaded, or is loading."""
        return self.templateName

    def isLoaded(self):
        """Return True if the template is finished loading."""
        return self.loaded

    def areWidgetsAttached(self):
        """Return True if the template is loaded and attachWidgets()
        has been called."""
        return self.widgetsAttached

    def setTemplateText(self, text):
        """
        Set the template text; if the template is not HTML, a subclass
        could override this to pre-process the text into HTML before
        passing it to the default implementation.
        """
        if self.allowEdit:
            self.originalText = text
        # If we have children, remove them all first since we are
        # trashing their DOM
        for child in List(self.children):
            self.remove(child)

        DOM.setInnerHTML(self.getElement(), text)
        self.elementsById = {}
        self.links = []
        self.metaTags = {}
        self.forms = []
        self.metaTagList = []

        # Make the ids unique and store a pointer to each named element
        for node in DOM.walkChildren(self.getElement()):
            #console.log("Passing node with name %s", node.nodeName)
            if node.nodeName == "META":
                name = node.getAttribute("name")
                content = node.getAttribute("content")
                console.log("Found meta %o name %s content %s",
                            node, name, content)
                self.metaTags[name] = content
                self.metaTagList.append(node)
            elif node.nodeName == "BODY":
                self.body = node
            elif node.nodeName == "TITLE":
                self.title = DOM.getInnerText(node)
            elif node.nodeName == "FORM":
                self.forms.append(node)

            nodeId = DOM.getAttribute(node, "id")
            if nodeId:
                self.elementsById[nodeId] = node
                DOM.setAttribute(node, "id", self.id + ":" + node.id)
            nodeHref = DOM.getAttribute(node, "href")
            if nodeHref:
                self.links.append(node)

        self.loaded = True
        if self.attached:
            self.attachWidgets()
            self.widgetsAttached = True

        if self.allowEdit:
            self.editor = None
            self.editButton = Label("edit "+unescape(self.templateName))
            self.editButton.addStyleName("link")
            self.editButton.addStyleName("ContentPanelEditLink")
            self.editButton.addClickListener(EventDelegate("onClick", self,
                                             self.onEditContentClick))
            ComplexPanel.insert(self, self.editButton, self.getElement(),
                                len(self.children))

        self.notifyLoadListeners()

    def onError(self, html, statusCode):
        if statusCode == 404 and self.allowEdit:
            self.editor = None
            self.originalText = ""
            DOM.setInnerHTML(self.getElement(), '')
            self.editButton = Label("create "+unescape(self.templateName))
            self.editButton.addStyleName("link")
            self.editButton.addStyleName("ContentPanelEditLink")
            self.editButton.addClickListener(EventDelegate("onClick", self,
                                             self.onEditContentClick))
            ComplexPanel.insert(self, self.editButton, self.getElement(),
                                len(self.children))
            return

        # Show the page we got in an iframe, which will hopefully show
        # the error better than we can.
        # DOM.setInnerHTML(self.getElement(), '<iframe src="'+self.getTemplatePath(self.templateName)+'"/>')

    def onTimeout(self, text):
        self.onError("Page loading timed out: "+text)

    def getElementsById(self):
        """Return a dict mapping an id to an element with that id
        inside the template; useful for post-processing."""
        return self.elementsById

    def getLinks(self):
        """Return a list of all the A HREF= elements found in the template."""
        return self.links

    def getForms(self):
        """Return a list of all the FORM elements found in the template."""
        return self.forms

    def onAttach(self):
        if not self.attached:
            SimplePanel.onAttach(self)
            if self.loaded and not self.widgetsAttached:
                self.attachWidgets()
                self.widgetsAttached = True

    def attachWidgets(self):
        """
        Attach and insert widgets into the DOM now that it has been
        loaded.  If any widgets were attached before loading, they
        will have been queued and the default implementation will
        attach them.

        Override this in subclasses to attach your own widgets after
        loading.
        """
        for attach in self.toAttach:
            self.attach(attach.name, attach.widget)
        for insert in self.toInsert:
            self.insert(insert.name, insert.widget)

    def getElementById(self, id):
        return self.elementsById[id]

    def insert(self, id, widget):
        """
        Insert a widget into the element with the given id, at the end
        of its children.
        """
        if not self.loaded:
            self.toInsert.append(PendingAttachOrInsert(id, widget))
        else:
            element = self.getElementById(id)
            if element:
                self.adopt(widget, element)
                self.children.append(widget)
            else:
                console.error("Page error: No such element " + id)
            return widget

    def attachToElement(self, element, widget):
        events = DOM.getEventsSunk(widget.getElement())
        widget.unsinkEvents(events)
        widget.setElement(element)
        widget.sinkEvents(events)
        self.adopt(widget, None)
        self.children.append(widget)

    def replaceElement(self, element, widget):
        """Replace an existing element with the given widget."""
        DOM.getParent(element).replaceChild(widget.getElement(), element)
        self.adopt(widget, None)
        self.children.append(widget)

    def attach(self, id, widget):
        """
        Attach a widget onto the element with the given id; the element
        currently associated with the widget is discarded.
        """
        if not self.loaded:
            self.toAttach.append(PendingAttachOrInsert(id, widget))
        else:
            element = self.getElementById(id)
            if element:
                self.attachToElement(element, widget)
            else:
                console.error("Page error: No such element " + id)
            return widget

    def getMeta(self, name):
        """
        Get the value of a meta-variable found in the template, or None if
        no meta tags were found with the given name.
        """
        return self.metaTags.get(name)

    def getTitle(self):
        """Return a user-friendly title for the page."""
        if self.title:
            return self.title
        else:
            return self.templateName

    def addLoadListener(self, listener):
        """
        The listener should be a function or an object implementing
        onTemplateLoaded. It will be called this TemplatePanel
        instance after the template has been loaded and after
        attachWidgets() is called.
        """
        self.loadListeners.append(listener)

    def removeLoadListener(self, listener):
        self.loadListeners.remove(listener)

    def notifyLoadListeners(self):
        for listener in self.loadListeners:
            if listener.onTemplateLoaded:
                listener.onTemplateLoaded(self)
            else:
                listener(self)

    def onEditContentClick(self, sender):
        if self.editor:
            editor = self.editor
            self.editor = None
            ComplexPanel.remove(self, editor)
            self.editButton.setText("edit " + unescape(self.templateName))
        else:
            self.editor = RichTextEditor(self.originalText)
            self.editor.addSaveListener(self)
            ComplexPanel.insert(self, self.editor, self.getElement(),
                                len(self.children))
            self.editButton.setText("close editor")

    def getTemplateSaveUrl(self, templateName):
        """
        Get the URL to post a template to when it is saved in the editor.
        """
        return self.getTemplatePath(templateName)

    def saveTemplateText(self, html):
        """
        Save the text. This method can be overridden to use a
        different save method. The default is to POST to the template
        save URL, passing a single parameter "content" with the html
        string.

        To change the target of the POST, override getTemplateSaveUrl().

        To preprocess the html, override this method in a subclass and
        perform processing there.
        """
        HTTPRequest().asyncPost(self.getTemplateSaveUrl(self.templateName),
                                "content=" + encodeURIComponent(html),
                                ContentSaveHandler(self))

    def onSave(self, sender):
        """Called when the user clicks save in the content editor."""
        html = self.editor.getHTML()
        self.saveTemplateText(html)

    def onSaveComplete(self):
        """
        Called when the template was successfully POSTed to the
        server; it reloads the template.

        Subclasses which don't use the default method of saving may
        want to call this after they successfully save the template.
        """
        self.loadTemplate(self.templateName)
Example #6
0
class WebPageEdit(Composite):
    def __init__(self, sink):
        Composite.__init__(self)

        self.remote = sink.remote

        panel = VerticalPanel(Width="100%", Spacing=8)

        self.view = Button("View", self)
        self.newpage = Button("New", self)
        self.todoId = None
        self.todoTextName = TextBox()
        self.todoTextName.addKeyboardListener(self)

        self.todoTextArea = RichTextEditor(basePath="/fckeditor/")
        self.todoTextArea.setWidth("100%")
        self.todoTextArea.addSaveListener(self)

        self.todoList = ListBox()
        self.todoList.setVisibleItemCount(7)
        self.todoList.setWidth("200px")
        self.todoList.addClickListener(self)

        self.fDialogButton = Button("Upload Files", self)
        
        self.status = HTML()

        panel.add(HTML("Status:"))
        panel.add(self.status)
        panel.add(self.fDialogButton)
        panel.add(Label("Create New Page (doesn't save current one!):"))
        panel.add(self.newpage)
        panel.add(Label("Add/Edit New Page:"))
        panel.add(self.todoTextName)
        panel.add(Label("Click to Load and Edit (doesn't save current one!):"))
        panel.add(self.todoList)
        panel.add(self.view)
        panel.add(Label("New Page HTML.  Click 'save' icon to save.  (pagename is editable as well)"))
        panel.add(self.todoTextArea)

        self.setWidget(panel)

        self.remote.getPages(self)

    def onKeyUp(self, sender, keyCode, modifiers):
        pass

    def onKeyDown(self, sender, keyCode, modifiers):
        pass

    def onKeyPress(self, sender, keyCode, modifiers):
        """
        This functon handles the onKeyPress event, and will add the item in the text box to the list when the user presses the enter key.  In the future, this method will also handle the auto complete feature.
        """
        pass

    def onSave(self, editor):
        self.status.setText("")
        name = self.todoTextName.getText()
        if not name:
            self.status.setText("Please enter a name for the page")
            return
        item = {
            'name': name,
            'text': self.todoTextArea.getHTML()
           }
        if self.todoId is None:
            rid = self.remote.addPage(item, self)
        else:
            item['id'] = self.todoId
            rid = self.remote.updatePage(item, self)

        if rid<0:
            self.status.setHTML("Server Error or Invalid Response")
            return

    def onClick(self, sender):
        if sender == self.newpage:
            self.todoId = None
            self.todoTextName.setText('')
            self.todoTextArea.setHTML('')
            return
        elif sender == self.view:
            name = self.todoTextName.getText()
            html = self.todoTextArea.getHTML()
            if not html:
                return
            p = HTMLDialog(name, html)
            p.setPopupPosition(10, 10)
            p.setWidth(Window.getClientWidth()-40)
            p.setHeight(Window.getClientHeight()-40)
            p.show()
            return
        elif sender == self.fDialogButton:
            Window.open(fileedit_url, "fileupload", "width=800,height=600")
            return
            dlg = FileDialog(fileedit_url)
            left = self.fDialogButton.getAbsoluteLeft() + 10
            top = self.fDialogButton.getAbsoluteTop() + 10
            dlg.setPopupPosition(left, top)
            dlg.show()


        id = self.remote.getPage(sender.getValue(sender.getSelectedIndex()),self)
        if id<0:
            self.status.setHTML("Server Error or Invalid Response")

    def onRemoteResponse(self, response, request_info):
        self.status.setHTML("response received")
        if request_info.method == 'getPage':
            self.status.setHTML(self.status.getText() + "HERE!")
            item = response[0]
            self.todoId = item['pk']
            self.todoTextName.setText(item['fields']['name'])
            self.todoTextArea.setHTML(item['fields']['text'])

        elif (request_info.method == 'getPages' or 
              request_info.method == 'addPage' or 
              request_info.method == 'deletePage'):
            self.status.setHTML(self.status.getText() + "HERE!")
            self.todoList.clear()
            for task in response:
                self.todoList.addItem(task['fields']['name'])
                self.todoList.setValue(self.todoList.getItemCount()-1,
                                       str(task['pk']))

        else:
            self.status.setHTML(self.status.getText() + "none!")

    def onRemoteError(self, code, message, request_info):
        self.status.setHTML("Server Error or Invalid Response: ERROR " + str(code) + " - " + str(message))