changeset 194:722b8f648630

Use addLoadEvent instead of window.onload=..., move countdown JS to external file imported from: moin--main--1.5--patch-196
author Thomas Waldmann <tw@waldmann-edv.de>
date Sat, 05 Nov 2005 20:41:13 +0000
parents 236f561fa21f
children 1c7437a33f90
files MoinMoin/PageEditor.py MoinMoin/PageGraphicalEditor.py MoinMoin/wikiutil.py wiki/htdocs/common/js/common.js wiki/htdocs/common/js/countdown.js wiki/htdocs/common/js/infobox.js
diffstat 6 files changed, 71 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/PageEditor.py	Sat Nov 05 17:39:14 2005 +0000
+++ b/MoinMoin/PageEditor.py	Sat Nov 05 20:41:13 2005 +0000
@@ -23,57 +23,15 @@
 ### Javascript code for editor page
 #############################################################################
 
-# This code is internal to allow I18N, else we'd use a .js file;
 # we avoid the "--" operator to make this XHTML happy!
-# TODO: move to external js file, send the translations when we load or
-# set the them as global variables.
 _countdown_js = """
+%(countdown_script)s
 <script type="text/javascript">
-var timeout_min = %(lock_timeout)s;
-var state = 0; // 0: start; 1: long count; 2: short count; 3: timeout; 4/5: blink
-var counter = 0, step = 1, delay = 1;
-
-function countdown() {
-    // change state if counter is down
-    if (counter <= 1) {
-        state += 1
-        if (state == 1) {
-            counter = timeout_min
-            step = 1
-            delay = 60000
-        }
-        if (state == 2) {
-            counter = 60
-            step = 5
-            delay = step * 1000
-        }
-        if (state == 3 || state == 5) {
-            window.status = "%(lock_expire)s"
-            state = 3
-            counter = 1
-            step = 1
-            delay = 500
-        }
-        if (state == 4) {
-            // blink the above text
-            window.status = " "
-            counter = 1
-            delay = 250
-        }
-    }
-
-    // display changes
-    if (state < 3) {
-        var msg
-        if (state == 1) msg = "%(lock_mins)s"
-        if (state == 2) msg = "%(lock_secs)s"
-        window.status = msg.replace(/#/, counter)
-    }
-    counter -= step
-
-    // Set timer for next update
-    setTimeout("countdown()", delay);    
-}
+var countdown_timeout_min = %(lock_timeout)s
+var countdown_lock_expire = "%(lock_expire)s"
+var countdown_lock_mins = "%(lock_mins)s"
+var countdown_lock_secs = "%(lock_secs)s"
+addLoadEvent(countdown)
 </script>
 """
 
@@ -278,6 +236,7 @@
             pagename=self.page_name, msg=status,
             html_head=self.lock.locktype and (
                 _countdown_js % {
+                     'countdown_script': self.request.theme.externalScript('countdown'),
                      'lock_timeout': lock_timeout,
                      'lock_expire': lock_expire,
                      'lock_mins': lock_mins,
--- a/MoinMoin/PageGraphicalEditor.py	Sat Nov 05 17:39:14 2005 +0000
+++ b/MoinMoin/PageGraphicalEditor.py	Sat Nov 05 20:41:13 2005 +0000
@@ -154,9 +154,9 @@
             title % {'pagename': self.split_title(self.request),},
             page=self,
             pagename=self.page_name, msg=status,
-            body_onload=self.lock.locktype and 'countdown()' or '', # broken / bug in Mozilla 1.5, when using #preview
             html_head=self.lock.locktype and (
                 PageEditor._countdown_js % {
+                     'countdown_script': self.request.theme.externalScript('countdown'),
                      'lock_timeout': lock_timeout,
                      'lock_expire': lock_expire,
                      'lock_mins': lock_mins,
--- a/MoinMoin/wikiutil.py	Sat Nov 05 17:39:14 2005 +0000
+++ b/MoinMoin/wikiutil.py	Sat Nov 05 20:41:13 2005 +0000
@@ -1211,14 +1211,14 @@
 %s
 """ % (
         ''.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),
             'media': keywords.get('media', 'screen'),
-        })
+        }),
+        keywords.get('html_head', ''),
     ))
 
     # Links
--- a/wiki/htdocs/common/js/common.js	Sat Nov 05 17:39:14 2005 +0000
+++ b/wiki/htdocs/common/js/common.js	Sat Nov 05 20:41:13 2005 +0000
@@ -2,6 +2,19 @@
 // MoinMoin commonly used JavaScript functions
 //
 
+// use this instead of assigning to window.onload directly:
+function addLoadEvent(func) {
+  var oldonload = window.onload;
+  if (typeof window.onload != 'function') {
+    window.onload = func;
+  } else {
+    window.onload = function() {
+      oldonload();
+      func();
+    }
+  }
+}
+
 function can_use_gui_editor() {
     var sAgent = navigator.userAgent.toLowerCase();
  
@@ -85,9 +98,6 @@
     
     // Editor stuff
     show_switch2gui();
-    // countdown is available when editing
-    try {countdown()}
-    catch (e) {}
 }
 
 
@@ -99,10 +109,9 @@
     catch (e) {}
 }
 
-
 // Initialize after loading the page
-window.onload = load;
+addLoadEvent(load)
 
 // Catch before unloading the page
-window.onbeforeunload = before_unload;
+window.onbeforeunload = before_unload
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wiki/htdocs/common/js/countdown.js	Sat Nov 05 20:41:13 2005 +0000
@@ -0,0 +1,45 @@
+var state = 0; // 0: start; 1: long count; 2: short count; 3: timeout; 4/5: blink
+var counter = 0, step = 1, delay = 1;
+
+function countdown() {
+    // change state if counter is down
+    if (counter <= 1) {
+        state += 1
+        if (state == 1) {
+            counter = countdown_timeout_min
+            step = 1
+            delay = 60000
+        }
+        if (state == 2) {
+            counter = 60
+            step = 5
+            delay = step * 1000
+        }
+        if (state == 3 || state == 5) {
+            window.status = countdown_lock_expire
+            state = 3
+            counter = 1
+            step = 1
+            delay = 500
+        }
+        if (state == 4) {
+            // blink the above text
+            window.status = " "
+            counter = 1
+            delay = 250
+        }
+    }
+
+    // display changes
+    if (state < 3) {
+        var msg
+        if (state == 1) msg = countdown_lock_mins
+        if (state == 2) msg = countdown_lock_secs
+        window.status = msg.replace(/#/, counter)
+    }
+    counter -= step
+
+    // Set timer for next update
+    setTimeout("countdown()", delay);    
+}
+
--- a/wiki/htdocs/common/js/infobox.js	Sat Nov 05 17:39:14 2005 +0000
+++ b/wiki/htdocs/common/js/infobox.js	Sat Nov 05 20:41:13 2005 +0000
@@ -75,8 +75,7 @@
 }
 
 // Initialize after loading the page
-old_onload=window.onload
-window.onload=infoinit;
+addLoadEvent(infoinit)
 
 // EOF infobox.js