alai_tree.js
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:18k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. /*******************************************************************************
  2. 阿赖目录树控件程序 由赖国欣设计于2003年6月,保留所有权利!
  3. *********************************************************************************/
  4. function alai_imagelist()
  5. {try{
  6. var item=[]
  7. count=0
  8. this.path=""
  9. this.type="gif"
  10. this.err=""
  11. iml=this
  12. this.copywrite="Copywrite by Alai(赖国欣) (c)2003,All right reserved!"
  13. this.add=function(src,key)
  14. {
  15. var img=new Image()
  16. if(src==null || src=="")return
  17. if(src.indexOf("/")==-1)src=iml.path+src
  18. if(!(/.gif$|.jpg$|.png$|.bmp$/i).test(src))src+="."+iml.type
  19. if(key==null || key=="")key=src.replace(/(.*/){0,}([^.]+).*/ig,"$2")
  20. item[count]=img;img.index=count;item[key]=img;count++;
  21. img.onerror=function()
  22. {
  23. alert("alai_imagelist cause run time error:nImage file "+img.src+" not found!")
  24. item[img.index]=null;item[key]=null;count--;
  25. }
  26. img.src=src
  27. return img
  28. }
  29. this.item=item
  30. this.count=function(){return count;}
  31. }
  32. catch(e){
  33. iml.err="alai_imagelist cause run time error!nError number:"+e.number+".nError description:"+e.description;
  34. iml.err+="n    -- nYou can send the case and error description message to A@lai.com.cn for support.Please visit http://www.9499.net to get the last release!"
  35. alert(iml.err);
  36. return iml;
  37. }
  38. }
  39. function alai_tree(imageList,indent,toObject,labelStyle)
  40. {try{
  41. if(!document.getElementById){throw new Error(999,"Your browser dosen't support Alai_tree(),IE5.0+ is recommended.");}
  42. var count=0,depth=0,nodes=[],tree=this
  43. this.copywrite="Copywrite by Alai(赖国欣) (c)2003,All right reserved!"
  44. this.err=""
  45. imageList=imageList==null?new alai_imagelist():imageList
  46. var icons=imageList.item
  47. this.imageList=imageList
  48. if(icons["default"]!=null){if(icons["link"]==null)icons["link"]=icons["default"]}
  49. var showExpand=icons["expand"]!=null && icons["collapse"]!=null
  50. if(showExpand)
  51. { if(icons["expand_top"]==null)icons["expand_top"]=icons["expand"]
  52. if(icons["expand_end"]==null)icons["expand_end"]=icons["expand"]
  53. if(icons["collapse_top"]==null)icons["collapse_top"]=icons["collapse"]
  54. if(icons["collapse_end"]==null)icons["collapse_end"]=icons["collapse"]
  55. }
  56. var showBranch=icons["leaf"]!=null && icons["twig"]!=null
  57. var showLine=icons["line"]!=null && icons["blank"]!=null
  58. var root={}
  59. root.children=[]
  60. root.expanded=true;
  61. root.getTier=function(){return 0;}
  62. var selectedNode=null,activeNode=null;
  63. Array.prototype.remove=function(index){if(index<0 || index>=this.length)return;for(var i=index;i<this.length;i++)this[i]=this[i+1];this.length--;}
  64. Array.prototype.indexOf=function(elm){for(var i=0;i<this.length;i++)if(elm==this[i])return i;return -1;}
  65. var setExIcon=function(srcNode)
  66. { if(srcNode.hasChild && showExpand)
  67. { var sEx=srcNode.next==null?"_end":"";
  68. if(srcNode==nodes[0])sEx="_top";
  69. srcNode.exIcon.src=srcNode.expanded?icons["collapse"+sEx].src:icons["expand"+sEx].src;
  70. srcNode.icon.style.marginLeft=0;
  71. }
  72. else
  73. { if(showBranch){srcNode.exIcon.src=srcNode.next==null?icons["twig"].src:icons["leaf"].src}
  74. else if(showExpand){srcNode.icon.style.marginLeft=icons["expand"].width}
  75. }
  76. }
  77. var setLine=function(srcNode,idx)
  78. { if(srcNode.hasChild){for(var i=0;i<srcNode.children.length;i++){srcNode.children[i].lineIcon[idx].src=icons["line"].src;setLine(srcNode.children[i],idx)}}
  79. }
  80. var doSelect=function(srcNode)
  81. { if(selectedNode!=null){selectedNode.label.style.background="";selectedNode.label.style.color="black";}
  82. srcNode.label.style.background="highlight";srcNode.label.style.color="highlighttext";
  83. selectedNode=srcNode
  84. }
  85. var doFocus=function(srcNode)
  86. { if(srcNode==selectedNode){srcNode.label.style.background="highlight";srcNode.label.style.color="highlighttext";}
  87. activeNode=srcNode
  88. }
  89. var doBlur=function(srcNode)
  90. { if(srcNode==selectedNode){srcNode.label.style.background="buttonface";srcNode.label.style.color="windowtext";}
  91. activeNode=null
  92. }
  93. var run=function(cmd,a0,a1,a2)
  94. {
  95. if(typeof(cmd)=="string")
  96. { try{return eval(cmd);}
  97. catch(E){alert("run script string error:n"+cmd);}
  98. }
  99. else if(typeof(cmd)=="function"){return cmd(a0,a1,a2);}
  100. }
  101. var doKeyDown=function(srcNode)
  102. { event.returnValue=false;
  103. switch(event.keyCode)
  104. { case 38:
  105. if(srcNode.prev==null){if(srcNode.parent!=root){srcNode.parent.focus();srcNode.parent.select();}}
  106. else{var node=srcNode.prev;while(true){if(!node.hasChild || !node.expanded)break;node=node.last;} node.focus();node.select();}
  107. break;
  108. case 40:
  109. if(srcNode.first!=null && srcNode.expanded){srcNode.first.focus();srcNode.first.select();}
  110. else{var node=srcNode;while(true){if(node.next!=null || node.parent==root)break;node=node.parent}if(node.next!=null){node.next.focus();node.next.select();}}
  111. break;
  112. case 189: ;case 109: ;case 37: srcNode.expand(false);break;
  113. case 107: ;case 187: ;case 39: srcNode.expand(true);break;
  114. case 13: srcNode.click();break;
  115. case 27: srcNode.blur();break;
  116. }
  117. }
  118. var addNode=function(toNode,relation,text,key,ico,exeCategory,exeArg)
  119. {
  120. var node={}
  121. var nbody=document.createElement("span")
  122. var container=document.createElement("div")
  123. container.insertAdjacentElement("afterBegin",nbody)
  124. var tier
  125. if(relation==null)relation="last"
  126. if(toNode==root && (relation!="first" && relation!="last"))relation="last"
  127. relation=relation.toLowerCase()
  128. node.first=node.last=node.next=node.prev=null
  129. switch(relation)
  130. { case "first":
  131. tier=toNode.getTier()+1
  132. if(toNode.hasChild){toNode.children[0].container.insertAdjacentElement("beforeBegin",container);}
  133. else {toNode.container.insertAdjacentElement("beforeEnd",container)}
  134. node.parent=toNode
  135. toNode.children[toNode.children.length]=node
  136. if(toNode.hasChild){node.next=toNode.first;toNode.first.prev=node;toNode.first=node;}
  137. else{toNode.first=toNode.last=node;}
  138. break;
  139. case "next":
  140. tier=toNode.getTier()
  141. toNode.container.insertAdjacentElement("afterEnd",container)
  142. node.parent=toNode.parent
  143. toNode.parent.children[toNode.parent.children.length]=node
  144. if(toNode.next==null)node.parent.last=node
  145. node.next=toNode.next;node.prev=toNode;toNode.next=node;
  146. break;
  147. case "prev":
  148. tier=toNode.getTier()
  149. toNode.container.insertAdjacentElement("beforeBegin",container)
  150. node.parent=toNode.parent
  151. toNode.parent.children[toNode.parent.children.length]=node
  152. if(toNode.prev==null)node.parent.first=node
  153. node.prev=toNode.prev;node.next=toNode;toNode.prev=node
  154. break;
  155. default:
  156. tier=toNode.getTier()+1
  157. toNode.children[toNode.children.length]=node
  158. var o=toNode==root?tree.body:toNode.container
  159. o.insertAdjacentElement("beforeEnd",container)
  160. node.parent=toNode
  161. if(toNode.hasChild){node.prev=toNode.last;toNode.last.next=node;toNode.last=node;}
  162. else{toNode.first=toNode.last=node;}
  163. }
  164. node.parent.hasChild=true
  165. if(depth<tier)depth=tier;
  166. var label=document.createElement("span")
  167. container.style.cssText="margin:0px;padding:0px;white-space:nowrap;"
  168. with(nbody)
  169. { insertAdjacentElement("beforeEnd",label)
  170. style.cssText="cursor:default;text-align:left;color:black;font-size:10pt;margin:0;"
  171. style.display=node.parent.expanded?"block":"none"
  172. indent=indent>=0?indent:16;nbody.style.textIndent=indent*(tier-1)
  173. }
  174. var icon=new Image()
  175. ico=ico==null||ico==""?"default":ico
  176. if(typeof(ico)=="string")if(ico.toLowerCase().indexOf(".gif")!=-1 || ico.toLowerCase().indexOf(".jpg")!=-1){ico=imageList.add(ico).index;}
  177. if(icons[ico]!=null)
  178. { icon.src=icons[ico].src
  179. nbody.insertAdjacentElement("afterBegin",icon)
  180. }
  181. label.innerHTML=text
  182. label.style.cssText=labelStyle!=null?labelStyle:"padding:1;margin-left:2;vertical-align:middle;text-align:left;"
  183. with(label)
  184. {
  185. onclick=function(){run(node.select);if(run(node.onclick))if(run(tree.onclick,node))run(node.execute);}
  186. ondblclick=function(){if(run(node.ondblclick))run(tree.ondblclick,node)}
  187. onmouseover=function(){if(event.fromElement!=node.icon)if(run(node.onmouseover))run(tree.onmouseover,node)}
  188. onmouseout=function(){if(event.toElement!=node.icon)if(run(node.onmouseout))run(tree.onmouseout,node);}
  189. onmousemove=function(){if(run(node.onmousemove))run(tree.onmousemove,node)}
  190. onmousedown=function(){if(run(node.onmousedown))run(tree.onmousedown,node)}
  191. onmouseup=function(){if(run(node.onmouseup))run(tree.onmouseup,node)}
  192. onkeypress=function(){if(run(node.onkeypress))run(tree.onkeypress,node)}
  193. onkeydown=function(){if(run(node.onkeydown)){if(run(tree.onkeydown,node))doKeyDown(node);}}
  194. onkeyup=function(){if(run(node.onkeyup))run(tree.onkeyup,node)}
  195. onfocus=function(){if(run(node.onfocus)){if(run(tree.onfocus,node))doFocus(node);}}
  196. onblur=function(){if(run(node.onblur)){if(run(tree.onblur,node))doBlur(node);}}
  197. }
  198. with(icon)
  199. {
  200. onclick=function(){run(node.select);if(run(node.onclick))if(run(tree.onclick,node))run(node.execute);}
  201. ondblclick=function(){if(run(node.ondblclick))run(tree.ondblclick,node)}
  202. onmouseover=function(){if(event.fromElement!=node.icon)if(run(node.onmouseover))run(tree.onmouseover,node)}
  203. onmouseout=function(){if(event.toElement!=node.icon)if(run(node.onmouseout))run(tree.onmouseout,node);}
  204. onmousemove=function(){if(run(node.onmousemove))run(tree.onmousemove,node)}
  205. onmousedown=function(){if(run(node.onmousedown))run(tree.onmousedown,node)}
  206. onmouseup=function(){if(run(node.onmouseup))run(tree.onmouseup,node)}
  207. onkeypress=function(){if(run(node.onkeypress))run(tree.onkeypress,node)}
  208. onkeydown=function(){if(run(node.onkeydown)){if(run(tree.onkeydown,node))doKeyDown(node);}}
  209. onkeyup=function(){if(run(node.onkeyup))run(tree.onkeyup,node)}
  210. onfocus=function(){if(run(node.onfocus)){if(run(tree.onfocus,node))doFocus(node);}}
  211. onblur=function(){if(run(node.onblur)){if(run(tree.onblur,node))doBlur(node);}}
  212. }
  213. nbody.onblur=function(){if(node.onfocus()){if(tree.onfocus(node))doFocus(node);}}
  214. nbody.onfocus=function(){if(node.onblur()){if(tree.onblur(node))doBlur(node);}}
  215. icon.tabIndex=label.tabIndex=1;icon.hideFocus=true;
  216. node.onclick=node.ondblclick=node.onmouseover=node.onmouseout=node.onmousemove=node.onmousedown=node.onmouseup=node.onkeypress=node.onkeydown=node.onkeyup=node.oncollapse=node.onexpand=node.onfocus=node.onblur=node.onselect=new Function("return true");
  217. node.click=function(){node.label.click();}
  218. node.focus=function(){node.label.focus();}
  219. node.blur=function(){node.icon.blur();node.label.blur();}
  220. node.select=function(){if(node.onselect()){if(tree.onselect(node))doSelect(node);}}
  221. node.body=nbody
  222. node.container=container
  223. node.exIcon=null
  224. if(showBranch){node.exIcon=new Image();nbody.insertAdjacentElement("afterBegin",node.exIcon);}
  225. node.lineIcon=[]
  226. if(showLine)
  227. { for(var i=tier-2;i>=0;i--){var img=new Image();img.src=icons["blank"].src;nbody.insertAdjacentElement("afterBegin",img);node.lineIcon[i]=img;}
  228. if(node.prev!=null)setLine(node.prev,tier-1)
  229. var n=node.parent;var i=tier-2;while(n!=root){if(n.next!=null)node.lineIcon[i].src=icons["line"].src;n=n.parent;i--}
  230. }
  231. if(showBranch){setExIcon(node);if(node.prev!=null)setExIcon(node.prev);if(node.next!=null)setExIcon(node.next);}
  232. var ma=node.parent;if(ma!=root){if(ma.exIcon==null && showExpand){ma.exIcon=new Image();var o=ma.icon.src==""?ma.label:ma.icon;o.insertAdjacentElement("beforeBegin",ma.exIcon);}setExIcon(ma);if(ma.exIcon){ma.exIcon.onmousedown=function(){ma.expand();}}}
  233. if(showExpand & !showBranch){icon.style.marginLeft=icons["expand"].width}
  234. node.label=label
  235. node.icon=icon
  236. node.add=function(text,ico,exeType,exeArg){return addNode(this,"last",text,"",ico,exeType,exeArg);}
  237. node.addLink=function(_url,text,target,img){if(text==null||text=="")text=_url;var exeArg="url";if(target!=null&&target!="")exeArg+=":"+target;if((img==null||img==""))img="link";return addNode(node,"last",text,"",img,exeArg,_url)}
  238. node.expanded=true
  239. node.expand=function(isShow,incSub)
  240. {
  241. if(node.children.length==0)return;
  242. if(isShow==null)node.expanded=!node.expanded;else node.expanded=isShow;
  243. incSub=incSub==null?false:incSub
  244. var sh=node.expanded?"block":"none"
  245. for(var i=0;i<node.children.length;i++){node.children[i].container.style.display=sh;if(incSub)node.children[i].expand(node.expanded,true);}
  246. setExIcon(node)
  247. if(node.expanded){if(run(node.onexpand)){run(tree.onexpand,node);}}
  248. else{ if(node.oncollapse()){tree.oncollapse(node);}}
  249. var n=node.parent;while(isShow && true){if(n==root)break;if(!n.expanded)n.expand(true);n=n.parent;}
  250. }
  251. node.remove=function(){tree.removeNode(node);}
  252. node.getPath=function(separator){separator=separator==null?"/":separator;var path="",oNode=node;for(var i=tier;i>0;i--){path=separator+oNode.label.innerText+path;oNode=oNode.parent;}return path;}
  253. node.getSibling=function(){return node.parent.children};
  254. node.getTier=function(){return tier;}
  255. node.getIndex=function(){for(var i=0;i<count;i++)if(tree.nodes[i]==node)return i;return -1;}
  256. node.children=[]
  257. node.hasChild=false
  258. node.getKey=function(){return key==""||key==null?null:key;}
  259. if(key!=null && key!="")tree.nodes[key]=node
  260. tree.nodes[count]=node
  261. count++
  262. node.execute=new Function()
  263. exeCategory=exeCategory==null?"":exeCategory.toLowerCase()
  264. switch(exeCategory)
  265. { case "expand":
  266. node.execute=node.expand;break;
  267. case "js":
  268. if(typeof(exeArg)!="string")break;node.execute=function(){eval(exeArg);};break;
  269. default:
  270. if(exeCategory.indexOf("url")!=-1)
  271. { if(typeof(exeArg)!="string"){break;}
  272. if(exeCategory.indexOf("url:")!=-1)
  273. { var tar=exeCategory.replace("url:","");node.execute=function(){open(exeArg,tar);}
  274. }else { node.execute=function(){open(exeArg,tree.target);}}
  275. exeCategory="url";
  276. }else {node.execute=node.expand;exeCategory="expand";}
  277. }
  278. run(tree.afteradd,node,exeCategory)
  279. return node
  280. }
  281. var remove=function(srcNode)
  282. { var ma=srcNode.parent,jj=srcNode.prev,mm=srcNode.next;
  283. if(ma.first==srcNode && ma.last==srcNode && ma!=root)
  284. { ma.hasChild=false;document.createElement("div").insertAdjacentElement("afterBegin",ma.exIcon);setExIcon(ma);ma.first=ma.last=null;
  285. }
  286. else
  287. { if(jj!=null){jj.next=mm;setExIcon(jj);}else{ma.first=mm;}
  288. if(mm!=null){mm.prev=jj;setExIcon(mm);}else{ma.last=jj;}
  289. }
  290. srcNode.container.removeNode(true);nodes.remove(srcNode.getIndex());count--;srcNode=null;
  291. }
  292. this.removeNode=function(srcNode){if(!srcNode.hasChild)remove(srcNode);else{for(var i=0;i<srcNode.children.length;i++){tree.removeNode(srcNode.children[i]);}tree.removeNode(srcNode);}}
  293. root.add=function(text,ico,exeType,exeArg){return addNode(root,"last",text,"",ico,exeType,exeArg);}
  294. root.addLink=function(_url,text,target,img){if(text==null||text=="")text=_url;var exeArg="url";if(target!=null&&target!="")exeArg+=":"+target;if((img==null||img==""))img="link";return addNode(root,"last",text,"",img,exeArg,_url)}
  295. this.getNodesByTier=function(num){var col=[];for(var i=0;i<count;i++)if(nodes[i].getTier()==num)col[col.length]=nodes[i];return col;}
  296. this.expandAll=function(isShow){isShow=isShow==null?!root.expanded:isShow;for(var i=0;i<count;i++)nodes[i].expand(isShow);root.expanded=isShow;}
  297. this.expandToTier=function(num){tree.expandAll(false);for(var i=0;i<count;i++)if(nodes[i].getTier()<num)nodes[i].expand(true);}
  298. this.body=document.createElement("div")
  299. this.embed=function(obj,where){if(obj==null)obj=document.body;if(!obj.insertAdjacentElement)obj=document.body;if(where==null)where="afterBegin";var s=where.toLowerCase();if(s!="beforebegin" || s!="afterbegin" || s!="beforeend" || s!="afterend")where="afterBegin";obj.insertAdjacentElement(where,tree.body);}
  300. this.count=function(){return count;}
  301. this.getDepth=function(){return depth;}
  302. this.onclick=this.ondblclick=this.onmousemove=this.onmouseover=this.onmouseout=this.onmouseup=this.onmousedown=this.onkeypress=this.onkeydown=this.onkeyup=this.oncollapse=this.onexpand=this.afteradd=this.onblur=this.onfocus=this.onselect=new Function("return true;");
  303. this.add=addNode
  304. this.getNodeByPath=function(path){if(path==null||path=="")return null;if(path=="/")return root;if(path.lastIndexOf("/")!=path.length-1)path+="/";for(var i=count-1;i>=0;i--)if((nodes[i].getPath()+"/")==path)return nodes[i];return null;}
  305. this.testPath=function(path){if(path==null||path=="")return false;if(path=="/")return true;if(path.lastIndexOf("/")!=path.length-1)path+="/";for(var i=count-1;i>=0;i--)if((nodes[i].path()+"/").indexOf(path)==0)return true;return false;}
  306. this.pathParse=function(pathList,sort,ps,ls,ico)
  307. { if(ps==null)ps="/";if(ls==null)ls=";";
  308. var list=pathList.split(ls);if(sort)list=list.sort();
  309. var colPath=[];for(var i=0;i<nodes.length;i++){colPath[colPath.length]=nodes[i].getPath();}
  310. var pathIndex=function(path){for(var i=colPath.length-1;i>=0;i--)if(colPath[i]==path)return i;return -1;}
  311. for(var i=0;i<list.length;i++)
  312. { if(list[i]=="")continue;var path=list[i].split(ps),node=root;
  313. for(var j=0;j<path.length;j++)
  314. { if(path[j]==""){continue;};var p="";for(var m=0;m<=j;m++){if(path[m]!=""){p+="/"+path[m];}}
  315. idx=pathIndex(p);if(idx!=-1){node=nodes[idx];}else {node=node.add(path[j],ico);colPath[colPath.length]=p;}
  316. }
  317. }
  318. }
  319. this.getSelectedNode=function(){return selectedNode;}
  320. this.getActiveNode=function(){return activeNode;}
  321. this.target="_self"
  322. this.root=root
  323. this.nodes=nodes
  324. this.embed(toObject)
  325. }
  326. catch(e){
  327. tree.err="alai_tree cause run time error!nError number:"+e.number+".nError description:"+e.description;
  328. tree.err+="n    -- nYou can send the case and error description message to A@lai.com.cn for support or visit http://www.9499.net to get the last release!"
  329. alert(tree.err);
  330. return tree;
  331. }
  332. }