现在的位置: 首页 > 综合 > 正文

js – Tree of Document

2013年09月07日 ⁄ 综合 ⁄ 共 14319字 ⁄ 字号 评论关闭
 

  1. <!-- dTree.css -->
  2. .dtree {
  3.     font-familyVerdana, Geneva, ArialHelveticasans-serif;
  4.     font-size11px;
  5.     color#666;
  6.     white-spacenowrap;
  7. }
  8. .dtree img {
  9.     border0px;
  10.     vertical-alignmiddle;
  11. }
  12. .dtree a {
  13.     color#333;
  14.     text-decorationnone;
  15. }
  16. .dtree a.node, .dtree a.nodeSel {
  17.     white-spacenowrap;
  18.     padding1px 2px 1px 2px;
  19. }
  20. .dtree a.node:hover, .dtree a.nodeSel:hover {
  21.     color#333;
  22.     text-decorationunderline;
  23. }
  24. .dtree a.nodeSel {
  25.     background-color#c0d2ec;
  26. }
  27. .dtree .clip {
  28.     overflowhidden;
  29. }

 

  1. <!-- dTree.js -->
  2. function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
  3.     this.id = id;
  4.     this.pid = pid;
  5.     this.name = name;
  6.     this.url = url;
  7.     this.title = title;
  8.     this.target = target;
  9.     this.icon = icon;
  10.     this.iconOpen = iconOpen;
  11.     this._io = open || false;
  12.     this._is = false;
  13.     this._ls = false;
  14.     this._hc = false;
  15.     this._ai = 0;
  16.     this._p;
  17. }
  18. // Tree object
  19. function dTree(objName) {
  20.     this.config = {
  21.         target              : null,
  22.         folderLinks         : true,
  23.         useSelection        : true,
  24.         useCookies          : true,
  25.         useLines            : true,
  26.         useIcons            : true,
  27.         useStatusText       : false,
  28.         closeSameLevel      : false,
  29.         inOrder             : false
  30.     }
  31.     this.icon = {
  32.         root            : 'img/base.gif',
  33.         folder          : 'img/folder.gif',
  34.         folderOpen      : 'img/folderopen.gif',
  35.         node            : 'img/page.gif',
  36.         empty           : 'img/empty.gif',
  37.         line            : 'img/line.gif',
  38.         join            : 'img/join.gif',
  39.         joinBottom      : 'img/joinbottom.gif',
  40.         plus            : 'img/plus.gif',
  41.         plusBottom      : 'img/plusbottom.gif',
  42.         minus           : 'img/minus.gif',
  43.         minusBottom     : 'img/minusbottom.gif',
  44.         nlPlus          : 'img/nolines_plus.gif',
  45.         nlMinus         : 'img/nolines_minus.gif'
  46.     };
  47.     this.obj = objName;
  48.     this.aNodes = [];
  49.     this.aIndent = [];
  50.     this.root = new Node(-1);
  51.     this.selectedNode = null;
  52.     this.selectedFound = false;
  53.     this.completed = false;
  54. };
  55. // Adds a new node to the node array
  56. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
  57.     this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
  58. };
  59. // Open/close all nodes
  60. dTree.prototype.openAll = function() {
  61.     this.oAll(true);
  62. };
  63. dTree.prototype.closeAll = function() {
  64.     this.oAll(false);
  65. };
  66. // Outputs the tree to the page
  67. dTree.prototype.toString = function() {
  68.     var str = '<div class="dtree">/n';
  69.     if (document.getElementById) {
  70.         if (this.config.useCookies) this.selectedNode = this.getSelected();
  71.         str += this.addNode(this.root);
  72.     } else str += 'Browser not supported.';
  73.     str += '</div>';
  74.     if (!this.selectedFound) this.selectedNode = null;
  75.     this.completed = true;
  76.     return str;
  77. };
  78. // Creates the tree structure
  79. dTree.prototype.addNode = function(pNode) {
  80.     var str = '';
  81.     var n=0;
  82.     if (this.config.inOrder) n = pNode._ai;
  83.     for (n; n<this.aNodes.length; n++) {
  84.         if (this.aNodes[n].pid == pNode.id) {
  85.             var cn = this.aNodes[n];
  86.             cn._p = pNode;
  87.             cn._ai = n;
  88.             this.setCS(cn);
  89.             if (!cn.target && this.config.target) cn.target = this.config.target;
  90.             if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
  91.             if (!this.config.folderLinks && cn._hc) cn.url = null;
  92.             if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
  93.                     cn._is = true;
  94.                     this.selectedNode = n;
  95.                     this.selectedFound = true;
  96.             }
  97.             str += this.node(cn, n);
  98.             if (cn._ls) break;
  99.         }
  100.     }
  101.     return str;
  102. };
  103. // Creates the node icon, url and text
  104. dTree.prototype.node = function(node, nodeId) {
  105.     var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
  106.     if (this.config.useIcons) {
  107.         if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
  108.         if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
  109.         if (this.root.id == node.pid) {
  110.             node.icon = this.icon.root;
  111.             node.iconOpen = this.icon.root;
  112.         }
  113.         str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
  114.     }
  115.     if (node.url) {
  116.         str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
  117.         if (node.title) str += ' title="' + node.title + '"';
  118.         if (node.target) str += ' target="' + node.target + '"';
  119.         if (this.config.useStatusText) str += ' onmouseover="window.status=/'' + node.name + '/';return true;" onmouseout="window.status=/'/';return true;" ';
  120.         if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
  121.             str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
  122.         str += '>';
  123.     }else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
  124.         str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
  125.     str += node.name;
  126.     if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
  127.     str += '</div>';
  128.     if (node._hc) {
  129.         str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
  130.         str += this.addNode(node);
  131.         str += '</div>';
  132.     }
  133.     this.aIndent.pop();
  134.     return str;
  135. };
  136. // Adds the empty and line icons
  137. dTree.prototype.indent = function(node, nodeId) {
  138.     var str = '';
  139.     if (this.root.id != node.pid) {
  140.         for (var n=0; n<this.aIndent.length; n++)
  141.             str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  142.         (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  143.         if (node._hc) {
  144.             str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
  145.             if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
  146.             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 ) );
  147.             str += '" alt="" /></a>';
  148.         } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
  149.     }
  150.     return str;
  151. };
  152. // Checks if a node has any children and if it is the last sibling
  153. dTree.prototype.setCS = function(node) {
  154.     var lastId;
  155.     for (var n=0; n<this.aNodes.length; n++) {
  156.         if (this.aNodes[n].pid == node.id) node._hc = true;
  157.         if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
  158.     }
  159.     if (lastId==node.id) node._ls = true;
  160. };
  161. // Returns the selected node
  162. dTree.prototype.getSelected = function() {
  163.     var sn = this.getCookie('cs' + this.obj);
  164.     return (sn) ? sn : null;
  165. };
  166. // Highlights the selected node
  167. dTree.prototype.s = function(id) {
  168.     if (!this.config.useSelection) return;
  169.     var cn = this.aNodes[id];
  170.     if (cn._hc && !this.config.folderLinks) return;
  171.     if (this.selectedNode != id) {
  172.         if (this.selectedNode || this.selectedNode==0) {
  173.             eOld = document.getElementById("s" + this.obj + this.selectedNode);
  174.             eOld.className = "node";
  175.         }
  176.         eNew = document.getElementById("s" + this.obj + id);
  177.         eNew.className = "nodeSel";
  178.         this.selectedNode = id;
  179.         if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
  180.     }
  181. };
  182. // Toggle Open or close
  183. dTree.prototype.o = function(id) {
  184.     var cn = this.aNodes[id];
  185.     this.nodeStatus(!cn._io, id, cn._ls);
  186.     cn._io = !cn._io;
  187.     if (this.config.closeSameLevel) this.closeLevel(cn);
  188.     if (this.config.useCookies) this.updateCookie();
  189. };
  190. // Open or close all nodes
  191. dTree.prototype.oAll = function(status) {
  192.     for (var n=0; n<this.aNodes.length; n++) {
  193.         if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
  194.             this.nodeStatus(status, n, this.aNodes[n]._ls)
  195.             this.aNodes[n]._io = status;
  196.         }
  197.     }
  198.     if (this.config.useCookies) this.updateCookie();
  199. };
  200. // Opens the tree to a specific node
  201. dTree.prototype.openTo = function(nId, bSelect, bFirst) {
  202.     if (!bFirst) {
  203.         for (var n=0; n<this.aNodes.length; n++) {
  204.             if (this.aNodes[n].id == nId) {
  205.                 nId=n;
  206.                 break;
  207.             }
  208.         }
  209.     }
  210.     var cn=this.aNodes[nId];
  211.     if (cn.pid==this.root.id || !cn._p) return;
  212.     cn._io = true;
  213.     cn._is = bSelect;
  214.     if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
  215.     if (this.completed && bSelect) this.s(cn._ai);
  216.     else if (bSelect) this._sn=cn._ai;
  217.     this.openTo(cn._p._ai, falsetrue);
  218. };
  219. // Closes all nodes on the same level as certain node
  220. dTree.prototype.closeLevel = function(node) {
  221.     for (var n=0; n<this.aNodes.length; n++) {
  222.         if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
  223.             this.nodeStatus(false, n, this.aNodes[n]._ls);
  224.             this.aNodes[n]._io = false;
  225.             this.closeAllChildren(this.aNodes[n]);
  226.         }
  227.     }
  228. }
  229. // Closes all children of a node
  230. dTree.prototype.closeAllChildren = function(node) {
  231.     for (var n=0; n<this.aNodes.length; n++) {
  232.         if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
  233.             if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
  234.             this.aNodes[n]._io = false;
  235.             this.closeAllChildren(this.aNodes[n]);      
  236.         }
  237.     }
  238. }
  239. // Change the status of a node(open or closed)
  240. dTree.prototype.nodeStatus = function(status, id, bottom) {
  241.     eDiv    = document.getElementById('d' + this.obj + id);
  242.     eJoin   = document.getElementById('j' + this.obj + id);
  243.     if (this.config.useIcons) {
  244.         eIcon   = document.getElementById('i' + this.obj + id);
  245.         eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  246.     }
  247.     eJoin.src = (this.config.useLines)?
  248.     ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
  249.     ((status)?this.icon.nlMinus:this.icon.nlPlus);
  250.     eDiv.style.display = (status) ? 'block''none';
  251. };
  252. // [Cookie] Clears a cookie
  253. dTree.prototype.clearCookie = function() {
  254.     var now = new Date();
  255.     var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  256.     this.setCookie('co'+this.obj, 'cookieValue', yesterday);
  257.     this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
  258. };
  259. // [Cookie] Sets value in a cookie
  260. dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
  261.     document.cookie =
  262.         escape(cookieName) + '=' + escape(cookieValue)
  263.         + (expires ? '; expires=' + expires.toGMTString() : '')
  264.         + (path ? '; path=' + path : '')
  265.         + (domain ? '; domain=' + domain : '')
  266.         + (secure ? '; secure' : '');
  267. };
  268. // [Cookie] Gets a value from a cookie
  269. dTree.prototype.getCookie = function(cookieName) {
  270.     var cookieValue = '';
  271.     var posName = document.cookie.indexOf(escape(cookieName) + '=');
  272.     if (posName != -1) {
  273.         var posValue = posName + (escape(cookieName) + '=').length;
  274.         var endPos = document.cookie.indexOf(';', posValue);
  275.         if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
  276.         else cookieValue = unescape(document.cookie.substring(posValue));
  277.     }
  278.     return (cookieValue);
  279. };
  280. // [Cookie] Returns ids of open nodes as a string
  281. dTree.prototype.updateCookie = function() {
  282.     var str = '';
  283.     for (var n=0; n<this.aNodes.length; n++) {
  284.         if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
  285.             if (str) str += '.';
  286.             str += this.aNodes[n].id;
  287.         }
  288.     }
  289.     this.setCookie('co' + this.obj, str);
  290. };
  291. // [Cookie] Checks if a node id is in a cookie
  292. dTree.prototype.isOpen = function(id) {
  293.     var aOpen = this.getCookie('co' + this.obj).split('.');
  294.     for (var n=0; n<aOpen.length; n++)
  295.         if (aOpen[n] == id) return true;
  296.     return false;
  297. };
  298. // If Push and pop is not implemented by the browser
  299. if (!Array.prototype.push) {
  300.     Array.prototype.push = function array_push() {
  301.         for(var i=0;i<arguments.length;i++)
  302.             this[this.length]=arguments[i];
  303.         return this.length;
  304.     }
  305. };
  306. if (!Array.prototype.pop) {
  307.     Array.prototype.pop = function array_pop() {
  308.         lastElement = this[this.length-1];
  309.         this.length = Math.max(this.length-1,0);
  310.         return lastElement;
  311.     }
  312. };

 

  1. <!-- dTree.htm -->
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html>
  4. <head>
  5.     <title>dTree</title>
  6.     <link rel="StyleSheet" href="dtree.css" type="text/css" />
  7.     <script type="text/javascript" src="dtree.js"></script>
  8. </head>
  9. <body>
  10. <div class="dtree">
  11.     <p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
  12.     <script type="text/javascript">
  13.         <!--
  14.         d = new dTree('d');
  15.         dsf = "asdfasdf"
  16.         d.add(0,-1,'My example tree');
  17.         d.add(1,0,'Node 1','javascript:alert(dsf)');
  18.         d.add(2,0,'Node 2');
  19.         d.add(3,1,'Node 1.1');
  20.         d.add(3,1,'new 1')
  21.         
  22.         d.add(4,0,'Node 3','example01.html');
  23.         d.add(5,3,'Node 1.1.1','example01.html');
  24.         d.add(6,5,'Node 1.1.1.1','example01.html');
  25.         d.add(7,0,'Node 4','example01.html');
  26.         d.add(8,1,'Node 1.2','example01.html');
  27.         d.add(9,0,'My Pictures','example01.html','Pictures I/'ve taken over the years','','','img/imgfolder.gif');
  28.         d.add(10,9,'The trip to Iceland','example01.html','Pictures of Gullfoss and Geysir');
  29.         d.add(11,9,'Mom/'s birthday','example01.html');
  30.         d.add(12,0,'Recycle Bin','example01.html','','','img/trash.gif');
  31.         d.add(13,0,'new 2','javascript:alert(dsf)')
  32.         d.add(14,13,'new 3')
  33.         d.add(15,14,'new 3')
  34.         d.add(16,15,'new 3')
  35.         d.add(17,16,'new 3')
  36.         d.add(18,17,'new 3','javascript:alert(dsf)')
  37.         document.write(d);
  38.         //-->
  39.     </script>
  40. </div>
  41. </body>
  42. </html>

抱歉!评论已关闭.