jquery.ztree.exedit.js 49 KB

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