RoadFlow2.1 临时演示

roadui.tree.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //树形控件
  2. ;RoadUI.Tree = function (options)
  3. {
  4. var defaults = {
  5. srcElement: null,
  6. id: "",
  7. showline: true,
  8. showico: true,
  9. showroot: true, //是否显示根节点
  10. animation: 0, //显示速度
  11. path: "", //json路径
  12. refreshpath: "", //刷新加载路径
  13. focusnode: null, //当前焦点节点
  14. onclick: null,
  15. ondblclick: null
  16. };
  17. this.opts = $.extend(defaults, options);
  18. this.$srcObj = null;
  19. if (this.opts.id.isNullOrEmpty() && this.opts.srcElement == null)
  20. {
  21. throw "要初始化为Tree的对象或对象ID为空!"; return false;
  22. }
  23. else
  24. {
  25. this.$srcObj = this.opts.srcElement != null ? $(this.opts.srcElement) : $("#" + this.opts.id);
  26. if (this.$srcObj == null || this.$srcObj.size() == 0)
  27. {
  28. throw "要初始化为Tree的对象为空!"; return false;
  29. }
  30. }
  31. if (this.opts.path.isNullOrEmpty())
  32. {
  33. throw "json路径无效!"; return false;
  34. }
  35. if (!this.opts.id)
  36. {
  37. this.opts.id = this.$srcObj.attr("id");
  38. }
  39. var instance = this;
  40. var $rootObj = this.$srcObj;
  41. this.init = function ()
  42. {
  43. instance.$srcObj.html('');
  44. $.ajax({
  45. type: "get", url: this.opts.path, dataType: "json", async: true, cache: false,
  46. success: function (json)
  47. {
  48. //当用户登录信息丢失时返回 {"soginstatus":-1} 跳转到登录页面
  49. if (json && json.loginstatus && -1 == json.loginstatus)
  50. {
  51. top.login();
  52. return;
  53. }
  54. //
  55. instance.addNodes(json, $rootObj, false, true);
  56. },
  57. error: function (obj, msg) { throw "json加载错误!" + msg; }
  58. });
  59. //添加右键
  60. //try
  61. //{
  62. // var contextMenu = new RoadUI.Menu({ srcElement: this.$srcObj });
  63. // contextMenu.addItem({ ico: "/images/ico/refresh.gif", title: "刷  新", onclick: function () { instance.refresh(instance.focusnode); } });
  64. //}
  65. //catch (e) { }
  66. };
  67. //添加节点 isRefresh:是否二次加载 isInit:是否初始化
  68. this.addNodes = function (json, $node, isRefresh, isInit)
  69. {
  70. if ($node == undefined || $node == null || $node.size() == 0)
  71. {
  72. return;
  73. }
  74. json = json.childs || json;
  75. if (json.length == 0)
  76. {
  77. return;
  78. }
  79. var $preDiv = $(">ul>li", $node);
  80. for (var i = 0; i < json.length; i++)
  81. {
  82. var jsoni = json[i];
  83. var id = jsoni.id || RoadUI.Core.newid(false);
  84. var title = jsoni.title || " ";
  85. var ico = jsoni.ico || "";
  86. var link = jsoni.link || "";
  87. var hasChilds = jsoni.childs && jsoni.childs.length > 0;
  88. var hasChildsInDB = jsoni.hasChilds && parseInt(jsoni.hasChilds) > 0;
  89. var isLast = i == json.length - 1;
  90. var istop = isInit && i == 0;
  91. var isroot = istop && this.opts.showroot && json.length == 1; //是否为根节点
  92. json[i].id = id;
  93. var $div = $('<div id="roadui_tree_' + id + '" class="tree_div"></div>');
  94. var $ul = $('<ul class="tree_ul"></ul>');
  95. $div.data("json", jsoni);
  96. if (isroot)
  97. {
  98. $div.attr("isroot", "1");
  99. $ul.css("padding-left", "0");
  100. }
  101. var jian = 1;
  102. if (this.opts.showico) jian++;
  103. if (this.opts.showline) jian++;
  104. for (var j = 0; j < $preDiv.size() - jian; j++)
  105. {
  106. $ul.append($preDiv.eq(j).clone());
  107. }
  108. if ("1" != $node.attr("isroot") && !isroot && !isInit)
  109. {
  110. var lineClass = this.opts.showline && $node.next().size() > 0 ? "tree_line_conn" : "tree_empty";
  111. var $line = $('<li class="tree_li"><span class="' + lineClass + '"></span></li>');
  112. $ul.append($line);
  113. }
  114. if (this.opts.showline && !isroot)
  115. {
  116. var line1Class = "";
  117. if (hasChilds)
  118. {
  119. if (istop)
  120. {
  121. line1Class = isLast ? "tree_minus_bottom_noprev" : "tree_minus_top_noprev";
  122. }
  123. else
  124. {
  125. line1Class = isLast ? "tree_minus_bottom" : "tree_minus_center";
  126. }
  127. }
  128. else if (hasChildsInDB)
  129. {
  130. if (istop)
  131. {
  132. line1Class = isLast ? "tree_plus_bottom_noprev" : "tree_plus_top_noprev";
  133. }
  134. else
  135. {
  136. line1Class = isLast ? "tree_plus_bottom" : "tree_plus_center";
  137. }
  138. }
  139. else
  140. {
  141. if (istop)
  142. {
  143. line1Class = isLast ? "tree_line_bottom" : "tree_line_top";
  144. }
  145. else
  146. {
  147. line1Class = isLast ? "tree_line_bottom" : "tree_line_center";
  148. }
  149. }
  150. var $line1 = $('<li class="tree_li"><span class="' + line1Class + '"></span></li>');
  151. if (line1Class != "tree_line_bottom" && line1Class != "tree_line_center")
  152. {
  153. $line1.bind("click", function () { instance.toggleNode($(this).parent()); });
  154. }
  155. $ul.append($line1);
  156. }
  157. if (this.opts.showico || isroot)
  158. {
  159. var icoClass = "tree_leaf";
  160. if (isroot)
  161. {
  162. icoClass = "tree_root";
  163. }
  164. else
  165. {
  166. if (hasChilds)
  167. {
  168. icoClass = "tree_open";
  169. }
  170. else if (hasChildsInDB)
  171. {
  172. icoClass = "tree_close";
  173. }
  174. }
  175. var $ico = $('<li class="tree_li"><span class="' + icoClass + '"' + (ico.trim().length > 0 ? ' style="background:url(' + ico + ') no-repeat left center;"' : '') + '></span></li>');
  176. if (icoClass != "tree_leaf")
  177. {
  178. $ico.bind("click", function () { instance.toggleNode($(this).parent()); return false; });
  179. }
  180. $ul.append($ico);
  181. }
  182. var $title = $('<li class="tree_li"><span class="tree_title">' + title + '</span></li>');
  183. $ul.append($title);
  184. if ($.isFunction(this.opts.onclick))
  185. {
  186. $title.bind("click", function ()
  187. {
  188. var jid = $(this).parent().parent().data("json");
  189. instance.opts.onclick(jid);
  190. });
  191. }
  192. if ($.isFunction(this.opts.ondblclick))
  193. {
  194. $title.bind("dblclick", function ()
  195. {
  196. var jid = $(this).parent().parent().data("json");
  197. instance.opts.ondblclick(jid);
  198. });
  199. }
  200. $ul.bind("mouseover", function ()
  201. {
  202. $(this).removeClass().addClass("tree_ul_over");
  203. }).bind("mouseout", function ()
  204. {
  205. if (instance.focusnode == null || instance.focusnode.size() == 0 || instance.focusnode.get(0) !== $(this).get(0))
  206. {
  207. $(this).removeClass().addClass("tree_ul");
  208. }
  209. }).bind("click", function ()
  210. {
  211. if (instance.focusnode != null && instance.focusnode.size() > 0)
  212. {
  213. instance.focusnode.removeClass().addClass("tree_ul");
  214. }
  215. instance.focusnode = $(this);
  216. $(this).removeClass().addClass("tree_ul_over");
  217. }); //.bind("contextmenu", function () { $(this).click(); });
  218. $div.append($ul);
  219. $node.append($div);
  220. }
  221. for (var i = 0; i < json.length; i++)
  222. {
  223. var jsoni = json[i];
  224. if (jsoni.childs && jsoni.childs.length > 0)
  225. {
  226. this.addNodes(jsoni, $("#roadui_tree_" + jsoni.id, $rootObj), isRefresh, false);
  227. }
  228. }
  229. };
  230. this.toggleNode = function ($ul, isRefresh)
  231. {
  232. if (!$ul || $ul.size() == 0)
  233. {
  234. return;
  235. }
  236. var $div = $ul.parent();
  237. if ($div.size() == 0)
  238. {
  239. return;
  240. }
  241. var $childNodes = $(">div", $div);
  242. //从数据库加载
  243. if (isRefresh || $childNodes.size() == 0)
  244. {
  245. toggleLoading($div, true);
  246. var id = "";
  247. var type = 0;
  248. var jsondata = $div.data("json");
  249. if (jsondata && jsondata.type && jsondata.id)
  250. {
  251. id = jsondata.id;
  252. type = jsondata.type;
  253. }
  254. var url = instance.opts.refreshpath;
  255. if (url.indexOf('?') >= 0)
  256. {
  257. url += "&refreshid=" + id + "&type=" + type;
  258. }
  259. else
  260. {
  261. url += "?refreshid=" + id + "&type=" + type;
  262. }
  263. $.ajax({
  264. type: "get", url: url, dataType: "json", async: true, cache: false,
  265. success: function (json)
  266. {
  267. if ($childNodes.length > 0)
  268. {
  269. $childNodes.remove();
  270. }
  271. instance.addNodes(json, $div, true, false);
  272. toggleLoading($div, false);
  273. if (json.length > 0)
  274. {
  275. toggleIco($ul, true);
  276. }
  277. },
  278. error: function (obj, msg) { throw "json加载错误!" + msg; }
  279. });
  280. }
  281. else
  282. {
  283. if ($childNodes.eq(0).is(":hidden"))
  284. {
  285. $childNodes.show(this.opts.animation || 0);
  286. toggleIco($ul, true);
  287. }
  288. else
  289. {
  290. $childNodes.hide(this.opts.animation || 0);
  291. toggleIco($ul, false);
  292. }
  293. }
  294. };
  295. var toggleIco = function ($ul, isShow)
  296. {
  297. var $spans = $(">li>span", $ul);
  298. if (isShow)
  299. {
  300. $spans.filter(".tree_close").removeClass().addClass("tree_open");
  301. $spans.filter(".tree_plus_bottom").removeClass().addClass("tree_minus_bottom");
  302. $spans.filter(".tree_plus_center").removeClass().addClass("tree_minus_center");
  303. $spans.filter(".tree_plus_top").removeClass().addClass("tree_minus_top");
  304. $spans.filter(".tree_plus_top_noprev").removeClass().addClass("tree_minus_top_noprev");
  305. $spans.filter(".tree_plus_bottom_noprev").removeClass().addClass("tree_minus_bottom_noprev");
  306. if ($(">li", $ul).length > 0)
  307. {
  308. $spans.filter(".tree_leaf").removeClass().addClass("tree_open").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
  309. $spans.filter(".tree_line_bottom").removeClass().addClass("tree_minus_bottom").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
  310. $spans.filter(".tree_line_center").removeClass().addClass("tree_minus_center").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
  311. $spans.filter(".tree_line_top").removeClass().addClass("tree_minus_top").parent().bind("click", function () { instance.toggleNode($(this).parent()); });
  312. }
  313. }
  314. else
  315. {
  316. $spans.filter(".tree_open").removeClass().addClass("tree_close");
  317. $spans.filter(".tree_minus_bottom").removeClass().addClass("tree_plus_bottom");
  318. $spans.filter(".tree_minus_center").removeClass().addClass("tree_plus_center");
  319. $spans.filter(".tree_minus_top").removeClass().addClass("tree_plus_top");
  320. $spans.filter(".tree_minus_top_noprev").removeClass().addClass("tree_plus_top_noprev");
  321. $spans.filter(".tree_minus_bottom_noprev").removeClass().addClass("tree_plus_bottom_noprev");
  322. }
  323. };
  324. this.refresh = function (divOrID)
  325. {
  326. if (typeof (divOrID) == "string")
  327. {
  328. var $div = $("#roadui_tree_" + divOrID);
  329. if ($div.size() == 0)
  330. {
  331. return;
  332. }
  333. this.toggleNode($div.children("ul"), true);
  334. }
  335. else
  336. {
  337. this.toggleNode($(divOrID), true);
  338. }
  339. };
  340. var toggleLoading = function ($div, isLoading)
  341. {
  342. if (isLoading)
  343. {
  344. var $ico = $(".tree_close", $(">ul", $div));
  345. $ico.removeClass().addClass("tree_loading");
  346. }
  347. else
  348. {
  349. var $ico = $(".tree_loading", $(">ul", $div));
  350. $ico.removeClass().addClass("tree_open");
  351. }
  352. };
  353. this.init();
  354. }