You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

575 lines
22 KiB

  1. (function ($) {
  2. $.fn.swapClass = function (c1, c2) {
  3. return this.removeClass(c1).addClass(c2);
  4. };
  5. $.fn.switchClass = function (c1, c2) {
  6. if (this.hasClass(c1)) {
  7. return this.swapClass(c1, c2);
  8. }
  9. else {
  10. return this.swapClass(c2, c1);
  11. }
  12. };
  13. $.fn.treeview = function (settings) {
  14. var dfop =
  15. {
  16. method: "GET",
  17. datatype: "json",
  18. url: false,
  19. param: null,
  20. cbiconpath: "/Content/js/wdtree/images/icons/",
  21. icons: ["checkbox_0.png", "checkbox_1.png", "checkbox_2.png"],
  22. showcheck: false,
  23. oncheckboxclick: false,
  24. onnodeclick: false,
  25. cascadecheck: true,
  26. data: null,
  27. clicktoggle: true,
  28. theme: "bbit-tree-arrows",
  29. isTool: false,
  30. nodeTools: []
  31. };
  32. $.extend(dfop, settings);
  33. var treenodes = dfop.data;
  34. var me = $(this);
  35. var id = me.attr("id");
  36. if (id == null || id == "") {
  37. id = "bbtree" + new Date().getTime();
  38. me.attr("id", id);
  39. }
  40. me.height(dfop.height);
  41. if (dfop.slimscroll == true) {
  42. me.css({ "overflow": "auto", "overflow-y": "hidden" });
  43. } else {
  44. me.css({ "overflow": "auto" });
  45. }
  46. var html = [];
  47. buildtree(dfop.data, html);
  48. me.html('');
  49. me.addClass("bbit-tree").append(html.join(""));
  50. InitEvent(me);
  51. html = null;
  52. if (dfop.showcheck) {
  53. for (var i = 0; i < 3; i++) {
  54. var im = new Image();
  55. im.src = dfop.cbiconpath + dfop.icons[i];
  56. }
  57. }
  58. function buildtree(data, ht) {
  59. ht.push("<div class='bbit-tree-bwrap'>");
  60. ht.push("<div class='bbit-tree-body " + id + "'>");
  61. ht.push("<ul class='bbit-tree-root ", dfop.theme, "'>");
  62. if (data && data.length > 0) {
  63. var l = data.length;
  64. for (var i = 0; i < l; i++) {
  65. buildnode(data[i], ht, 0, i, i == l - 1);
  66. }
  67. }
  68. else {
  69. asnyloadc(null, false, function (data) {
  70. if (data && data.length > 0) {
  71. treenodes = data;
  72. dfop.data = data;
  73. if (dfop.description) {
  74. data.unshift({
  75. "id": "",
  76. "text": dfop.description,
  77. "value": "",
  78. "img": "-1",
  79. "parentnodes": "0",
  80. "showcheck": false,
  81. "isexpand": true,
  82. "complete": true,
  83. "hasChildren": false,
  84. "ChildNodes": []
  85. });
  86. }
  87. var l = data.length;
  88. for (var i = 0; i < l; i++) {
  89. buildnode(data[i], ht, 0, i, i == l - 1);
  90. }
  91. }
  92. });
  93. }
  94. ht.push("</ul>");
  95. ht.push("</div>");
  96. ht.push("</div>");
  97. }
  98. function buildnode(nd, ht, deep, path, isend) {
  99. var nid = nd.id.replace(/[^\w]/gi, "_");
  100. ht.push("<li class='bbit-tree-node'>");
  101. var title = nd.title;
  102. if (title) {
  103. title = nd.title;
  104. } else {
  105. title = nd.text;
  106. }
  107. ht.push("<div id='", id, "_", nid, "' tpath='", path, "' unselectable='on' title='", title, "'");
  108. var cs = [];
  109. cs.push("bbit-tree-node-el");
  110. if (nd.hasChildren) {
  111. cs.push(nd.isexpand ? "bbit-tree-node-expanded" : "bbit-tree-node-collapsed");
  112. }
  113. else {
  114. cs.push("bbit-tree-node-leaf");
  115. }
  116. if (nd.classes) { cs.push(nd.classes); }
  117. ht.push(" class='", cs.join(" "), "'>");
  118. ht.push("<span class='bbit-tree-node-indent'>");
  119. if (deep == 1) {
  120. ht.push("<img class='bbit-tree-icon' src='" + dfop.cbiconpath + "s.gif'/>");
  121. }
  122. else if (deep > 1) {
  123. ht.push("<img class='bbit-tree-icon' src='" + dfop.cbiconpath + "s.gif'/>");
  124. for (var j = 1; j < deep; j++) {
  125. ht.push("<img class='bbit-tree-elbow-line' src='" + dfop.cbiconpath + "s.gif'/>");
  126. }
  127. }
  128. ht.push("</span>");
  129. cs.length = 0;
  130. if (nd.hasChildren) {
  131. if (nd.isexpand) {
  132. cs.push(isend ? "bbit-tree-elbow-end-minus" : "bbit-tree-elbow-minus");
  133. }
  134. else {
  135. cs.push(isend ? "bbit-tree-elbow-end-plus" : "bbit-tree-elbow-plus");
  136. }
  137. }
  138. else {
  139. cs.push(isend ? "bbit-tree-elbow-end" : "bbit-tree-elbow");
  140. }
  141. ht.push("<img class='bbit-tree-ec-icon ", cs.join(" "), "' src='" + dfop.cbiconpath + "s.gif'/>");
  142. if (dfop.showcheck && nd.showcheck) {
  143. if (nd.checkstate == null || nd.checkstate == undefined) {
  144. nd.checkstate = 0;
  145. }
  146. ht.push("<img id='", id, "_", nid, "_cb' class='bbit-tree-node-cb' src='", dfop.cbiconpath, dfop.icons[nd.checkstate], "'/>");
  147. }
  148. if (nd.hasChildren) {
  149. if (nd.img == -1) {
  150. ht.push("");
  151. } else
  152. if (!!nd.img) {
  153. ht.push("<i class=\"" + nd.img + "\"></i>");
  154. } else {
  155. ht.push("<i class=\"fa fa-folder-open\" style='width:15px'></i>");
  156. }
  157. } else {
  158. if (nd.img == -1) {
  159. ht.push("");
  160. } else
  161. if (!!nd.img) {
  162. ht.push("<i class=\"" + nd.img + "\"></i>");
  163. } else {
  164. ht.push("<i class=\"fa fa-file-text-o\"></i>");
  165. }
  166. }
  167. ht.push("<a hideFocus class='bbit-tree-node-anchor' tabIndex=1 href='javascript:void(0);'>");
  168. ht.push("<span data-value='" + nd.id + "' class='bbit-tree-node-text' unselectable='on'>", nd.text, "</span>");
  169. ht.push("</a>");
  170. if (dfop.isTool) {
  171. ht.push("<div class='bbit-tree-node-tool'>");
  172. for (var ii in dfop.nodeTools) {
  173. var toolItem = dfop.nodeTools[ii];
  174. ht.push("<span class='" + toolItem.img + "' title='" + toolItem.text + "'></span>");
  175. }
  176. ht.push("</div>");
  177. }
  178. ht.push("</div>");
  179. if (nd.hasChildren) {
  180. if (nd.isexpand) {
  181. ht.push("<ul class='bbit-tree-node-ct' style='z-index: 0; position: static; visibility: visible; top: auto; left: auto;'>");
  182. if (nd.ChildNodes) {
  183. var l = nd.ChildNodes.length;
  184. for (var k = 0; k < l; k++) {
  185. nd.ChildNodes[k].parent = nd;
  186. buildnode(nd.ChildNodes[k], ht, deep + 1, path + "." + k, k == l - 1);
  187. }
  188. }
  189. ht.push("</ul>");
  190. }
  191. else {
  192. ht.push("<ul style='display:none;'>");
  193. if (nd.ChildNodes) {
  194. var l = nd.ChildNodes.length;
  195. for (var k = 0; k < l; k++) {
  196. nd.ChildNodes[k].parent = nd;
  197. buildnode(nd.ChildNodes[k], ht, deep + 1, path + "." + k, k == l - 1);
  198. }
  199. }
  200. ht.push("</ul>");
  201. }
  202. }
  203. ht.push("</li>");
  204. nd.render = true;
  205. }
  206. function getItem(path) {
  207. var ap = path.split(".");
  208. var t = treenodes;
  209. for (var i = 0; i < ap.length; i++) {
  210. if (i == 0) {
  211. t = t[ap[i]];
  212. }
  213. else {
  214. t = t.ChildNodes[ap[i]];
  215. }
  216. }
  217. return t;
  218. }
  219. function check(item, state, type) {
  220. var pstate = item.checkstate;
  221. if (type == 1) {
  222. item.checkstate = state;
  223. }
  224. else {
  225. var cs = item.ChildNodes;
  226. var l = cs.length;
  227. var ch = true;
  228. for (var i = 0; i < l; i++) {
  229. if ((state == 1 && cs[i].checkstate != 1) || state == 0 && cs[i].checkstate != 0) {
  230. ch = false;
  231. break;
  232. }
  233. }
  234. if (ch) {
  235. item.checkstate = state;
  236. }
  237. else {
  238. item.checkstate = 2;
  239. }
  240. }
  241. if (item.render && pstate != item.checkstate) {
  242. var nid = item.id.replace(/[^\w]/gi, "_");
  243. var et = $("#" + id + "_" + nid + "_cb");
  244. if (et.length == 1) {
  245. et.attr("src", dfop.cbiconpath + dfop.icons[item.checkstate]);
  246. }
  247. }
  248. }
  249. function cascade(fn, item, args) {
  250. if (fn(item, args, 1) != false) {
  251. if (item.ChildNodes != null && item.ChildNodes.length > 0) {
  252. var cs = item.ChildNodes;
  253. for (var i = 0, len = cs.length; i < len; i++) {
  254. cascade(fn, cs[i], args);
  255. }
  256. }
  257. }
  258. }
  259. function bubble(fn, item, args) {
  260. var p = item.parent;
  261. while (p) {
  262. if (fn(p, args, 0) === false) {
  263. break;
  264. }
  265. p = p.parent;
  266. }
  267. }
  268. function nodeclick(e) {
  269. var path = $(this).attr("tpath");
  270. var et = e.target || e.srcElement;
  271. var item = getItem(path);
  272. if (et.tagName == "IMG") {
  273. if ($(et).hasClass("bbit-tree-elbow-plus") || $(et).hasClass("bbit-tree-elbow-end-plus")) {
  274. if ($(this).find('i').hasClass('fa-folder')) {
  275. $(this).find('i').swapClass('fa-folder', 'fa-folder-open');
  276. }
  277. var ul = $(this).next();
  278. if (ul.hasClass("bbit-tree-node-ct")) {
  279. ul.slideDown(200);
  280. }
  281. else {
  282. var deep = path.split(".").length;
  283. if (item.complete) {
  284. item.ChildNodes != null && asnybuild(item.ChildNodes, deep, path, ul, item);
  285. }
  286. else {
  287. $(this).addClass("bbit-tree-node-loading");
  288. asnyloadc(item, true, function (data) {
  289. item.complete = true;
  290. item.ChildNodes = data;
  291. asnybuild(data, deep, path, ul, item);
  292. });
  293. }
  294. }
  295. if ($(et).hasClass("bbit-tree-elbow-plus")) {
  296. $(et).swapClass("bbit-tree-elbow-plus", "bbit-tree-elbow-minus");
  297. }
  298. else {
  299. $(et).swapClass("bbit-tree-elbow-end-plus", "bbit-tree-elbow-end-minus");
  300. }
  301. $(this).swapClass("bbit-tree-node-collapsed", "bbit-tree-node-expanded");
  302. }
  303. else if ($(et).hasClass("bbit-tree-elbow-minus") || $(et).hasClass("bbit-tree-elbow-end-minus")) {
  304. if ($(this).find('i').hasClass('fa-folder-open')) {
  305. $(this).find('i').swapClass('fa-folder-open', 'fa-folder');
  306. }
  307. $(this).next().slideUp(200);
  308. if ($(et).hasClass("bbit-tree-elbow-minus")) {
  309. $(et).swapClass("bbit-tree-elbow-minus", "bbit-tree-elbow-plus");
  310. }
  311. else {
  312. $(et).swapClass("bbit-tree-elbow-end-minus", "bbit-tree-elbow-end-plus");
  313. }
  314. $(this).swapClass("bbit-tree-node-expanded", "bbit-tree-node-collapsed");
  315. }
  316. else if ($(et).hasClass("bbit-tree-node-cb")) {
  317. var s = item.checkstate != 1 ? 1 : 0;
  318. var r = true;
  319. if (dfop.oncheckboxclick) {
  320. r = dfop.oncheckboxclick.call(et, item, s);
  321. }
  322. if (r != false) {
  323. if (dfop.cascadecheck) {
  324. cascade(check, item, s);
  325. bubble(check, item, s);
  326. }
  327. else {
  328. check(item, s, 1);
  329. }
  330. }
  331. }
  332. }
  333. else {
  334. if (dfop.citem) {
  335. var nid = dfop.citem.id.replace(/[^\w]/gi, "_");
  336. $("." + id).removeClass("bbit-tree-selected");
  337. }
  338. dfop.citem = item;
  339. $("." + id).find('div').removeClass("bbit-tree-selected");
  340. $(this).addClass("bbit-tree-selected");
  341. if (dfop.onnodeclick) {
  342. if (!item.expand) {
  343. item.expand = function () { expandnode.call(item); };
  344. }
  345. dfop.onnodeclick.call(this, item);
  346. }
  347. }
  348. }
  349. function expandnode() {
  350. var item = this;
  351. var nid = item.id.replace(/[^\w]/gi, "_");
  352. var img = $("#" + id + "_" + nid + " img.bbit-tree-ec-icon");
  353. if (img.length > 0) {
  354. img.click();
  355. }
  356. }
  357. function asnybuild(nodes, deep, path, ul, pnode) {
  358. var l = nodes.length;
  359. if (l > 0) {
  360. var ht = [];
  361. for (var i = 0; i < l; i++) {
  362. nodes[i].parent = pnode;
  363. buildnode(nodes[i], ht, deep, path + "." + i, i == l - 1);
  364. }
  365. ul.html(ht.join(""));
  366. ht = null;
  367. InitEvent(ul);
  368. }
  369. ul.addClass("bbit-tree-node-ct").css({ "z-index": 0, position: "static", visibility: "visible", top: "auto", left: "auto", display: "" });
  370. ul.prev().removeClass("bbit-tree-node-loading");
  371. }
  372. function asnyloadc(pnode, isAsync, callback) {
  373. if (dfop.url) {
  374. if (pnode && pnode != null)
  375. var param = builparam(pnode);
  376. if (dfop.param != null) {
  377. var param = dfop.param
  378. }
  379. $.ajax({
  380. type: dfop.method,
  381. url: dfop.url,
  382. data: param,
  383. async: isAsync,
  384. dataType: dfop.datatype,
  385. success: callback,
  386. error: function (e) { dialogMsg("������δ��Ӧ��", -1); }
  387. });
  388. }
  389. }
  390. function builparam(node) {
  391. var p = [{ name: "id", value: encodeURIComponent(node.id) }
  392. , { name: "text", value: encodeURIComponent(node.text) }
  393. , { name: "value", value: encodeURIComponent(node.value) }
  394. , { name: "checkstate", value: node.checkstate }];
  395. return p;
  396. }
  397. function bindevent() {
  398. $(this).hover(function () {
  399. $(this).addClass("bbit-tree-node-over");
  400. }, function () {
  401. $(this).removeClass("bbit-tree-node-over");
  402. }).click(nodeclick)
  403. .find("img.bbit-tree-ec-icon").each(function (e) {
  404. if (!$(this).hasClass("bbit-tree-elbow")) {
  405. $(this).hover(function () {
  406. $(this).parent().addClass("bbit-tree-ec-over");
  407. }, function () {
  408. $(this).parent().removeClass("bbit-tree-ec-over");
  409. });
  410. }
  411. });
  412. }
  413. function InitEvent(parent) {
  414. var nodes = $("li.bbit-tree-node>div", parent);
  415. nodes.each(bindevent);
  416. }
  417. function reflash(itemId) {
  418. var nid = itemId.replace(/[^\w-]/gi, "_");
  419. var node = $("#" + id + "_" + nid);
  420. if (node.length > 0) {
  421. node.addClass("bbit-tree-node-loading");
  422. var isend = node.hasClass("bbit-tree-elbow-end") || node.hasClass("bbit-tree-elbow-end-plus") || node.hasClass("bbit-tree-elbow-end-minus");
  423. var path = node.attr("tpath");
  424. var deep = path.split(".").length;
  425. var item = getItem(path);
  426. if (item) {
  427. asnyloadc(item, true, function (data) {
  428. item.complete = true;
  429. item.ChildNodes = data;
  430. item.isexpand = true;
  431. if (data && data.length > 0) {
  432. item.hasChildren = true;
  433. }
  434. else {
  435. item.hasChildren = false;
  436. }
  437. var ht = [];
  438. buildnode(item, ht, deep - 1, path, isend);
  439. ht.shift();
  440. ht.pop();
  441. var li = node.parent();
  442. li.html(ht.join(""));
  443. ht = null;
  444. InitEvent(li);
  445. bindevent.call(li.find(">div"));
  446. });
  447. }
  448. }
  449. }
  450. function getck(items, c, fn) {
  451. for (var i = 0, l = items.length; i < l; i++) {
  452. (items[i].showcheck == true && items[i].checkstate == 1) && c.push(fn(items[i]));
  453. if (items[i].ChildNodes != null && items[i].ChildNodes.length > 0) {
  454. getck(items[i].ChildNodes, c, fn);
  455. }
  456. }
  457. }
  458. function getCkAndHalfCk(items, c, fn) {
  459. for (var i = 0, l = items.length; i < l; i++) {
  460. (items[i].showcheck == true && (items[i].checkstate == 1 || items[i].checkstate == 2)) && c.push(fn(items[i]));
  461. if (items[i].ChildNodes != null && items[i].ChildNodes.length > 0) {
  462. getCkAndHalfCk(items[i].ChildNodes, c, fn);
  463. }
  464. }
  465. }
  466. me[0].t = {
  467. getSelectedNodes: function (gethalfchecknode) {
  468. var s = [];
  469. if (gethalfchecknode) {
  470. getCkAndHalfCk(treenodes, s, function (item) { return item; });
  471. }
  472. else {
  473. getck(treenodes, s, function (item) { return item; });
  474. }
  475. return s;
  476. },
  477. getSelectedValues: function () {
  478. var s = [];
  479. getck(treenodes, s, function (item) { return item.value; });
  480. return s;
  481. },
  482. getCurrentItem: function () {
  483. return dfop.citem;
  484. },
  485. reflash: function (itemOrItemId) {
  486. var id;
  487. if (typeof (itemOrItemId) == "string") {
  488. id = itemOrItemId;
  489. }
  490. else {
  491. id = itemOrItemId.id;
  492. }
  493. reflash(id);
  494. }
  495. };
  496. return me;
  497. };
  498. $.fn.getCheckedNodes = function () {
  499. var $id = $(this);
  500. var _length = $id.attr('id').trim().length + 1;
  501. var value = []
  502. $id.find('.bbit-tree-node-cb').each(function () {
  503. var _src = $(this).attr('src');
  504. _src = _src.substr(_src.lastIndexOf("/") + 1);
  505. if (_src == 'checkbox_1.png' || _src == 'checkbox_2.png') {
  506. var _value = $(this).attr('id').substring(parseInt(_length)).replace(/_/g, "-");
  507. _value = _value.substring(0, _value.length - 3);
  508. value.push(_value)
  509. }
  510. });
  511. return value;
  512. };
  513. $.fn.setCheckedNodes = function (data) {
  514. var $id = $(this);
  515. var id = $id.attr('id').trim();
  516. $.each(data, function (i, item) {
  517. var object = $id.find(('#' + id + '_' + item.replace(/-/g, "_") + '_cb'));
  518. if (object.length != 0) {
  519. object.trigger("click");
  520. }
  521. });
  522. }
  523. $.fn.setCheckedNodeOne = function (data) {
  524. var $id = $(this);
  525. var id = $id.attr('id').trim();
  526. var object = $id.find(('#' + id + '_' + data.replace(/-/g, "_") + '_cb'));
  527. if (object.length != 0) {
  528. object.trigger("click");
  529. }
  530. }
  531. $.fn.setNoCheckedNodes = function (item) {
  532. var $id = $(this);
  533. var id = $id.attr('id').trim();
  534. var object = $id.find(('#' + id + '_' + item.replace(/-/g, "_") + '_cb'));
  535. var _src = object.attr('src');
  536. object.attr('src', _src.replace('checkbox_1.png', 'checkbox_0.png'));
  537. }
  538. $.fn.getTSNs = function (gethalfchecknode) {
  539. if (this[0].t) {
  540. return this[0].t.getSelectedNodes(gethalfchecknode);
  541. }
  542. return null;
  543. };
  544. $.fn.getCurrentNode = function () {
  545. if (this[0].t) {
  546. return this[0].t.getCurrentItem();
  547. }
  548. return null;
  549. };
  550. $.fn.reflash = function (ItemOrItemId) {
  551. if (this[0].t) {
  552. return this[0].t.reflash(ItemOrItemId);
  553. }
  554. };
  555. $.fn.setTreeHeight = function (height) {
  556. var me = $(this);
  557. me.height(height);
  558. }
  559. $.fn.setNodeChecked = function (value) {
  560. var $id = $(this);
  561. var id = $id.attr('id').trim();
  562. $id.find('.bbit-tree-selected').removeClass('bbit-tree-selected');
  563. var object = $id.find(('#' + id + '_' + value.replace(/-/g, "_")));
  564. object.addClass('bbit-tree-selected');
  565. }
  566. })(jQuery);