changeset 128:2fba84615be7

create gui editor links by javascript and dom, tested with Safari, Firefox and IE imported from: moin--main--1.5--patch-130
author Nir Soffer <nirs@freeshell.org>
date Sat, 22 Oct 2005 19:48:26 +0000
parents df23ca172546
children f0e84aeb51dc
files MoinMoin/theme/__init__.py MoinMoin/wikiutil.py wiki/htdocs/common/js/common.js
diffstat 3 files changed, 135 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/theme/__init__.py	Fri Oct 21 21:33:29 2005 +0000
+++ b/MoinMoin/theme/__init__.py	Sat Oct 22 19:48:26 2005 +0000
@@ -801,12 +801,19 @@
         """
         html = [
             u'<title>%(title)s - %(sitename)s</title>' % d,
+            self.externalScript('common'),
             self.headscript(d), # Should move to separate .js file
+            self.guiEditorScript(d),
             self.html_stylesheets(d),
             self.rsslink(),
             ]
         return '\n'.join(html)
 
+    def externalScript(self, name):
+        """ Format external script html """
+        src = '%s/common/js/%s.js' % (self.request.cfg.url_prefix, name)
+        return '<script type="text/javascript" src="%s"></script>' % src
+
     def credits(self, d, **keywords):
         """ Create credits html from credits list """
         if isinstance(self.cfg.page_credits, (list, tuple)):
@@ -994,20 +1001,17 @@
 
     def editbarItems(self, page):
         """ Return list of items to show on the editbar 
-        
+
         This is separate method to make it easy to customize the
         edtibar in sub classes.
         """
-        links = []
-        links.append(self.parentLink(page))
-        links.extend(self.editLinks(page))
-        links.append(self.infoLink(page))
-        links.append(self.subscribeLink(page))
-        links.append(self.quicklinkLink(page))
-        links.append(self.attachmentsLink(page))
-        links.append(self.actionsMenu(page))
-
-        return links
+        return [self.parentLink(page),
+                self.editorLink(page),
+                self.infoLink(page),
+                self.subscribeLink(page),
+                self.quicklinkLink(page),
+                self.attachmentsLink(page),
+                self.actionsMenu(page),]
 
     def parentLink(self, page):
         """ Return link to parent page """
@@ -1017,28 +1021,61 @@
             return ''
         return parent.link_to(self.request, _("Parent Page", formatted=False))
 
-    def editLinks(self, page):
-        """ Return list of links to page editors """
+    def editorLink(self, page):
+        """ Return a link to the editor 
+        
+        If the user can't edit, return a disabled edit link.
+        
+        If the user want to show both editors, it will display "Edit
+        (Text)", otherwise as "Edit".
+        """
         if not (page.isWritable() and
                 self.request.user.may.write(page.page_name)):
-            return [self.disabledEdit()]
+            return self.disabledEdit()
         
         _ = self.request.getText
+        params = (wikiutil.quoteWikinameURL(page.page_name) +
+                  '?action=edit&amp;editor=')
+
+        if self.showBothEditLinks():
+            text = _('Edit (Text)', formatted=False)
+            params = params + 'text'
+            attrs = 'name="texteditlink"'
+        else:
+            text = _('Edit', formatted=False)
+            # 'textonly' will be upgraded dynamically to 'guipossible' by JS
+            params = params + 'textonly'
+            attrs='name="editlink"'
+        
+        return wikiutil.link_tag(self.request, params, text, attrs=attrs)
+
+    def showBothEditLinks(self):
+        """ Return True if both edit links should be displayed """
         editor = self.request.user.editor_ui
         if editor == '<default>':
             editor = self.request.cfg.editor_ui
-        params = wikiutil.quoteWikinameURL(page.page_name) + '?action=edit'
+        return editor == 'freechoice'
 
-        if editor == 'freechoice':
-            return [wikiutil.link_tag(self.request, params + '&editor=text', 
-                                      _('Edit (Text)', formatted=False), attrs='id="texteditlink"'),
-                    wikiutil.link_tag(self.request, params + '&editor=gui', 
-                                      _('Edit (GUI)', formatted=False), attrs='id="guieditlink"'),]
-        else: # editor == 'theonepreferred'.
-            # unspecified editor will use the user prefered edtior.
-            # 'textonly' will be upgraded dynamically to 'guipossible' by JS
-            return [wikiutil.link_tag(self.request, params + '&editor=textonly',
-                                      _('Edit', formatted=False), attrs='id="editlink"'),]
+    def guiEditorScript(self, d):
+        """ Return a script that set the gui editor link variables
+        
+        The link will be created only when javascript is enabled and
+        the browser is compatible with the editor.
+        """
+        page = d['page']
+        if not (page.isWritable() and
+                self.request.user.may.write(page.page_name) and
+                self.showBothEditLinks()):
+            return ''
+
+        _ = self.request.getText
+        return """\
+<script type="text/javascript">
+var gui_editor_link_href = "%(url)s?action=edit&editor=gui";
+var gui_editor_link_text = "%(text)s";
+</script>        
+""" % {'url': wikiutil.quoteWikinameURL(page.page_name),
+       'text': _('Edit (GUI)', formatted=False),}
 
     def disabledEdit(self):
         """ Return a disabled edit link """
--- a/MoinMoin/wikiutil.py	Fri Oct 21 21:33:29 2005 +0000
+++ b/MoinMoin/wikiutil.py	Sat Oct 22 19:48:26 2005 +0000
@@ -1196,6 +1196,7 @@
         ''.join(user_head),
         keywords.get('html_head', ''),
         request.theme.html_head({
+            'page': page,
             'title': escape(text),
             'sitename': escape(request.cfg.html_pagetitle or request.cfg.sitename),
             'print_mode': keywords.get('print_mode', False),
@@ -1245,8 +1246,6 @@
         '<link rel="Glossary" href="%s/%s">\n' % (scriptname, quoteWikinameURL(page_word_index)),
         '<link rel="Help" href="%s/%s">\n' % (scriptname, quoteWikinameURL(page_help_formatting)),
                   ])
-
-    output.append('<script type="text/javascript" src="%s/common/js/common.js"></script>\n' % request.cfg.url_prefix)
     
     output.append("</head>\n")
     request.write(''.join(output))
--- a/wiki/htdocs/common/js/common.js	Fri Oct 21 21:33:29 2005 +0000
+++ b/wiki/htdocs/common/js/common.js	Sat Oct 22 19:48:26 2005 +0000
@@ -23,39 +23,86 @@
     }
 }
 
-function update_ui() {
-    if (can_use_gui_editor()) {
-        guieditlink = document.getElementById("guieditlink");
-        if (guieditlink) {
-            guieditlink.style.display = 'inline';
-        }
-        editlink = document.getElementById("editlink")
-        if (editlink) {
-            href = editlink.href.replace('editor=textonly',
-                                         'editor=guipossible');
-            editlink.href = href;
-        }
-        switch2gui = document.getElementById('switch2gui')
-        if (switch2gui) {
-            switch2gui.style.display = 'inline';
-        }
+
+function update_edit_links() {
+    // Update editlink according if if the browser is compatible
+    if (can_use_gui_editor() == false) return;
+
+    var editlinks = document.getElementsByName("editlink");
+    for (i = 0; i < editlinks.length; i++) {
+        var link = editlinks[i];
+        href = link.href.replace('editor=textonly','editor=guipossible');
+        link.href = href;
     }
 }
 
-function onload() {
-    update_ui()
-    // countdown is available when editing
-    try {countdown()} catch (e) {}
+
+function add_gui_editor_links() {
+    // Add gui editor link after the text editor link
+    
+    // If the variable is not set or browser is not compatible, exit
+    try {gui_editor_link_href} catch (e) {return}
+    if (can_use_gui_editor() == false) return;
+    
+    var all = document.getElementsByName('texteditlink');
+    for (i = 0; i < all.length; i++) {
+        var textEditorLink = all[i];
+        // Create a list item with a link
+        var guiEditorLink = document.createElement('a');
+        guiEditorLink.href = gui_editor_link_href;
+        var text = document.createTextNode(gui_editor_link_text);
+        guiEditorLink.appendChild(text);
+        var listItem = document.createElement('li')
+        listItem.appendChild(guiEditorLink);
+        // Insert in the editbar
+        var editbar = textEditorLink.parentNode.parentNode
+        var nextListItem = textEditorLink.parentNode.nextSibling;
+        editbar.insertBefore(listItem, nextListItem);
+    }
+}
+  
+
+function show_switch2gui() {
+    // Show switch to gui editor link if the browser is compatible
+    if (can_use_gui_editor() == false) return;
+    
+    var switch2gui = document.getElementById('switch2gui')
+    if (switch2gui) {
+        switch2gui.style.display = 'inline';
+    }
 }
 
-function beforeunload(evt) {
-    // confirmleaving is available when editing
-    return confirmleaving()
+
+function load() {
+    // Do not name this "onload", it does not work with IE :-)
+    // TODO: create separate onload for each type of view and set the
+    // correct function name in the html. 
+    // e.g <body onlod='editor_onload()'>
+    
+    // Page view stuff
+    update_edit_links();
+    add_gui_editor_links();
+    
+    // Editor stuff
+    show_switch2gui();
+    // countdown is available when editing
+    try {countdown()}
+    catch (e) {}
 }
 
+
+function before_unload(evt) {
+    // TODO: Better to set this in the editor html, as it does not make
+    // sense elsehwere.
+    // confirmleaving is available when editing
+    try {return confirmleaving();}
+    catch (e) {}
+}
+
+
 // Initialize after loading the page
-window.onload = onload
+window.onload = load;
 
 // Catch before unloading the page
-window.onbeforeunload = beforeunload
+window.onbeforeunload = before_unload;