擴展dtree支持checkbox

擴展dtree支持checkbox

最近仔細看了一下dtree的代碼,它不支持checkbox很不爽,就自己動手加上了。修改後的代碼與原來的dtree兼容,可以直接替換原工程中的dtree。詳細示例請看附件。
Js代碼 複製代碼
  1. /*--------------------------------------------------|  
  2. | dTree 2.05 | www.destroydrop.com/javascript/tree/ |  
  3. |---------------------------------------------------|  
  4. | Copyright (c) 2002-2003 Geir Landr?               |  
  5. |                                                   |  
  6. | This script can be used freely as long as all     |  
  7. | copyright messages are intact.                    |  
  8. |                                                   |  
  9. | Updated: 17.04.2003                               |  
  10. |--------------------------------------------------*/  
  11. //modified by wallimn. 20070831   
  12. //to support checkbox object   
  13. //updated 2009-02-13   
  14. //welcome to visit my blog: http://blog.csdn.net/wallimn   
  15. //please keep these message.   
  16. // Node object   
  17. function Node(id, pid, name, url, title, target, icon, iconOpen, open) {   
  18.     this.id = id;   
  19.     this.pid = pid;   
  20.     this.name = name;   
  21.     this.url = url;   
  22.     this.title = title;   
  23.     this.target = target;   
  24.     this.icon = icon;   
  25.     this.iconOpen = iconOpen;   
  26.     this._io = open || false;   
  27.     this._is = false;   
  28.     this._ls = false;   
  29.     this._hc = false;   
  30.     this._ai = 0;   
  31.     this._p;   
  32. };   
  33. // Tree object   
  34. function dTree(objName,iconPath) {   
  35.     this.config = {   
  36.         target                  : null,   
  37.         folderLinks         : true,   
  38.         useSelection        : true,   
  39.         useCookies          : true,   
  40.         useLines                : true,   
  41.         useIcons                : true,   
  42.         useStatusText       : false,   
  43.         closeSameLevel  : false,   
  44.         inOrder                 : false,   
  45.         check:false     //added by wallimn 2009-02-05, to control whether checkbox is showed   
  46.     };   
  47.     //modified by wallimn 2009-02-05, in order to make client can modify path of icons;   
  48.     if(!iconPath)iconPath="img/";//set the default value, be compatible with dtree   
  49.     this.icon = {   
  50.             root        : iconPath+'base.gif',   
  51.             folder      : iconPath+'folder.gif',   
  52.             folderOpen  : iconPath+'folderopen.gif',   
  53.             node        : iconPath+'page.gif',   
  54.             empty       : iconPath+'empty.gif',   
  55.             line        : iconPath+'line.gif',   
  56.             join        : iconPath+'join.gif',   
  57.             joinBottom  : iconPath+'joinbottom.gif',   
  58.             plus        : iconPath+'plus.gif',   
  59.             plusBottom  : iconPath+'plusbottom.gif',   
  60.             minus       : iconPath+'minus.gif',   
  61.             minusBottom : iconPath+'minusbottom.gif',   
  62.             nlPlus      : iconPath+'nolines_plus.gif',   
  63.             nlMinus     : iconPath+'nolines_minus.gif'  
  64.         };   
  65.     //added by wallimn, to cache checkbox object and improve speed of set checked status   
  66.     this.cbCollection = new Object();   
  67.     this.obj = objName;   
  68.     this.aNodes = [];   
  69.     this.aIndent = [];   
  70.     this.root = new Node(-1);   
  71.     this.selectedNode = null;   
  72.     this.selectedFound = false;   
  73.     this.completed = false;   
  74. };   
  75.   
  76. // Adds a new node to the node array   
  77. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {   
  78.     this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);   
  79. };   
  80.   
  81. // Open/close all nodes   
  82. dTree.prototype.openAll = function() {   
  83.     this.oAll(true);   
  84. };   
  85. dTree.prototype.closeAll = function() {   
  86.     this.oAll(false);   
  87. };   
  88.   
  89. // Outputs the tree to the page   
  90. dTree.prototype.toString = function() {   
  91.     var str = '<div class="dtree">/n';   
  92.     if (document.getElementById) {   
  93.         if (this.config.useCookies) this.selectedNode = this.getSelected();   
  94.         str += this.addNode(this.root);   
  95.     } else str += 'Browser not supported.';   
  96.     str += '</div>';   
  97.     if (!this.selectedFound) this.selectedNode = null;   
  98.     this.completed = true;   
  99.     return str;   
  100. };   
  101.   
  102. // Creates the tree structure   
  103. dTree.prototype.addNode = function(pNode) {   
  104.     var str = '';   
  105.     var n=0;   
  106.     if (this.config.inOrder) n = pNode._ai;   
  107.     for (n; n<this.aNodes.length; n++) {   
  108.         if (this.aNodes[n].pid == pNode.id) {   
  109.             var cn = this.aNodes[n];   
  110.             cn._p = pNode;   
  111.             cn._ai = n;   
  112.             this.setCS(cn);   
  113.             if (!cn.target && this.config.target) cn.target = this.config.target;   
  114.             if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);   
  115.             if (!this.config.folderLinks && cn._hc) cn.url = null;   
  116.             if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {   
  117.                     cn._is = true;   
  118.                     this.selectedNode = n;   
  119.                     this.selectedFound = true;   
  120.             }   
  121.             str += this.node(cn, n);   
  122.             if (cn._ls) break;   
  123.         }   
  124.     }   
  125.     return str;   
  126. };   
  127. //get checkbox object by id(input by client)   
  128. //added by wallimn,   
  129. dTree.prototype.co=function(id){   
  130.     if (this.cbCollection[id])return this.cbCollection[id];   
  131.     for(var n=0; n<this.aNodes.length; n++){   
  132.         if(this.aNodes[n].id==id){   
  133.             this.cbCollection[id]=document.getElementById("c"+this.obj+n);   
  134.             break;   
  135.         }   
  136.     }   
  137.     return this.cbCollection[id];   
  138. };   
  139. //get the ids of the checkbox which check status is true;   
  140. //added by wallimn, updated: 2009-02-13   
  141. dTree.prototype.getCheckedNodes=function(){   
  142.     var res = new Array();   
  143.     var cko;//checkobject   
  144.     for(var n=0; n<this.aNodes.length; n++){   
  145.         //alert("c"+this.obj+n+(document.getElementById("c"+this.obj+n).checked));   
  146.         //cache the object to improve speed when you have very,very many nodes and call this function many times in one page   
  147.         //i.e. with ajax technology   
  148.         //document.getElementById("c"+this.obj+n).checked   
  149.         cko = this.co(this.aNodes[n].id);   
  150.         if(cko.checked==true){   
  151.             res[res.length]=this.aNodes[n].id;   
  152.         }   
  153.     }   
  154.     return res;   
  155. }   
  156. //added by wallimn   
  157. dTree.prototype.cc=function(nodeId){   
  158.     var cs = document.getElementById("c"+this.obj+nodeId).checked;   
  159.     var n,node = this.aNodes[nodeId];   
  160.     var len =this.aNodes.length;   
  161.     for (n=0; n<len; n++) {   
  162.         //alert(this.aNodes[n].pid+"--"+this.aNodes[n].id);   
  163.         if (this.aNodes[n].pid == node.id) {   
  164.             //if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);   
  165.             //this.aNodes[n]._io = false;   
  166.             document.getElementById("c"+this.obj+n).checked=cs;   
  167.             this.cc(n);        
  168.         }   
  169.     }   
  170.     if(cs==false)return;   
  171.     var pid=node.pid;   
  172.     var bSearch;   
  173.     do{   
  174.         bSearch=false;   
  175.         for(n=0;n<len;n++){   
  176.             if(this.aNodes[n].id==pid){   
  177.                 document.getElementById("c"+this.obj+n).checked=true;   
  178.                 pid=this.aNodes[n].pid;   
  179.                 bSearch= true;                 
  180.                 break;   
  181.             }   
  182.         }   
  183.     }while(bSearch==true)   
  184. }   
  185.   
  186. // Creates the node icon, url and text   
  187. dTree.prototype.node = function(node, nodeId) {   
  188.     var str = '<div class="dTreeNode">' + this.indent(node, nodeId);   
  189.     if (this.config.useIcons) {   
  190.         if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);   
  191.         if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;   
  192.         if (this.root.id == node.pid) {   
  193.             node.icon = this.icon.root;   
  194.             node.iconOpen = this.icon.root;   
  195.         }   
  196.         str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io)?node.iconOpen : node.icon) + '" alt="" />';   
  197.     }   
  198.     if(this.config.check==true){   
  199.         str+= '<input type="checkbox" class="cx" id="c'+  this.obj + nodeId + '" οnclick="javascript:'+this.obj+'.cc('+nodeId+')"/>';   
  200.         //alert(str);   
  201.     }   
  202.     if (node.url) {   
  203.         str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';   
  204.         if (node.title) str += ' title="' + node.title + '"';   
  205.         if (node.target) str += ' target="' + node.target + '"';   
  206.         if (this.config.useStatusText) str += ' οnmοuseοver="window.status=/'' + node.name + '/';return true;" οnmοuseοut="window.status=/'/';return true;" ';   
  207.         if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))   str += ' οnclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';   
  208.         str += '>';   
  209.     }   
  210.     else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)   
  211.         str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';   
  212.     str += node.name;   
  213.     if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';   
  214.     str += '</div>';   
  215.     if (node._hc) {   
  216.         str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';   
  217.         str += this.addNode(node);   
  218.         str += '</div>';   
  219.     }   
  220.     this.aIndent.pop();   
  221.     //alert(str);   
  222.     return str;   
  223. };   
  224.   
  225. // Adds the empty and line icons   
  226. dTree.prototype.indent = function(node, nodeId) {   
  227.     var str = '';   
  228.     if (this.root.id != node.pid) {   
  229.         for (var n=0; n<this.aIndent.length; n++)   
  230.             str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';   
  231.         (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);   
  232.         if (node._hc) {   
  233.             str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';   
  234.             if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;   
  235.             else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );   
  236.             str += '" alt="" /></a>';   
  237.         } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';   
  238.     }   
  239.     return str;   
  240. };   
  241.   
  242. // Checks if a node has any children and if it is the last sibling   
  243. dTree.prototype.setCS = function(node) {   
  244.     var lastId;   
  245.     for (var n=0; n<this.aNodes.length; n++) {   
  246.         if (this.aNodes[n].pid == node.id) node._hc = true;   
  247.         if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;   
  248.     }   
  249.     if (lastId==node.id) node._ls = true;   
  250. };   
  251.   
  252. // Returns the selected node   
  253. dTree.prototype.getSelected = function() {   
  254.     var sn = this.getCookie('cs' + this.obj);   
  255.     return (sn) ? sn : null;   
  256. };   
  257.   
  258. // Highlights the selected node   
  259. dTree.prototype.s = function(id) {   
  260.     if (!this.config.useSelection) return;   
  261.     var cn = this.aNodes[id];   
  262.     if (cn._hc && !this.config.folderLinks) return;   
  263.     if (this.selectedNode != id) {   
  264.         if (this.selectedNode || this.selectedNode==0) {   
  265.             eOld = document.getElementById("s" + this.obj + this.selectedNode);   
  266.             eOld.className = "node";   
  267.         }   
  268.         eNew = document.getElementById("s" + this.obj + id);   
  269.         eNew.className = "nodeSel";   
  270.         this.selectedNode = id;   
  271.         if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);   
  272.     }   
  273. };   
  274.   
  275. // Toggle Open or close   
  276. dTree.prototype.o = function(id) {   
  277.     var cn = this.aNodes[id];   
  278.     this.nodeStatus(!cn._io, id, cn._ls);   
  279.     cn._io = !cn._io;   
  280.     if (this.config.closeSameLevel) this.closeLevel(cn);   
  281.     if (this.config.useCookies) this.updateCookie();   
  282. };   
  283.   
  284. // Open or close all nodes   
  285. dTree.prototype.oAll = function(status) {   
  286.     for (var n=0; n<this.aNodes.length; n++) {   
  287.         if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {   
  288.             this.nodeStatus(status, n, this.aNodes[n]._ls)   
  289.             this.aNodes[n]._io = status;   
  290.         }   
  291.     }   
  292.     if (this.config.useCookies) this.updateCookie();   
  293. };   
  294.   
  295. // Opens the tree to a specific node   
  296. dTree.prototype.openTo = function(nId, bSelect, bFirst) {   
  297.     if (!bFirst) {   
  298.         for (var n=0; n<this.aNodes.length; n++) {   
  299.             if (this.aNodes[n].id == nId) {   
  300.                 nId=n;   
  301.                 break;   
  302.             }   
  303.         }   
  304.     }   
  305.     var cn=this.aNodes[nId];   
  306.     if (cn.pid==this.root.id || !cn._p) return;   
  307.     cn._io = true;   
  308.     cn._is = bSelect;   
  309.     if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);   
  310.     if (this.completed && bSelect) this.s(cn._ai);   
  311.     else if (bSelect) this._sn=cn._ai;   
  312.     this.openTo(cn._p._ai, falsetrue);   
  313. };   
  314.   
  315. // Closes all nodes on the same level as certain node   
  316. dTree.prototype.closeLevel = function(node) {   
  317.     for (var n=0; n<this.aNodes.length; n++) {   
  318.         if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {   
  319.             this.nodeStatus(false, n, this.aNodes[n]._ls);   
  320.             this.aNodes[n]._io = false;   
  321.             this.closeAllChildren(this.aNodes[n]);   
  322.         }   
  323.     }   
  324. }   
  325.   
  326. // Closes all children of a node   
  327. dTree.prototype.closeAllChildren = function(node) {   
  328.     for (var n=0; n<this.aNodes.length; n++) {   
  329.         if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {   
  330.             if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);   
  331.             this.aNodes[n]._io = false;   
  332.             this.closeAllChildren(this.aNodes[n]);         
  333.         }   
  334.     }   
  335. }   
  336.   
  337. // Change the status of a node(open or closed)   
  338. dTree.prototype.nodeStatus = function(status, id, bottom) {   
  339.     eDiv    = document.getElementById('d' + this.obj + id);   
  340.     eJoin   = document.getElementById('j' + this.obj + id);   
  341.     if (this.config.useIcons) {   
  342.         eIcon   = document.getElementById('i' + this.obj + id);   
  343.         eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;   
  344.     }   
  345.     eJoin.src = (this.config.useLines)?   
  346.     ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):   
  347.     ((status)?this.icon.nlMinus:this.icon.nlPlus);   
  348.     eDiv.style.display = (status) ? 'block''none';   
  349. };   
  350. // [Cookie] Clears a cookie   
  351. dTree.prototype.clearCookie = function() {   
  352.     var now = new Date();   
  353.     var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);   
  354.     this.setCookie('co'+this.obj, 'cookieValue', yesterday);   
  355.     this.setCookie('cs'+this.obj, 'cookieValue', yesterday);   
  356. };   
  357.   
  358. // [Cookie] Sets value in a cookie   
  359. dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure)    
  360.   
  361. {   
  362.     document.cookie =   
  363.         escape(cookieName) + '=' + escape(cookieValue)   
  364.         + (expires ? '; expires=' + expires.toGMTString() : '')   
  365.         + (path ? '; path=' + path : '')   
  366.         + (domain ? '; domain=' + domain : '')   
  367.         + (secure ? '; secure' : '');   
  368. };   
  369.   
  370. // [Cookie] Gets a value from a cookie   
  371. dTree.prototype.getCookie = function(cookieName) {   
  372.     var cookieValue = '';   
  373.     var posName = document.cookie.indexOf(escape(cookieName) + '=');   
  374.     if (posName != -1) {   
  375.         var posValue = posName + (escape(cookieName) + '=').length;   
  376.         var endPos = document.cookie.indexOf(';', posValue);   
  377.         if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));   
  378.         else cookieValue = unescape(document.cookie.substring(posValue));   
  379.     }   
  380.     return (cookieValue);   
  381. };   
  382.   
  383. // [Cookie] Returns ids of open nodes as a string   
  384. dTree.prototype.updateCookie = function() {   
  385.     var str = '';   
  386.     for (var n=0; n<this.aNodes.length; n++) {   
  387.         if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {   
  388.             if (str) str += '.';   
  389.             str += this.aNodes[n].id;   
  390.         }   
  391.     }   
  392.     this.setCookie('co' + this.obj, str);   
  393. };   
  394.   
  395. // [Cookie] Checks if a node id is in a cookie   
  396. dTree.prototype.isOpen = function(id) {   
  397.     var aOpen = this.getCookie('co' + this.obj).split('.');   
  398.     for (var n=0; n<aOpen.length; n++)   
  399.         if (aOpen[n] == id) return true;   
  400.     return false;   
  401. };   
  402.   
  403. // If Push and pop is not implemented by the browser   
  404. if (!Array.prototype.push) {   
  405.     Array.prototype.push = function array_push() {   
  406.         for(var i=0;i<arguments.length;i++)   
  407.             this[this.length]=arguments[i];   
  408.         return this.length;   
  409.     }   
  410. };   
  411. if (!Array.prototype.pop) {   
  412.     Array.prototype.pop = function array_pop() {   
  413.         lastElement = this[this.length-1];   
  414.         this.length = Math.max(this.length-1,0);   
  415.         return lastElement;   
  416.     }   
  417. };  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章