added custom images for root nodes
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 11 Nov 2005 22:37:57 +0000 (22:37 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 11 Nov 2005 22:37:57 +0000 (22:37 +0000)
added ability to add nodes without parent nodes present (needs to be smarter)

git-svn-id: svn://svn.open-ils.org/ILS/trunk@2010 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/opac/common/js/slimtree.js

index 6efc328..6c301ad 100644 (file)
@@ -11,74 +11,122 @@ var stimgblank     = elem('img',{src:stpicblank,border:0});
 var stimgline  = elem('img',{src:stpicline,border:0});
 var stimgjoin  = elem('img',{src:stpicjoin,border:0, style:'display:inline;'});
 
-function SlimTree(context) { this.context = context; }
+function _apc(root,node) { root.appendChild(node); }
 
-SlimTree.prototype.addNode = function( id, pid, name, action ) {
+function SlimTree(context, handle, rootimg) { 
+       this.context    = context; 
+       this.handle             = handle;
+       this.cache              = new Object();
+       if(rootimg) 
+               this.rootimg = elem('img', {src:rootimg,border:0});
+}
+
+SlimTree.prototype.cacheMe = function( id, pid, name, action, title ) {
+       if(this.cache[id]) return;
+       this.cache[id]                          = {};
+       this.cache[id].pid              = pid
+       this.cache[id].name             = name
+       this.cache[id].action   = action
+       this.cache[id].title            = title
+}
+
+SlimTree.prototype.flushCache = function() {
+       for( var c in this.cache ) {
+               var obj = this.cache[c];
+               if(obj && getId(obj.pid)) {
+                       this.cache[c] = null;
+                       this.addNode(c,obj.pid, obj.name, obj.action,obj.title);
+               }
+       }
+}
+
+SlimTree.prototype.addNode = function( id, pid, name, action, title ) {
+
+       if( pid != -1 && !getId(pid)) {
+               if(this.cache[pid]) {
+                       var obj = this.cache[pid];
+                       this.addNode(pid, obj.pid,obj.name, obj.action,obj.title );
+                       this.cache[pid] = null;
+               } else {
+                       this.cacheMe(id, pid, name, action, title);
+                       return;
+               }
+       }
 
        var div                 = elem('div',{id:id});
        var topdiv              = elem('div',{style:'vertical-align:middle'});
        var link                        = elem('a', {id:'stlink_' + id}); 
        var actionref   = elem('a',{href:action}, name);
        var contdiv             = elem('div',{id:'stcont_' + id});
+       if(action) actionref.setAttribute('href',action);
+       if(title) actionref.setAttribute('title',title);
+       else actionref.setAttribute('title',name);
 
-       topdiv.appendChild(link);
-       topdiv.appendChild(actionref);
-       div.appendChild(topdiv);
-       div.appendChild(contdiv);
+       _apc(topdiv,link);
+       _apc(topdiv,actionref);
+       _apc(div,topdiv);
+       _apc(div,contdiv);
 
        if( pid == -1 ) { 
                this.rootid = id;
-               this.context.appendChild(div);
-               link.appendChild(stimgblank.cloneNode(true));
+               _apc(this.context,div);
+               if(this.rootimg) _apc(link,this.rootimg.cloneNode(true));
+               else _apc(link,stimgblank.cloneNode(true));
        } else {
-               if(pid == this.rootid) stOpen(pid);
-               else stClose(pid);
+               if(pid == this.rootid) this.open(pid);
+               else this.close(pid);
                getId(pid).setAttribute('haschild','1');
-               link.appendChild(stimgblank.cloneNode(true));
+               _apc(link,stimgblank.cloneNode(true));
                div.style.paddingLeft = '18px';
                div.style.backgroundImage = 'url('+stpicjoinb+')';
                div.style.backgroundRepeat = 'no-repeat';
-               getId('stcont_' + pid).appendChild(div);
-               if (div.previousSibling) {
-                       div.previousSibling.firstChild.appendChild(stimgjoin.cloneNode(true));
-                       div.previousSibling.firstChild.appendChild(div.previousSibling.firstChild.firstChild);
-                       div.previousSibling.firstChild.appendChild(div.previousSibling.firstChild.firstChild);
-                       div.previousSibling.firstChild.firstChild.style.marginLeft = '-18px';
-
-                       div.previousSibling.style.backgroundImage = 'url('+stpicline+')';
-                       div.previousSibling.style.backgroundRepeat = 'repeat-y';
-               }
+               _apc(getId('stcont_' + pid), div);
+               if (div.previousSibling) stMakePaths(div);
        }
+       this.flushCache();
 }
 
-SlimTree.prototype.expandAll = function() { stFlex(this.rootid, 'open'); }
-SlimTree.prototype.closeAll = function() { stFlex(this.rootid,'close', this.rootid); }
-function stFlex(id, type, root) {
-       if(type=='open') stOpen(id);
-       else { if (id != root) stClose(id); }
+function stMakePaths(div) {
+       _apc(div.previousSibling.firstChild,stimgjoin.cloneNode(true));
+       _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
+       _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
+       div.previousSibling.firstChild.firstChild.style.marginLeft = '-18px';
+       div.previousSibling.style.backgroundImage = 'url('+stpicline+')';
+       div.previousSibling.style.backgroundRepeat = 'repeat-y';
+}
+
+SlimTree.prototype.expandAll = function() { this.flex(this.rootid, 'open'); }
+SlimTree.prototype.closeAll = function() { this.flex(this.rootid,'close'); }
+SlimTree.prototype.flex = function(id, type) {
+       if(type=='open') this.open(id);
+       else { if (id != this.rootid) this.close(id); }
        var n = getId('stcont_' + id);
        for( var c = 0; c != n.childNodes.length; c++ ) {
                var ch = n.childNodes[c];
                if(ch.nodeName.toLowerCase() == 'div') {
                        if(getId(ch.id).getAttribute('haschild') == '1') 
-                               stFlex(ch.id, type);
+                               this.flex(ch.id, type);
                }
        }
 }
 
-function stOpen( id ) {
+SlimTree.prototype.open = function(id) {
        var link = getId('stlink_' + id);
-       removeChildren(link);
-       link.appendChild(stimgclose.cloneNode(true));
-       link.setAttribute('href','javascript:stClose("'+id+'");');
+       if(id != this.rootid || !this.rootimg) {
+               removeChildren(link);
+               _apc(link,stimgclose.cloneNode(true));
+       }
+       link.setAttribute('href','javascript:' + this.handle + '.close("'+id+'");');
        unHideMe(getId('stcont_' + id));
 }
 
-function stClose( id ) {
+SlimTree.prototype.close = function(id) {
        var link = getId('stlink_' + id);
-       removeChildren(link);
-       link.appendChild(stimgopen.cloneNode(true));
-       link.setAttribute('href','javascript:stOpen("'+id+'");');
+       if(id != this.rootid || !this.rootimg) {
+               removeChildren(link);
+               _apc(link,stimgopen.cloneNode(true));
+       }
+       link.setAttribute('href','javascript:' + this.handle + '.open("'+id+'");');
        hideMe(getId('stcont_' + id));
 }