allow ikiwiki.js to be loaded twice w/o clobbering previous hooks
[ikiwiki.git] / underlays / javascript / ikiwiki.js
1 // ikiwiki's javascript utility function library
2
3 var hooks;
4 window.onload = run_hooks_onload;
5
6 function run_hooks_onload() {
7         run_hooks("onload");
8 }
9
10 function run_hooks(name) {
11         if (typeof(hooks) != "undefined") {
12                 for (var i = 0; i < hooks.length; i++) {
13                         if (hooks[i].name == name) {
14                                 hooks[i].call();
15                         }
16                 }
17         }
18 }
19
20 function hook(name, call) {
21         if (typeof(hooks) == "undefined")
22                 hooks = new Array;
23         hooks.push({name: name, call: call});
24 }
25
26 function getElementsByClass(cls, node, tag) {
27         if (document.getElementsByClass)
28                 return document.getElementsByClass(cls, node, tag);
29         if (! node) node = document;
30         if (! tag) tag = '*';
31         var ret = new Array();
32         var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
33         var els = node.getElementsByTagName(tag);
34         for (i = 0; i < els.length; i++) {
35                 if ( pattern.test(els[i].className) ) {
36                         ret.push(els[i]);
37                 }
38         }
39         return ret;
40 }