dragWithOther.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - drag with other DOM</TITLE>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  7. <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  8. <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  10. <!--<script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>-->
  11. <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>
  12. <SCRIPT type="text/javascript">
  13. <!--
  14. var MoveTest = {
  15. errorMsg: "Error!...Please drag it to the correct category!",
  16. curTarget: null,
  17. curTmpTarget: null,
  18. noSel: function() {
  19. try {
  20. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  21. } catch(e){}
  22. },
  23. dragTree2Dom: function(treeId, treeNodes) {
  24. return !treeNodes[0].isParent;
  25. },
  26. prevTree: function(treeId, treeNodes, targetNode) {
  27. return !targetNode.isParent && targetNode.parentTId == treeNodes[0].parentTId;
  28. },
  29. nextTree: function(treeId, treeNodes, targetNode) {
  30. return !targetNode.isParent && targetNode.parentTId == treeNodes[0].parentTId;
  31. },
  32. innerTree: function(treeId, treeNodes, targetNode) {
  33. return targetNode!=null && targetNode.isParent && targetNode.tId == treeNodes[0].parentTId;
  34. },
  35. dropTree2Dom: function(e, treeId, treeNodes, targetNode, moveType) {
  36. var domId = "dom_" + treeNodes[0].getParentNode().id;
  37. if (moveType == null && (domId == e.target.id || $(e.target).parents("#" + domId).length > 0)) {
  38. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  39. zTree.removeNode(treeNodes[0]);
  40. var newDom = $("span[domId=" + treeNodes[0].id + "]");
  41. if (newDom.length > 0) {
  42. newDom.removeClass("domBtn_Disabled");
  43. newDom.addClass("domBtn");
  44. } else {
  45. $("#" + domId).append("<span class='domBtn' domId='" + treeNodes[0].id + "'>" + treeNodes[0].name + "</span>");
  46. }
  47. MoveTest.updateType();
  48. } else if ( $(e.target).parents(".domBtnDiv").length > 0) {
  49. alert(MoveTest.errorMsg);
  50. }
  51. },
  52. dom2Tree: function(e, treeId, treeNode) {
  53. var target = MoveTest.curTarget, tmpTarget = MoveTest.curTmpTarget;
  54. if (!target) return;
  55. var zTree = $.fn.zTree.getZTreeObj("treeDemo"), parentNode;
  56. if (treeNode != null && treeNode.isParent && "dom_" + treeNode.id == target.parent().attr("id")) {
  57. parentNode = treeNode;
  58. } else if (treeNode != null && !treeNode.isParent && "dom_" + treeNode.getParentNode().id == target.parent().attr("id")) {
  59. parentNode = treeNode.getParentNode();
  60. }
  61. if (tmpTarget) tmpTarget.remove();
  62. if (!!parentNode) {
  63. var nodes = zTree.addNodes(parentNode, {id:target.attr("domId"), name: target.text()});
  64. zTree.selectNode(nodes[0]);
  65. } else {
  66. target.removeClass("domBtn_Disabled");
  67. target.addClass("domBtn");
  68. alert(MoveTest.errorMsg);
  69. }
  70. MoveTest.curTarget = null;
  71. MoveTest.curTmpTarget = null;
  72. },
  73. updateType: function() {
  74. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  75. nodes = zTree.getNodes();
  76. for (var i=0, l=nodes.length; i<l; i++) {
  77. var num = nodes[i].children ? nodes[i].children.length : 0;
  78. nodes[i].name = nodes[i].name.replace(/ \(.*\)/gi, "") + " (" + num + ")";
  79. zTree.updateNode(nodes[i]);
  80. }
  81. },
  82. bindDom: function() {
  83. $(".domBtnDiv").bind("mousedown", MoveTest.bindMouseDown);
  84. },
  85. bindMouseDown: function(e) {
  86. var target = e.target;
  87. if (target!=null && target.className=="domBtn") {
  88. var doc = $(document), target = $(target),
  89. docScrollTop = doc.scrollTop(),
  90. docScrollLeft = doc.scrollLeft();
  91. target.addClass("domBtn_Disabled");
  92. target.removeClass("domBtn");
  93. curDom = $("<span class='dom_tmp domBtn'>" + target.text() + "</span>");
  94. curDom.appendTo("body");
  95. curDom.css({
  96. "top": (e.clientY + docScrollTop + 3) + "px",
  97. "left": (e.clientX + docScrollLeft + 3) + "px"
  98. });
  99. MoveTest.curTarget = target;
  100. MoveTest.curTmpTarget = curDom;
  101. doc.bind("mousemove", MoveTest.bindMouseMove);
  102. doc.bind("mouseup", MoveTest.bindMouseUp);
  103. doc.bind("selectstart", MoveTest.docSelect);
  104. }
  105. if(e.preventDefault) {
  106. e.preventDefault();
  107. }
  108. },
  109. bindMouseMove: function(e) {
  110. MoveTest.noSel();
  111. var doc = $(document),
  112. docScrollTop = doc.scrollTop(),
  113. docScrollLeft = doc.scrollLeft(),
  114. tmpTarget = MoveTest.curTmpTarget;
  115. if (tmpTarget) {
  116. tmpTarget.css({
  117. "top": (e.clientY + docScrollTop + 3) + "px",
  118. "left": (e.clientX + docScrollLeft + 3) + "px"
  119. });
  120. }
  121. return false;
  122. },
  123. bindMouseUp: function(e) {
  124. var doc = $(document);
  125. doc.unbind("mousemove", MoveTest.bindMouseMove);
  126. doc.unbind("mouseup", MoveTest.bindMouseUp);
  127. doc.unbind("selectstart", MoveTest.docSelect);
  128. var target = MoveTest.curTarget, tmpTarget = MoveTest.curTmpTarget;
  129. if (tmpTarget) tmpTarget.remove();
  130. if ($(e.target).parents("#treeDemo").length == 0) {
  131. if (target) {
  132. target.removeClass("domBtn_Disabled");
  133. target.addClass("domBtn");
  134. }
  135. MoveTest.curTarget = null;
  136. MoveTest.curTmpTarget = null;
  137. }
  138. },
  139. bindSelect: function() {
  140. return false;
  141. }
  142. };
  143. var setting = {
  144. edit: {
  145. enable: true,
  146. showRemoveBtn: false,
  147. showRenameBtn: false,
  148. drag: {
  149. prev: MoveTest.prevTree,
  150. next: MoveTest.nextTree,
  151. inner: MoveTest.innerTree
  152. }
  153. },
  154. data: {
  155. keep: {
  156. parent: true,
  157. leaf: true
  158. },
  159. simpleData: {
  160. enable: true
  161. }
  162. },
  163. callback: {
  164. beforeDrag: MoveTest.dragTree2Dom,
  165. onDrop: MoveTest.dropTree2Dom,
  166. onMouseUp: MoveTest.dom2Tree
  167. },
  168. view: {
  169. selectedMulti: false
  170. }
  171. };
  172. var zNodes =[
  173. { id:1, pId:0, name:"PLANT", isParent: true, open:true},
  174. { id:2, pId:0, name:"ANIMAL", isParent: true, open:true},
  175. { id:20, pId:2, name:"Elephant"},
  176. { id:29, pId:2, name:"Shark"},
  177. { id:10, pId:1, name:"Cabbage"},
  178. { id:19, pId:1, name:"Tomato"}
  179. ];
  180. $(document).ready(function(){
  181. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  182. MoveTest.updateType();
  183. MoveTest.bindDom();
  184. });
  185. //-->
  186. </SCRIPT>
  187. <style type="text/css">
  188. .dom_line {margin:2px;border-bottom:1px gray dotted;height:1px}
  189. .domBtnDiv {display:block;padding:2px;border:1px gray dotted;background-color:powderblue}
  190. .categoryDiv {display:inline-block; width:335px}
  191. .domBtn {display:inline-block;cursor:pointer;padding:2px;margin:2px 10px;border:1px gray solid;background-color:#FFE6B0}
  192. .domBtn_Disabled {display:inline-block;cursor:default;padding:2px;margin:2px 10px;border:1px gray solid;background-color:#DFDFDF;color:#999999}
  193. .dom_tmp {position:absolute;font-size:12px;}
  194. </style>
  195. </HEAD>
  196. <BODY>
  197. <h1>Drag With Other DOMs</h1>
  198. <h6>[ File Path: super/dragWithOther.html ]</h6>
  199. <div class="content_wrap">
  200. <div class="zTreeDemoBackground left">
  201. <ul id="treeDemo" class="ztree"></ul>
  202. </div>
  203. <div class="right">
  204. <ul class="info">
  205. <li class="title"><h2>Explanation of implementation method</h2>
  206. <ul class="list">
  207. <li>zTree v3.2 modified the event object in onDrag/onDrop, thus the nodes can be draged to other DOMs easily.</li>
  208. <li class="highlight_red">To drag other DOMS to zTree, you need to build the drag codes by yourself.</li>
  209. <li class="highlight_red">This is just a simple Demo. If you need more features, please build more complicated codes.</li>
  210. </ul>
  211. </li>
  212. <li class="title"><h2>Please drag and drop the following content to the tree node</h2>
  213. <div class="domBtnDiv">
  214. <div id="dom_1" class="categoryDiv"><span class="domBtn" domId="11">Tree</span><span class="domBtn" domId="12">Grass</span><span class="domBtn" domId="13">Flower</span></div>
  215. <div class="dom_line"></div>
  216. <div id="dom_2" class="categoryDiv"><span class="domBtn" domId="21">Tiger</span><span class="domBtn" domId="22">Bear</span><span class="domBtn" domId="23">Lion</span></div>
  217. </div>
  218. <span class="highlight_red">You can drag the nodes of level 2 to the above layer.</span>
  219. </li>
  220. </ul>
  221. </div>
  222. </div>
  223. </BODY>
  224. </HTML>