source: documentation/trunk/packages/dokuwiki-2011-05-25a/lib/plugins/simpletabs/script.js@ 30098

Last change on this file since 30098 was 30098, checked in by jmt12, 9 years ago

Initial checkin of plugin code

File size: 4.5 KB
Line 
1function tabSwitch(tabarea, tab_count, active_tab_number)
2{
3 // Active tab is null if this is the first ever time the page is loaded
4 // by this user. Default to tab 1---which should be Greenstone 3.
5 if (active_tab_number == null)
6 {
7 active_tab_number = 1;
8 }
9 //alert("tabSwitch(" + tabarea + "," + tab_count + "," + active_tab_number + ")");
10
11 for (var i = 1; i < tab_count + 1; i++)
12 {
13 if (i != active_tab_number)
14 {
15 document.getElementById('t' + tabarea + '_' + i).className = '';
16 document.getElementById('tc' + tabarea + '_' + i).style.display = 'none';
17 _tabTOCEntries('tc' + tabarea + '_' + i, false);
18 }
19 }
20 document.getElementById('t' + tabarea + '_' + active_tab_number).className = 'active';
21 document.getElementById('tc' + tabarea + '_' + active_tab_number).style.display = 'block';
22 _tabTOCEntries('tc' + tabarea + '_' + active_tab_number, true);
23 // Store the default tab for later page loads
24 _setCookie('dokuwiki_gs_default_tab', active_tab_number, 365);
25}
26
27function initTabArea()
28{
29 // Get the default tab cookie
30 var active_tab_number = _getCookie("dokuwiki_gs_default_tab");
31
32 // Switch tabs appropriately
33 var tab_area_counter = 1;
34 var possible_tab_area_id = 't' + tab_area_counter;
35 // Sometimes - for unknown reasons - t1 is not defined, so lets just try
36 // and update for the first tab tabs
37 for (tab_area_counter = 1; tab_area_counter < 5; tab_area_counter++)
38 {
39 if (document.getElementById('t' + tab_area_counter) != null)
40 {
41 var tab_count = 0;
42 var possible_tab_id = 't' + tab_area_counter + '_' + (tab_count + 1);
43 while (document.getElementById(possible_tab_id) != null)
44 {
45 tab_count++;
46 possible_tab_id = possible_tab_area_id + '_' + (tab_count + 1);
47 }
48 tabSwitch(tab_area_counter, tab_count, active_tab_number);
49 }
50 }
51 // Carry on while we can still find more tabs
52 while (document.getElementById('t' + tab_area_counter) != null)
53 {
54 var tab_count = 0;
55 var possible_tab_id = 't' + tab_area_counter + '_' + (tab_count + 1);
56 while (document.getElementById(possible_tab_id) != null)
57 {
58 tab_count++;
59 possible_tab_id = possible_tab_area_id + '_' + (tab_count + 1);
60 }
61 tabSwitch(tab_area_counter, tab_count, active_tab_number);
62 tab_area_counter++;
63 }
64}
65
66
67/**
68 * source: http://www.w3schools.com/js/js_cookies.asp
69 */
70function _getCookie(c_name)
71{
72 var c_value = document.cookie;
73 var c_start = c_value.indexOf(" " + c_name + "=");
74 if (c_start == -1)
75 {
76 c_start = c_value.indexOf(c_name + "=");
77 }
78 if (c_start == -1)
79 {
80 c_value = null;
81 }
82 else
83 {
84 c_start = c_value.indexOf("=", c_start) + 1;
85 var c_end = c_value.indexOf(";", c_start);
86 if (c_end == -1)
87 {
88 c_end = c_value.length;
89 }
90 c_value = unescape(c_value.substring(c_start, c_end));
91 }
92 return c_value;
93}
94/** _getCookie() **/
95
96
97/**
98 * source: http://www.w3schools.com/js/js_cookies.asp
99 */
100function _setCookie(c_name, value, exdays)
101{
102 var exdate=new Date();
103 exdate.setDate(exdate.getDate() + exdays);
104 var c_value = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
105 document.cookie=c_name + "=" + c_value;
106}
107/** _setCookie() **/
108
109/**
110 * Toggle the visibility of TOC entries based upon what tab is selected.
111*/
112function _tabTOCEntries(tab_id, make_visible)
113{
114 // Find all the heading in the indicated tab
115 var the_tab = document.getElementById(tab_id);
116 var headers = the_tab.getElementsByTagName('a');
117 for (var i = 0; i < headers.length; i++)
118 {
119 var id = headers[i].getAttribute('id');
120 var toc_id = 'toc_' + id;
121 var toc_li = document.getElementById(toc_id);
122 if (toc_li)
123 {
124 if (make_visible)
125 {
126 toc_li.style.display = 'block';
127 }
128 else
129 {
130 toc_li.style.display = 'none';
131 }
132 }
133 }
134}
135/** _tabTOCEntries(tab_id, make_visible) **/
136
137/** Register new init function - my first ever use of jQuery!
138 * - sigh. Doesn't work in Rincewind
139 */
140/*
141jQuery(function() {
142 initTabArea();
143});
144*/
145
146/** Old code */
147addInitEvent(function(){
148 initTabArea();
149});
Note: See TracBrowser for help on using the repository browser.