ks-tree.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. bindEventHandler = function(obj, evtName, func) {
  2. var args = new Array(arguments.length - 3);
  3. for (var i = 0; i < args.length; i ++) {
  4. args[i] = arguments[i + 3];
  5. }
  6. if ($.browser.msie) {
  7. obj.attachEvent("on" + evtName, function(evt){
  8. args[args.length] = window.event;
  9. return func.apply(obj, args)}
  10. );
  11. } else {
  12. obj.addEventListener(evtName, function(evt){
  13. args[args.length] = evt;
  14. return func.apply(obj, args)}, false);
  15. }
  16. }
  17. findChildByTagName = function(obj, tagName) {
  18. var children = obj.childNodes;
  19. for (var i = 0; i < children.length; i++) {
  20. if (children[i].tagName == tagName) {
  21. return children[i];
  22. }
  23. }
  24. }
  25. jQuery.fn.hcksTree = function(options) {
  26. $(this).each(function(){
  27. var tree = new HcksTreeview($(this), options);
  28. if (options && options.loadImmediate) {
  29. tree.asynInitChildNodes(null);
  30. }
  31. });
  32. };
  33. jQuery.fn.contextMenu = function(options) {
  34. var menu = new HcksMenu(options);
  35. $(this).bind("contextmenu", function(e) {
  36. menu.show(e);
  37. return false;
  38. });
  39. return this;
  40. };
  41. /**
  42. * 菜单
  43. * @param {} options
  44. */
  45. function HcksMenu(options) {
  46. var extraParams = null;
  47. var menu = document.createElement("DIV");
  48. menu.className = "hcks-menu";
  49. document.body.appendChild(menu);
  50. var $menu = $(menu);
  51. $menu.bind("click", function(e) { e.stopPropagation(); });
  52. var ul = document.createElement("UL");
  53. menu.appendChild(ul);
  54. var shadow = document.createElement("DIV");
  55. shadow.className = "hcks-menu-shadow";
  56. document.body.appendChild(shadow);
  57. function hide() {
  58. menu.style.display = "none";
  59. shadow.style.display = "none";
  60. }
  61. this.setExtraParams = function(params) {
  62. extraParams = params;
  63. }
  64. this.addItem = function(menuItem) {
  65. var item = document.createElement("LI");
  66. $(item).attr("command", menuItem.command).hover(
  67. function() {
  68. $(this).addClass("menuitem-hover");
  69. },
  70. function(){
  71. $(this).removeClass("menuitem-hover");
  72. }
  73. );
  74. if (options && typeof(options.handler) == 'function') {
  75. $(item).click(function(evt){
  76. options.handler.call(this, menuItem.command, extraParams);
  77. hide();
  78. extraParams = null;
  79. });
  80. }
  81. var icon = document.createElement("img");
  82. icon.src = __CONTEXT_PATH + "/resource/images/icons/" + menuItem.icon;
  83. item.appendChild(icon);
  84. item.appendChild(document.createTextNode(menuItem.text));
  85. ul.appendChild(item);
  86. }
  87. if (options && options.menus) {
  88. for (var cmd in options.menus) {
  89. var mi = options.menus[cmd];
  90. mi.command = cmd;
  91. this.addItem(mi);
  92. }
  93. }
  94. this.show = function(e, commands) {
  95. var mustShow = true;
  96. if (commands) {
  97. mustShow = false;
  98. $menu.find("li").each(function(){
  99. if (commands.indexOf($(this).attr("command")) == -1) {
  100. $(this).hide();
  101. } else {
  102. $(this).show();
  103. mustShow = true;
  104. }
  105. });
  106. }
  107. if (mustShow) {
  108. var x = e.pageX ? e.pageX : e.clientX;
  109. var y = e.pageY ? e.pageY : e.clientY;
  110. if (!e.pageX) {
  111. var doc = document.documentElement, body = document.body;
  112. x = x + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
  113. y = y + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
  114. }
  115. $menu.css({'left':x, 'top':y}).show();
  116. $(shadow).css({width:$menu.width(),height:$menu.height(),left:x+2,top:y+2}).show();
  117. $(document).one('click', hide);
  118. }
  119. }
  120. }
  121. /**
  122. * 树
  123. * @param {} container
  124. * @param {} options
  125. */
  126. function HcksTreeview(container, options) {
  127. var loading = false;
  128. var rootNode = $("<ul></ul>").addClass("x-tree-root-ct");
  129. options.name = options.name || "";
  130. options.parentName = options.parentName || "parentId";
  131. options.valueName = options.valueName || "value";
  132. options.showLine = options.showLine || true;
  133. options.loadingMessage = options.loadingMessage || "正在加载,请稍候...";
  134. if (options.showLine) {
  135. rootNode.addClass("x-tree-lines");
  136. } else {
  137. rootNode.addClass("x-tree-no-lines");
  138. }
  139. var loadingMessage = document.createElement("LI");
  140. loadingMessage.className = "x-tree-loading";
  141. loadingMessage.innerHTML = options.loadingMessage;
  142. if (typeof(container) == 'string') {
  143. container = $("#" + container);
  144. } else {
  145. container = $(container);
  146. }
  147. container.addClass("x-hcks-tree").append(rootNode);
  148. container.bind("contextmenu", function(e){e.stopPropagation(); return false;});//阻止事件冒泡
  149. if ($.browser.msie) {
  150. container.addClass("ext-ie");//不显示超过容器尺寸的内容
  151. container.get(0).onselectstart = function(){return false}
  152. }
  153. this.selectedNode = null;
  154. var blankImg = function() {
  155. var img = document.createElement("IMG");
  156. img.src = __BLANK_IMAGE;
  157. return img;
  158. }
  159. var isLeaf = function(node) {
  160. return node.children("div").hasClass("x-tree-node-leaf");
  161. }
  162. var fixNode = function(node) {
  163. if (!node) return null;
  164. if (typeof(node) == 'string') {
  165. node = $("#" + node);
  166. } else {
  167. node = $(node);
  168. }
  169. return node;
  170. }
  171. var setToLeaf = function(node) {
  172. var div = node.children("div:first");//查找节点下首个div块
  173. var handlerIcon = div.children("img:first");
  174. if (handlerIcon.hasClass("x-tree-elbow-end-minus") || handlerIcon.hasClass("x-tree-elbow-end-plus")) {
  175. handlerIcon.removeClass("x-tree-elbow-end-minus").removeClass("x-tree-elbow-end-plus");
  176. handlerIcon.addClass("x-tree-elbow-end");
  177. } else if (handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-minus")) {
  178. handlerIcon.removeClass("x-tree-elbow-plus").removeClass("x-tree-elbow-minus");
  179. handlerIcon.addClass("x-tree-elbow");
  180. }
  181. var icon = div.children("img:last");
  182. if (icon.attr("src") != __BLANK_IMAGE) {
  183. if (icon.attr("collapsedIcon")) {
  184. icon.attr("src", icon.attr("collapsedIcon"));
  185. }
  186. } else {
  187. div.removeClass("x-tree-node-expanded").addClass("x-tree-node-collapsed");
  188. }
  189. }
  190. this.setToFolder = function(node) {
  191. node = fixNode(node);
  192. alert(node.html());
  193. var div = node.children("div:first");//查找节点下首个div块
  194. var handlerIcon = div.children("img:first");
  195. if (handlerIcon.hasClass("x-tree-elbow-end")) {
  196. handlerIcon.removeClass("x-tree-elbow-end");
  197. handlerIcon.addClass("x-tree-elbow-end-plus");
  198. } else if (handlerIcon.hasClass("x-tree-elbow") ) {
  199. handlerIcon.removeClass("x-tree-elbow");
  200. handlerIcon.addClass("x-tree-elbow-plus");
  201. }
  202. // if (handlerIcon.hasClass("x-tree-elbow-end-minus") || handlerIcon.hasClass("x-tree-elbow-end-plus")) {
  203. // handlerIcon.removeClass("x-tree-elbow-end-minus").removeClass("x-tree-elbow-end-plus");
  204. // handlerIcon.addClass("x-tree-elbow-end");
  205. // } else if (handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-minus")) {
  206. // handlerIcon.removeClass("x-tree-elbow-plus").removeClass("x-tree-elbow-minus");
  207. // handlerIcon.addClass("x-tree-elbow");
  208. // }
  209. var icon = div.children("img:last");
  210. if (icon.attr("src") != __BLANK_IMAGE) {
  211. if (icon.attr("expandedIcon")) {
  212. icon.attr("src", icon.attr("expandedIcon"));
  213. }
  214. } else {
  215. div.removeClass("x-tree-node-collapsed").addClass("x-tree-node-expanded");
  216. }
  217. }
  218. this.clear = function(node) {
  219. rootNode.empty();
  220. rootNode.get(0).innerHTML = "";
  221. }
  222. this.expanded = function(node) {
  223. node = fixNode(node);
  224. var handlerIcon = node.children("div:first").children("img:first");
  225. return handlerIcon.hasClass("x-tree-elbow-minus") || handlerIcon.hasClass("x-tree-elbow-end-minus");
  226. }
  227. this.collapsed = function(node) {
  228. node = fixNode(node);
  229. var handlerIcon = node.children("div:first").children("img:first");
  230. return handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-end-plus");
  231. }
  232. this.asynInitChildNodes = function(pNode, extraParams, dynamic, asyn, recursive) {
  233. if (typeof(asyn) == 'undefined') asyn = true;
  234. if (loading && asyn) return;
  235. if (!options.url) return;
  236. var node = fixNode(pNode);
  237. var root = node == null;
  238. var loadingContainer = root ? rootNode : node.children("ul").show();
  239. loadingContainer.append(loadingMessage);
  240. var tree = this;
  241. var params = {};
  242. if (!root) {
  243. params[options.parentName] = this.getNodeAttr(node, "id");
  244. params[options.valueName] = this.getNodeAttr(node, "value");
  245. params["checked"] = this.getNodeAttr(node, "checked");
  246. }
  247. if (options.extraParams) {
  248. for (var k in options.extraParams) {
  249. params[k] = options.extraParams[k];
  250. }
  251. }
  252. if (extraParams) {
  253. for (var k in extraParams) {
  254. params[k] = extraParams[k];
  255. }
  256. }
  257. loading = true;
  258. if (options && typeof(options.beforeAsyn) == 'function') {
  259. options.beforeAsyn.call(tree, node, params);
  260. }
  261. $.ajax({
  262. url:options.url,
  263. data:params,
  264. type:'post',
  265. cache:false,
  266. async: (typeof(asyn) == 'undefined'? true:asyn),
  267. success:function(data) {
  268. loadingContainer.empty();
  269. loadingContainer.innerHTML = "";
  270. if (!root) {
  271. node.attr("init", 'y');
  272. }
  273. try {
  274. var nodesData = eval(data);
  275. if (dynamic == false) {
  276. tree.initalize(nodesData,
  277. node == null ? null : node.children("ul:last").get(0),
  278. node == null ? 0 : parseInt(node.attr("level")) + 1, asyn, recursive);
  279. } else {
  280. for(var i = 0; i < nodesData.length; i++) {
  281. var nodeData = nodesData[i];
  282. var newNode = tree.addNode(nodeData, node);
  283. if (newNode && newNode.getAttribute("init") == "n" && recursive) {
  284. tree.asynInitChildNodes(newNode, extraParams, dynamic, asyn, recursive);
  285. }
  286. };
  287. }
  288. if (! root ) {
  289. if (nodesData.length == 0) {
  290. setToLeaf(node);
  291. } else {
  292. tree.expand(node); ;
  293. }
  294. }
  295. } catch (e) {
  296. alert('程序发生意外错误!\n' + e.description)
  297. }
  298. },
  299. complete:function() {
  300. tree = null;
  301. loading = false;
  302. }
  303. });
  304. }
  305. /**
  306. * 展开当前节点
  307. * @param {} node
  308. */
  309. this.expand = function(node) {
  310. node = fixNode(node);
  311. if (node.attr("init") != 'y') {
  312. this.asynInitChildNodes(node, null, false);
  313. return;
  314. }
  315. var div = node.children("div:first");
  316. var handlerIcon = div.children("img:first");
  317. if (handlerIcon.hasClass("x-tree-elbow-end-plus")) {
  318. handlerIcon.removeClass("x-tree-elbow-end-plus").addClass("x-tree-elbow-end-minus");
  319. } else if (handlerIcon.hasClass("x-tree-elbow-plus")) {
  320. handlerIcon.removeClass("x-tree-elbow-plus").addClass("x-tree-elbow-minus");
  321. }
  322. var icon = div.children("img:last");
  323. if (icon.attr("src") != __BLANK_IMAGE) {
  324. if (icon.attr("expandedIcon")) icon.attr("src", icon.attr("expandedIcon"));
  325. } else {
  326. div.removeClass("x-tree-node-collapsed").addClass("x-tree-node-expanded");
  327. }
  328. node.attr("init", 'y');
  329. var delay = options && options.delay ? options.delay : null;
  330. node.children("ul").show(delay);
  331. }
  332. this.collapse = function(node) {
  333. node = fixNode(node);
  334. if (node == null) return;
  335. var div = node.children("div:first");
  336. var handlerIcon = div.children("img:first");
  337. if (handlerIcon.hasClass("x-tree-elbow-end-minus")) {
  338. handlerIcon.removeClass("x-tree-elbow-end-minus").addClass("x-tree-elbow-end-plus");
  339. } else if (handlerIcon.hasClass("x-tree-elbow-minus")) {
  340. handlerIcon.removeClass("x-tree-elbow-minus").addClass("x-tree-elbow-plus");
  341. }
  342. var icon = div.children("img:last");
  343. if (icon.attr("src") != __BLANK_IMAGE) {
  344. if (icon.attr("collapsedIcon")) icon.attr("src", icon.attr("collapsedIcon"));
  345. } else {
  346. div.removeClass("x-tree-node-expanded").addClass("x-tree-node-collapsed");
  347. }
  348. var delay = options && options.delay ? options.delay : null;
  349. node.children("ul").hide(delay);
  350. }
  351. this.toggle = function(node) {
  352. if (this.expanded(node)) {
  353. this.collapse(node);
  354. } else if (this.collapsed(node)) {
  355. this.expand(node);
  356. }
  357. }
  358. /**
  359. * 设置节点属性
  360. * @param {} node 节点
  361. * @param {} attr 属性名称
  362. * @param {} value 值
  363. */
  364. this.setNodeAttr = function(node, attr, value) {
  365. node = fixNode(node);
  366. if (attr == "text") {
  367. node.children("div:first").children("a").html(value);
  368. } else if (attr == "href") {
  369. node.children("div:first").children("a").attr(attr, value);
  370. } else if (attr == "target") {
  371. node.children("div:first").children("a").attr(attr, value);
  372. } else if (attr == "checked") {
  373. var checkImg = node.children("div:first").children("img:eq(1)");
  374. if(value == true){
  375. checkImg.removeClass("x-node-unchecked").addClass("x-node-check-all");
  376. }else{
  377. checkImg.removeClass("x-node-check-all").addClass("x-node-unchecked");
  378. }
  379. }else {
  380. node.attr(attr, value);
  381. }
  382. }
  383. this.getNodeAttr = function(node, attr) {
  384. node = fixNode(node);
  385. if (node == null) return "";
  386. if (attr == "text") {
  387. r = node.children("div:first").children("a").html();
  388. } else if (attr == "value") {
  389. r = node.attr("val");
  390. } else if (attr == "href" || attr == "target") {
  391. r = node.children("div:first").children("a").attr(attr);
  392. }else if (attr == "checked") {
  393. var checkImg = node.children("div:first").children("img:eq(1)");
  394. r = checkImg.hasClass("x-node-check-all") || checkImg.hasClass("x-node-check-partly");
  395. } else if (attr == "id") {
  396. r = node.attr(attr).substr(options.name.length);
  397. } else {
  398. r = node.attr(attr);
  399. }
  400. if (typeof(r) == 'undefined') {
  401. return "";
  402. }
  403. return r;
  404. }
  405. this.getSelectedNode = function() {
  406. return this.selectedNode;
  407. }
  408. this.getCheckedNodes = function(fn) {
  409. return container.find("li.x-tree-node[canSelect='true']").filter(
  410. function() {
  411. var r = (typeof(fn) == 'undefined' || ($.isFunction(fn) && fn(this))) && $(this).children("div:first").children("img:eq(1)").hasClass("x-node-check-all");
  412. return r;
  413. }
  414. );
  415. }
  416. this.checkAll = function(visible) {
  417. if (visible)
  418. container.find("li.x-tree-node:visible img.x-node-unchecked").filter(function(){
  419. return $(this).is(":visible") && $(this).parents("li.x-tree-node:hidden").length==0;
  420. }).removeClass("x-node-unchecked").addClass("x-node-check-all");
  421. else
  422. container.find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
  423. }
  424. this.uncheckAll = function(visible) {
  425. if (visible)
  426. container.find("li.x-tree-node:visible img.x-node-check-all").filter(function(){
  427. return $(this).is(":visible") && $(this).parents("li.x-tree-node:hidden").length==0;
  428. }).removeClass("x-node-check-all").addClass("x-node-unchecked");
  429. else
  430. container.find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
  431. }
  432. this.getParent = function(node) {
  433. if (node == null || typeof(node) == "undefined")
  434. return null;
  435. return fixNode(node).parents("li:first");
  436. }
  437. this.getAncensor = function(node, attr, val) {
  438. var p = this.getParent(node)
  439. while ( p != null && p.length > 0 ) {
  440. if (this.getNodeAttr(p, attr) == val) {
  441. return p;
  442. }
  443. p = this.getParent(p);
  444. }
  445. return null;
  446. }
  447. this.getChildren = function(node) {
  448. if (node != null)
  449. return fixNode(node).children("ul:first").children("li");
  450. else
  451. return rootNode.children("li");
  452. }
  453. this.getNodes = function(level) {
  454. return rootNode.find("li.x-tree-node[level=" + level + "]");
  455. }
  456. this.getRoot = function() {
  457. return rootNode;
  458. }
  459. this.removeNode = function(node) {
  460. node = fixNode(node);
  461. var prev = node.prev();
  462. var next = node.next();
  463. //本层最后一个节点
  464. if (next.length == 0 && prev.length > 0) {
  465. var preHandler = prev.children("div:first").children("img:first");
  466. if (preHandler.hasClass("x-tree-elbow-line")) {
  467. preHandler.removeClass("x-tree-elbow-line").addClass("x-tree-elbow-end");
  468. } else if (preHandler.hasClass("x-tree-elbow")) {
  469. preHandler.removeClass("x-tree-elbow").addClass("x-tree-elbow-end");
  470. } else {
  471. if (preHandler.hasClass("x-tree-elbow-minus")) {
  472. preHandler.removeClass("x-tree-elbow-minus").addClass("x-tree-elbow-end-minus");
  473. } else if (preHandler.hasClass("x-tree-elbow-plus")) {
  474. preHandler.removeClass("x-tree-elbow-plus").addClass("x-tree-elbow-end-plus");
  475. }
  476. }
  477. prev.find("div > span.x-tree-node-indent").each(function(){
  478. $(this).children("img:eq(" + node.attr("level") + ")")
  479. .removeClass("x-tree-elbow-line").addClass("x-tree-icon");
  480. });
  481. }
  482. //仅有的一个子节点
  483. if (next.length == 0 && prev.length == 0 && node.attr("level") > 0) {
  484. var p = node.parents("li:first");
  485. setToLeaf(p);
  486. }
  487. if (this.selectedNode == node) this.selectedNode = null;
  488. node.get(0).innerHTML = "";
  489. node.remove();
  490. }
  491. this.addNode = function(n, p, expandParent) {
  492. if (typeof(p) == 'string') {
  493. p = $("#" + p);
  494. }
  495. if (options && typeof(options.beforeInitNode) == "function") {
  496. var r = options.beforeInitNode.call(this, n);
  497. if (r == false) {
  498. return null;
  499. }
  500. }
  501. /*
  502. <LI id=402881822a6f66dc012a7065ab8d0001 class=x-tree-node val="CATALOG" level="1" init="n" canSelect="true" jQuery1294638645738="35">
  503. <DIV class="x-tree-node-el x-tree-node-collapsed" jQuery1294638645738="36">
  504. <SPAN class=x-tree-node-indent>
  505. <IMG class=x-tree-icon src="/derscm/resource/images/default/s.gif" width=1 height=1>
  506. </SPAN>
  507. <IMG class="x-tree-ec-icon x-tree-elbow-plus" src="/derscm/resource/images/default/s.gif" width=1 height=1 jQuery1294638645738="33">
  508. <IMG class=x-tree-node-icon src="/derscm/resource/images/default/s.gif" width=1 height=1>
  509. <A class=x-tree-node-anchor jQuery1294638645738="34">大板</A>
  510. </DIV>
  511. <UL style="DISPLAY: none" class="x-tree-node-ct x-tree-lines"></UL>
  512. </LI>
  513. */
  514. var li = document.createElement("LI");
  515. li.className = "x-tree-node";
  516. li.id = options.name + n.id;
  517. li.setAttribute("init", n.init == true ? 'y' : 'n');//是否初始化属性
  518. if (n.value) li.setAttribute("val", n.value); //类别
  519. //设置扩展属性
  520. if (n.extendAttr) {
  521. for (var key in n.extendAttr) {
  522. li.setAttribute(key, n.extendAttr[key]);
  523. }
  524. }
  525. var div = document.createElement("div");
  526. div.className = 'x-tree-node-el';
  527. var indent = $("<span></span>").addClass('x-tree-node-indent');
  528. var level = 0;
  529. if (p != null) { //父节点存在
  530. level = p.attr("level") + 1;
  531. var pIndent = p.find("div:first > span.x-tree-node-indent:first");
  532. indent.append(pIndent.children().clone()); //继承父节点的line style
  533. //根据情况增加自己的line
  534. var imgInd = blankImg();
  535. if(p.next().length == 0) {
  536. //console.log("1");
  537. imgInd.className = "x-tree-icon";
  538. } else {
  539. //console.log("2");
  540. imgInd.className = "x-tree-elbow-line";
  541. }
  542. indent.append(imgInd);
  543. div.appendChild(indent.get(0));
  544. }
  545. li.setAttribute("level", level);
  546. var handlerIcon = blankImg();
  547. div.appendChild(handlerIcon);
  548. if (n.checkbox) {
  549. var checkImg = blankImg();
  550. if (n.checked) {
  551. checkImg.className = "x-tree-node-icon x-node-check-all";
  552. } else {
  553. checkImg.className = "x-tree-node-icon x-node-unchecked";
  554. }
  555. $(checkImg).click(function() {
  556. if ($(this).hasClass("x-node-unchecked")) {
  557. $(this).removeClass("x-node-unchecked").addClass("x-node-check-all");
  558. $(li).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
  559. } else if ($(this).hasClass("x-node-check-all")) {
  560. $(this).removeClass("x-node-check-all").addClass("x-node-unchecked");
  561. $(li).find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
  562. } else if($(this).hasClass("x-node-check-incomplete")){
  563. $(obj).removeClass("x-node-check-incomplete").addClass("x-node-check-all");
  564. $(li).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
  565. $(li).find("img.x-node-check-incomplete").removeClass("x-node-check-incomplete").addClass("x-node-check-all");
  566. }
  567. });
  568. div.appendChild(checkImg);
  569. this.setParentCheckboxStatus(n);
  570. }
  571. var icon = document.createElement("IMG");
  572. icon.className = "x-tree-node-icon";
  573. if (n.expandedIcon || n.collapsedIcon || n.icon) {
  574. div.className += " x-tree-node-cusicon";
  575. icon.src = n.icon;
  576. icon.expandedIcon = n.expandedIcon;
  577. icon.collapsedIcon = n.collapsedIcon;
  578. if (n.expanded && n.expandedIcon) {
  579. icon.src = n.expandedIcon;
  580. } else if (n.collapsedIcon) {
  581. icon.src = n.collapsedIcon;
  582. }
  583. li.setAttribute("hasIcon", true);
  584. } else {
  585. icon.src = __BLANK_IMAGE;
  586. if (n.leaf) {
  587. div.className += " x-tree-node-leaf";
  588. } else {
  589. div.className += n.expanded ? " x-tree-node-expanded" : " x-tree-node-collapsed";
  590. }
  591. }
  592. div.appendChild(icon);
  593. var txt = document.createElement("a");
  594. if (n.href) {
  595. txt.href = n.href;
  596. if (n.target) txt.target = n.target;
  597. }
  598. txt.className = 'x-tree-node-anchor';
  599. txt.innerHTML = n.text;
  600. //txt.appendChild(document.createTextNode(n.text));
  601. div.appendChild(txt);
  602. var childUl = document.createElement("UL");
  603. childUl.className = "x-tree-node-ct x-tree-lines";
  604. if (!n.expanded) childUl.style.display = "none";
  605. li.appendChild(div);
  606. li.appendChild(childUl);
  607. if (p != null) {
  608. p.children("ul:last").append(li);
  609. } else {
  610. rootNode.append(li);
  611. }
  612. var preLi = $(li).prev();
  613. var preHandler = preLi.children("div").children("img:first");
  614. if (preHandler.hasClass("x-tree-elbow-end"))
  615. preHandler.removeClass("x-tree-elbow-end").addClass("x-tree-elbow");
  616. preLi.find("div > span.x-tree-node-indent").each(function(){
  617. $(this).children("img:eq(" + level + ")").removeClass("x-tree-icon").addClass("x-tree-elbow-line");
  618. });
  619. if (n.leaf || ((n.init || n.expanded) && (!n.nodes || n.nodes.length == 0))) {
  620. handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end";
  621. } else {
  622. if (n.expanded) {
  623. handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end-minus";
  624. li.setAttribute("init", 'y');
  625. } else {
  626. handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end-plus";
  627. }
  628. }
  629. if (preHandler.hasClass("x-tree-elbow-end-minus")) {
  630. preHandler.removeClass("x-tree-elbow-end-minus").addClass("x-tree-elbow-minus");
  631. } else if (preHandler.hasClass("x-tree-elbow-end-plus")) {
  632. preHandler.removeClass("x-tree-elbow-end-plus").addClass("x-tree-elbow-plus");
  633. }
  634. if (p != null) {
  635. var pDiv = p.children("div:first");
  636. pDiv.removeClass("x-tree-node-leaf");//去掉叶子图标
  637. var pHandler = pDiv.children("img:first");
  638. if (pHandler.hasClass("x-tree-elbow-end")) {
  639. pHandler.removeClass("x-tree-elbow-end");
  640. if (pDiv.hasClass("x-tree-node-expanded")) {
  641. pHandler.addClass("x-tree-elbow-end-minus");
  642. } else {
  643. pHandler.addClass("x-tree-elbow-end-plus");
  644. }
  645. } else if (pHandler.hasClass("x-tree-elbow")) {
  646. pHandler.removeClass("x-tree-elbow");
  647. if (pDiv.hasClass("x-tree-node-expanded")) {
  648. pHandler.addClass("x-tree-elbow-minus");
  649. } else {
  650. pHandler.addClass("x-tree-elbow-plus");
  651. }
  652. } else if (pHandler.hasClass("x-tree-elbow-line")) {
  653. pHandler.removeClass("x-tree-elbow-line");
  654. if (pDiv.hasClass("x-tree-node-expanded")) {
  655. pHandler.addClass("x-tree-elbow-minus");
  656. } else {
  657. pHandler.addClass("x-tree-elbow-plus");
  658. }
  659. }
  660. if (!pDiv.hasClass("x-tree-node-expanded")) {
  661. if (expandParent) this.expand(p);
  662. }
  663. p.attr("init", 'y');
  664. }
  665. $(handlerIcon).bind("click", this, function(evt) {
  666. evt.data.toggle(li);
  667. });
  668. if (n.canSelect) {
  669. $(txt).bind("click", this, function(evt) {
  670. if (evt.data.selectedNode) {
  671. evt.data.selectedNode.children("div").children("a").removeClass("x-tree-selected");
  672. }
  673. evt.data.selectedNode = $(li);
  674. $(this).addClass("x-tree-selected");
  675. if (options && $.isFunction(options.onselect)) {
  676. options.onselect.call(evt.data.selectedNode, evt.data);
  677. }
  678. });
  679. li.setAttribute("canSelect", "true");
  680. }
  681. if (n.operators && options && options.contextmenu){
  682. $(txt).bind("contextmenu", this, function(evt) {
  683. options.contextmenu.setExtraParams({node:$(li),tree:evt.data});
  684. options.contextmenu.show(evt, n.operators);
  685. if (evt.data.selectedNode) {
  686. evt.data.selectedNode.children("div").children("a").removeClass("x-tree-selected");
  687. }
  688. evt.data.selectedNode = $(li);
  689. $(this).addClass("x-tree-selected");
  690. return false;
  691. });
  692. }
  693. if (!n.leaf) {
  694. $(txt).bind('dblclick', [this, li], function(evt){
  695. evt.data[0].toggle(evt.data[1]);
  696. });
  697. }
  698. if (n.nodes && n.nodes.length > 0){
  699. for (var i = 0; i < n.nodes.length; i++){
  700. this.addNode(n.nodes[i], $(li));
  701. }
  702. }
  703. return li;
  704. }
  705. this.setParentCheckboxStatus = function(node){
  706. var p = this.getParent(node);
  707. var cnodes = this.getChildren(p);
  708. //alert($(node).find("img.x-node-check-all").length);
  709. if($(node).find("img.x-node-check-all").length == cnodes.length){
  710. }
  711. // //当前同级别或低级别的节点是否都选中了
  712. // var isCheckedComplete = $(".x-node-unchecked", p).length == 0;
  713. // //当前同级别或低级别的节点是否都没有选中
  714. // var isCheckedNull = $(".x-node-check-all", p).length == 0;
  715. // if (isCheckedComplete)
  716. // {
  717. // p.prev().find(".l-checkbox")
  718. // .removeClass("x-node-unchecked x-node-check-incomplete")
  719. // .addClass("x-node-check-all");
  720. // }
  721. // else if (isCheckedNull)
  722. // {
  723. // treeitem.parent().prev().find("> .l-checkbox")
  724. // .removeClass("l-checkbox-checked l-checkbox-incomplete")
  725. // .addClass("l-checkbox-unchecked");
  726. // }
  727. // else
  728. // {
  729. // treeitem.parent().prev().find("> .l-checkbox")
  730. // .removeClass("l-checkbox-unchecked l-checkbox-checked")
  731. // .addClass("l-checkbox-incomplete");
  732. // }
  733. // if (treeitem.parent().parent("li").length > 0)
  734. // po.setParentCheckboxStatus(treeitem.parent().parent("li"));
  735. }
  736. this.initalize = function(ncol, p, lev, asyn, recursive) {
  737. var ncount = ncol.length;
  738. for (var ci = 0; ci < ncount; ci ++) {
  739. var n = ncol[ci];
  740. if (options && typeof(options.beforeInitNode) == "function") {
  741. var r = options.beforeInitNode.call(this, n);
  742. if (r == false) {
  743. continue;
  744. }
  745. }
  746. var end = ci == ncount - 1; //最后一个节点
  747. var noChild = n.leaf || (n.nodes != null && n.nodes.length == 0);
  748. var li = document.createElement("LI");
  749. li.className = "x-tree-node";
  750. li.id = options.name + n.id;
  751. li.setAttribute("init", n.init == true || n.nodes != null || n.expanded ? 'y' : 'n');
  752. li.setAttribute("level", lev);
  753. if (n.value) li.setAttribute("val", n.value);
  754. if (n.extendAttr) {
  755. for (var key in n.extendAttr) {
  756. li.setAttribute(key, n.extendAttr[key]);
  757. }
  758. }
  759. var div = document.createElement("div");
  760. div.className = 'x-tree-node-el';
  761. var indent = document.createElement("SPAN");
  762. indent.className = 'x-tree-node-indent';
  763. if (p) {
  764. var pIndent = p.parentNode.firstChild.firstChild.childNodes;
  765. for (var i = 0; i < pIndent.length; i++) {//继承父节点的line style
  766. indent.appendChild(pIndent[i].cloneNode(false));
  767. }
  768. var imgInd = blankImg();
  769. if(p.parentNode.nextSibling == null) {
  770. imgInd.className = "x-tree-icon";
  771. } else {
  772. imgInd.className = "x-tree-elbow-line";
  773. }
  774. indent.appendChild(imgInd);
  775. }
  776. div.appendChild(indent);
  777. var handlerIcon = blankImg();
  778. if (!end) {
  779. handlerIcon.className = n.leaf || noChild ? "x-tree-ec-icon x-tree-elbow" :
  780. (n.expanded ? "x-tree-ec-icon x-tree-elbow-minus" : "x-tree-ec-icon x-tree-elbow-plus");
  781. } else {
  782. handlerIcon.className = n.leaf || noChild ? "x-tree-ec-icon x-tree-elbow-end" :
  783. (n.expanded ? "x-tree-ec-icon x-tree-elbow-end-minus" : "x-tree-ec-icon x-tree-elbow-end-plus");
  784. }
  785. div.appendChild(handlerIcon);
  786. if (n.checkbox) {
  787. var checkImg = blankImg();
  788. if(n.checked == null){
  789. checkImg.className = "x-tree-node-icon x-node-check-incomplete";
  790. } else if (n.checked) {
  791. checkImg.className = "x-tree-node-icon x-node-check-all";
  792. } else {
  793. checkImg.className = "x-tree-node-icon x-node-unchecked";
  794. }
  795. bindEventHandler(checkImg, "click", function(tree, node, evt) {
  796. if ($.isFunction(options.oncheckchanging)) {
  797. options.oncheckchanging(tree, node);
  798. }
  799. if ($(this).hasClass("x-node-unchecked")) {
  800. $(this).removeClass("x-node-unchecked").addClass("x-node-check-all");
  801. if (!evt.ctrlKey)
  802. $(node).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
  803. } else if ($(this).hasClass("x-node-check-all")) {
  804. $(this).removeClass("x-node-check-all").addClass("x-node-unchecked");
  805. if (!evt.ctrlKey)
  806. $(node).find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
  807. }
  808. if ($.isFunction(options.oncheckchanged)) {
  809. options.oncheckchanged(tree, node);
  810. }
  811. }, this, li);
  812. div.appendChild(checkImg);
  813. }
  814. var icon = blankImg();
  815. icon.className = "x-tree-node-icon";
  816. if (n.expandedIcon || n.collapsedIcon || n.icon) {
  817. div.className += " x-tree-node-cusicon";
  818. icon.src = n.icon;
  819. icon.expandedIcon = n.expandedIcon;
  820. icon.collapsedIcon = n.collapsedIcon;
  821. if (n.expanded && n.expandedIcon) {
  822. icon.src = n.expandedIcon;
  823. } else if (n.collapsedIcon) {
  824. icon.src = n.collapsedIcon;
  825. }
  826. li.setAttribute("hasIcon", true);
  827. } else {
  828. icon.src = __BLANK_IMAGE;
  829. if (n.leaf) {
  830. div.className += " x-tree-node-leaf";
  831. } else {
  832. div.className += n.expanded ? " x-tree-node-expanded" : " x-tree-node-collapsed";
  833. }
  834. }
  835. div.appendChild(icon);
  836. var txt = document.createElement("a");
  837. if (n.href) {
  838. txt.href = n.href;
  839. if (n.target) txt.target = n.target;
  840. }
  841. txt.className = 'x-tree-node-anchor';
  842. txt.innerHTML = n.text;
  843. //txt.appendChild(document.createTextNode(n.text));
  844. if (!noChild) {
  845. bindEventHandler(handlerIcon, "click", function(tree, node) {tree.toggle(node)}, this, li);
  846. bindEventHandler(txt, "dblclick", function(tree, node){tree.toggle(node)}, this, li);
  847. }
  848. if (n.canSelect) {
  849. bindEventHandler(txt, "click", function(tree, node) {
  850. if (tree.selectedNode) {
  851. tree.selectedNode.children("div").children("a").removeClass("x-tree-selected");
  852. }
  853. tree.selectedNode = $(node);
  854. $(this).addClass("x-tree-selected");
  855. if (options && $.isFunction(options.onselect)) {
  856. options.onselect.call(tree.selectedNode, tree);
  857. }
  858. }, this, li);
  859. li.setAttribute("canSelect", "true");
  860. }
  861. if (n.operators && options && options.contextmenu) {
  862. bindEventHandler(txt, "contextmenu", function(tree, node, operators, evt) {
  863. options.contextmenu.setExtraParams({node:$(li),tree:tree});
  864. options.contextmenu.show(evt, operators);
  865. if (tree.selectedNode) {
  866. tree.selectedNode.children("div").children("a").removeClass("x-tree-selected");
  867. }
  868. tree.selectedNode = $(node);
  869. $(this).addClass("x-tree-selected");
  870. return false;
  871. }, this, li, n.operators);
  872. }
  873. div.appendChild(txt);
  874. var childUl = document.createElement("UL");
  875. childUl.className = "x-tree-node-ct x-tree-lines";
  876. if (!n.expanded) childUl.style.display = "none";
  877. li.appendChild(div);
  878. li.appendChild(childUl);
  879. if (n.nodes != null && n.nodes.length > 0) {
  880. this.initalize(n.nodes, childUl, parseInt(lev) + 1);
  881. }
  882. if (p != null) {
  883. p.appendChild(li);
  884. } else {
  885. rootNode.get(0).appendChild(li);
  886. }
  887. if (li.getAttribute("init") == 'n' && recursive) {
  888. this.asynInitChildNodes(li, null, false, asyn, recursive);
  889. }
  890. }
  891. }
  892. }