1 /* MarkTree JavaScript code
3 * The contents of this file are subject to the Mozilla Public License Version
4 * 1.1 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
6 * http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS IS" basis,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10 * for the specific language governing rights and limitations under the
13 * Miika Nurminen, 12.7.2004.
16 /* cross-browser (tested with ie5, mozilla 1 and opera 5) keypress detection */
17 function get_keycode(evt) {
19 code = document.layers ? evt.which
20 : document.all ? event.keyCode // event.keyCode!=evt.keyCode!
24 code=evt.which; // for NS
31 var lastnodetype=''; // determines if node is a link, input or text;
33 // up, left, down, right, keypress codes
35 //var keys = new Array(105,106,107,108);
37 //var keys = new Array(56,52,50,54);
39 // var press2 = new Array(119,97,115,100);
40 var press = new Array(47,45,42,43);
43 // var keys2=new Array(87,65,83,68);
44 var keys= new Array(38,37,40,39);
46 // keyset 1 = keydown, otherwise press
47 function checkup(keyset,n) {
48 if (keyset==1) return (n==keys[0]);
49 return ((n==press[0]) /*|| (n==press2[0])*/)
52 function checkdn(keyset,n) {
53 if (keyset==1) return (n==keys[2]);
54 return ((n==press[2]) /*|| (n==press2[2])*/)
57 function checkl(keyset,n) {
58 if (keyset==1) return (n==keys[1]);
59 return ((n==press[1]) /*|| (n==press2[1])*/)
62 function checkr(keyset,n) {
63 if (keyset==1) return (n==keys[3]);
64 return ((n==press[3]) /*|| (n==press2[3])*/)
72 if (n==null) return false;
73 return ((n.className=='exp') || (n.className=='exp_active'));
77 if (n==null) return false;
78 return ((n.className=='col') || (n.className=='col_active'));
81 function is_basic(n) {
82 if (n==null) return false;
83 return ((n.className=='basic') || (n.className=='basic_active'));
88 /* returns i>=0 if true */
89 function is_active(node) {
90 if (node.className==null) return false
91 return node.className.indexOf('_active');
94 function toggle_class(node) {
95 if ((node==null) || (node.className==null)) return;
98 i = str.indexOf('_active');
100 result= str.substr(0,i);
102 result= str+"_active";
103 node.className=result;
107 function activate(node) {
108 node.style.backgroundColor='#eeeeff';
111 function deactivate(node) {
112 node.style.backgroundColor='#ffffff';
115 function is_list_node(n) {
116 if (n==null) return false;
117 if (n.className==null) return false;
121 return true; else return false;
125 function get_href(n) {
128 hr = alist.getNamedItem('href');
129 if (hr!=null) return hr.nodeValue;
131 if (n.childNodes.length==0) return '';
132 for (var i=0; i<n.childNodes.length; i++) {
133 s = get_href(n.childNodes[i]);
139 function get_link(n) {
140 if (n==null) return null;
141 if (n.style==null) return null;
143 // disabling uncontrolled recursion to prevent error messages on IE
144 // when trying to focus to invisible links (readonly mode)
145 // alert(n.nodeName+' '+n.className);
146 if ((n.nodeName=='UL') && (n.className=='sub')) return null;
148 if (n.nodeName=='A') return n;
149 if (n.childNodes.length==0) return null;
150 for (var i=0; i<n.childNodes.length; i++) {
151 s = get_link(n.childNodes[i]);
152 if (s!=null) return s;
157 function set_lastnode(n) {
158 /*var d = new Date();
159 var t_mil = d.getMilliseconds();*/
160 // testattu nopeuksia explorerilla, ei merkittäviä eroja
161 if (lastnode==n) return;
162 /* deactivate(lastnode)
164 activate(lastnode);*/
166 if (is_active(lastnode)>=0)
167 toggle_class(lastnode);
169 if (!(is_active(lastnode)>=0))
170 toggle_class(lastnode);
173 /*var d2 = new Date();
174 var t_mil2 = d2.getMilliseconds();
175 window.alert(t_mil2-t_mil);*/
178 function next_list_node() {
179 tempIndex = list_index;
180 while (tempIndex<listnodes.length-1) {
182 var x = listnodes[tempIndex];
183 if (is_list_node(x)) {
184 list_index=tempIndex;
190 function prev_list_node() {
191 tempIndex = list_index;
192 while (tempIndex>0) {
194 var x = listnodes[tempIndex];
195 if (is_list_node(x)) {
196 list_index=tempIndex;
204 function getsub (li) {
205 if (li.childNodes.length==0) return null;
206 for (var c = 0; c < li.childNodes.length; c++)
207 if ( (li.childNodes[c].className == 'sub') || (li.childNodes[c].className == 'subexp') )
208 return li.childNodes[c];
211 function find_listnode_recursive (li) {
212 if (is_list_node(li)) return li;
213 if (li.childNodes.length==0) return null;
215 for (var c = 0; c < li.childNodes.length; c++) {
216 result=find_listnode_recursive(li.childNodes[c]);
217 if (result!=null) return result;
222 function next_child_listnode(li) {
224 for (var i=0; i<li.childNodes.length; i++) {
225 result=find_listnode_recursive(li.childNodes[i]);
226 if (result!=null) return result;
231 function next_actual_sibling_listnode(li) {
232 if (li==null) return null;
235 var n = temp.nextSibling;
237 n=parent_listnode(temp);
238 return next_actual_sibling_listnode(n);
240 if (is_list_node(n)) return n;
245 function next_sibling_listnode(li) {
246 if (li==null) return null;
249 if (is_col(temp)) return next_child_listnode(temp);
251 var n = temp.nextSibling;
253 n=parent_listnode(temp);
254 return next_actual_sibling_listnode(n);
256 if (is_list_node(n)) return n;
261 function last_sibling_listnode(li) {
262 if (li==null) return null;
266 var n = temp.nextSibling;
267 if (is_list_node(temp))
270 if (is_col(last)) return last_sibling_listnode(next_child_listnode(last));
277 function prev_sibling_listnode(li) {
278 if (li==null) return null;
282 n = temp.previousSibling;
284 return parent_listnode(li);
286 if (is_list_node(n)) {
288 return last_sibling_listnode(next_child_listnode(n));
299 function parent_listnode(li) {
300 // added 12.7.2004 to prevent IE error when readonly mode==true
301 if (li==null) return null;
305 if (n==null) return null;
306 if (is_list_node(n)) return n;
310 function getVisibleParents(id) {
311 var n = document.getElementById(id);
314 n = parent_listnode(n);
319 function onClickHandler (evt) {
322 listnodes = document.getElementsByTagName('li');
323 lastnode=listnodes[1];
328 var target = evt ? evt.target : event.srcElement;
329 if (!is_list_node(target)) return;
331 set_lastnode(target);
335 function expand(node) {
336 if (!is_exp(node)) return;
337 if (node.className=='exp_active')
338 node.className='col_active';
340 node.className='col';
341 setSubClass(node,'subexp');
342 // getsub(node).className='subexp';
345 function collapse(node) {
346 if (!is_col(node)) return;
348 if (node.className=='col_active')
349 node.className='exp_active'
351 node.className='exp';
353 setSubClass(node,'sub');
354 // getsub(node).className='sub';
358 function setSubClass(node,name) {
360 if (sub==null) return;
364 function toggle(target) {
365 if (!is_list_node(target)) return;
366 if (is_col(target)) {
367 target.className='exp';
368 setSubClass(target,'sub');
369 // getsub(target).className='sub';
371 else if (is_exp(target)) {
372 target.className='col';
373 setSubClass(target,'subexp');
374 // getsub(target).className='subexp';
379 function expandAll(node) {
380 if (node.className=='exp') {
381 node.className='col';
382 setSubClass(node,'subexp');
383 // getsub(node).className='subexp';
386 if (node.childNodes!=null)
387 // if (node.hasChildNodes())
388 for ( i = 0; i<node.childNodes.length; i++)
389 expandAll(node.childNodes[i]);
392 function collapseAll(node) {
393 if (node.className=='col') {
394 node.className='exp';
395 setSubClass(node,'sub');
396 // getsub(node).className='sub';
399 if (node.childNodes!=null)
400 // for opera if (node.hasChildNodes())
401 for ( i = 0; i<node.childNodes.length; i++)
402 collapseAll(node.childNodes[i]);
407 function unFocus(node) {
408 // unfocuses potential link that is to be hidden (if a==null there is no link so it should not be blurred).
409 // tested with mozilla 1.7, 12.7.2004. /mn (
410 intemp=parent_listnode(node);
411 a = get_link(intemp); // added 6.4. to get keyboard working with
412 // moved before collapse to prevent an error message with IE when readonly==true
413 if (a!=null) a.blur(); // netscape after collapsing a focused node
417 // mode: 0==keypress, 1==keyup
418 function keyfunc(evt,mode) {
419 var c = get_keycode(evt);
423 if (lastnode==null) {
424 listnodes = document.getElementsByTagName('li');
425 lastnode=listnodes[1];
430 if (checkup(mode,c)) { // i
431 temp=prev_sibling_listnode(lastnode);
433 else if (checkdn(mode,c)) { // k
434 temp=next_sibling_listnode(lastnode);
436 else if (checkr(mode,c)) { // l
438 // temp=next_child_listnode(lastnode);
440 a = get_link(lastnode);
441 if (a!=null) a.focus(); else self.focus();
444 else if (checkl(mode,c)) { // j
445 if (is_col(lastnode)) {
450 temp=unFocus(lastnode);
453 // if (temp==null) lastnode.focus(); // forces focus to correct div (try mozilla typesearch) (doesn't seem to work -mn/6.4.2004)
456 if (temp!=null) set_lastnode(temp);
458 // alert('pressed ' + String.fromCharCode(c) + '(' + c + ')');
463 function keytest (evt) {
464 return keyfunc(evt,1);
468 function presstest (evt) {
469 return keyfunc(evt,0);
473 document.onclick = onClickHandler;
474 document.onkeypress = presstest;
475 document.onkeyup = keytest;