説明なし

jquery.ztree.exedit.js 41KB

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