Subversion Repositories wimsdev

Rev

Rev 7670 | Go to most recent revision | 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.  */
  15. function popup(mylink, windowname){
  16.   if (! window.focus)return true;
  17.   var href;
  18.   if (typeof(mylink) == 'string')
  19.     href=mylink;
  20.   else
  21.     href=mylink.href;
  22.   window.open(href, windowname, 'width=800,height=500,scrollbars=yes');
  23.   return false;
  24. }
  25.  
  26. function autoInit_trees() {
  27.         var candidates = document.getElementsByTagName('ul');
  28.         for(var i=0;i<candidates.length;i++) {
  29.                 if(candidates[i].className && candidates[i].className.indexOf('tree') != -1) {
  30.                         initTree(candidates[i]);
  31.                         candidates[i].className = candidates[i].className.replace(/ ?unformatted ?/, ' ');
  32.                 }
  33.         }
  34. }
  35.  
  36. /*
  37.  * Initialise a tree node, converting all its LIs appropriately
  38.  */
  39. function initTree(el) {
  40.         var i,j;
  41.         var spanA, spanB, spanC;
  42.         var startingPoint, stoppingPoint, childUL;
  43.        
  44.         // Find all LIs to process
  45.         for(i=0;i<el.childNodes.length;i++) {
  46.                 if(el.childNodes[i].tagName && el.childNodes[i].tagName.toLowerCase() == 'li') {
  47.                         var li = el.childNodes[i];
  48.  
  49.                         // Create our extra spans
  50.                         spanA = document.createElement('span');
  51.                         spanB = document.createElement('span');
  52.                         spanC = document.createElement('span');
  53.                         spanA.appendChild(spanB);
  54.                         spanB.appendChild(spanC);
  55.                         spanA.className = 'a ' + li.className.replace('closed','spanClosed');
  56.                         spanA.onMouseOver = function() {}
  57.                         spanB.className = 'b';
  58.                         spanB.onclick = treeToggle;
  59.                         spanC.className = 'c';
  60.                        
  61.                        
  62.                         // Find the UL within the LI, if it exists
  63.                         stoppingPoint = li.childNodes.length;
  64.                         startingPoint = 0;
  65.                         childUL = null;
  66.                         for(j=0;j<li.childNodes.length;j++) {
  67.                                 if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'div') {
  68.                                         startingPoint = j + 1;
  69.                                         continue;
  70.                                 }
  71.  
  72.                                 if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'ul') {
  73.                                         childUL = li.childNodes[j];
  74.                                         stoppingPoint = j;
  75.                                         break;                                 
  76.                                 }
  77.                         }
  78.                                
  79.                         // Move all the nodes up until that point into spanC
  80.                         for(j=startingPoint;j<stoppingPoint;j++) {
  81.                                 spanC.appendChild(li.childNodes[startingPoint]);
  82.                         }
  83.                        
  84.                         // Insert the outermost extra span into the tree
  85.                         if(li.childNodes.length > startingPoint) li.insertBefore(spanA, li.childNodes[startingPoint]);
  86.                         else li.appendChild(spanA);
  87.                        
  88.                         // Process the children
  89.                         if(childUL != null) {
  90.                                 if(initTree(childUL)) {
  91.                                         addClass(li, 'children', 'closed');
  92.                                         addClass(spanA, 'children', 'spanClosed');
  93.                                 }
  94.                         }
  95.                 }
  96.         }
  97.        
  98.         if(li) {
  99.                 // li and spanA will still be set to the last item
  100.  
  101.                 addClass(li, 'last', 'closed');
  102.                 addClass(spanA, 'last', 'spanClosed');
  103.                 return true;
  104.         } else {
  105.                 return false;
  106.         }
  107.                
  108. }
  109.  
  110.  
  111. /*
  112.  * +/- toggle the tree, where el is the <span class="b"> node
  113.  * force, will force it to "open" or "close"
  114.  */
  115. function treeToggle(el, force) {
  116.         el = this;
  117.         while(el != null && (!el.tagName || el.tagName.toLowerCase() != "li")) el = el.parentNode;
  118.        
  119.         // Get UL within the LI
  120.         var childSet = findChildWithTag(el, 'ul');
  121.         var topSpan = findChildWithTag(el, 'span');
  122.  
  123.         if( force != null ){
  124.                
  125.                 if( force == "open"){
  126.                         treeOpen( topSpan, el )
  127.                 }
  128.                 else if( force == "close" ){
  129.                         treeClose( topSpan, el )
  130.                 }
  131.                
  132.         }
  133.        
  134.         else if( childSet != null) {
  135.                 // Is open, close it
  136.                 if(!el.className.match(/(^| )closed($| )/)) {          
  137.                         treeClose( topSpan, el )
  138.                 // Is closed, open it
  139.                 } else {                       
  140.                         treeOpen( topSpan, el )
  141.                 }
  142.         }
  143. }
  144.  
  145.  
  146. function treeOpen(a, b ){
  147.         removeClass(a,'spanClosed');
  148.         removeClass(b,'closed');
  149. }
  150.  
  151. /*
  152.  * [Jquery] treeToggleAll : Open/close all tree inside "elem"
  153.  * elem can be a css id (#ident) or a css class (.classname)
  154.  * added by obado
  155.  */
  156. function treeToggleAll( elem ){
  157.         $(elem+" .children").toggleClass("closed");
  158. }
  159.        
  160.        
  161. function treeClose(a, b ){
  162.         addClass(a,'spanClosed');
  163.         addClass(b,'closed');
  164. }
  165.  
  166. /*
  167.  * Find the a child of el of type tag
  168.  */
  169. function findChildWithTag(el, tag) {
  170.         for(var i=0;i<el.childNodes.length;i++) {
  171.                 if(el.childNodes[i].tagName != null && el.childNodes[i].tagName.toLowerCase() == tag) return el.childNodes[i];
  172.         }
  173.         return null;
  174. }
  175.  
  176. /*
  177.  * Functions to add and remove class names
  178.  * Mac IE hates unnecessary spaces
  179.  */
  180. function addClass(el, cls, forceBefore) {
  181.         if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) {
  182.                 el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore);
  183.  
  184.         } else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) {
  185.                 el.className += ' ' + cls;
  186.                 el.className = el.className.replace(/(^ +)|( +$)/g, '');
  187.         }
  188. }
  189. function removeClass(el, cls) {
  190.         var old = el.className;
  191.         var newCls = ' ' + el.className + ' ';
  192.         newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' ');
  193.         el.className = newCls.replace(/(^ +)|( +$)/g, '');
  194. }
  195.  
  196. /*
  197.  * Handlers for automated loading
  198.  */
  199.  _LOADERS = Array();
  200.  
  201. function callAllLoaders() {
  202.         var i, loaderFunc;
  203.         for(i=0;i<_LOADERS.length;i++) {
  204.                 loaderFunc = _LOADERS[i];
  205.                 if(loaderFunc != callAllLoaders) loaderFunc();
  206.         }
  207. }
  208.  
  209. function appendLoader(loaderFunc) {
  210.         if(window.onload && window.onload != callAllLoaders)
  211.                 _LOADERS[_LOADERS.length] = window.onload;
  212.  
  213.         window.onload = callAllLoaders;
  214.  
  215.         _LOADERS[_LOADERS.length] = loaderFunc;
  216. }
  217.  
  218. appendLoader(autoInit_trees);
  219.