jquery.ztree.exedit.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*
  2. * JQuery zTree exedit v3.5.28
  3. * http://treejs.cn/
  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: 2017-01-20
  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, -1, [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, -1, [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. function clearMove() {
  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. }
  503. if (!canPrev && !canNext && !canInner) {
  504. clearMove();
  505. } else {
  506. var tmpTargetA = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget),
  507. tmpNextA = tmpTargetNode.isLastNode ? null : $("#" + tmpTargetNode.getNextNode().tId + consts.id.A, tmpTarget.next()),
  508. tmpTop = tmpTargetA.offset().top,
  509. tmpLeft = tmpTargetA.offset().left,
  510. prevPercent = canPrev ? (canInner ? 0.25 : (canNext ? 0.5 : 1) ) : -1,
  511. nextPercent = canNext ? (canInner ? 0.75 : (canPrev ? 0.5 : 0) ) : -1,
  512. dY_percent = (event.clientY + docScrollTop - tmpTop)/tmpTargetA.height();
  513. if ((prevPercent==1 || dY_percent<=prevPercent && dY_percent>=-.2) && canPrev) {
  514. dX = 1 - tmpArrow.width();
  515. dY = tmpTop - tmpArrow.height()/2;
  516. moveType = consts.move.TYPE_PREV;
  517. } else if ((nextPercent==0 || dY_percent>=nextPercent && dY_percent<=1.2) && canNext) {
  518. dX = 1 - tmpArrow.width();
  519. dY = (tmpNextA == null || (tmpTargetNode.isParent && tmpTargetNode.open)) ? (tmpTop + tmpTargetA.height() - tmpArrow.height()/2) : (tmpNextA.offset().top - tmpArrow.height()/2);
  520. moveType = consts.move.TYPE_NEXT;
  521. } else if (canInner) {
  522. dX = 5 - tmpArrow.width();
  523. dY = tmpTop;
  524. moveType = consts.move.TYPE_INNER;
  525. } else {
  526. clearMove();
  527. }
  528. if (tmpTarget) {
  529. tmpArrow.css({
  530. "display":"block",
  531. "top": dY + "px",
  532. "left": (tmpLeft + dX) + "px"
  533. });
  534. tmpTargetA.addClass(consts.node.TMPTARGET_NODE + "_" + moveType);
  535. if (preTmpTargetNodeId != tmpTargetNodeId || preTmpMoveType != moveType) {
  536. startTime = (new Date()).getTime();
  537. }
  538. if (tmpTargetNode && tmpTargetNode.isParent && moveType == consts.move.TYPE_INNER) {
  539. var startTimer = true;
  540. if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId !== tmpTargetNode.tId) {
  541. clearTimeout(window.zTreeMoveTimer);
  542. window.zTreeMoveTargetNodeTId = null;
  543. } else if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId === tmpTargetNode.tId) {
  544. startTimer = false;
  545. }
  546. if (startTimer) {
  547. window.zTreeMoveTimer = setTimeout(function() {
  548. if (moveType != consts.move.TYPE_INNER) return;
  549. if (tmpTargetNode && tmpTargetNode.isParent && !tmpTargetNode.open && (new Date()).getTime() - startTime > targetSetting.edit.drag.autoOpenTime
  550. && tools.apply(targetSetting.callback.beforeDragOpen, [targetSetting.treeId, tmpTargetNode], true)) {
  551. view.switchNode(targetSetting, tmpTargetNode);
  552. if (targetSetting.edit.drag.autoExpandTrigger) {
  553. targetSetting.treeObj.trigger(consts.event.EXPAND, [targetSetting.treeId, tmpTargetNode]);
  554. }
  555. }
  556. }, targetSetting.edit.drag.autoOpenTime+50);
  557. window.zTreeMoveTargetNodeTId = tmpTargetNode.tId;
  558. }
  559. }
  560. }
  561. }
  562. } else {
  563. moveType = consts.move.TYPE_INNER;
  564. if (tmpTarget && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, null], !!targetSetting.edit.drag.inner)) {
  565. tmpTarget.addClass(consts.node.TMPTARGET_TREE);
  566. } else {
  567. tmpTarget = null;
  568. }
  569. tmpArrow.css({
  570. "display":"none"
  571. });
  572. if (window.zTreeMoveTimer) {
  573. clearTimeout(window.zTreeMoveTimer);
  574. window.zTreeMoveTargetNodeTId = null;
  575. }
  576. }
  577. preTmpTargetNodeId = tmpTargetNodeId;
  578. preTmpMoveType = moveType;
  579. setting.treeObj.trigger(consts.event.DRAGMOVE, [event, setting.treeId, nodes]);
  580. }
  581. return false;
  582. }
  583. doc.bind("mouseup", _docMouseUp);
  584. function _docMouseUp(event) {
  585. if (window.zTreeMoveTimer) {
  586. clearTimeout(window.zTreeMoveTimer);
  587. window.zTreeMoveTargetNodeTId = null;
  588. }
  589. preTmpTargetNodeId = null;
  590. preTmpMoveType = null;
  591. doc.unbind("mousemove", _docMouseMove);
  592. doc.unbind("mouseup", _docMouseUp);
  593. doc.unbind("selectstart", _docSelect);
  594. body.css("cursor", "auto");
  595. if (tmpTarget) {
  596. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  597. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  598. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  599. }
  600. tools.showIfameMask(setting, false);
  601. roots.showHoverDom = true;
  602. if (root.dragFlag == 0) return;
  603. root.dragFlag = 0;
  604. var i, l, tmpNode;
  605. for (i=0, l=nodes.length; i<l; i++) {
  606. tmpNode = nodes[i];
  607. if (tmpNode.isParent && root.dragNodeShowBefore[tmpNode.tId] && !tmpNode.open) {
  608. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  609. delete root.dragNodeShowBefore[tmpNode.tId];
  610. }
  611. }
  612. if (curNode) curNode.remove();
  613. if (tmpArrow) tmpArrow.remove();
  614. var isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy);
  615. if (!isCopy && tmpTarget && tmpTargetNodeId && nodes[0].parentTId && tmpTargetNodeId==nodes[0].parentTId && moveType == consts.move.TYPE_INNER) {
  616. tmpTarget = null;
  617. }
  618. if (tmpTarget) {
  619. var dragTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId);
  620. if (tools.apply(setting.callback.beforeDrop, [targetSetting.treeId, nodes, dragTargetNode, moveType, isCopy], true) == false) {
  621. view.selectNodes(sourceSetting, nodes);
  622. return;
  623. }
  624. var newNodes = isCopy ? tools.clone(nodes) : nodes;
  625. function dropCallback() {
  626. if (isOtherTree) {
  627. if (!isCopy) {
  628. for(var i=0, l=nodes.length; i<l; i++) {
  629. view.removeNode(setting, nodes[i]);
  630. }
  631. }
  632. if (moveType == consts.move.TYPE_INNER) {
  633. view.addNodes(targetSetting, dragTargetNode, -1, newNodes);
  634. } else {
  635. view.addNodes(targetSetting, dragTargetNode.getParentNode(), moveType == consts.move.TYPE_PREV ? dragTargetNode.getIndex() : dragTargetNode.getIndex()+1, newNodes);
  636. }
  637. } else {
  638. if (isCopy && moveType == consts.move.TYPE_INNER) {
  639. view.addNodes(targetSetting, dragTargetNode, -1, newNodes);
  640. } else if (isCopy) {
  641. view.addNodes(targetSetting, dragTargetNode.getParentNode(), moveType == consts.move.TYPE_PREV ? dragTargetNode.getIndex() : dragTargetNode.getIndex()+1, newNodes);
  642. } else {
  643. if (moveType != consts.move.TYPE_NEXT) {
  644. for (i=0, l=newNodes.length; i<l; i++) {
  645. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  646. }
  647. } else {
  648. for (i=-1, l=newNodes.length-1; i<l; l--) {
  649. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  650. }
  651. }
  652. }
  653. }
  654. view.selectNodes(targetSetting, newNodes);
  655. var a = $$(newNodes[0], setting).get(0);
  656. view.scrollIntoView(a);
  657. setting.treeObj.trigger(consts.event.DROP, [event, targetSetting.treeId, newNodes, dragTargetNode, moveType, isCopy]);
  658. }
  659. if (moveType == consts.move.TYPE_INNER && tools.canAsync(targetSetting, dragTargetNode)) {
  660. view.asyncNode(targetSetting, dragTargetNode, false, dropCallback);
  661. } else {
  662. dropCallback();
  663. }
  664. } else {
  665. view.selectNodes(sourceSetting, nodes);
  666. setting.treeObj.trigger(consts.event.DROP, [event, setting.treeId, nodes, null, null, null]);
  667. }
  668. }
  669. doc.bind("selectstart", _docSelect);
  670. function _docSelect() {
  671. return false;
  672. }
  673. //Avoid FireFox's Bug
  674. //If zTree Div CSS set 'overflow', so drag node outside of zTree, and event.target is error.
  675. if(eventMouseDown.preventDefault) {
  676. eventMouseDown.preventDefault();
  677. }
  678. return true;
  679. }
  680. },
  681. //method of tools for zTree
  682. _tools = {
  683. getAbs: function (obj) {
  684. var oRect = obj.getBoundingClientRect(),
  685. scrollTop = document.body.scrollTop+document.documentElement.scrollTop,
  686. scrollLeft = document.body.scrollLeft+document.documentElement.scrollLeft;
  687. return [oRect.left+scrollLeft,oRect.top+scrollTop];
  688. },
  689. inputFocus: function(inputObj) {
  690. if (inputObj.get(0)) {
  691. inputObj.focus();
  692. tools.setCursorPosition(inputObj.get(0), inputObj.val().length);
  693. }
  694. },
  695. inputSelect: function(inputObj) {
  696. if (inputObj.get(0)) {
  697. inputObj.focus();
  698. inputObj.select();
  699. }
  700. },
  701. setCursorPosition: function(obj, pos){
  702. if(obj.setSelectionRange) {
  703. obj.focus();
  704. obj.setSelectionRange(pos,pos);
  705. } else if (obj.createTextRange) {
  706. var range = obj.createTextRange();
  707. range.collapse(true);
  708. range.moveEnd('character', pos);
  709. range.moveStart('character', pos);
  710. range.select();
  711. }
  712. },
  713. showIfameMask: function(setting, showSign) {
  714. var root = data.getRoot(setting);
  715. //clear full mask
  716. while (root.dragMaskList.length > 0) {
  717. root.dragMaskList[0].remove();
  718. root.dragMaskList.shift();
  719. }
  720. if (showSign) {
  721. //show mask
  722. var iframeList = $$("iframe", setting);
  723. for (var i = 0, l = iframeList.length; i < l; i++) {
  724. var obj = iframeList.get(i),
  725. r = tools.getAbs(obj),
  726. 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);
  727. dragMask.appendTo($$("body", setting));
  728. root.dragMaskList.push(dragMask);
  729. }
  730. }
  731. }
  732. },
  733. //method of operate ztree dom
  734. _view = {
  735. addEditBtn: function(setting, node) {
  736. if (node.editNameFlag || $$(node, consts.id.EDIT, setting).length > 0) {
  737. return;
  738. }
  739. if (!tools.apply(setting.edit.showRenameBtn, [setting.treeId, node], setting.edit.showRenameBtn)) {
  740. return;
  741. }
  742. var aObj = $$(node, consts.id.A, setting),
  743. 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>";
  744. aObj.append(editStr);
  745. $$(node, consts.id.EDIT, setting).bind('click',
  746. function() {
  747. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeEditName, [setting.treeId, node], true) == false) return false;
  748. view.editNode(setting, node);
  749. return false;
  750. }
  751. ).show();
  752. },
  753. addRemoveBtn: function(setting, node) {
  754. if (node.editNameFlag || $$(node, consts.id.REMOVE, setting).length > 0) {
  755. return;
  756. }
  757. if (!tools.apply(setting.edit.showRemoveBtn, [setting.treeId, node], setting.edit.showRemoveBtn)) {
  758. return;
  759. }
  760. var aObj = $$(node, consts.id.A, setting),
  761. 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>";
  762. aObj.append(removeStr);
  763. $$(node, consts.id.REMOVE, setting).bind('click',
  764. function() {
  765. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return false;
  766. view.removeNode(setting, node);
  767. setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  768. return false;
  769. }
  770. ).bind('mousedown',
  771. function(eventMouseDown) {
  772. return true;
  773. }
  774. ).show();
  775. },
  776. addHoverDom: function(setting, node) {
  777. if (data.getRoots().showHoverDom) {
  778. node.isHover = true;
  779. if (setting.edit.enable) {
  780. view.addEditBtn(setting, node);
  781. view.addRemoveBtn(setting, node);
  782. }
  783. tools.apply(setting.view.addHoverDom, [setting.treeId, node]);
  784. }
  785. },
  786. cancelCurEditNode: function (setting, forceName, isCancel) {
  787. var root = data.getRoot(setting),
  788. nameKey = setting.data.key.name,
  789. node = root.curEditNode;
  790. if (node) {
  791. var inputObj = root.curEditInput,
  792. newName = forceName ? forceName:(isCancel ? node[nameKey]: inputObj.val());
  793. if (tools.apply(setting.callback.beforeRename, [setting.treeId, node, newName, isCancel], true) === false) {
  794. return false;
  795. }
  796. node[nameKey] = newName;
  797. var aObj = $$(node, consts.id.A, setting);
  798. aObj.removeClass(consts.node.CURSELECTED_EDIT);
  799. inputObj.unbind();
  800. view.setNodeName(setting, node);
  801. node.editNameFlag = false;
  802. root.curEditNode = null;
  803. root.curEditInput = null;
  804. view.selectNode(setting, node, false);
  805. setting.treeObj.trigger(consts.event.RENAME, [setting.treeId, node, isCancel]);
  806. }
  807. root.noSelection = true;
  808. return true;
  809. },
  810. editNode: function(setting, node) {
  811. var root = data.getRoot(setting);
  812. view.editNodeBlur = false;
  813. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  814. setTimeout(function() {tools.inputFocus(root.curEditInput);}, 0);
  815. return;
  816. }
  817. var nameKey = setting.data.key.name;
  818. node.editNameFlag = true;
  819. view.removeTreeDom(setting, node);
  820. view.cancelCurEditNode(setting);
  821. view.selectNode(setting, node, false);
  822. $$(node, consts.id.SPAN, setting).html("<input type=text class='rename' id='" + node.tId + consts.id.INPUT + "' treeNode" + consts.id.INPUT + " >");
  823. var inputObj = $$(node, consts.id.INPUT, setting);
  824. inputObj.attr("value", node[nameKey]);
  825. if (setting.edit.editNameSelectAll) {
  826. tools.inputSelect(inputObj);
  827. } else {
  828. tools.inputFocus(inputObj);
  829. }
  830. inputObj.bind('blur', function(event) {
  831. if (!view.editNodeBlur) {
  832. view.cancelCurEditNode(setting);
  833. }
  834. }).bind('keydown', function(event) {
  835. if (event.keyCode=="13") {
  836. view.editNodeBlur = true;
  837. view.cancelCurEditNode(setting);
  838. } else if (event.keyCode=="27") {
  839. view.cancelCurEditNode(setting, null, true);
  840. }
  841. }).bind('click', function(event) {
  842. return false;
  843. }).bind('dblclick', function(event) {
  844. return false;
  845. });
  846. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED_EDIT);
  847. root.curEditInput = inputObj;
  848. root.noSelection = false;
  849. root.curEditNode = node;
  850. },
  851. moveNode: function(setting, targetNode, node, moveType, animateFlag, isSilent) {
  852. var root = data.getRoot(setting),
  853. childKey = setting.data.key.children;
  854. if (targetNode == node) return;
  855. if (setting.data.keep.leaf && targetNode && !targetNode.isParent && moveType == consts.move.TYPE_INNER) return;
  856. var oldParentNode = (node.parentTId ? node.getParentNode(): root),
  857. targetNodeIsRoot = (targetNode === null || targetNode == root);
  858. if (targetNodeIsRoot && targetNode === null) targetNode = root;
  859. if (targetNodeIsRoot) moveType = consts.move.TYPE_INNER;
  860. var targetParentNode = (targetNode.parentTId ? targetNode.getParentNode() : root);
  861. if (moveType != consts.move.TYPE_PREV && moveType != consts.move.TYPE_NEXT) {
  862. moveType = consts.move.TYPE_INNER;
  863. }
  864. if (moveType == consts.move.TYPE_INNER) {
  865. if (targetNodeIsRoot) {
  866. //parentTId of root node is null
  867. node.parentTId = null;
  868. } else {
  869. if (!targetNode.isParent) {
  870. targetNode.isParent = true;
  871. targetNode.open = !!targetNode.open;
  872. view.setNodeLineIcos(setting, targetNode);
  873. }
  874. node.parentTId = targetNode.tId;
  875. }
  876. }
  877. //move node Dom
  878. var targetObj, target_ulObj;
  879. if (targetNodeIsRoot) {
  880. targetObj = setting.treeObj;
  881. target_ulObj = targetObj;
  882. } else {
  883. if (!isSilent && moveType == consts.move.TYPE_INNER) {
  884. view.expandCollapseNode(setting, targetNode, true, false);
  885. } else if (!isSilent) {
  886. view.expandCollapseNode(setting, targetNode.getParentNode(), true, false);
  887. }
  888. targetObj = $$(targetNode, setting);
  889. target_ulObj = $$(targetNode, consts.id.UL, setting);
  890. if (!!targetObj.get(0) && !target_ulObj.get(0)) {
  891. var ulstr = [];
  892. view.makeUlHtml(setting, targetNode, ulstr, '');
  893. targetObj.append(ulstr.join(''));
  894. }
  895. target_ulObj = $$(targetNode, consts.id.UL, setting);
  896. }
  897. var nodeDom = $$(node, setting);
  898. if (!nodeDom.get(0)) {
  899. nodeDom = view.appendNodes(setting, node.level, [node], null, -1, false, true).join('');
  900. } else if (!targetObj.get(0)) {
  901. nodeDom.remove();
  902. }
  903. if (target_ulObj.get(0) && moveType == consts.move.TYPE_INNER) {
  904. target_ulObj.append(nodeDom);
  905. } else if (targetObj.get(0) && moveType == consts.move.TYPE_PREV) {
  906. targetObj.before(nodeDom);
  907. } else if (targetObj.get(0) && moveType == consts.move.TYPE_NEXT) {
  908. targetObj.after(nodeDom);
  909. }
  910. //repair the data after move
  911. var i,l,
  912. tmpSrcIndex = -1,
  913. tmpTargetIndex = 0,
  914. oldNeighbor = null,
  915. newNeighbor = null,
  916. oldLevel = node.level;
  917. if (node.isFirstNode) {
  918. tmpSrcIndex = 0;
  919. if (oldParentNode[childKey].length > 1 ) {
  920. oldNeighbor = oldParentNode[childKey][1];
  921. oldNeighbor.isFirstNode = true;
  922. }
  923. } else if (node.isLastNode) {
  924. tmpSrcIndex = oldParentNode[childKey].length -1;
  925. oldNeighbor = oldParentNode[childKey][tmpSrcIndex - 1];
  926. oldNeighbor.isLastNode = true;
  927. } else {
  928. for (i = 0, l = oldParentNode[childKey].length; i < l; i++) {
  929. if (oldParentNode[childKey][i].tId == node.tId) {
  930. tmpSrcIndex = i;
  931. break;
  932. }
  933. }
  934. }
  935. if (tmpSrcIndex >= 0) {
  936. oldParentNode[childKey].splice(tmpSrcIndex, 1);
  937. }
  938. if (moveType != consts.move.TYPE_INNER) {
  939. for (i = 0, l = targetParentNode[childKey].length; i < l; i++) {
  940. if (targetParentNode[childKey][i].tId == targetNode.tId) tmpTargetIndex = i;
  941. }
  942. }
  943. if (moveType == consts.move.TYPE_INNER) {
  944. if (!targetNode[childKey]) targetNode[childKey] = new Array();
  945. if (targetNode[childKey].length > 0) {
  946. newNeighbor = targetNode[childKey][targetNode[childKey].length - 1];
  947. newNeighbor.isLastNode = false;
  948. }
  949. targetNode[childKey].splice(targetNode[childKey].length, 0, node);
  950. node.isLastNode = true;
  951. node.isFirstNode = (targetNode[childKey].length == 1);
  952. } else if (targetNode.isFirstNode && moveType == consts.move.TYPE_PREV) {
  953. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  954. newNeighbor = targetNode;
  955. newNeighbor.isFirstNode = false;
  956. node.parentTId = targetNode.parentTId;
  957. node.isFirstNode = true;
  958. node.isLastNode = false;
  959. } else if (targetNode.isLastNode && moveType == consts.move.TYPE_NEXT) {
  960. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  961. newNeighbor = targetNode;
  962. newNeighbor.isLastNode = false;
  963. node.parentTId = targetNode.parentTId;
  964. node.isFirstNode = false;
  965. node.isLastNode = true;
  966. } else {
  967. if (moveType == consts.move.TYPE_PREV) {
  968. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  969. } else {
  970. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  971. }
  972. node.parentTId = targetNode.parentTId;
  973. node.isFirstNode = false;
  974. node.isLastNode = false;
  975. }
  976. data.fixPIdKeyValue(setting, node);
  977. data.setSonNodeLevel(setting, node.getParentNode(), node);
  978. //repair node what been moved
  979. view.setNodeLineIcos(setting, node);
  980. view.repairNodeLevelClass(setting, node, oldLevel)
  981. //repair node's old parentNode dom
  982. if (!setting.data.keep.parent && oldParentNode[childKey].length < 1) {
  983. //old parentNode has no child nodes
  984. oldParentNode.isParent = false;
  985. oldParentNode.open = false;
  986. var tmp_ulObj = $$(oldParentNode, consts.id.UL, setting),
  987. tmp_switchObj = $$(oldParentNode, consts.id.SWITCH, setting),
  988. tmp_icoObj = $$(oldParentNode, consts.id.ICON, setting);
  989. view.replaceSwitchClass(oldParentNode, tmp_switchObj, consts.folder.DOCU);
  990. view.replaceIcoClass(oldParentNode, tmp_icoObj, consts.folder.DOCU);
  991. tmp_ulObj.css("display", "none");
  992. } else if (oldNeighbor) {
  993. //old neigbor node
  994. view.setNodeLineIcos(setting, oldNeighbor);
  995. }
  996. //new neigbor node
  997. if (newNeighbor) {
  998. view.setNodeLineIcos(setting, newNeighbor);
  999. }
  1000. //repair checkbox / radio
  1001. if (!!setting.check && setting.check.enable && view.repairChkClass) {
  1002. view.repairChkClass(setting, oldParentNode);
  1003. view.repairParentChkClassWithSelf(setting, oldParentNode);
  1004. if (oldParentNode != node.parent)
  1005. view.repairParentChkClassWithSelf(setting, node);
  1006. }
  1007. //expand parents after move
  1008. if (!isSilent) {
  1009. view.expandCollapseParentNode(setting, node.getParentNode(), true, animateFlag);
  1010. }
  1011. },
  1012. removeEditBtn: function(setting, node) {
  1013. $$(node, consts.id.EDIT, setting).unbind().remove();
  1014. },
  1015. removeRemoveBtn: function(setting, node) {
  1016. $$(node, consts.id.REMOVE, setting).unbind().remove();
  1017. },
  1018. removeTreeDom: function(setting, node) {
  1019. node.isHover = false;
  1020. view.removeEditBtn(setting, node);
  1021. view.removeRemoveBtn(setting, node);
  1022. tools.apply(setting.view.removeHoverDom, [setting.treeId, node]);
  1023. },
  1024. repairNodeLevelClass: function(setting, node, oldLevel) {
  1025. if (oldLevel === node.level) return;
  1026. var liObj = $$(node, setting),
  1027. aObj = $$(node, consts.id.A, setting),
  1028. ulObj = $$(node, consts.id.UL, setting),
  1029. oldClass = consts.className.LEVEL + oldLevel,
  1030. newClass = consts.className.LEVEL + node.level;
  1031. liObj.removeClass(oldClass);
  1032. liObj.addClass(newClass);
  1033. aObj.removeClass(oldClass);
  1034. aObj.addClass(newClass);
  1035. ulObj.removeClass(oldClass);
  1036. ulObj.addClass(newClass);
  1037. },
  1038. selectNodes : function(setting, nodes) {
  1039. for (var i=0, l=nodes.length; i<l; i++) {
  1040. view.selectNode(setting, nodes[i], i>0);
  1041. }
  1042. }
  1043. },
  1044. _z = {
  1045. tools: _tools,
  1046. view: _view,
  1047. event: _event,
  1048. data: _data
  1049. };
  1050. $.extend(true, $.fn.zTree.consts, _consts);
  1051. $.extend(true, $.fn.zTree._z, _z);
  1052. var zt = $.fn.zTree,
  1053. tools = zt._z.tools,
  1054. consts = zt.consts,
  1055. view = zt._z.view,
  1056. data = zt._z.data,
  1057. event = zt._z.event,
  1058. $$ = tools.$;
  1059. data.exSetting(_setting);
  1060. data.addInitBind(_bindEvent);
  1061. data.addInitUnBind(_unbindEvent);
  1062. data.addInitCache(_initCache);
  1063. data.addInitNode(_initNode);
  1064. data.addInitProxy(_eventProxy);
  1065. data.addInitRoot(_initRoot);
  1066. data.addZTreeTools(_zTreeTools);
  1067. var _cancelPreSelectedNode = view.cancelPreSelectedNode;
  1068. view.cancelPreSelectedNode = function (setting, node) {
  1069. var list = data.getRoot(setting).curSelectedList;
  1070. for (var i=0, j=list.length; i<j; i++) {
  1071. if (!node || node === list[i]) {
  1072. view.removeTreeDom(setting, list[i]);
  1073. if (node) break;
  1074. }
  1075. }
  1076. if (_cancelPreSelectedNode) _cancelPreSelectedNode.apply(view, arguments);
  1077. }
  1078. var _createNodes = view.createNodes;
  1079. view.createNodes = function(setting, level, nodes, parentNode, index) {
  1080. if (_createNodes) {
  1081. _createNodes.apply(view, arguments);
  1082. }
  1083. if (!nodes) return;
  1084. if (view.repairParentChkClassWithSelf) {
  1085. view.repairParentChkClassWithSelf(setting, parentNode);
  1086. }
  1087. }
  1088. var _makeNodeUrl = view.makeNodeUrl;
  1089. view.makeNodeUrl = function(setting, node) {
  1090. return setting.edit.enable ? null : (_makeNodeUrl.apply(view, arguments));
  1091. }
  1092. var _removeNode = view.removeNode;
  1093. view.removeNode = function(setting, node) {
  1094. var root = data.getRoot(setting);
  1095. if (root.curEditNode === node) root.curEditNode = null;
  1096. if (_removeNode) {
  1097. _removeNode.apply(view, arguments);
  1098. }
  1099. }
  1100. var _selectNode = view.selectNode;
  1101. view.selectNode = function(setting, node, addFlag) {
  1102. var root = data.getRoot(setting);
  1103. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  1104. return false;
  1105. }
  1106. if (_selectNode) _selectNode.apply(view, arguments);
  1107. view.addHoverDom(setting, node);
  1108. return true;
  1109. }
  1110. var _uCanDo = tools.uCanDo;
  1111. tools.uCanDo = function(setting, e) {
  1112. var root = data.getRoot(setting);
  1113. if (e && (tools.eqs(e.type, "mouseover") || tools.eqs(e.type, "mouseout") || tools.eqs(e.type, "mousedown") || tools.eqs(e.type, "mouseup"))) {
  1114. return true;
  1115. }
  1116. if (root.curEditNode) {
  1117. view.editNodeBlur = false;
  1118. root.curEditInput.focus();
  1119. }
  1120. return (!root.curEditNode) && (_uCanDo ? _uCanDo.apply(view, arguments) : true);
  1121. }
  1122. })(jQuery);