ks-downtree.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. HcksTreeview.prototype.loaded = false;
  2. $.fn.extend({
  3. treeInput : function(opt) {
  4. this.each(function() {
  5. var $this = $(this);
  6. var treeName = this.getAttribute("id");
  7. if (!treeName || treeName.length == 0) {
  8. treeName = this.getAttribute("name");
  9. }
  10. var multipleChooser = $this.is("div");
  11. var tree = $this.data("tree");
  12. if (tree == null) {
  13. tree = {};
  14. $this.data("multipleChooser", multipleChooser);
  15. var treePanel = $("<div></div>");
  16. treePanel.addClass("x-tree-dropdown-panel");
  17. $("body").append(treePanel);
  18. var treePanelCover = null;
  19. if ($.browser.msie && $.browser.version < 7) {
  20. treePanelCover = $("<iframe frameborder='0'></iframe>");
  21. treePanelCover.addClass("x-tree-dropdown-panel").css("z-index", "1000");
  22. $("body").append(treePanelCover);
  23. }
  24. $(document).mouseup(function(evt){
  25. if (treePanel.is(":hidden")) return true;
  26. var p = evt.target;
  27. while(p) {
  28. if (p == treePanel.get(0)) {
  29. break;
  30. }
  31. p = p.parentNode;
  32. }
  33. if (p == null || typeof(p) == 'undefined') {
  34. treePanel.hide();
  35. if (treePanelCover) treePanelCover.hide();
  36. }
  37. });
  38. tree.panel = treePanel;
  39. tree.cover = treePanelCover;
  40. if (multipleChooser) {
  41. var btnTab = $("<table width='100%'><tr><td width=100 align=center></td><td width=1></td><td></td><td align='right'></td></tr></table>").addClass("header");
  42. var okBtn = $("<input type='button' class='button' value='确定'/>");
  43. var canBtn = $("<input type='button' class='button' value='取消'/>");
  44. if (opt.selectAll) {
  45. var selAllBtn = $("<input type='checkbox' />");
  46. $("tr td:first-child", btnTab).append(selAllBtn).append("全选/反选");
  47. selAllBtn.click(function() {
  48. if (this.checked)
  49. tree.tree.checkAll(true);
  50. else
  51. tree.tree.uncheckAll(true);
  52. });
  53. }
  54. if (opt.allowFilter) {
  55. var txtFilterDealer = $("<input type='text' class='text' size='8'/>");
  56. $("tr td:nth-child(2)", btnTab).append(txtFilterDealer);
  57. txtFilterDealer.keyup(function(evt){
  58. if (this.getAttribute('lastWord') != this.value) {
  59. var nodes = tree.tree.getRoot().get(0).getElementsByTagName("li");
  60. if (this.value.length > 0) {
  61. var filterText = this.value;
  62. for (var i = 0; i < nodes.length; i++) {
  63. var node = nodes[i];
  64. var filterResult = opt.filter.call(node, tree.tree, filterText, $this);
  65. if (filterResult >= 0) {
  66. var vis = filterResult == 1;
  67. if (vis) {
  68. if (node.style.display == 'none') node.style.display = 'block';
  69. } else {
  70. if (node.style.display != 'none') node.style.display = 'none';
  71. }
  72. if (node.getAttribute("level") == "0")
  73. continue;
  74. while (!node.nextSibling) {
  75. if (!vis) {
  76. var prevSibling = node.previousSibling;
  77. while(prevSibling != null) {
  78. if (prevSibling.style.display != 'none') {
  79. vis = true;
  80. break;
  81. }
  82. prevSibling = prevSibling.previousSibling;
  83. }
  84. }
  85. node = node.parentNode.parentNode;
  86. if (node.getAttribute("level") == null) {
  87. break;
  88. }
  89. node.style.display = vis ? 'block' : 'none';
  90. }
  91. }
  92. }
  93. } else {
  94. for (var i = 0; i < nodes.length; i++) {
  95. if (nodes[i].style.display == 'none')
  96. nodes[i].style.display = 'block';
  97. }
  98. }
  99. }
  100. this.setAttribute('lastWord', this.value);
  101. });
  102. }
  103. $("tr td:last-child", btnTab).append(okBtn).append("&nbsp;").append(canBtn);
  104. var mainPanel = $("<div style='padding:4px;overflow:auto'></div>")
  105. .css({height:treePanel.height() - 40});
  106. if ($.browser.msie && $.browser.version < 7.0)
  107. mainPanel.css({width:'98%'});
  108. treePanel.css({"overflow":"hidden"});
  109. treePanel.append(btnTab);
  110. treePanel.append(mainPanel);
  111. canBtn.click(function(){
  112. tree.hide();
  113. });
  114. okBtn.click(function(){
  115. $this.contents().not("a").not($("#selView")[0]).remove();
  116. var nodes = tree.tree.getCheckedNodes();
  117. var v = $("#selView");
  118. v.html("");
  119. nodes.each(function(i){
  120. var hn = $this.attr("name");
  121. if ($.isFunction(opt.getSubmitName)) {
  122. hn = opt.getSubmitName.call($this, this, tree.tree);
  123. }
  124. var hd = tree.tree.getNodeAttr(this, "text");
  125. if ($.isFunction(opt.getSelectDesc)) {
  126. hd = opt.getSelectDesc.call($this, this, tree.tree);
  127. }
  128. var hv = tree.tree.getNodeAttr(this, "id");
  129. if ($(":checkbox[name='" + hn + "'][value='" + hv + "']", $this).length > 0) {
  130. return;
  131. }
  132. var hidValue = $("<input class='value' type='hidden'/>").val(hv);
  133. hidValue.attr("name", hn);
  134. $this.append(hidValue);
  135. var hidDesc = $("<input type='hidden' class='desc'/>").val(hd);
  136. hidDesc.attr("name", hn + "_DESC");
  137. $this.append(hidDesc);
  138. // //ls add
  139. // if(i==0){
  140. // v.append(hd).append(" ; ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  141. // }else if( i== nodes.length-1){
  142. // if((i+1) % 3 == 0){
  143. // v.append(hd).append(" ; ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>");
  144. // }else{
  145. // v.append(hd).append(" ; ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  146. // }
  147. // }else {
  148. // if((i+1) % 3 == 0){
  149. // v.append(hd).append(" ; ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>");
  150. // }else{
  151. // v.append(hd).append(" ; ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  152. // }
  153. // }
  154. // if( i== nodes.length-1){
  155. // if(i % 3 == 0 && i !=0 ){
  156. // v.append(hd);
  157. // }else{
  158. //
  159. // }
  160. // }else{
  161. // if(i % 3 == 0 && i !=0 ){
  162. // v.append(hd).append("; ").append("&nbsp;<br/>");
  163. // }else{
  164. // v.append(hd).append("; ").append("&nbsp;");
  165. // }
  166. //
  167. // }
  168. if ($this.hasClass("checkbox")) {
  169. var cb = $("<input type='checkbox' checked='checked'/>")
  170. .attr("dataName", hn)
  171. .attr("dataValue", hidValue.val())
  172. .attr("title", hd);
  173. cb.bind("click", function(e) {
  174. var dataName = this.getAttribute("dataName");
  175. var dataValue = this.getAttribute("dataValue");
  176. if (this.checked) {
  177. $this.append($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
  178. $this.append($("<input class='desc' type='hidden'/>").attr("name", dataName + "_DESC").attr("value", this.title));
  179. } else {
  180. $this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
  181. $this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
  182. }
  183. });
  184. $this.append(cb);
  185. $this.append(cb.attr("title"));
  186. }
  187. });
  188. if (!$this.hasClass("checkbox")) {
  189. $("span", $this).text(nodes.length);
  190. }
  191. if (opt && opt.onSelected) {
  192. opt.onSelected.call($this, true, nodes, tree.tree);
  193. }
  194. tree.hide();
  195. });
  196. } else {
  197. treePanel.prepend($("<IMG alt='清除' class='clear-handler'/>").attr("src", __BLANK_IMAGE).bind("click", tree, function(e) {
  198. $this.val("");
  199. if (opt && $.isFunction(opt.onClear)) {
  200. opt.onClear.call($this);
  201. }
  202. e.data.hide();
  203. }));
  204. treePanel.prepend($("<IMG alt='关闭' class='close-handler'/>").attr("src", __BLANK_IMAGE).bind("click", tree, function(e) {
  205. e.data.hide();
  206. }));
  207. }
  208. tree.selectNode = function(t) {
  209. if (opt && $.isFunction(opt.onSelected)) {
  210. opt.onSelected.call($this, false, t.getSelectedNode(), t);
  211. if(!multipleChooser){
  212. tree.hide();
  213. }
  214. }
  215. var selectedV = t.getNodeAttr(t.getSelectedNode(), "id");
  216. if ($this.attr("onselectedchange") && $this.attr("selectedValue") != selectedV) {
  217. var changeHandler = eval($this.attr("onselectedchange"));
  218. if ($.isFunction(changeHandler))
  219. changeHandler(selectedV, t.getNodeAttr(t.getSelectedNode(), "text"));
  220. }
  221. $this.attr("selectedValue", selectedV);
  222. }
  223. var treeView = new HcksTreeview(
  224. multipleChooser ? treePanel.get(0).lastChild : treePanel, {
  225. url: __CONTEXT_PATH + opt.ajaxUrl,
  226. onselect:tree.selectNode,
  227. oncheckchanging : function(t, n){
  228. if(t.getNodeAttr(n, 'init') == 'n') {
  229. t.asynInitChildNodes(n, null, false, false, true);
  230. }
  231. },
  232. beforeAsyn : function(n, p) {
  233. if ($.isFunction(opt.extraParams)) {
  234. var params = opt.extraParams($this);
  235. for (var k in params) {
  236. p[k] = params[k];
  237. }
  238. }
  239. }
  240. });
  241. tree.tree = treeView;
  242. tree.show = function(x, y) {
  243. this.panel.css("left", x).css("top", y);
  244. this.panel.show();
  245. if (this.cover) this.cover.css("left", x).css("top", y).show();
  246. if (this.tree.loaded == false) {
  247. this.tree.asynInitChildNodes(null, null, false);
  248. this.tree.loaded = true;
  249. }
  250. }
  251. tree.tryLoad = function() {
  252. if (this.tree.loaded == false) {
  253. this.tree.asynInitChildNodes(null, null, false);
  254. this.tree.loaded = true;
  255. }
  256. }
  257. tree.hide = function() {
  258. this.panel.hide();
  259. if (this.cover) this.cover.hide();
  260. }
  261. tree.isHidden = function() {
  262. return this.panel.is(":hidden");
  263. }
  264. tree.refresh = function() {
  265. $this.flushCache();
  266. return this.tree.asynInitChildNodes(null, null, false);
  267. }
  268. $this.data("tree", tree);
  269. }
  270. if (!multipleChooser) {
  271. $this.click(function(e) {
  272. var pos = $this.offset();
  273. if (pos.left + $this.width() - e.clientX < 18) {
  274. if (tree.isHidden()) {
  275. tree.show(pos.left, pos.top + $this.height() + 5);
  276. } else {
  277. tree.hide();
  278. }
  279. }
  280. });
  281. $this.hover(
  282. function(){$this.addClass("chooserMouseOver")},
  283. function(){$this.removeClass("chooserMouseOver")}
  284. );
  285. } else {
  286. var btn = $("<a class='btn' href='javascript:void(0)'>请选择 </a>");
  287. var selections = $this.children(":hidden.value");
  288. if (!$this.hasClass("checkbox")) {
  289. var linkSelected = $("<a href='javascript:void(0)'>(已选 <span style='font-weight:bold'>" + selections.length + "</span>)</a>");
  290. var selectedViews = $("(<div id='selView'></div>)");
  291. $this.prepend("&nbsp;").prepend(selectedViews).prepend("&nbsp;").prepend(linkSelected).prepend("&nbsp;").prepend(btn);
  292. linkSelected.click(function() {
  293. var pan = $("#__treeChooserSelection");
  294. if (pan.length == 0) {
  295. pan = $("<div id='__treeChooserSelection' class='x-dropdowntree-selections'><div class='title'>已选择列表</div><div class='list'><table width='100%' cellpadding=0 cellspacing=0></table></div></div>")
  296. var chooserSelectionCover = null;
  297. if ($.browser.msie && $.browser.version < 7) {
  298. chooserSelectionCover = $("<iframe frameborder='0'></iframe>");
  299. chooserSelectionCover.addClass("x-dropdowntree-selections").css("z-index", "1000");
  300. $("body").append(chooserSelectionCover);
  301. }
  302. pan.dblclick(function(){
  303. $(this).hide()
  304. if (chooserSelectionCover) chooserSelectionCover.hide();
  305. });
  306. pan.children("div.title").prepend($("<IMG alt='关闭' class='close-handler'/>").attr("src", __BLANK_IMAGE).bind("click", function(e) {
  307. pan.hide();
  308. if (chooserSelectionCover) chooserSelectionCover.hide();
  309. }));
  310. $(document.body).append(pan);
  311. }
  312. var tab = $("table:first", pan);
  313. tab.empty();
  314. selections = $this.children(":hidden.value");
  315. var selectionTitles = $this.children(":hidden.desc");
  316. for (var i = 0; i < selections.length; i ++) {
  317. if ($(":checkbox[name='" + selections.get(i).name + "'][value='" + selections.get(i).value + "']", $this).length > 0) {
  318. continue;
  319. }
  320. var sr = $("<tr><td width='1'></td><td align='left'></td></tr>")
  321. .css("background-color", i % 2 == 0 ? "#fff":"#eee");
  322. var title = "";
  323. if (selectionTitles.length == selections.length) {
  324. title = selectionTitles.get(i).value;
  325. } else {
  326. var tn = document.getElementById(selections.get(i).value);
  327. if (typeof(tn) != 'undefined' && tn.level) {
  328. title = tree.tree.getNodeAttr(tn, "text");
  329. }
  330. }
  331. var cb = $("<input type='checkbox' checked='checked'/>")
  332. .attr("dataName", selections.get(i).name)
  333. .attr("dataValue", selections.get(i).value)
  334. .attr("title", title);
  335. var counter = selections.length;
  336. cb.bind("click", function(e) {
  337. var dataName = this.getAttribute("dataName");
  338. var dataValue = this.getAttribute("dataValue");
  339. if (this.checked) {
  340. $this.append($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
  341. $this.append($("<input class='desc' type='hidden'/>").attr("name", dataName + "_DESC").attr("value", this.title));
  342. counter ++;
  343. } else {
  344. $this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
  345. $this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
  346. counter--;
  347. }
  348. $("span", $this).text(counter.toString());
  349. });
  350. sr.children(":first-child").append(cb);
  351. sr.children(":last-child").html(cb.attr("title"));
  352. tab.append(sr);
  353. }
  354. var spos = $(this).offset();
  355. pan.css({"left":spos.left, "top":spos.top}).show();
  356. if (chooserSelectionCover) chooserSelectionCover.css({"left":spos.left, "top":spos.top}).show();
  357. });
  358. } else {
  359. $this.prepend(btn);
  360. var selections = $this.children(":hidden.value");
  361. var selectionTitles = $this.children(":hidden.desc");
  362. for (var i = 0; i < selections.length; i++) {
  363. var f = ":checkbox[name='" + selections.get(i).name + "'][value='" + selections.get(i).value + "']";
  364. if ($(f).length > 0) {
  365. continue;
  366. }
  367. var title = "";
  368. if (selectionTitles.length == selections.length) {
  369. title = selectionTitles.get(i).value;
  370. } else {
  371. var tn = document.getElementById(selections.get(i).value);
  372. if (typeof(tn) != 'undefined' && tn.level) {
  373. title = tree.tree.getNodeAttr(tn, "text");
  374. }
  375. }
  376. var cb = $("<input type='checkbox' checked='checked'/>")
  377. .attr("dataName", selections.get(i).name)
  378. .attr("dataValue", selections.get(i).value)
  379. .attr("title", title);
  380. cb.bind("click", function(e) {
  381. var dataName = this.getAttribute("dataName");
  382. var dataValue = this.getAttribute("dataValue");
  383. if (this.checked) {
  384. $this.append($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
  385. $this.append($("<input class='desc' type='hidden'/>").attr("name", dataName + "_DESC").attr("value", this.title));
  386. } else {
  387. $this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
  388. $this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
  389. }
  390. });
  391. $this.append(cb);
  392. $this.append(cb.attr("title"));
  393. }
  394. }
  395. btn.click (function() {
  396. if (tree.isHidden()) {
  397. var pos = $(this).offset();
  398. tree.show(pos.left, pos.top);
  399. } else {
  400. tree.hide();
  401. }
  402. });
  403. }
  404. });
  405. }
  406. });