jquery.ztree.exedit-3.5.js 42 KB

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