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