initial commit
[emacs-init.git] / nxhtml / nxhtml / html-wtoc / html-wtoc.js
1
2 // © Copyright 2006 Lennart Borgman, http://www.OurComments.org/. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 3, or (at
7 // your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING.  If not, write to
16 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth
17 // Floor, Boston, MA 02110-1301, USA.
18
19
20 var HTML_WTOC_NS_sCurrTocId;
21
22
23 HTML_WTOC_NS = {
24
25         /////////////////////////////
26         //// Basic event functions
27         /////////////////////////////
28
29         getEventObject : function (ev) {
30                 var o;
31                 if (window.event)
32                         o = window.event.srcElement;
33                 else if (null != ev)
34                         o = ( ev.target );
35                 return o;
36         },
37         getEvent : function (ev) {
38                 if (window.event) {
39                         return window.event;
40                 } else if (null != ev) {
41                         return ev;
42                 }
43         },
44
45         eventStopPropagation : function (e) {
46           if (e.stopPropagation)
47                 e.stopPropagation();
48           else
49                 e.cancelBubble=true;
50         },
51
52         eventPreventDefault : function (e) {
53           if (e.preventDefault)
54                 e.preventDefault();
55           else
56                 e.returnValue=false;
57         },
58
59         /////////////////////////////
60         //// TOC hide
61         /////////////////////////////
62
63         show_content : function (on) {
64                 var toc = document.getElementById("html-wtoc-id-toccol").style;
65                 var tdv = document.getElementById("html-wtoc-id-tocdiv").style;
66                 var shw = document.getElementById("html-wtoc-id-showtoc").style;
67                 var hid = document.getElementById("html-wtoc-id-hidetoc").style;
68                 if (on) {
69                         toc.display = "";
70                         tdv.display = "";
71                         shw.display = "none";
72                         hid.display = "";
73                         HTML_WTOC_NS.focus_page_link(0);
74                 } else {
75                         toc.display = "none";
76                         tdv.display = "none";
77                         shw.display = "";
78                         hid.display = "none";
79                 }
80         },
81
82
83
84
85
86         /////////////////////////////
87         //// Open-Close
88         /////////////////////////////
89         onblur_action : function(ev) {
90                 HTML_WTOC_NS_sCurrTocId = null;
91         },
92         onfocus_action : function(ev) {
93                 var o = HTML_WTOC_NS.getEventObject(ev);
94                 if (!o) return;
95
96                 HTML_WTOC_NS_sCurrTocId = o.id;
97         },
98         onclick_action : function(ev) {
99                 var o = HTML_WTOC_NS.getEventObject(ev);
100                 var e = HTML_WTOC_NS.getEvent(ev);
101                 if (13 == e.keyCode) return true;
102                 if (!o) return true;
103                 if ("IMG" == o.tagName) o = o.parentNode;
104                 var iId = HTML_WTOC_NS.getIdnumFromId(o.id);
105                 var sChildId = "toc_child_"+iId;
106                 var sOldCurrTocId = HTML_WTOC_NS_sCurrTocId;
107                 HTML_WTOC_NS.toggle_open(sChildId, o);
108                 HTML_WTOC_NS_sCurrTocId = sOldCurrTocId;
109                 return false;
110         },
111
112         toggle_open : function (id, parent) {
113                 var child = document.getElementById(id).style;
114                 var sInner = parent.innerHTML;
115                 var re = new RegExp("[^/]*\.gif", "i");
116                 if ("none" == child.display) {
117                         child.display = "";
118                         parent.innerHTML = sInner.replace(re, "down.gif")+"";
119                 } else {
120                         child.display = "none";
121                         parent.innerHTML = sInner.replace(re, "right.gif")+"";
122                 }
123         },
124
125
126
127         /////////////////////////////
128         //// Load
129         /////////////////////////////
130
131         onload_actions : function (iPageNum) {
132                 document.body.onkeydown   = HTML_WTOC_NS.onkeydown_action;
133                 document.body.onmouseover = HTML_WTOC_NS.onmouseover_action;
134                 var aATags = document.getElementsByTagName("a");
135                 for(var i = 0; i < aATags.length; i++) {
136                         var o = aATags[i];
137                         if (null != HTML_WTOC_NS.getIdnumFromId(o.id)) {
138                                 o.onfocus = HTML_WTOC_NS.onfocus_action;
139                                 o.onblur  = HTML_WTOC_NS.onblur_action;
140                                 if (o.id.substr(0, 12) == "opener_text_") {
141                                         o.onclick = HTML_WTOC_NS.onclick_action;
142                                         o.title = "Open/Close";
143                                 } else if (o.id.substr(0, 7) == "opener_") {
144                                         o.onclick = HTML_WTOC_NS.onclick_action;
145                                         o.className = "html-wtoc-mark";
146                                         o.title = "Open/Close";
147                                 }
148                         }
149                 }
150                 HTML_WTOC_NS.focus_page_link(iPageNum);
151         },
152         focus_page_link : function (iPageNum) {
153                 // Element might be hidden
154                 try {
155                         document.getElementById("toc_link_"+iPageNum).focus();
156                 } catch (exc) {
157                 }
158         },
159
160
161
162
163
164
165
166         /////////////////////
167         //// Mouse
168         /////////////////////
169
170         onmouseover_action : function (ev) {
171                 if (null == HTML_WTOC_NS_sCurrTocId) return true;
172                 var o = HTML_WTOC_NS.getEventObject(ev);
173                 var iId = HTML_WTOC_NS.getIdnumFromId(o.id);
174                 if (null == iId) return true;
175                 o.focus();
176         },
177
178
179
180         /////////////////////
181         //// Key
182         /////////////////////
183
184         onkeydown_action: function (ev) {
185                 var keyDown    = 40;
186                 var keyUp      = 38;
187                 var keyLeft    = 37;
188                 var keyRight   = 39;
189                 var keyReturn  = 13;
190                 var keyF2      = 113;
191                 var keyInsert  = 45;
192                 // Opera
193                 var keyOperaDown    = 57386;
194                 var keyOperaUp      = 57385;
195                 var keyOperaLeft    = 57387;
196                 var keyOperaRight   = 57388;
197                 var keyOperaF2      = 57346;
198                 var keyOperaInsert  = 57394;
199
200                 var SwitchKey      = keyInsert;
201                 var SwitchKeyOpera = keyOperaInsert;
202
203                 var bUp;
204                 var e = HTML_WTOC_NS.getEvent(ev);
205                 if (null == HTML_WTOC_NS_sCurrTocId) {
206                         switch (e.keyCode) {
207                                 case SwitchKey:
208                                 case SwitchKeyOpera:
209                                         HTML_WTOC_NS.focus_page_link(0);
210                                         HTML_WTOC_NS.eventStopPropagation(e);
211                                         HTML_WTOC_NS.eventPreventDefault(e);
212                                         return false;
213                         }
214                         return true;
215                 }
216                 switch (e.keyCode) {
217                         case keyLeft:
218                         case keyOperaLeft:
219                         case keyRight:
220                         case keyOperaRight:
221                                 HTML_WTOC_NS.handle_leftright_keys(e);
222                                 HTML_WTOC_NS.eventStopPropagation(e);
223                                 HTML_WTOC_NS.eventPreventDefault(e);
224                                 return false;
225                         case keyDown:
226                         case keyOperaDown:
227                                 bUp = false;
228                                 break;
229                         case keyUp:
230                         case keyOperaUp:
231                                 bUp = true;
232                                 break;
233                         case SwitchKey:
234                         case SwitchKeyOpera:
235                                 if (null != HTML_WTOC_NS_sCurrTocId) {
236                                         var o = document.getElementById(HTML_WTOC_NS_sCurrTocId);
237                                         if (o) o.blur();
238                                         HTML_WTOC_NS_sCurrTocId = null;
239                                 }
240                                 HTML_WTOC_NS.eventStopPropagation(e);
241                                 HTML_WTOC_NS.eventPreventDefault(e);
242                                 return false;
243                         default:
244                         //alert(e.keyCode);
245                                 return true;
246                 }
247                 var oOpener;
248                 oOpener = HTML_WTOC_NS.getNextVisOpener(HTML_WTOC_NS_sCurrTocId, bUp);
249                 oOpener.focus();
250                 HTML_WTOC_NS.eventStopPropagation(e);
251                 HTML_WTOC_NS.eventPreventDefault(e);
252                 return false;
253         },
254
255         handle_leftright_keys: function (e) {
256                 var keyLeft  = 37;
257                 var keyRight = 39;
258                 var keyOperaLeft  = 57387;
259                 var keyOperaRight = 57388;
260                 var iId = HTML_WTOC_NS.getIdnumFromId(HTML_WTOC_NS_sCurrTocId);
261                 if (null == iId) return;
262                 var sId = "opener_" + iId;
263                 var oOpener = document.getElementById(sId);
264                 var sId = HTML_WTOC_NS_sCurrTocId;      // It will be cleared before getNextVis
265
266                 var bOpenAction;
267                 var bOpened;
268                 var bUp;
269                 var oChild = document.getElementById("toc_child_"+iId);
270                 if (null == oChild) {
271                 } else {
272                         bOpened = (oChild.style.display != "none");
273                 }
274                 switch (e.keyCode) {
275                         case keyLeft:
276                         case keyOperaLeft:
277                                 bUp = true;
278                                 bOpenAction = (null != bOpened) && (bOpened);
279                                 break;
280                         case keyRight:
281                         case keyOperaRight:
282                                 bUp = false;
283                                 bOpenAction = (null != bOpened) && (!bOpened);
284                                 break;
285                         default:
286                                 alert("bad key handling...");
287                 }
288                 if (bOpenAction) {
289                         oOpener.click();
290                         HTML_WTOC_NS_sCurrTocId = sId;
291                 } else {
292                         var oPrev = HTML_WTOC_NS.getNextVisOpener(sId, bUp);
293                         oPrev.focus();
294                 }
295         },
296
297
298
299
300
301
302         //////////////////////
303         //// Util
304         //////////////////////
305         getNameFromId: function (sId) {
306                 var re = new RegExp("(.*?_)(\\d+)", "i");
307                 if (!re.test(sId)) return null;
308                 var iId = sId.replace(re, "$1");
309                 return iId;
310         },
311         getIdnumFromId: function (sId) {
312                 var re = new RegExp("(.*?_)(\\d+)", "i");
313                 if (!re.test(sId)) return null;
314                 var iId = sId.replace(re, "$2");
315                 return iId;
316         },
317
318
319         getNextVisOpener: function (sId, bUp, bTrace) {
320                 if (bTrace) alert("getNextVisOpener("+sId+","+bUp+")");
321                 var iId = HTML_WTOC_NS.getIdnumFromId(sId);
322                 if (null == iId) {
323                         alert("getNextVisOpener err iId==null");
324                         return;
325                 }
326                 var sIdName = HTML_WTOC_NS.getNameFromId(sId);
327                 if (null == sIdName) {
328                         alert("getNextVisOpener err sIdName==null");
329                         return;
330                 }
331                 var oOpener;
332                 var iLoop = -2;
333                 while (oOpener == null) {
334                         if (bTrace) alert(iId);
335                         if (iLoop++ > iMaxChildNum) { alert("Child num error"); return; }
336                         if (!bUp) {
337                                 iId++;
338                         } else {
339                                 iId--;
340                         }
341                         if (iId > iMaxChildNum) { iId = 0; }
342                         if (iId <  0) { iId = iMaxChildNum; }
343                         var s = sIdName+iId;
344                         oOpener = document.getElementById(s);
345                         if (oOpener != null) {
346                                 if (bTrace) alert(oOpener.offsetLeft);
347                                 if (oOpener.style.display == "none") { // All
348                                         oOpener = null;
349                                 } else if (oOpener.offsetLeft < 0) { // IE
350                                         oOpener = null;
351                                 } else if (0 == oOpener.scrollWidth) { // Opera
352                                         oOpener = null;
353                                 }
354                         }
355                 }
356                 return oOpener;
357         }
358
359
360
361 }; //HTML_WTOC_NS