83dd2e1d07d9f88e9a0515b5828a07cbcd30de76.svn-base 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * JQuery zTree exedit v3.5.17
  3. * http://zTree.me/
  4. *
  5. * Copyright (c) 2010 Hunter.z
  6. *
  7. * Licensed same as jquery - MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * email: hunter.z@263.net
  11. * Date: 2014-05-08
  12. */
  13. (function($){
  14. //default consts of exedit
  15. var _consts = {
  16. event: {
  17. DRAG: "ztree_drag",
  18. DROP: "ztree_drop",
  19. RENAME: "ztree_rename",
  20. DRAGMOVE:"ztree_dragmove"
  21. },
  22. id: {
  23. EDIT: "_edit",
  24. INPUT: "_input",
  25. REMOVE: "_remove"
  26. },
  27. move: {
  28. TYPE_INNER: "inner",
  29. TYPE_PREV: "prev",
  30. TYPE_NEXT: "next"
  31. },
  32. node: {
  33. CURSELECTED_EDIT: "curSelectedNode_Edit",
  34. TMPTARGET_TREE: "tmpTargetzTree",
  35. TMPTARGET_NODE: "tmpTargetNode"
  36. }
  37. },
  38. //default setting of exedit
  39. _setting = {
  40. edit: {
  41. enable: false,
  42. editNameSelectAll: false,
  43. showRemoveBtn: true,
  44. showRenameBtn: true,
  45. removeTitle: "remove",
  46. renameTitle: "rename",
  47. drag: {
  48. autoExpandTrigger: false,
  49. isCopy: true,
  50. isMove: true,
  51. prev: true,
  52. next: true,
  53. inner: true,
  54. minMoveSize: 5,
  55. borderMax: 10,
  56. borderMin: -5,
  57. maxShowNodeNum: 5,
  58. autoOpenTime: 500
  59. }
  60. },
  61. view: {
  62. addHoverDom: null,
  63. removeHoverDom: null
  64. },
  65. callback: {
  66. beforeDrag:null,
  67. beforeDragOpen:null,
  68. beforeDrop:null,
  69. beforeEditName:null,
  70. beforeRename:null,
  71. onDrag:null,
  72. onDragMove:null,
  73. onDrop:null,
  74. onRename:null
  75. }
  76. },
  77. //default root of exedit
  78. _initRoot = function (setting) {
  79. var r = data.getRoot(setting), rs = data.getRoots();
  80. r.curEditNode = null;
  81. r.curEditInput = null;
  82. r.curHoverNode = null;
  83. r.dragFlag = 0;
  84. r.dragNodeShowBefore = [];
  85. r.dragMaskList = new Array();
  86. rs.showHoverDom = true;
  87. },
  88. //default cache of exedit
  89. _initCache = function(treeId) {},
  90. //default bind event of exedit
  91. _bindEvent = function(setting) {
  92. var o = setting.treeObj;
  93. var c = consts.event;
  94. o.bind(c.RENAME, function (event, treeId, treeNode, isCancel) {
  95. tools.apply(setting.callback.onRename, [event, treeId, treeNode, isCancel]);
  96. });
  97. o.bind(c.DRAG, function (event, srcEvent, treeId, treeNodes) {
  98. tools.apply(setting.callback.onDrag, [srcEvent, treeId, treeNodes]);
  99. });
  100. o.bind(c.DRAGMOVE,function(event, srcEvent, treeId, treeNodes){
  101. tools.apply(setting.callback.onDragMove,[srcEvent, treeId, treeNodes]);
  102. });
  103. o.bind(c.DROP, function (event, srcEvent, treeId, treeNodes, targetNode, moveType, isCopy) {
  104. tools.apply(setting.callback.onDrop, [srcEvent, treeId, treeNodes, targetNode, moveType, isCopy]);
  105. });
  106. },
  107. _unbindEvent = function(setting) {
  108. var o = setting.treeObj;
  109. var c = consts.event;
  110. o.unbind(c.RENAME);
  111. o.unbind(c.DRAG);
  112. o.unbind(c.DRAGMOVE);
  113. o.unbind(c.DROP);
  114. },
  115. //default event proxy of exedit
  116. _eventProxy = function(e) {
  117. var target = e.target,
  118. setting = data.getSetting(e.data.treeId),
  119. relatedTarget = e.relatedTarget,
  120. tId = "", node = null,
  121. nodeEventType = "", treeEventType = "",
  122. nodeEventCallback = null, treeEventCallback = null,
  123. tmp = null;
  124. if (tools.eqs(e.type, "mouseover")) {
  125. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  126. if (tmp) {
  127. tId = tools.getNodeMainDom(tmp).id;
  128. nodeEventType = "hoverOverNode";
  129. }
  130. } else if (tools.eqs(e.type, "mouseout")) {
  131. tmp = tools.getMDom(setting, relatedTarget, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  132. if (!tmp) {
  133. tId = "remove";
  134. nodeEventType = "hoverOutNode";
  135. }
  136. } else if (tools.eqs(e.type, "mousedown")) {
  137. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  138. if (tmp) {
  139. tId = tools.getNodeMainDom(tmp).id;
  140. nodeEventType = "mousedownNode";
  141. }
  142. }
  143. if (tId.length>0) {
  144. node = data.getNodeCache(setting, tId);
  145. switch (nodeEventType) {
  146. case "mousedownNode" :
  147. nodeEventCallback = _handler.onMousedownNode;
  148. break;
  149. case "hoverOverNode" :
  150. nodeEventCallback = _handler.onHoverOverNode;
  151. break;
  152. case "hoverOutNode" :
  153. nodeEventCallback = _handler.onHoverOutNode;
  154. break;
  155. }
  156. }
  157. var proxyResult = {
  158. stop: false,
  159. node: node,
  160. nodeEventType: nodeEventType,
  161. nodeEventCallback: nodeEventCallback,
  162. treeEventType: treeEventType,
  163. treeEventCallback: treeEventCallback
  164. };
  165. return proxyResult
  166. },
  167. //default init node of exedit
  168. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  169. if (!n) return;
  170. n.isHover = false;
  171. n.editNameFlag = false;
  172. },
  173. //update zTreeObj, add method of edit
  174. _zTreeTools = function(setting, zTreeTools) {
  175. zTreeTools.cancelEditName = function(newName) {
  176. var root = data.getRoot(this.setting);
  177. if (!root.curEditNode) return;
  178. view.cancelCurEditNode(this.setting, newName?newName:null, true);
  179. }
  180. zTreeTools.copyNode = function(targetNode, node, moveType, isSilent) {
  181. if (!node) return null;
  182. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) return null;
  183. var _this = this,
  184. newNode = tools.clone(node);
  185. if (!targetNode) {
  186. targetNode = null;
  187. moveType = consts.move.TYPE_INNER;
  188. }
  189. if (moveType == consts.move.TYPE_INNER) {
  190. function copyCallback() {
  191. view.addNodes(_this.setting, targetNode, [newNode], isSilent);
  192. }
  193. if (tools.canAsync(this.setting, targetNode)) {
  194. view.asyncNode(this.setting, targetNode, isSilent, copyCallback);
  195. } else {
  196. copyCallback();
  197. }
  198. } else {
  199. view.addNodes(this.setting, targetNode.parentNode, [newNode], isSilent);
  200. view.moveNode(this.setting, targetNode, newNode, moveType, false, isSilent);
  201. }
  202. return newNode;
  203. }
  204. zTreeTools.editName = function(node) {
  205. if (!node || !node.tId || node !== data.getNodeCache(this.setting, node.tId)) return;
  206. if (node.parentTId) view.expandCollapseParentNode(this.setting, node.getParentNode(), true);
  207. view.editNode(this.setting, node)
  208. }
  209. zTreeTools.moveNode = function(targetNode, node, moveType, isSilent) {
  210. if (!node) return node;
  211. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) {
  212. return null;
  213. } else if (targetNode && ((node.parentTId == targetNode.tId && moveType == consts.move.TYPE_INNER) || $$(node, this.setting).find("#" + targetNode.tId).length > 0)) {
  214. return null;
  215. } else if (!targetNode) {
  216. targetNode = null;
  217. }
  218. var _this = this;
  219. function moveCallback() {
  220. view.moveNode(_this.setting, targetNode, node, moveType, false, isSilent);
  221. }
  222. if (tools.canAsync(this.setting, targetNode) && moveType === consts.move.TYPE_INNER) {
  223. view.asyncNode(this.setting, targetNode, isSilent, moveCallback);
  224. } else {
  225. moveCallback();
  226. }
  227. return node;
  228. }
  229. zTreeTools.setEditable = function(editable) {
  230. this.setting.edit.enable = editable;
  231. return this.refresh();
  232. }
  233. },
  234. //method of operate data
  235. _data = {
  236. setSonNodeLevel: function(setting, parentNode, node) {
  237. if (!node) return;
  238. var childKey = setting.data.key.children;
  239. node.level = (parentNode)? parentNode.level + 1 : 0;
  240. if (!node[childKey]) return;
  241. for (var i = 0, l = node[childKey].length; i < l; i++) {
  242. if (node[childKey][i]) data.setSonNodeLevel(setting, node, node[childKey][i]);
  243. }
  244. }
  245. },
  246. //method of event proxy
  247. _event = {
  248. },
  249. //method of event handler
  250. _handler = {
  251. onHoverOverNode: function(event, node) {
  252. var setting = data.getSetting(event.data.treeId),
  253. root = data.getRoot(setting);
  254. if (root.curHoverNode != node) {
  255. _handler.onHoverOutNode(event);
  256. }
  257. root.curHoverNode = node;
  258. view.addHoverDom(setting, node);
  259. },
  260. onHoverOutNode: function(event, node) {
  261. var setting = data.getSetting(event.data.treeId),
  262. root = data.getRoot(setting);
  263. if (root.curHoverNode && !data.isSelectedNode(setting, root.curHoverNode)) {
  264. view.removeTreeDom(setting, root.curHoverNode);
  265. root.curHoverNode = null;
  266. }
  267. },
  268. onMousedownNode: function(eventMouseDown, _node) {
  269. var i,l,
  270. setting = data.getSetting(eventMouseDown.data.treeId),
  271. root = data.getRoot(setting), roots = data.getRoots();
  272. //right click can't drag & drop
  273. if (eventMouseDown.button == 2 || !setting.edit.enable || (!setting.edit.drag.isCopy && !setting.edit.drag.isMove)) return true;
  274. //input of edit node name can't drag & drop
  275. var target = eventMouseDown.target,
  276. _nodes = data.getRoot(setting).curSelectedList,
  277. nodes = [];
  278. if (!data.isSelectedNode(setting, _node)) {
  279. nodes = [_node];
  280. } else {
  281. for (i=0, l=_nodes.length; i<l; i++) {
  282. if (_nodes[i].editNameFlag && tools.eqs(target.tagName, "input") && target.getAttribute("treeNode"+consts.id.INPUT) !== null) {
  283. return true;
  284. }
  285. nodes.push(_nodes[i]);
  286. if (nodes[0].parentTId !== _nodes[i].parentTId) {
  287. nodes = [_node];
  288. break;
  289. }
  290. }
  291. }
  292. view.editNodeBlur = true;
  293. view.cancelCurEditNode(setting);
  294. var doc = $(setting.treeObj.get(0).ownerDocument),
  295. body = $(setting.treeObj.get(0).ownerDocument.body), curNode, tmpArrow, tmpTarget,
  296. isOtherTree = false,
  297. targetSetting = setting,
  298. sourceSetting = setting,
  299. preNode, nextNode,
  300. preTmpTargetNodeId = null,
  301. preTmpMoveType = null,
  302. tmpTargetNodeId = null,
  303. moveType = consts.move.TYPE_INNER,
  304. mouseDownX = eventMouseDown.clientX,
  305. mouseDownY = eventMouseDown.clientY,
  306. startTime = (new Date()).getTime();
  307. if (tools.uCanDo(setting)) {
  308. doc.bind("mousemove", _docMouseMove);
  309. }
  310. function _docMouseMove(event) {
  311. //avoid start drag after click node
  312. if (root.dragFlag == 0 && Math.abs(mouseDownX - event.clientX) < setting.edit.drag.minMoveSize
  313. && Math.abs(mouseDownY - event.clientY) < setting.edit.drag.minMoveSize) {
  314. return true;
  315. }
  316. var i, l, tmpNode, tmpDom, tmpNodes,
  317. childKey = setting.data.key.children;
  318. body.css("cursor", "pointer");
  319. if (root.dragFlag == 0) {
  320. if (tools.apply(setting.callback.beforeDrag, [setting.treeId, nodes], true) == false) {
  321. _docMouseUp(event);
  322. return true;
  323. }
  324. for (i=0, l=nodes.length; i<l; i++) {
  325. if (i==0) {
  326. root.dragNodeShowBefore = [];
  327. }
  328. tmpNode = nodes[i];
  329. if (tmpNode.isParent && tmpNode.open) {
  330. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  331. root.dragNodeShowBefore[tmpNode.tId] = true;
  332. } else {
  333. root.dragNodeShowBefore[tmpNode.tId] = false;
  334. }
  335. }
  336. root.dragFlag = 1;
  337. roots.showHoverDom = false;
  338. tools.showIfameMask(setting, true);
  339. //sort
  340. var isOrder = true, lastIndex = -1;
  341. if (nodes.length>1) {
  342. var pNodes = nodes[0].parentTId ? nodes[0].getParentNode()[childKey] : data.getNodes(setting);
  343. tmpNodes = [];
  344. for (i=0, l=pNodes.length; i<l; i++) {
  345. if (root.dragNodeShowBefore[pNodes[i].tId] !== undefined) {
  346. if (isOrder && lastIndex > -1 && (lastIndex+1) !== i) {
  347. isOrder = false;
  348. }
  349. tmpNodes.push(pNodes[i]);
  350. lastIndex = i;
  351. }
  352. if (nodes.length === tmpNodes.length) {
  353. nodes = tmpNodes;
  354. break;
  355. }
  356. }
  357. }
  358. if (isOrder) {
  359. preNode = nodes[0].getPreNode();
  360. nextNode = nodes[nodes.length-1].getNextNode();
  361. }
  362. //set node in selected
  363. curNode = $$("<ul class='zTreeDragUL'></ul>", setting);
  364. for (i=0, l=nodes.length; i<l; i++) {
  365. tmpNode = nodes[i];
  366. tmpNode.editNameFlag = false;
  367. view.selectNode(setting, tmpNode, i>0);
  368. view.removeTreeDom(setting, tmpNode);
  369. if (i > setting.edit.drag.maxShowNodeNum-1) {
  370. continue;
  371. }
  372. tmpDom = $$("<li id='"+ tmpNode.tId +"_tmp'></li>", setting);
  373. tmpDom.append($$(tmpNode, consts.id.A, setting).clone());
  374. tmpDom.css("padding", "0");
  375. tmpDom.children("#" + tmpNode.tId + consts.id.A).removeClass(consts.node.CURSELECTED);
  376. curNode.append(tmpDom);
  377. if (i == setting.edit.drag.maxShowNodeNum-1) {
  378. tmpDom = $$("<li id='"+ tmpNode.tId +"_moretmp'><a> ... </a></li>", setting);
  379. curNode.append(tmpDom);
  380. }
  381. }
  382. curNode.attr("id", nodes[0].tId + consts.id.UL + "_tmp");
  383. curNode.addClass(setting.treeObj.attr("class"));
  384. curNode.appendTo(body);
  385. tmpArrow = $$("<span class='tmpzTreeMove_arrow'></span>", setting);
  386. tmpArrow.attr("id", "zTreeMove_arrow_tmp");
  387. tmpArrow.appendTo(body);
  388. setting.treeObj.trigger(consts.event.DRAG, [event, setting.treeId, nodes]);
  389. }
  390. if (root.dragFlag == 1) {
  391. if (tmpTarget && tmpArrow.attr("id") == event.target.id && tmpTargetNodeId && (event.clientX + doc.scrollLeft()+2) > ($("#" + tmpTargetNodeId + consts.id.A, tmpTarget).offset().left)) {
  392. var xT = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget);
  393. event.target = (xT.length > 0) ? xT.get(0) : event.target;
  394. } else if (tmpTarget) {
  395. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  396. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  397. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  398. }
  399. tmpTarget = null;
  400. tmpTargetNodeId = null;
  401. //judge drag & drop in multi ztree
  402. isOtherTree = false;
  403. targetSetting = setting;
  404. var settings = data.getSettings();
  405. for (var s in settings) {
  406. if (settings[s].treeId && settings[s].edit.enable && settings[s].treeId != setting.treeId
  407. && (event.target.id == settings[s].treeId || $(event.target).parents("#" + settings[s].treeId).length>0)) {
  408. isOtherTree = true;
  409. targetSetting = settings[s];
  410. }
  411. }
  412. var docScrollTop = doc.scrollTop(),
  413. docScrollLeft = doc.scrollLeft(),
  414. treeOffset = targetSetting.treeObj.offset(),
  415. scrollHeight = targetSetting.treeObj.get(0).scrollHeight,
  416. scrollWidth = targetSetting.treeObj.get(0).scrollWidth,
  417. dTop = (event.clientY + docScrollTop - treeOffset.top),
  418. dBottom = (targetSetting.treeObj.height() + treeOffset.top - event.clientY - docScrollTop),
  419. dLeft = (event.clientX + docScrollLeft - treeOffset.left),
  420. dRight = (targetSetting.treeObj.width() + treeOffset.left - event.clientX - docScrollLeft),
  421. isTop = (dTop < setting.edit.drag.borderMax && dTop > setting.edit.drag.borderMin),
  422. isBottom = (dBottom < setting.edit.drag.borderMax && dBottom > setting.edit.drag.borderMin),
  423. isLeft = (dLeft < setting.edit.drag.borderMax && dLeft > setting.edit.drag.borderMin),
  424. isRight = (dRight < setting.edit.drag.borderMax && dRight > setting.edit.drag.borderMin),
  425. isTreeInner = dTop > setting.edit.drag.borderMin && dBottom > setting.edit.drag.borderMin && dLeft > setting.edit.drag.borderMin && dRight > setting.edit.drag.borderMin,
  426. isTreeTop = (isTop && targetSetting.treeObj.scrollTop() <= 0),
  427. isTreeBottom = (isBottom && (targetSetting.treeObj.scrollTop() + targetSetting.treeObj.height()+10) >= scrollHeight),
  428. isTreeLeft = (isLeft && targetSetting.treeObj.scrollLeft() <= 0),
  429. isTreeRight = (isRight && (targetSetting.treeObj.scrollLeft() + targetSetting.treeObj.width()+10) >= scrollWidth);
  430. if (event.target && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  431. //get node <li> dom
  432. var targetObj = event.target;
  433. while (targetObj && targetObj.tagName && !tools.eqs(targetObj.tagName, "li") && targetObj.id != targetSetting.treeId) {
  434. targetObj = targetObj.parentNode;
  435. }
  436. var canMove = true;
  437. //don't move to self or children of self
  438. for (i=0, l=nodes.length; i<l; i++) {
  439. tmpNode = nodes[i];
  440. if (targetObj.id === tmpNode.tId) {
  441. canMove = false;
  442. break;
  443. } else if ($$(tmpNode, setting).find("#" + targetObj.id).length > 0) {
  444. canMove = false;
  445. break;
  446. }
  447. }
  448. if (canMove && event.target && tools.isChildOrSelf(event.target, targetObj.id + consts.id.A)) {
  449. tmpTarget = $(targetObj);
  450. tmpTargetNodeId = targetObj.id;
  451. }
  452. }
  453. //the mouse must be in zTree
  454. tmpNode = nodes[0];
  455. if (isTreeInner && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  456. //judge mouse move in root of ztree
  457. if (!tmpTarget && (event.target.id == targetSetting.treeId || isTreeTop || isTreeBottom || isTreeLeft || isTreeRight) && (isOtherTree || (!isOtherTree && tmpNode.parentTId))) {
  458. tmpTarget = targetSetting.treeObj;
  459. }
  460. //auto scroll top
  461. if (isTop) {
  462. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()-10);
  463. } else if (isBottom) {
  464. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()+10);
  465. }
  466. if (isLeft) {
  467. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()-10);
  468. } else if (isRight) {
  469. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+10);
  470. }
  471. //auto scroll left
  472. if (tmpTarget && tmpTarget != targetSetting.treeObj && tmpTarget.offset().left < targetSetting.treeObj.offset().left) {
  473. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+ tmpTarget.offset().left - targetSetting.treeObj.offset().left);
  474. }
  475. }
  476. curNode.css({
  477. "top": (event.clientY + docScrollTop + 3) + "px",
  478. "left": (event.clientX + docScrollLeft + 3) + "px"
  479. });
  480. var dX = 0;
  481. var dY = 0;
  482. if (tmpTarget && tmpTarget.attr("id")!=targetSetting.treeId) {
  483. var tmpTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId),
  484. isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy),
  485. isPrev = !!(preNode && tmpTargetNodeId === preNode.tId),
  486. isNext = !!(nextNode && tmpTargetNodeId === nextNode.tId),
  487. isInner = (tmpNode.parentTId && tmpNode.parentTId == tmpTargetNodeId),
  488. canPrev = (isCopy || !isNext) && tools.apply(targetSetting.edit.drag.prev, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.prev),
  489. canNext = (isCopy || !isPrev) && tools.apply(targetSetting.edit.drag.next, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.next),
  490. canInner = (isCopy || !isInner) && !(targetSetting.data.keep.leaf && !tmpTargetNode.isParent) && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.inner);
  491. if (!canPrev && !canNext && !canInner) {
  492. tmpTarget = null;
  493. tmpTargetNodeId = "";
  494. moveType = consts.move.TYPE_INNER;
  495. tmpArrow.css({
  496. "display":"none"
  497. });
  498. if (window.zTreeMoveTimer) {
  499. clearTimeout(window.zTreeMoveTimer);
  500. window.zTreeMoveTargetNodeTId = null
  501. }
  502. } else {
  503. var tmpTargetA = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget),
  504. tmpNextA = tmpTargetNode.isLastNode ? null : $("#" + tmpTargetNode.getNextNode().tId + consts.id.A, tmpTarget.next()),
  505. tmpTop = tmpTargetA.offset().top,
  506. tmpLeft = tmpTargetA.offset().left,
  507. prevPercent = canPrev ? (canInner ? 0.25 : (canNext ? 0.5 : 1) ) : -1,
  508. nextPercent = canNext ? (canInner ? 0.75 : (canPrev ? 0.5 : 0) ) : -1,
  509. dY_percent = (event.clientY + docScrollTop - tmpTop)/tmpTargetA.height();
  510. if ((prevPercent==1 ||dY_percent<=prevPercent && dY_percent>=-.2) && canPrev) {
  511. dX = 1 - tmpArrow.width();
  512. dY = tmpTop - tmpArrow.height()/2;
  513. moveType = consts.move.TYPE_PREV;
  514. } else if ((nextPercent==0 || dY_percent>=nextPercent && dY_percent<=1.2) && canNext) {
  515. dX = 1 - tmpArrow.width();
  516. dY = (tmpNextA == null || (tmpTargetNode.isParent && tmpTargetNode.open)) ? (tmpTop + tmpTargetA.height() - tmpArrow.height()/2) : (tmpNextA.offset().top - tmpArrow.height()/2);
  517. moveType = consts.move.TYPE_NEXT;
  518. }else {
  519. dX = 5 - tmpArrow.width();
  520. dY = tmpTop;
  521. moveType = consts.move.TYPE_INNER;
  522. }
  523. tmpArrow.css({
  524. "display":"block",
  525. "top": dY + "px",
  526. "left": (tmpLeft + dX) + "px"
  527. });
  528. tmpTargetA.addClass(consts.node.TMPTARGET_NODE + "_" + moveType);
  529. if (preTmpTargetNodeId != tmpTargetNodeId || preTmpMoveType != moveType) {
  530. startTime = (new Date()).getTime();
  531. }
  532. if (tmpTargetNode && tmpTargetNode.isParent && moveType == consts.move.TYPE_INNER) {
  533. var startTimer = true;
  534. if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId !== tmpTargetNode.tId) {
  535. clearTimeout(window.zTreeMoveTimer);
  536. window.zTreeMoveTargetNodeTId = null;
  537. }else if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId === tmpTargetNode.tId) {
  538. startTimer = false;
  539. }
  540. if (startTimer) {
  541. window.zTreeMoveTimer = setTimeout(function() {
  542. if (moveType != consts.move.TYPE_INNER) return;
  543. if (tmpTargetNode && tmpTargetNode.isParent && !tmpTargetNode.open && (new Date()).getTime() - startTime > targetSetting.edit.drag.autoOpenTime
  544. && tools.apply(targetSetting.callback.beforeDragOpen, [targetSetting.treeId, tmpTargetNode], true)) {
  545. view.switchNode(targetSetting, tmpTargetNode);
  546. if (targetSetting.edit.drag.autoExpandTrigger) {
  547. targetSetting.treeObj.trigger(consts.event.EXPAND, [targetSetting.treeId, tmpTargetNode]);
  548. }
  549. }
  550. }, targetSetting.edit.drag.autoOpenTime+50);
  551. window.zTreeMoveTargetNodeTId = tmpTargetNode.tId;
  552. }
  553. }
  554. }
  555. } else {
  556. moveType = consts.move.TYPE_INNER;
  557. if (tmpTarget && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, null], !!targetSetting.edit.drag.inner)) {
  558. tmpTarget.addClass(consts.node.TMPTARGET_TREE);
  559. } else {
  560. tmpTarget = null;
  561. }
  562. tmpArrow.css({
  563. "display":"none"
  564. });
  565. if (window.zTreeMoveTimer) {
  566. clearTimeout(window.zTreeMoveTimer);
  567. window.zTreeMoveTargetNodeTId = null;
  568. }
  569. }
  570. preTmpTargetNodeId = tmpTargetNodeId;
  571. preTmpMoveType = moveType;
  572. setting.treeObj.trigger(consts.event.DRAGMOVE, [event, setting.treeId, nodes]);
  573. }
  574. return false;
  575. }
  576. doc.bind("mouseup", _docMouseUp);
  577. function _docMouseUp(event) {
  578. if (window.zTreeMoveTimer) {
  579. clearTimeout(window.zTreeMoveTimer);
  580. window.zTreeMoveTargetNodeTId = null;
  581. }
  582. preTmpTargetNodeId = null;
  583. preTmpMoveType = null;
  584. doc.unbind("mousemove", _docMouseMove);
  585. doc.unbind("mouseup", _docMouseUp);
  586. doc.unbind("selectstart", _docSelect);
  587. body.css("cursor", "auto");
  588. if (tmpTarget) {
  589. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  590. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  591. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  592. }
  593. tools.showIfameMask(setting, false);
  594. roots.showHoverDom = true;
  595. if (root.dragFlag == 0) return;
  596. root.dragFlag = 0;
  597. var i, l, tmpNode;
  598. for (i=0, l=nodes.length; i<l; i++) {
  599. tmpNode = nodes[i];
  600. if (tmpNode.isParent && root.dragNodeShowBefore[tmpNode.tId] && !tmpNode.open) {
  601. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  602. delete root.dragNodeShowBefore[tmpNode.tId];
  603. }
  604. }
  605. if (curNode) curNode.remove();
  606. if (tmpArrow) tmpArrow.remove();
  607. var isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy);
  608. if (!isCopy && tmpTarget && tmpTargetNodeId && nodes[0].parentTId && tmpTargetNodeId==nodes[0].parentTId && moveType == consts.move.TYPE_INNER) {
  609. tmpTarget = null;
  610. }
  611. if (tmpTarget) {
  612. var dragTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId);
  613. if (tools.apply(setting.callback.beforeDrop, [targetSetting.treeId, nodes, dragTargetNode, moveType, isCopy], true) == false) {
  614. view.selectNodes(sourceSetting, nodes);
  615. return;
  616. }
  617. var newNodes = isCopy ? tools.clone(nodes) : nodes;
  618. function dropCallback() {
  619. if (isOtherTree) {
  620. if (!isCopy) {
  621. for(var i=0, l=nodes.length; i<l; i++) {
  622. view.removeNode(setting, nodes[i]);
  623. }
  624. }
  625. if (moveType == consts.move.TYPE_INNER) {
  626. view.addNodes(targetSetting, dragTargetNode, newNodes);
  627. } else {
  628. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  629. if (moveType == consts.move.TYPE_PREV) {
  630. for (i=0, l=newNodes.length; i<l; i++) {
  631. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  632. }
  633. } else {
  634. for (i=-1, l=newNodes.length-1; i<l; l--) {
  635. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  636. }
  637. }
  638. }
  639. } else {
  640. if (isCopy && moveType == consts.move.TYPE_INNER) {
  641. view.addNodes(targetSetting, dragTargetNode, newNodes);
  642. } else {
  643. if (isCopy) {
  644. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  645. }
  646. if (moveType != consts.move.TYPE_NEXT) {
  647. for (i=0, l=newNodes.length; i<l; i++) {
  648. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  649. }
  650. } else {
  651. for (i=-1, l=newNodes.length-1; i<l; l--) {
  652. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  653. }
  654. }
  655. }
  656. }
  657. view.selectNodes(targetSetting, newNodes);
  658. $$(newNodes[0], setting).focus().blur();
  659. setting.treeObj.trigger(consts.event.DROP, [event, targetSetting.treeId, newNodes, dragTargetNode, moveType, isCopy]);
  660. }
  661. if (moveType == consts.move.TYPE_INNER && tools.canAsync(targetSetting, dragTargetNode)) {
  662. view.asyncNode(targetSetting, dragTargetNode, false, dropCallback);
  663. } else {
  664. dropCallback();
  665. }
  666. } else {
  667. view.selectNodes(sourceSetting, nodes);
  668. setting.treeObj.trigger(consts.event.DROP, [event, setting.treeId, nodes, null, null, null]);
  669. }
  670. }
  671. doc.bind("selectstart", _docSelect);
  672. function _docSelect() {
  673. return false;
  674. }
  675. //Avoid FireFox's Bug
  676. //If zTree Div CSS set 'overflow', so drag node outside of zTree, and event.target is error.
  677. if(eventMouseDown.preventDefault) {
  678. eventMouseDown.preventDefault();
  679. }
  680. return true;
  681. }
  682. },
  683. //method of tools for zTree
  684. _tools = {
  685. getAbs: function (obj) {
  686. var oRect = obj.getBoundingClientRect(),
  687. scrollTop = document.body.scrollTop+document.documentElement.scrollTop,
  688. scrollLeft = document.body.scrollLeft+document.documentElement.scrollLeft;
  689. return [oRect.left+scrollLeft,oRect.top+scrollTop];
  690. },
  691. inputFocus: function(inputObj) {
  692. if (inputObj.get(0)) {
  693. inputObj.focus();
  694. tools.setCursorPosition(inputObj.get(0), inputObj.val().length);
  695. }
  696. },
  697. inputSelect: function(inputObj) {
  698. if (inputObj.get(0)) {
  699. inputObj.focus();
  700. inputObj.select();
  701. }
  702. },
  703. setCursorPosition: function(obj, pos){
  704. if(obj.setSelectionRange) {
  705. obj.focus();
  706. obj.setSelectionRange(pos,pos);
  707. } else if (obj.createTextRange) {
  708. var range = obj.createTextRange();
  709. range.collapse(true);
  710. range.moveEnd('character', pos);
  711. range.moveStart('character', pos);
  712. range.select();
  713. }
  714. },
  715. showIfameMask: function(setting, showSign) {
  716. var root = data.getRoot(setting);
  717. //clear full mask
  718. while (root.dragMaskList.length > 0) {
  719. root.dragMaskList[0].remove();
  720. root.dragMaskList.shift();
  721. }
  722. if (showSign) {
  723. //show mask
  724. var iframeList = $$("iframe", setting);
  725. for (var i = 0, l = iframeList.length; i < l; i++) {
  726. var obj = iframeList.get(i),
  727. r = tools.getAbs(obj),
  728. dragMask = $$("<div id='zTreeMask_" + i + "' class='zTreeMask' style='top:" + r[1] + "px; left:" + r[0] + "px; width:" + obj.offsetWidth + "px; height:" + obj.offsetHeight + "px;'></div>", setting);
  729. dragMask.appendTo($$("body", setting));
  730. root.dragMaskList.push(dragMask);
  731. }
  732. }
  733. }
  734. },
  735. //method of operate ztree dom
  736. _view = {
  737. addEditBtn: function(setting, node) {
  738. if (node.editNameFlag || $$(node, consts.id.EDIT, setting).length > 0) {
  739. return;
  740. }
  741. if (!tools.apply(setting.edit.showRenameBtn, [setting.treeId, node], setting.edit.showRenameBtn)) {
  742. return;
  743. }
  744. var aObj = $$(node, consts.id.A, setting),
  745. editStr = "<span class='" + consts.className.BUTTON + " edit' id='" + node.tId + consts.id.EDIT + "' title='"+tools.apply(setting.edit.renameTitle, [setting.treeId, node], setting.edit.renameTitle)+"' treeNode"+consts.id.EDIT+" style='display:none;'></span>";
  746. aObj.append(editStr);
  747. $$(node, consts.id.EDIT, setting).bind('click',
  748. function() {
  749. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeEditName, [setting.treeId, node], true) == false) return false;
  750. view.editNode(setting, node);
  751. return false;
  752. }
  753. ).show();
  754. },
  755. addRemoveBtn: function(setting, node) {
  756. if (node.editNameFlag || $$(node, consts.id.REMOVE, setting).length > 0) {
  757. return;
  758. }
  759. if (!tools.apply(setting.edit.showRemoveBtn, [setting.treeId, node], setting.edit.showRemoveBtn)) {
  760. return;
  761. }
  762. var aObj = $$(node, consts.id.A, setting),
  763. removeStr = "<span class='" + consts.className.BUTTON + " remove' id='" + node.tId + consts.id.REMOVE + "' title='"+tools.apply(setting.edit.removeTitle, [setting.treeId, node], setting.edit.removeTitle)+"' treeNode"+consts.id.REMOVE+" style='display:none;'></span>";
  764. aObj.append(removeStr);
  765. $$(node, consts.id.REMOVE, setting).bind('click',
  766. function() {
  767. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return false;
  768. view.removeNode(setting, node);
  769. setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  770. return false;
  771. }
  772. ).bind('mousedown',
  773. function(eventMouseDown) {
  774. return true;
  775. }
  776. ).show();
  777. },
  778. addHoverDom: function(setting, node) {
  779. if (data.getRoots().showHoverDom) {
  780. node.isHover = true;
  781. if (setting.edit.enable) {
  782. view.addEditBtn(setting, node);
  783. view.addRemoveBtn(setting, node);
  784. }
  785. tools.apply(setting.view.addHoverDom, [setting.treeId, node]);
  786. }
  787. },
  788. cancelCurEditNode: function (setting, forceName, isCancel) {
  789. var root = data.getRoot(setting),
  790. nameKey = setting.data.key.name,
  791. node = root.curEditNode;
  792. if (node) {
  793. var inputObj = root.curEditInput,
  794. newName = forceName ? forceName:(isCancel ? node[nameKey]: inputObj.val());
  795. if (tools.apply(setting.callback.beforeRename, [setting.treeId, node, newName, isCancel], true) === false) {
  796. return false;
  797. } else {
  798. node[nameKey] = newName;
  799. setting.treeObj.trigger(consts.event.RENAME, [setting.treeId, node, isCancel]);
  800. }
  801. var aObj = $$(node, consts.id.A, setting);
  802. aObj.removeClass(consts.node.CURSELECTED_EDIT);
  803. inputObj.unbind();
  804. view.setNodeName(setting, node);
  805. node.editNameFlag = false;
  806. root.curEditNode = null;
  807. root.curEditInput = null;
  808. view.selectNode(setting, node, false);
  809. }
  810. root.noSelection = true;
  811. return true;
  812. },
  813. editNode: function(setting, node) {
  814. var root = data.getRoot(setting);
  815. view.editNodeBlur = false;
  816. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  817. setTimeout(function() {tools.inputFocus(root.curEditInput);}, 0);
  818. return;
  819. }
  820. var nameKey = setting.data.key.name;
  821. node.editNameFlag = true;
  822. view.removeTreeDom(setting, node);
  823. view.cancelCurEditNode(setting);
  824. view.selectNode(setting, node, false);
  825. $$(node, consts.id.SPAN, setting).html("<input type=text class='rename' id='" + node.tId + consts.id.INPUT + "' treeNode" + consts.id.INPUT + " >");
  826. var inputObj = $$(node, consts.id.INPUT, setting);
  827. inputObj.attr("value", node[nameKey]);
  828. if (setting.edit.editNameSelectAll) {
  829. tools.inputSelect(inputObj);
  830. } else {
  831. tools.inputFocus(inputObj);
  832. }
  833. inputObj.bind('blur', function(event) {
  834. if (!view.editNodeBlur) {
  835. view.cancelCurEditNode(setting);
  836. }
  837. }).bind('keydown', function(event) {
  838. if (event.keyCode=="13") {
  839. view.editNodeBlur = true;
  840. view.cancelCurEditNode(setting);
  841. } else if (event.keyCode=="27") {
  842. view.cancelCurEditNode(setting, null, true);
  843. }
  844. }).bind('click', function(event) {
  845. return false;
  846. }).bind('dblclick', function(event) {
  847. return false;
  848. });
  849. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED_EDIT);
  850. root.curEditInput = inputObj;
  851. root.noSelection = false;
  852. root.curEditNode = node;
  853. },
  854. moveNode: function(setting, targetNode, node, moveType, animateFlag, isSilent) {
  855. var root = data.getRoot(setting),
  856. childKey = setting.data.key.children;
  857. if (targetNode == node) return;
  858. if (setting.data.keep.leaf && targetNode && !targetNode.isParent && moveType == consts.move.TYPE_INNER) return;
  859. var oldParentNode = (node.parentTId ? node.getParentNode(): root),
  860. targetNodeIsRoot = (targetNode === null || targetNode == root);
  861. if (targetNodeIsRoot && targetNode === null) targetNode = root;
  862. if (targetNodeIsRoot) moveType = consts.move.TYPE_INNER;
  863. var targetParentNode = (targetNode.parentTId ? targetNode.getParentNode() : root);
  864. if (moveType != consts.move.TYPE_PREV && moveType != consts.move.TYPE_NEXT) {
  865. moveType = consts.move.TYPE_INNER;
  866. }
  867. if (moveType == consts.move.TYPE_INNER) {
  868. if (targetNodeIsRoot) {
  869. //parentTId of root node is null
  870. node.parentTId = null;
  871. } else {
  872. if (!targetNode.isParent) {
  873. targetNode.isParent = true;
  874. targetNode.open = !!targetNode.open;
  875. view.setNodeLineIcos(setting, targetNode);
  876. }
  877. node.parentTId = targetNode.tId;
  878. }
  879. }
  880. //move node Dom
  881. var targetObj, target_ulObj;
  882. if (targetNodeIsRoot) {
  883. targetObj = setting.treeObj;
  884. target_ulObj = targetObj;
  885. } else {
  886. if (!isSilent && moveType == consts.move.TYPE_INNER) {
  887. view.expandCollapseNode(setting, targetNode, true, false);
  888. } else if (!isSilent) {
  889. view.expandCollapseNode(setting, targetNode.getParentNode(), true, false);
  890. }
  891. targetObj = $$(targetNode, setting);
  892. target_ulObj = $$(targetNode, consts.id.UL, setting);
  893. if (!!targetObj.get(0) && !target_ulObj.get(0)) {
  894. var ulstr = [];
  895. view.makeUlHtml(setting, targetNode, ulstr, '');
  896. targetObj.append(ulstr.join(''));
  897. }
  898. target_ulObj = $$(targetNode, consts.id.UL, setting);
  899. }
  900. var nodeDom = $$(node, setting);
  901. if (!nodeDom.get(0)) {
  902. nodeDom = view.appendNodes(setting, node.level, [node], null, false, true).join('');
  903. } else if (!targetObj.get(0)) {
  904. nodeDom.remove();
  905. }
  906. if (target_ulObj.get(0) && moveType == consts.move.TYPE_INNER) {
  907. target_ulObj.append(nodeDom);
  908. } else if (targetObj.get(0) && moveType == consts.move.TYPE_PREV) {
  909. targetObj.before(nodeDom);
  910. } else if (targetObj.get(0) && moveType == consts.move.TYPE_NEXT) {
  911. targetObj.after(nodeDom);
  912. }
  913. //repair the data after move
  914. var i,l,
  915. tmpSrcIndex = -1,
  916. tmpTargetIndex = 0,
  917. oldNeighbor = null,
  918. newNeighbor = null,
  919. oldLevel = node.level;
  920. if (node.isFirstNode) {
  921. tmpSrcIndex = 0;
  922. if (oldParentNode[childKey].length > 1 ) {
  923. oldNeighbor = oldParentNode[childKey][1];
  924. oldNeighbor.isFirstNode = true;
  925. }
  926. } else if (node.isLastNode) {
  927. tmpSrcIndex = oldParentNode[childKey].length -1;
  928. oldNeighbor = oldParentNode[childKey][tmpSrcIndex - 1];
  929. oldNeighbor.isLastNode = true;
  930. } else {
  931. for (i = 0, l = oldParentNode[childKey].length; i < l; i++) {
  932. if (oldParentNode[childKey][i].tId == node.tId) {
  933. tmpSrcIndex = i;
  934. break;
  935. }
  936. }
  937. }
  938. if (tmpSrcIndex >= 0) {
  939. oldParentNode[childKey].splice(tmpSrcIndex, 1);
  940. }
  941. if (moveType != consts.move.TYPE_INNER) {
  942. for (i = 0, l = targetParentNode[childKey].length; i < l; i++) {
  943. if (targetParentNode[childKey][i].tId == targetNode.tId) tmpTargetIndex = i;
  944. }
  945. }
  946. if (moveType == consts.move.TYPE_INNER) {
  947. if (!targetNode[childKey]) targetNode[childKey] = new Array();
  948. if (targetNode[childKey].length > 0) {
  949. newNeighbor = targetNode[childKey][targetNode[childKey].length - 1];
  950. newNeighbor.isLastNode = false;
  951. }
  952. targetNode[childKey].splice(targetNode[childKey].length, 0, node);
  953. node.isLastNode = true;
  954. node.isFirstNode = (targetNode[childKey].length == 1);
  955. } else if (targetNode.isFirstNode && moveType == consts.move.TYPE_PREV) {
  956. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  957. newNeighbor = targetNode;
  958. newNeighbor.isFirstNode = false;
  959. node.parentTId = targetNode.parentTId;
  960. node.isFirstNode = true;
  961. node.isLastNode = false;
  962. } else if (targetNode.isLastNode && moveType == consts.move.TYPE_NEXT) {
  963. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  964. newNeighbor = targetNode;
  965. newNeighbor.isLastNode = false;
  966. node.parentTId = targetNode.parentTId;
  967. node.isFirstNode = false;
  968. node.isLastNode = true;
  969. } else {
  970. if (moveType == consts.move.TYPE_PREV) {
  971. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  972. } else {
  973. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  974. }
  975. node.parentTId = targetNode.parentTId;
  976. node.isFirstNode = false;
  977. node.isLastNode = false;
  978. }
  979. data.fixPIdKeyValue(setting, node);
  980. data.setSonNodeLevel(setting, node.getParentNode(), node);
  981. //repair node what been moved
  982. view.setNodeLineIcos(setting, node);
  983. view.repairNodeLevelClass(setting, node, oldLevel)
  984. //repair node's old parentNode dom
  985. if (!setting.data.keep.parent && oldParentNode[childKey].length < 1) {
  986. //old parentNode has no child nodes
  987. oldParentNode.isParent = false;
  988. oldParentNode.open = false;
  989. var tmp_ulObj = $$(oldParentNode, consts.id.UL, setting),
  990. tmp_switchObj = $$(oldParentNode, consts.id.SWITCH, setting),
  991. tmp_icoObj = $$(oldParentNode, consts.id.ICON, setting);
  992. view.replaceSwitchClass(oldParentNode, tmp_switchObj, consts.folder.DOCU);
  993. view.replaceIcoClass(oldParentNode, tmp_icoObj, consts.folder.DOCU);
  994. tmp_ulObj.css("display", "none");
  995. } else if (oldNeighbor) {
  996. //old neigbor node
  997. view.setNodeLineIcos(setting, oldNeighbor);
  998. }
  999. //new neigbor node
  1000. if (newNeighbor) {
  1001. view.setNodeLineIcos(setting, newNeighbor);
  1002. }
  1003. //repair checkbox / radio
  1004. if (!!setting.check && setting.check.enable && view.repairChkClass) {
  1005. view.repairChkClass(setting, oldParentNode);
  1006. view.repairParentChkClassWithSelf(setting, oldParentNode);
  1007. if (oldParentNode != node.parent)
  1008. view.repairParentChkClassWithSelf(setting, node);
  1009. }
  1010. //expand parents after move
  1011. if (!isSilent) {
  1012. view.expandCollapseParentNode(setting, node.getParentNode(), true, animateFlag);
  1013. }
  1014. },
  1015. removeEditBtn: function(setting, node) {
  1016. $$(node, consts.id.EDIT, setting).unbind().remove();
  1017. },
  1018. removeRemoveBtn: function(setting, node) {
  1019. $$(node, consts.id.REMOVE, setting).unbind().remove();
  1020. },
  1021. removeTreeDom: function(setting, node) {
  1022. node.isHover = false;
  1023. view.removeEditBtn(setting, node);
  1024. view.removeRemoveBtn(setting, node);
  1025. tools.apply(setting.view.removeHoverDom, [setting.treeId, node]);
  1026. },
  1027. repairNodeLevelClass: function(setting, node, oldLevel) {
  1028. if (oldLevel === node.level) return;
  1029. var liObj = $$(node, setting),
  1030. aObj = $$(node, consts.id.A, setting),
  1031. ulObj = $$(node, consts.id.UL, setting),
  1032. oldClass = consts.className.LEVEL + oldLevel,
  1033. newClass = consts.className.LEVEL + node.level;
  1034. liObj.removeClass(oldClass);
  1035. liObj.addClass(newClass);
  1036. aObj.removeClass(oldClass);
  1037. aObj.addClass(newClass);
  1038. ulObj.removeClass(oldClass);
  1039. ulObj.addClass(newClass);
  1040. },
  1041. selectNodes : function(setting, nodes) {
  1042. for (var i=0, l=nodes.length; i<l; i++) {
  1043. view.selectNode(setting, nodes[i], i>0);
  1044. }
  1045. }
  1046. },
  1047. _z = {
  1048. tools: _tools,
  1049. view: _view,
  1050. event: _event,
  1051. data: _data
  1052. };
  1053. $.extend(true, $.fn.zTree.consts, _consts);
  1054. $.extend(true, $.fn.zTree._z, _z);
  1055. var zt = $.fn.zTree,
  1056. tools = zt._z.tools,
  1057. consts = zt.consts,
  1058. view = zt._z.view,
  1059. data = zt._z.data,
  1060. event = zt._z.event,
  1061. $$ = tools.$;
  1062. data.exSetting(_setting);
  1063. data.addInitBind(_bindEvent);
  1064. data.addInitUnBind(_unbindEvent);
  1065. data.addInitCache(_initCache);
  1066. data.addInitNode(_initNode);
  1067. data.addInitProxy(_eventProxy);
  1068. data.addInitRoot(_initRoot);
  1069. data.addZTreeTools(_zTreeTools);
  1070. var _cancelPreSelectedNode = view.cancelPreSelectedNode;
  1071. view.cancelPreSelectedNode = function (setting, node) {
  1072. var list = data.getRoot(setting).curSelectedList;
  1073. for (var i=0, j=list.length; i<j; i++) {
  1074. if (!node || node === list[i]) {
  1075. view.removeTreeDom(setting, list[i]);
  1076. if (node) break;
  1077. }
  1078. }
  1079. if (_cancelPreSelectedNode) _cancelPreSelectedNode.apply(view, arguments);
  1080. }
  1081. var _createNodes = view.createNodes;
  1082. view.createNodes = function(setting, level, nodes, parentNode) {
  1083. if (_createNodes) {
  1084. _createNodes.apply(view, arguments);
  1085. }
  1086. if (!nodes) return;
  1087. if (view.repairParentChkClassWithSelf) {
  1088. view.repairParentChkClassWithSelf(setting, parentNode);
  1089. }
  1090. }
  1091. var _makeNodeUrl = view.makeNodeUrl;
  1092. view.makeNodeUrl = function(setting, node) {
  1093. return setting.edit.enable ? null : (_makeNodeUrl.apply(view, arguments));
  1094. }
  1095. var _removeNode = view.removeNode;
  1096. view.removeNode = function(setting, node) {
  1097. var root = data.getRoot(setting);
  1098. if (root.curEditNode === node) root.curEditNode = null;
  1099. if (_removeNode) {
  1100. _removeNode.apply(view, arguments);
  1101. }
  1102. }
  1103. var _selectNode = view.selectNode;
  1104. view.selectNode = function(setting, node, addFlag) {
  1105. var root = data.getRoot(setting);
  1106. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  1107. return false;
  1108. }
  1109. if (_selectNode) _selectNode.apply(view, arguments);
  1110. view.addHoverDom(setting, node);
  1111. return true;
  1112. }
  1113. var _uCanDo = tools.uCanDo;
  1114. tools.uCanDo = function(setting, e) {
  1115. var root = data.getRoot(setting);
  1116. if (e && (tools.eqs(e.type, "mouseover") || tools.eqs(e.type, "mouseout") || tools.eqs(e.type, "mousedown") || tools.eqs(e.type, "mouseup"))) {
  1117. return true;
  1118. }
  1119. if (root.curEditNode) {
  1120. view.editNodeBlur = false;
  1121. root.curEditInput.focus();
  1122. }
  1123. return (!root.curEditNode) && (_uCanDo ? _uCanDo.apply(view, arguments) : true);
  1124. }
  1125. })(jQuery);