summaryrefslogtreecommitdiff
path: root/data/js/tabs.js
blob: 861cb9ff11dbb71dc1ce6a143d95a9c29be4c40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Javascript tabs. General usage:
 *
 *   <ul id="jt_select">
 *    <li><a href="#<name>" id="jt_sel_<name>">..</a></li>
 *    ..
 *   </ul>
 *
 * Can then be used to show/hide the following box:
 *
 *   <div id="jt_box_<name>"> .. </div>
 *
 * The name of the active box will be set to and (at page load) read from
 * location.hash. The parent node of the active link will get the 'tabselected'
 * class. A link with the special name "all" will display all boxes associated
 * with jt_select links.
 *
 * Only one jt_select list-of-tabs can be used on a single page.
 */
(function(){
  var links = byId('jt_select') ? byName(byId('jt_select'), 'a') : [];

  function init() {
    var sel;
    var first;
    for(var i=0; i<links.length; i++) {
      links[i].onclick = function() { set(this.id); return false };
      if(!first)
        first = links[i].id;
      if(location.hash && links[i].id == 'jt_sel_'+location.hash.substr(1))
        sel = links[i].id;
    }
    if(first)
      set(sel||first, 1);
  }

  function set(which, nolink) {
    which = which.substr(7);

    for(var i=0; i<links.length; i++) {
      var name = links[i].id.substr(7);
      if(name != 'all')
        setClass(byId('jt_box_'+name), 'hidden', which != 'all' && which != name);
      setClass(links[i].parentNode, 'tabselected', name == which);
    }

    if(!nolink)
      location.href = '#'+which;
  }

  init();
})()