Subversion Repositories wimsdev

Rev

Rev 8588 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Content-seperated javascript tree widget
  3.  * Copyright (C) 2005 SilverStripe Limited
  4.  * Feel free to use this on your websites, but please leave this message in the fies
  5.  * http://www.silverstripe.com/blog
  6. */
  7.  
  8. /*
  9.  * Initialise all trees identified by <ul class="tree">
  10.  */
  11.  
  12. /**
  13.  * Usefull function for popup window displaying
  14.  * @param {*} mylink
  15.  * @param {*} windowname
  16.  * @returns
  17.  */
  18. function popup(mylink, windowname){
  19.   if (! window.focus)return true;
  20.   var href;
  21.   if (typeof(mylink) == 'string')
  22.     href=mylink;
  23.   else
  24.     href=mylink.href;
  25.   window.open(href, windowname, 'width=800,height=500,scrollbars=yes');
  26.   return false;
  27. }
  28.  
  29. /**
  30.  * autoInit_trees
  31.  */
  32. function autoInit_trees() {
  33.   //console.log("autoInit_trees");
  34.   var candidates = document.getElementsByTagName('ul');
  35.   for(var i=0;i<candidates.length;i++) {
  36.     if(candidates[i].className && candidates[i].className.indexOf('tree') != -1) {
  37.       initTree(candidates[i]);
  38.       candidates[i].className = candidates[i].className.replace(/ ?unformatted ?/, ' ');
  39.     }
  40.   }
  41. }
  42.  
  43. /**
  44.  * Initialise a tree node, converting all its LIs appropriately
  45.  * @param {*} el
  46.  * @returns
  47.  */
  48. function initTree(el) {
  49.   //console.log("initTree");
  50.   var i,j;
  51.   var spanA, spanB, spanC;
  52.   var startingPoint, stoppingPoint, childUL;
  53.  
  54.   // Find all LIs to process
  55.   for(i=0;i<el.childNodes.length;i++) {
  56.     if(el.childNodes[i].tagName && el.childNodes[i].tagName.toLowerCase() == 'li') {
  57.       var li = el.childNodes[i];
  58.  
  59.       // Create our extra spans
  60.       spanA = document.createElement('span');
  61.       spanB = document.createElement('span');
  62.       spanC = document.createElement('span');
  63.       spanA.appendChild(spanB);
  64.       spanB.appendChild(spanC);
  65.       spanA.className = 'a ' + li.className.replace('closed','spanClosed');
  66.       spanA.onMouseOver = function() {}
  67.       spanB.className = 'b';
  68.       spanB.onclick = treeToggle;
  69.       spanC.className = 'c';
  70.  
  71.       // Find the UL within the LI, if it exists
  72.       stoppingPoint = li.childNodes.length;
  73.       startingPoint = 0;
  74.       childUL = null;
  75.       for(j=0;j<li.childNodes.length;j++) {
  76.         if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'div') {
  77.           startingPoint = j + 1;
  78.           continue;
  79.         }
  80.  
  81.         if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'ul') {
  82.           childUL = li.childNodes[j];
  83.           stoppingPoint = j;
  84.           break;
  85.         }
  86.       }
  87.  
  88.       // Move all the nodes up until that point into spanC
  89.       for(j=startingPoint;j<stoppingPoint;j++) {
  90.         spanC.appendChild(li.childNodes[startingPoint]);
  91.       }
  92.  
  93.       // Insert the outermost extra span into the tree
  94.       if(li.childNodes.length > startingPoint) {
  95.         li.insertBefore(spanA, li.childNodes[startingPoint]);
  96.       } else {
  97.         li.appendChild(spanA);
  98.       }
  99.  
  100.       // Process the children
  101.       if(childUL != null) {
  102.         if(initTree(childUL)) {
  103.           li.classList.add('children');
  104.           spanA.classList.add('children');
  105.         }
  106.       }
  107.     }
  108.   }
  109.  
  110.   if(li) {
  111.     // li and spanA will still be set to the last item
  112.     li.classList.add('last');
  113.     spanA.classList.add('last');
  114.     return true;
  115.   } else {
  116.     return false;
  117.   }
  118. }
  119.  
  120. /**
  121.  * +/- toggle the tree
  122.  *
  123.  * @param {*} el the <span class="b"> node
  124.  * @param {*} force will force it to "open" or "close"
  125.  */
  126. function treeToggle(el, force) {
  127.   //console.log("treeToggle");
  128.   el = this;
  129.   while(el != null && (!el.tagName || el.tagName.toLowerCase() != "li")) el = el.parentNode;
  130.  
  131.   // Get UL within the LI
  132.   var childSet = findChildWithTag(el, 'ul');
  133.   var topSpan = findChildWithTag(el, 'span');
  134.  
  135.   if( force != null ){
  136.     if( force == "open"){
  137.       treeOpen( topSpan, el )
  138.     } else if( force == "close" ){
  139.       treeClose( topSpan, el )
  140.     }
  141.   } else if( childSet != null) {
  142.     // Is open, close it
  143.     if(!el.className.match(/(^| )closed($| )/)) {
  144.       treeClose( topSpan, el )
  145.     // Is closed, open it
  146.     } else {
  147.       treeOpen( topSpan, el )
  148.     }
  149.   }
  150. }
  151.  
  152. /**
  153.  * treeOpen
  154.  * @param {*} a
  155.  * @param {*} b
  156.  */
  157. function treeOpen(a, b ){
  158.   //console.log("treeOpen");
  159.   a.classList.remove('spanClosed');
  160.   b.classList.remove('closed');
  161. }
  162.  
  163. /**
  164.  *  * [Jquery] treeToggleAll : Open/close all tree inside "elem"
  165.  * elem can be a css id (#ident) or a css class (.classname)
  166.  * added by obado
  167.  * @param {*} elem
  168.  */
  169. function treeToggleAll( elem ){
  170.   //console.log("treeToggleAll");
  171.   //$(elem+" .children").toggleClass("closed");
  172.   if ($(elem).hasClass("all_open")){
  173.     $(elem+" .children").addClass("closed");
  174.     $(elem).removeClass("all_open");
  175.   }
  176.   else{
  177.     $(elem+" .children").removeClass("closed");
  178.     $(elem).addClass("all_open");
  179.   }
  180. }
  181.  
  182. /**
  183.  * treeClose
  184.  * @param {*} a
  185.  * @param {*} b
  186.  */
  187. function treeClose(a, b ){
  188.   //console.log("treeClose");
  189.   a.classList.add('spanClosed');
  190.   b.classList.add('closed');
  191. }
  192.  
  193. /**
  194.  * Find the a child of el of type tag
  195.  * @param {*} el
  196.  * @param {*} tag
  197.  * @returns
  198.  */
  199. function findChildWithTag(el, tag) {
  200.   //console.log("findChildWithTag");
  201.   for(var i=0;i<el.childNodes.length;i++) {
  202.     if(el.childNodes[i].tagName != null && el.childNodes[i].tagName.toLowerCase() == tag) return el.childNodes[i];
  203.   }
  204.   return null;
  205. }
  206.  
  207. /**
  208.  * Functions to add and remove class names
  209.  * Mac IE hates unnecessary spaces
  210.  * @param {*} el
  211.  * @param {*} cls
  212.  * @param {*} forceBefore
  213.  */
  214. function addClass(el, cls, forceBefore) {
  215.   //console.log(`addClass ${cls}`);
  216.   if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) {
  217.     el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore);
  218.  
  219.   } else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) {
  220.     el.className += ' ' + cls;
  221.     el.className = el.className.replace(/(^ +)|( +$)/g, '');
  222.   }
  223. }
  224.  
  225. /**
  226.  * removeClass
  227.  * @param {*} el
  228.  * @param {*} cls
  229.  */
  230. function removeClass(el, cls) {
  231.   //console.log("removeClass");
  232.   var old = el.className;
  233.   var newCls = ' ' + el.className + ' ';
  234.   newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' ');
  235.   el.className = newCls.replace(/(^ +)|( +$)/g, '');
  236. }
  237.  
  238. /*
  239.  * Handlers for automated loading
  240.  */
  241. _LOADERS = Array();
  242.  
  243. /**
  244.  * callAllLoaders
  245.  */
  246. function callAllLoaders() {
  247.   var i, loaderFunc;
  248.   for(i=0;i<_LOADERS.length;i++) {
  249.     loaderFunc = _LOADERS[i];
  250.     if(loaderFunc != callAllLoaders) loaderFunc();
  251.   }
  252. }
  253.  
  254. /**
  255.  * appendLoader
  256.  * @param {*} loaderFunc
  257.  */
  258. function appendLoader(loaderFunc) {
  259.   if(window.onload && window.onload != callAllLoaders)
  260.     _LOADERS[_LOADERS.length] = window.onload;
  261.  
  262.   window.onload = callAllLoaders;
  263.  
  264.   _LOADERS[_LOADERS.length] = loaderFunc;
  265. }
  266.  
  267. appendLoader(autoInit_trees);
  268.