marktree.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* MarkTree JavaScript code
  2. *
  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/
  7. *
  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
  11. * License.
  12. *
  13. * Miika Nurminen, 12.7.2004.
  14. */
  15. /* cross-browser (tested with ie5, mozilla 1 and opera 5) keypress detection */
  16. function get_keycode(evt) {
  17. // IE
  18. code = document.layers ? evt.which
  19. : document.all ? event.keyCode // event.keyCode!=evt.keyCode!
  20. : evt.keyCode;
  21. if (code==0)
  22. code=evt.which; // for NS
  23. return code;
  24. }
  25. var lastnode=null;
  26. var listnodes = null;
  27. var list_index=1;
  28. var lastnodetype=''; // determines if node is a link, input or text;
  29. // up, left, down, right, keypress codes
  30. //ijkl
  31. //var keys = new Array(105,106,107,108);
  32. //num arrows
  33. //var keys = new Array(56,52,50,54);
  34. //wasd
  35. // var press2 = new Array(119,97,115,100);
  36. var press = new Array(47,45,42,43);
  37. // keydown codes
  38. // var keys2=new Array(87,65,83,68);
  39. var keys= new Array(38,37,40,39);
  40. // keyset 1 = keydown, otherwise press
  41. function checkup(keyset,n) {
  42. if (keyset==1) return (n==keys[0]);
  43. return ((n==press[0]) /*|| (n==press2[0])*/)
  44. }
  45. function checkdn(keyset,n) {
  46. if (keyset==1) return (n==keys[2]);
  47. return ((n==press[2]) /*|| (n==press2[2])*/)
  48. }
  49. function checkl(keyset,n) {
  50. if (keyset==1) return (n==keys[1]);
  51. return ((n==press[1]) /*|| (n==press2[1])*/)
  52. }
  53. function checkr(keyset,n) {
  54. if (keyset==1) return (n==keys[3]);
  55. return ((n==press[3]) /*|| (n==press2[3])*/)
  56. }
  57. function is_exp(n) {
  58. if (n==null) return false;
  59. return ((n.className=='exp') || (n.className=='exp_active'));
  60. }
  61. function is_col(n) {
  62. if (n==null) return false;
  63. return ((n.className=='col') || (n.className=='col_active'));
  64. }
  65. function is_basic(n) {
  66. if (n==null) return false;
  67. return ((n.className=='basic') || (n.className=='basic_active'));
  68. }
  69. /* returns i>=0 if true */
  70. function is_active(node) {
  71. if (node.className==null) return false
  72. return node.className.indexOf('_active');
  73. }
  74. function toggle_class(node) {
  75. if ((node==null) || (node.className==null)) return;
  76. str=node.className;
  77. result="";
  78. i = str.indexOf('_active');
  79. if (i>0)
  80. result= str.substr(0,i);
  81. else
  82. result= str+"_active";
  83. node.className=result;
  84. return node;
  85. }
  86. function activate(node) {
  87. node.style.backgroundColor='#eeeeff';
  88. }
  89. function deactivate(node) {
  90. node.style.backgroundColor='#ffffff';
  91. }
  92. function is_list_node(n) {
  93. if (n==null) return false;
  94. if (n.className==null) return false;
  95. if ( (is_exp(n)) ||
  96. (is_col(n)) ||
  97. (is_basic(n)) )
  98. return true; else return false;
  99. }
  100. function get_href(n) {
  101. alist=n.attributes;
  102. if (alist!=null) {
  103. hr = alist.getNamedItem('href');
  104. if (hr!=null) return hr.nodeValue;
  105. }
  106. if (n.childNodes.length==0) return '';
  107. for (var i=0; i<n.childNodes.length; i++) {
  108. s = get_href(n.childNodes[i]);
  109. if (s!='') return s;
  110. }
  111. return '';
  112. }
  113. function get_link(n) {
  114. if (n==null) return null;
  115. if (n.style==null) return null;
  116. // disabling uncontrolled recursion to prevent error messages on IE
  117. // when trying to focus to invisible links (readonly mode)
  118. // alert(n.nodeName+' '+n.className);
  119. if ((n.nodeName=='UL') && (n.className=='sub')) return null;
  120. if (n.nodeName=='A') return n;
  121. if (n.childNodes.length==0) return null;
  122. for (var i=0; i<n.childNodes.length; i++) {
  123. s = get_link(n.childNodes[i]);
  124. if (s!=null) return s;
  125. }
  126. return null;
  127. }
  128. function set_lastnode(n) {
  129. /*var d = new Date();
  130. var t_mil = d.getMilliseconds();*/
  131. // testattu nopeuksia explorerilla, ei merkittäviä eroja
  132. if (lastnode==n) return;
  133. /* deactivate(lastnode)
  134. lastnode=n;
  135. activate(lastnode);*/
  136. if (is_active(lastnode)>=0)
  137. toggle_class(lastnode);
  138. lastnode=n;
  139. if (!(is_active(lastnode)>=0))
  140. toggle_class(lastnode);
  141. /*var d2 = new Date();
  142. var t_mil2 = d2.getMilliseconds();
  143. window.alert(t_mil2-t_mil);*/
  144. }
  145. function next_list_node() {
  146. tempIndex = list_index;
  147. while (tempIndex<listnodes.length-1) {
  148. tempIndex++;
  149. var x = listnodes[tempIndex];
  150. if (is_list_node(x)) {
  151. list_index=tempIndex;
  152. return;
  153. }
  154. }
  155. }
  156. function prev_list_node() {
  157. tempIndex = list_index;
  158. while (tempIndex>0) {
  159. tempIndex--;
  160. var x = listnodes[tempIndex];
  161. if (is_list_node(x)) {
  162. list_index=tempIndex;
  163. return;
  164. }
  165. }
  166. }
  167. function getsub (li) {
  168. if (li.childNodes.length==0) return null;
  169. for (var c = 0; c < li.childNodes.length; c++)
  170. if ( (li.childNodes[c].className == 'sub') || (li.childNodes[c].className == 'subexp') )
  171. return li.childNodes[c];
  172. }
  173. function find_listnode_recursive (li) {
  174. if (is_list_node(li)) return li;
  175. if (li.childNodes.length==0) return null;
  176. result=null;
  177. for (var c = 0; c < li.childNodes.length; c++) {
  178. result=find_listnode_recursive(li.childNodes[c]);
  179. if (result!=null) return result;
  180. }
  181. return null;
  182. }
  183. function next_child_listnode(li) {
  184. var result=null;
  185. for (var i=0; i<li.childNodes.length; i++) {
  186. result=find_listnode_recursive(li.childNodes[i]);
  187. if (result!=null) return result;
  188. }
  189. return null;
  190. }
  191. function next_actual_sibling_listnode(li) {
  192. if (li==null) return null;
  193. var temp=li;
  194. while (1) {
  195. var n = temp.nextSibling;
  196. if (n==null) {
  197. n=parent_listnode(temp);
  198. return next_actual_sibling_listnode(n);
  199. }
  200. if (is_list_node(n)) return n;
  201. temp=n;
  202. }
  203. }
  204. function next_sibling_listnode(li) {
  205. if (li==null) return null;
  206. var result=null;
  207. var temp=li;
  208. if (is_col(temp)) return next_child_listnode(temp);
  209. while (1) {
  210. var n = temp.nextSibling;
  211. if (n==null) {
  212. n=parent_listnode(temp);
  213. return next_actual_sibling_listnode(n);
  214. }
  215. if (is_list_node(n)) return n;
  216. temp=n;
  217. }
  218. }
  219. function last_sibling_listnode(li) {
  220. if (li==null) return null;
  221. var temp=li;
  222. var last=null;
  223. while(1) {
  224. var n = temp.nextSibling;
  225. if (is_list_node(temp))
  226. last = temp;
  227. if (n==null) {
  228. if (is_col(last)) return last_sibling_listnode(next_child_listnode(last));
  229. else return last;
  230. }
  231. temp = n;
  232. }
  233. }
  234. function prev_sibling_listnode(li) {
  235. if (li==null) return null;
  236. var temp=li;
  237. var n = null;
  238. while (1) {
  239. n = temp.previousSibling;
  240. if (n==null) {
  241. return parent_listnode(li);
  242. }
  243. if (is_list_node(n)) {
  244. if (is_col(n)) {
  245. return last_sibling_listnode(next_child_listnode(n));
  246. }
  247. else {
  248. return n;
  249. }
  250. }
  251. temp=n;
  252. }
  253. }
  254. function parent_listnode(li) {
  255. // added 12.7.2004 to prevent IE error when readonly mode==true
  256. if (li==null) return null;
  257. n=li;
  258. while (1) {
  259. n=n.parentNode;
  260. if (n==null) return null;
  261. if (is_list_node(n)) return n;
  262. }
  263. }
  264. function getVisibleParents(id) {
  265. var n = document.getElementById(id);
  266. while(1) {
  267. expand(n);
  268. n = parent_listnode(n);
  269. if (n==null) return;
  270. }
  271. }
  272. function onClickHandler (evt) {
  273. if (lastnode==null)
  274. {
  275. listnodes = document.getElementsByTagName('li');
  276. lastnode=listnodes[1];
  277. temp=listnodes[1];
  278. }
  279. var target = evt ? evt.target : event.srcElement;
  280. if (!is_list_node(target)) return;
  281. toggle(target);
  282. set_lastnode(target);
  283. }
  284. function expand(node) {
  285. if (!is_exp(node)) return;
  286. if (node.className=='exp_active')
  287. node.className='col_active';
  288. else
  289. node.className='col';
  290. setSubClass(node,'subexp');
  291. // getsub(node).className='subexp';
  292. }
  293. function collapse(node) {
  294. if (!is_col(node)) return;
  295. if (node.className=='col_active')
  296. node.className='exp_active'
  297. else
  298. node.className='exp';
  299. setSubClass(node,'sub');
  300. // getsub(node).className='sub';
  301. }
  302. function setSubClass(node,name) {
  303. sub = getsub(node);
  304. if (sub==null) return;
  305. sub.className=name;
  306. }
  307. function toggle(target) {
  308. if (!is_list_node(target)) return;
  309. if (is_col(target)) {
  310. target.className='exp';
  311. setSubClass(target,'sub');
  312. // getsub(target).className='sub';
  313. }
  314. else if (is_exp(target)) {
  315. target.className='col';
  316. setSubClass(target,'subexp');
  317. // getsub(target).className='subexp';
  318. }
  319. }
  320. function expandAll(node) {
  321. if (node.className=='exp') {
  322. node.className='col';
  323. setSubClass(node,'subexp');
  324. // getsub(node).className='subexp';
  325. }
  326. var i;
  327. if (node.childNodes!=null)
  328. // if (node.hasChildNodes())
  329. for ( i = 0; i<node.childNodes.length; i++)
  330. expandAll(node.childNodes[i]);
  331. }
  332. function collapseAll(node) {
  333. if (node.className=='col') {
  334. node.className='exp';
  335. setSubClass(node,'sub');
  336. // getsub(node).className='sub';
  337. }
  338. var i;
  339. if (node.childNodes!=null)
  340. // for opera if (node.hasChildNodes())
  341. for ( i = 0; i<node.childNodes.length; i++)
  342. collapseAll(node.childNodes[i]);
  343. }
  344. function unFocus(node) {
  345. // unfocuses potential link that is to be hidden (if a==null there is no link so it should not be blurred).
  346. // tested with mozilla 1.7, 12.7.2004. /mn (
  347. intemp=parent_listnode(node);
  348. a = get_link(intemp); // added 6.4. to get keyboard working with
  349. // moved before collapse to prevent an error message with IE when readonly==true
  350. if (a!=null) a.blur(); // netscape after collapsing a focused node
  351. return intemp;
  352. }
  353. // mode: 0==keypress, 1==keyup
  354. function keyfunc(evt,mode) {
  355. var c = get_keycode(evt);
  356. var temp = null;
  357. var a = null;
  358. if (lastnode==null) {
  359. listnodes = document.getElementsByTagName('li');
  360. lastnode=listnodes[1];
  361. temp=listnodes[1];
  362. }
  363. //window.alert(c);
  364. if (checkup(mode,c)) { // i
  365. temp=prev_sibling_listnode(lastnode);
  366. }
  367. else if (checkdn(mode,c)) { // k
  368. temp=next_sibling_listnode(lastnode);
  369. }
  370. else if (checkr(mode,c)) { // l
  371. expand(lastnode);
  372. // temp=next_child_listnode(lastnode);
  373. // if (temp==null) {
  374. a = get_link(lastnode);
  375. if (a!=null) a.focus(); else self.focus();
  376. //}
  377. }
  378. else if (checkl(mode,c)) { // j
  379. if (is_col(lastnode)) {
  380. unFocus(lastnode);
  381. collapse(lastnode);
  382. }
  383. else {
  384. temp=unFocus(lastnode);
  385. collapse(temp);
  386. }
  387. // if (temp==null) lastnode.focus(); // forces focus to correct div (try mozilla typesearch) (doesn't seem to work -mn/6.4.2004)
  388. }
  389. else return;
  390. if (temp!=null) set_lastnode(temp);
  391. // alert('pressed ' + String.fromCharCode(c) + '(' + c + ')');
  392. return true;
  393. }
  394. function keytest (evt) {
  395. return keyfunc(evt,1);
  396. };
  397. function presstest (evt) {
  398. return keyfunc(evt,0);
  399. };
  400. document.onclick = onClickHandler;
  401. document.onkeypress = presstest;
  402. document.onkeyup = keytest;