Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/js/tabs.js
blob: 58828a75c0dfe4538f64f86471bf433d1ecc6d56 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * $Id$
 */
addEvent(window, "load", initTabs);
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

var _TAB_DIVS;

// tabs handling {{{
// show tab with given id
function showTabById(tabid, noEval) { // {{{
  var divs = document.getElementsByTagName('div');
  var tab = document.getElementById(tabid);
  var submenu = document.getElementById('submenu');
  var i;
  var el = document.getElementById(tabid + "taba");

  if (!noEval && el) {
    eval(el.getAttribute('onmousedown'));
  }
  for (i=0; i<divs.length; i++) {
    if (divs[i].className && (divs[i].className.indexOf('tab') > -1)) {
      //divs[i].style.display = 'none';
      if (divs[i].className && (divs[i].className.indexOf(' active') > -1)) {
        divs[i].className = divs[i].className.substr(0, divs[i].className.length-7);
      }
    }
  }

  if (tab) {
    //tab.style.display = 'block';
    tab.className = tab.className +' active';

    if (submenu) {
      var links = submenu.getElementsByTagName('a');
      for (i=0; i<links.length; i++) {
        if (links[i].href.match('^.*#'+tabid+'$')) {
          links[i].className = links[i].className.replace(/ active\b/g, '') + ' active';
        } else { links[i].className = links[i].className.replace(/ active\b/g, ''); }
      }
    }
  }
  //if (window.scrollTo(0,0)) {
  //    window.scrollTo(0,0);
  //}
} // }}}

// create JavaScript calls to switch tabs
function makeTabLinks() { // {{{
  var submenu = document.getElementById('submenu');
  var links, i, target;

  if (submenu) {
    links = submenu.getElementsByTagName('a');
    for (i=0; i<links.length; i++) {
      var href = links[i].getAttribute('href');
      target = href.substring(href.indexOf('#')+1);
      links[i]['onclick'] = new Function("showTabById('"+target+"'); return false;");
    }
  }
} // }}}

// show tab with given number
function showTabByNumber(number) { // {{{
  var targets = new Array();    // tab names
  var divs = document.getElementsByTagName('div');
  var i;

  for (i=0; i<divs.length; i++) {
    // tweak for displaying comments-tab 'tab active' also if javascript is disabled.
    if (divs[i].className == 'tab' || divs[i].className == 'tab active') {
      targets[targets.length] = divs[i].id;             //array[array.length]= same as .push, but IE-compatible.
    }
  }
  if (number >= targets.length) {
    number = targets.length-1;
  }
  showTabById(targets[number]);
} // }}}

// get list of all DIVs that contain tabs
function getTabDivs() {/*{{{*/
    if (_TAB_DIVS == null) {
        _TAB_DIVS = new Array();
        var divs = document.getElementsByTagName('div');
        var i;
        for (i=0; i<divs.length; i++) {
            if (divs[i].className && (divs[i].className.indexOf('tab') > -1)) {
                _TAB_DIVS[_TAB_DIVS.length] = divs[i];  //array[array.length]= same as .push, but IE-compatible.
            }
        }
    }
    return _TAB_DIVS;
}/*}}}*/

// tabs init
// show first tab or tab with given name (string after #)
function initTabs() {/*{{{*/
  var target = location.href.substring(location.href.indexOf('#')+1);
  if (target.match(/comment/)) {
    target = "comments";
  }
  
  makeTabLinks();

  if (target && document.getElementById(target) && document.getElementById(target).className == 'tab') {
    showTabById(target);
  } else {
    showTabByNumber(0);
  }
  var history = document.getElementById('history');
  if (history) {
    addEvent(history,'click',uglyHistoryCommentFix);
  }

}/*}}}*/
function uglyHistoryCommentFix(e) {
  var target = e.target || window.event.srcElement;
  if ('A' == target.nodeName) {
    var re= /#comments/;
    if (re.test(target.href)) {
      showTabById('comments');
    }
  }
  var history = document.getElementById('history');
  if (history) {
    addEvent(history,'click',uglyHistoryCommentFix);
  }
}
// }}}

// }}}

// vim:enc=utf-8:fenc=utf-8:fdm=marker