PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs/extjs/4.2.1/src/tree/ViewDropZone.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 342 lines | 221 code | 42 blank | 79 comment | 71 complexity | f2ea56c2860a965bc6b834e4483ca6c0 MD5 | raw file
  1. /*
  2. This file is part of Ext JS 4.2
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-05-16 14:36:50 (f9be68accb407158ba2b1be2c226a6ce1f649314)
  14. */
  15. /**
  16. * @private
  17. */
  18. Ext.define('Ext.tree.ViewDropZone', {
  19. extend: 'Ext.view.DropZone',
  20. /**
  21. * @cfg {Boolean} allowParentInserts
  22. * Allow inserting a dragged node between an expanded parent node and its first child that will become a
  23. * sibling of the parent when dropped.
  24. */
  25. allowParentInserts: false,
  26. /**
  27. * @cfg {Boolean} allowContainerDrop
  28. * True if drops on the tree container (outside of a specific tree node) are allowed.
  29. */
  30. allowContainerDrops: false,
  31. /**
  32. * @cfg {Boolean} appendOnly
  33. * True if the tree should only allow append drops (use for trees which are sorted).
  34. */
  35. appendOnly: false,
  36. /**
  37. * @cfg {Number} expandDelay
  38. * The delay in milliseconds to wait before expanding a target tree node while dragging a droppable node
  39. * over the target.
  40. */
  41. expandDelay : 500,
  42. indicatorCls: Ext.baseCSSPrefix + 'tree-ddindicator',
  43. // @private
  44. expandNode : function(node) {
  45. var view = this.view;
  46. this.expandProcId = false;
  47. if (!node.isLeaf() && !node.isExpanded()) {
  48. view.expand(node);
  49. this.expandProcId = false;
  50. }
  51. },
  52. // @private
  53. queueExpand : function(node) {
  54. this.expandProcId = Ext.Function.defer(this.expandNode, this.expandDelay, this, [node]);
  55. },
  56. // @private
  57. cancelExpand : function() {
  58. if (this.expandProcId) {
  59. clearTimeout(this.expandProcId);
  60. this.expandProcId = false;
  61. }
  62. },
  63. getPosition: function(e, node) {
  64. var view = this.view,
  65. record = view.getRecord(node),
  66. y = e.getPageY(),
  67. noAppend = record.isLeaf(),
  68. noBelow = false,
  69. region = Ext.fly(node).getRegion(),
  70. fragment;
  71. // If we are dragging on top of the root node of the tree, we always want to append.
  72. if (record.isRoot()) {
  73. return 'append';
  74. }
  75. // Return 'append' if the node we are dragging on top of is not a leaf else return false.
  76. if (this.appendOnly) {
  77. return noAppend ? false : 'append';
  78. }
  79. if (!this.allowParentInserts) {
  80. noBelow = record.hasChildNodes() && record.isExpanded();
  81. }
  82. fragment = (region.bottom - region.top) / (noAppend ? 2 : 3);
  83. if (y >= region.top && y < (region.top + fragment)) {
  84. return 'before';
  85. }
  86. else if (!noBelow && (noAppend || (y >= (region.bottom - fragment) && y <= region.bottom))) {
  87. return 'after';
  88. }
  89. else {
  90. return 'append';
  91. }
  92. },
  93. isValidDropPoint : function(node, position, dragZone, e, data) {
  94. if (!node || !data.item) {
  95. return false;
  96. }
  97. var view = this.view,
  98. targetNode = view.getRecord(node),
  99. draggedRecords = data.records,
  100. dataLength = draggedRecords.length,
  101. ln = draggedRecords.length,
  102. i, record;
  103. // No drop position, or dragged records: invalid drop point
  104. if (!(targetNode && position && dataLength)) {
  105. return false;
  106. }
  107. // If the targetNode is within the folder we are dragging
  108. for (i = 0; i < ln; i++) {
  109. record = draggedRecords[i];
  110. if (record.isNode && record.contains(targetNode)) {
  111. return false;
  112. }
  113. }
  114. // Respect the allowDrop field on Tree nodes
  115. if (position === 'append' && targetNode.get('allowDrop') === false) {
  116. return false;
  117. }
  118. else if (position != 'append' && targetNode.parentNode.get('allowDrop') === false) {
  119. return false;
  120. }
  121. // If the target record is in the dragged dataset, then invalid drop
  122. if (Ext.Array.contains(draggedRecords, targetNode)) {
  123. return false;
  124. }
  125. return view.fireEvent('nodedragover', targetNode, position, data, e) !== false;
  126. },
  127. onNodeOver : function(node, dragZone, e, data) {
  128. var position = this.getPosition(e, node),
  129. returnCls = this.dropNotAllowed,
  130. view = this.view,
  131. targetNode = view.getRecord(node),
  132. indicator = this.getIndicator(),
  133. indicatorY = 0;
  134. // auto node expand check
  135. this.cancelExpand();
  136. if (position == 'append' && !this.expandProcId && !Ext.Array.contains(data.records, targetNode) && !targetNode.isLeaf() && !targetNode.isExpanded()) {
  137. this.queueExpand(targetNode);
  138. }
  139. if (this.isValidDropPoint(node, position, dragZone, e, data)) {
  140. this.valid = true;
  141. this.currentPosition = position;
  142. this.overRecord = targetNode;
  143. indicator.setWidth(Ext.fly(node).getWidth());
  144. indicatorY = Ext.fly(node).getY() - Ext.fly(view.el).getY() - 1;
  145. /*
  146. * In the code below we show the proxy again. The reason for doing this is showing the indicator will
  147. * call toFront, causing it to get a new z-index which can sometimes push the proxy behind it. We always
  148. * want the proxy to be above, so calling show on the proxy will call toFront and bring it forward.
  149. */
  150. if (position == 'before') {
  151. returnCls = targetNode.isFirst() ? Ext.baseCSSPrefix + 'tree-drop-ok-above' : Ext.baseCSSPrefix + 'tree-drop-ok-between';
  152. indicator.showAt(0, indicatorY);
  153. dragZone.proxy.show();
  154. } else if (position == 'after') {
  155. returnCls = targetNode.isLast() ? Ext.baseCSSPrefix + 'tree-drop-ok-below' : Ext.baseCSSPrefix + 'tree-drop-ok-between';
  156. indicatorY += Ext.fly(node).getHeight();
  157. indicator.showAt(0, indicatorY);
  158. dragZone.proxy.show();
  159. } else {
  160. returnCls = Ext.baseCSSPrefix + 'tree-drop-ok-append';
  161. // @TODO: set a class on the parent folder node to be able to style it
  162. indicator.hide();
  163. }
  164. } else {
  165. this.valid = false;
  166. }
  167. this.currentCls = returnCls;
  168. return returnCls;
  169. },
  170. // The mouse is no longer over a tree node, so dropping is not valid
  171. onNodeOut : function(n, dd, e, data){
  172. this.valid = false;
  173. this.getIndicator().hide();
  174. },
  175. onContainerOver : function(dd, e, data) {
  176. return e.getTarget('.' + this.indicatorCls) ? this.currentCls : this.dropNotAllowed;
  177. },
  178. notifyOut: function() {
  179. this.callParent(arguments);
  180. this.cancelExpand();
  181. },
  182. handleNodeDrop : function(data, targetNode, position) {
  183. var me = this,
  184. targetView = me.view,
  185. parentNode = targetNode ? targetNode.parentNode : targetView.panel.getRootNode(),
  186. Model = targetView.getStore().treeStore.model,
  187. records, i, len, record,
  188. insertionMethod, argList,
  189. needTargetExpand,
  190. transferData;
  191. // If the copy flag is set, create a copy of the models
  192. if (data.copy) {
  193. records = data.records;
  194. data.records = [];
  195. for (i = 0, len = records.length; i < len; i++) {
  196. record = records[i];
  197. if (record.isNode) {
  198. data.records.push(record.copy(undefined, true));
  199. } else {
  200. // If it's not a node, make a node copy
  201. data.records.push(new Model(record.data, record.getId()));
  202. }
  203. }
  204. }
  205. // Cancel any pending expand operation
  206. me.cancelExpand();
  207. // Grab a reference to the correct node insertion method.
  208. // Create an arg list array intended for the apply method of the
  209. // chosen node insertion method.
  210. // Ensure the target object for the method is referenced by 'targetNode'
  211. if (position == 'before') {
  212. insertionMethod = parentNode.insertBefore;
  213. argList = [null, targetNode];
  214. targetNode = parentNode;
  215. }
  216. else if (position == 'after') {
  217. if (targetNode.nextSibling) {
  218. insertionMethod = parentNode.insertBefore;
  219. argList = [null, targetNode.nextSibling];
  220. }
  221. else {
  222. insertionMethod = parentNode.appendChild;
  223. argList = [null];
  224. }
  225. targetNode = parentNode;
  226. }
  227. else {
  228. if (!(targetNode.isExpanded() || targetNode.isLoading())) {
  229. needTargetExpand = true;
  230. }
  231. insertionMethod = targetNode.appendChild;
  232. argList = [null];
  233. }
  234. // A function to transfer the data into the destination tree
  235. transferData = function() {
  236. var color,
  237. n;
  238. // Coalesce layouts caused by node removal, appending and sorting
  239. Ext.suspendLayouts();
  240. targetView.getSelectionModel().clearSelections();
  241. // Insert the records into the target node
  242. for (i = 0, len = data.records.length; i < len; i++) {
  243. record = data.records[i];
  244. if (!record.isNode) {
  245. if (record.isModel) {
  246. record = new Model(record.data, record.getId());
  247. } else {
  248. record = new Model(record);
  249. }
  250. data.records[i] = record;
  251. }
  252. argList[0] = record;
  253. insertionMethod.apply(targetNode, argList);
  254. }
  255. // If configured to sort on drop, do it according to the TreeStore's comparator
  256. if (me.sortOnDrop) {
  257. targetNode.sort(targetNode.getOwnerTree().store.generateComparator());
  258. }
  259. Ext.resumeLayouts(true);
  260. // Kick off highlights after everything's been inserted, so they are
  261. // more in sync without insertion/render overhead.
  262. // Element.highlight can handle highlighting table nodes.
  263. if (Ext.enableFx && me.dropHighlight) {
  264. color = me.dropHighlightColor;
  265. for (i = 0; i < len; i++) {
  266. n = targetView.getNode(data.records[i]);
  267. if (n) {
  268. Ext.fly(n).highlight(color);
  269. }
  270. }
  271. }
  272. };
  273. // If dropping right on an unexpanded node, transfer the data after it is expanded.
  274. if (needTargetExpand) {
  275. targetNode.expand(false, transferData);
  276. }
  277. // If the node is waiting for its children, we must transfer the data after the expansion.
  278. // The expand event does NOT signal UI expansion, it is the SIGNAL for UI expansion.
  279. // It's listened for by the NodeStore on the root node. Which means that listeners on the target
  280. // node get notified BEFORE UI expansion. So we need a delay.
  281. // TODO: Refactor NodeInterface.expand/collapse to notify its owning tree directly when it needs to expand/collapse.
  282. else if (targetNode.isLoading()) {
  283. targetNode.on({
  284. expand: transferData,
  285. delay: 1,
  286. single: true
  287. });
  288. }
  289. // Otherwise, call the data transfer function immediately
  290. else {
  291. transferData();
  292. }
  293. }
  294. });