From 79b376f9912891a8748fcbb4580969e4dbf7fc75 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 17 Oct 2008 20:28:18 -0400 Subject: Add an underlay for javascript, and add ikiwiki.js containing some utility code. * Add an underlay for javascript, and add ikiwiki.js containing some utility code. * toggle: Stop embedding the full toggle code on each page using it, and move it to toggle.js in the javascript underlay. --- underlays/javascript/ikiwiki.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 underlays/javascript/ikiwiki.js (limited to 'underlays/javascript/ikiwiki.js') diff --git a/underlays/javascript/ikiwiki.js b/underlays/javascript/ikiwiki.js new file mode 100644 index 000000000..29de7ec6f --- /dev/null +++ b/underlays/javascript/ikiwiki.js @@ -0,0 +1,37 @@ +// ikiwiki's javascript utility function library + +var hooks = new Array; +window.onload = run_hooks_onload; + +function run_hooks_onload() { + run_hooks("onload"); +} + +function run_hooks(name) { + for (var i = 0; i < hooks.length; i++) { + if (hooks[i].name == name) { + hooks[i].call(); + } + } +} + +function hook(name, call) { + var h={name: name, call: call}; + hooks.push(h); +} + +function getElementsByClass(cls, node, tag) { + if (document.getElementsByClass) + return document.getElementsByClass(cls, node, tag); + if (! node) node = document; + if (! tag) tag = '*'; + var ret = new Array(); + var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)"); + var els = node.getElementsByTagName(tag); + for (i = 0; i < els.length; i++) { + if ( pattern.test(els[i].className) ) { + ret.push(els[i]); + } + } + return ret; +} -- cgit v1.2.3 From 18806fa6b0f2afab703d558a499da8918214a1c7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sat, 18 Oct 2008 12:47:08 -0400 Subject: allow ikiwiki.js to be loaded twice w/o clobbering previous hooks Clearly it's suboptimal for it to be loaded twice, but this is a quick fix at least. --- underlays/javascript/ikiwiki.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'underlays/javascript/ikiwiki.js') diff --git a/underlays/javascript/ikiwiki.js b/underlays/javascript/ikiwiki.js index 29de7ec6f..14ddd0745 100644 --- a/underlays/javascript/ikiwiki.js +++ b/underlays/javascript/ikiwiki.js @@ -1,6 +1,6 @@ // ikiwiki's javascript utility function library -var hooks = new Array; +var hooks; window.onload = run_hooks_onload; function run_hooks_onload() { @@ -8,16 +8,19 @@ function run_hooks_onload() { } function run_hooks(name) { - for (var i = 0; i < hooks.length; i++) { - if (hooks[i].name == name) { - hooks[i].call(); + if (typeof(hooks) != "undefined") { + for (var i = 0; i < hooks.length; i++) { + if (hooks[i].name == name) { + hooks[i].call(); + } } } } function hook(name, call) { - var h={name: name, call: call}; - hooks.push(h); + if (typeof(hooks) == "undefined") + hooks = new Array; + hooks.push({name: name, call: call}); } function getElementsByClass(cls, node, tag) { -- cgit v1.2.3 From 002be0c87afd6c7310d2dd2babbb6a03dac8f881 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 19 Oct 2008 15:45:29 -0400 Subject: partial support for calling onload once the DOM is ready This adds support for gecko and newer versions of opera to call onload once the DOM is ready, rather than waiting for all images in the page to load. Makes relativedate behave somewhat better. Dealing with this means jumping into the browser incompatability waters that I prefer to avoid. Full solutions for most of the major browsers are listed here: http://dean.edwards.name/weblog/2006/06/again/ However, no *license* is listed there, so I can't use that code. Also, the more involved code appears to have various issues (such as the inline IE code not working via https). So I only added the simple call to a hook needed for gecko/opera. It seems that the only standards-compliant way to do this is using the `defer` attribute to a `script` tag, using an external script that will be loaded once the DOM is ready, and can call onload. However, that has browser compatability issues of its own, since not all browsers honor `defer`. Perhaps I should really just be using one of the javascript frameworks, that include code to solve this for the major browsers. But something about them still puts me off, and this issue is minor enough that I'm willing to live with incomplete support for now. --- underlays/javascript/ikiwiki.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'underlays/javascript/ikiwiki.js') diff --git a/underlays/javascript/ikiwiki.js b/underlays/javascript/ikiwiki.js index 14ddd0745..1252f244f 100644 --- a/underlays/javascript/ikiwiki.js +++ b/underlays/javascript/ikiwiki.js @@ -1,9 +1,21 @@ // ikiwiki's javascript utility function library var hooks; + +// Run onload as soon as the DOM is ready, if possible. +// gecko, opera 9 +if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", run_hooks_onload, false); +} +// other browsers window.onload = run_hooks_onload; function run_hooks_onload() { + // avoid firing twice + if (arguments.callee.done) + return; + arguments.callee.done = true; + run_hooks("onload"); } -- cgit v1.2.3