DragSource.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-dd-DragSource'>/**
  19. </span> * A simple class that provides the basic implementation needed to make any element draggable.
  20. */
  21. Ext.define('Ext.dd.DragSource', {
  22. extend: 'Ext.dd.DDProxy',
  23. requires: [
  24. 'Ext.dd.StatusProxy',
  25. 'Ext.dd.DragDropManager'
  26. ],
  27. <span id='Ext-dd-DragSource-cfg-ddGroup'> /**
  28. </span> * @cfg {String} ddGroup
  29. * A named drag drop group to which this object belongs. If a group is specified, then this object will only
  30. * interact with other drag drop objects in the same group.
  31. */
  32. <span id='Ext-dd-DragSource-cfg-dropAllowed'> /**
  33. </span> * @cfg {String} dropAllowed
  34. * The CSS class returned to the drag source when drop is allowed.
  35. */
  36. dropAllowed : Ext.baseCSSPrefix + 'dd-drop-ok',
  37. <span id='Ext-dd-DragSource-cfg-dropNotAllowed'> /**
  38. </span> * @cfg {String} dropNotAllowed
  39. * The CSS class returned to the drag source when drop is not allowed.
  40. */
  41. dropNotAllowed : Ext.baseCSSPrefix + 'dd-drop-nodrop',
  42. <span id='Ext-dd-DragSource-cfg-animRepair'> /**
  43. </span> * @cfg {Boolean} animRepair
  44. * If true, animates the proxy element back to the position of the handle element used to trigger the drag.
  45. */
  46. animRepair: true,
  47. <span id='Ext-dd-DragSource-cfg-repairHighlightColor'> /**
  48. </span> * @cfg {String} repairHighlightColor
  49. * The color to use when visually highlighting the drag source in the afterRepair
  50. * method after a failed drop (defaults to light blue). The color must be a 6 digit hex value, without
  51. * a preceding '#'.
  52. */
  53. repairHighlightColor: 'c3daf9',
  54. <span id='Ext-dd-DragSource-method-constructor'> /**
  55. </span> * Creates new drag-source.
  56. * @param {String/HTMLElement/Ext.Element} el The container element or ID of it.
  57. * @param {Object} config (optional) Config object.
  58. */
  59. constructor: function(el, config) {
  60. this.el = Ext.get(el);
  61. if(!this.dragData){
  62. this.dragData = {};
  63. }
  64. Ext.apply(this, config);
  65. if(!this.proxy){
  66. this.proxy = new Ext.dd.StatusProxy({
  67. id: this.el.id + '-drag-status-proxy',
  68. animRepair: this.animRepair
  69. });
  70. }
  71. this.callParent([this.el.dom, this.ddGroup || this.group,
  72. {dragElId : this.proxy.id, resizeFrame: false, isTarget: false, scroll: this.scroll === true}]);
  73. this.dragging = false;
  74. },
  75. <span id='Ext-dd-DragSource-method-getDragData'> /**
  76. </span> * Returns the data object associated with this drag source
  77. * @return {Object} data An object containing arbitrary data
  78. */
  79. getDragData : function(e){
  80. return this.dragData;
  81. },
  82. // private
  83. onDragEnter : function(e, id){
  84. var target = Ext.dd.DragDropManager.getDDById(id),
  85. status;
  86. this.cachedTarget = target;
  87. if (this.beforeDragEnter(target, e, id) !== false) {
  88. if (target.isNotifyTarget) {
  89. status = target.notifyEnter(this, e, this.dragData);
  90. this.proxy.setStatus(status);
  91. } else {
  92. this.proxy.setStatus(this.dropAllowed);
  93. }
  94. if (this.afterDragEnter) {
  95. <span id='Ext-dd-DragSource-method-afterDragEnter'> /**
  96. </span> * An empty function by default, but provided so that you can perform a custom action
  97. * when the dragged item enters the drop target by providing an implementation.
  98. * @param {Ext.dd.DragDrop} target The drop target
  99. * @param {Event} e The event object
  100. * @param {String} id The id of the dragged element
  101. * @method afterDragEnter
  102. */
  103. this.afterDragEnter(target, e, id);
  104. }
  105. }
  106. },
  107. <span id='Ext-dd-DragSource-method-beforeDragEnter'> /**
  108. </span> * An empty function by default, but provided so that you can perform a custom action
  109. * before the dragged item enters the drop target and optionally cancel the onDragEnter.
  110. * @param {Ext.dd.DragDrop} target The drop target
  111. * @param {Event} e The event object
  112. * @param {String} id The id of the dragged element
  113. * @return {Boolean} isValid True if the drag event is valid, else false to cancel
  114. * @template
  115. */
  116. beforeDragEnter: function(target, e, id) {
  117. return true;
  118. },
  119. // private
  120. onDragOver: function(e, id) {
  121. var target = this.cachedTarget || Ext.dd.DragDropManager.getDDById(id),
  122. status;
  123. if (this.beforeDragOver(target, e, id) !== false) {
  124. if(target.isNotifyTarget){
  125. status = target.notifyOver(this, e, this.dragData);
  126. this.proxy.setStatus(status);
  127. }
  128. if (this.afterDragOver) {
  129. <span id='Ext-dd-DragSource-method-afterDragOver'> /**
  130. </span> * An empty function by default, but provided so that you can perform a custom action
  131. * while the dragged item is over the drop target by providing an implementation.
  132. * @param {Ext.dd.DragDrop} target The drop target
  133. * @param {Event} e The event object
  134. * @param {String} id The id of the dragged element
  135. * @method afterDragOver
  136. */
  137. this.afterDragOver(target, e, id);
  138. }
  139. }
  140. },
  141. <span id='Ext-dd-DragSource-method-beforeDragOver'> /**
  142. </span> * An empty function by default, but provided so that you can perform a custom action
  143. * while the dragged item is over the drop target and optionally cancel the onDragOver.
  144. * @param {Ext.dd.DragDrop} target The drop target
  145. * @param {Event} e The event object
  146. * @param {String} id The id of the dragged element
  147. * @return {Boolean} isValid True if the drag event is valid, else false to cancel
  148. * @template
  149. */
  150. beforeDragOver: function(target, e, id) {
  151. return true;
  152. },
  153. // private
  154. onDragOut: function(e, id) {
  155. var target = this.cachedTarget || Ext.dd.DragDropManager.getDDById(id);
  156. if (this.beforeDragOut(target, e, id) !== false) {
  157. if (target.isNotifyTarget) {
  158. target.notifyOut(this, e, this.dragData);
  159. }
  160. this.proxy.reset();
  161. if (this.afterDragOut) {
  162. <span id='Ext-dd-DragSource-method-afterDragOut'> /**
  163. </span> * An empty function by default, but provided so that you can perform a custom action
  164. * after the dragged item is dragged out of the target without dropping.
  165. * @param {Ext.dd.DragDrop} target The drop target
  166. * @param {Event} e The event object
  167. * @param {String} id The id of the dragged element
  168. * @method afterDragOut
  169. */
  170. this.afterDragOut(target, e, id);
  171. }
  172. }
  173. this.cachedTarget = null;
  174. },
  175. <span id='Ext-dd-DragSource-method-beforeDragOut'> /**
  176. </span> * An empty function by default, but provided so that you can perform a custom action before the dragged
  177. * item is dragged out of the target without dropping, and optionally cancel the onDragOut.
  178. * @param {Ext.dd.DragDrop} target The drop target
  179. * @param {Event} e The event object
  180. * @param {String} id The id of the dragged element
  181. * @return {Boolean} isValid True if the drag event is valid, else false to cancel
  182. * @template
  183. */
  184. beforeDragOut: function(target, e, id){
  185. return true;
  186. },
  187. // private
  188. onDragDrop: function(e, id){
  189. var target = this.cachedTarget || Ext.dd.DragDropManager.getDDById(id);
  190. if (this.beforeDragDrop(target, e, id) !== false) {
  191. if (target.isNotifyTarget) {
  192. if (target.notifyDrop(this, e, this.dragData) !== false) { // valid drop?
  193. this.onValidDrop(target, e, id);
  194. } else {
  195. this.onInvalidDrop(target, e, id);
  196. }
  197. } else {
  198. this.onValidDrop(target, e, id);
  199. }
  200. if (this.afterDragDrop) {
  201. <span id='Ext-dd-DragSource-method-afterDragDrop'> /**
  202. </span> * An empty function by default, but provided so that you can perform a custom action
  203. * after a valid drag drop has occurred by providing an implementation.
  204. * @param {Ext.dd.DragDrop} target The drop target
  205. * @param {Event} e The event object
  206. * @param {String} id The id of the dropped element
  207. * @method afterDragDrop
  208. */
  209. this.afterDragDrop(target, e, id);
  210. }
  211. }
  212. delete this.cachedTarget;
  213. },
  214. <span id='Ext-dd-DragSource-method-beforeDragDrop'> /**
  215. </span> * An empty function by default, but provided so that you can perform a custom action before the dragged
  216. * item is dropped onto the target and optionally cancel the onDragDrop.
  217. * @param {Ext.dd.DragDrop} target The drop target
  218. * @param {Event} e The event object
  219. * @param {String} id The id of the dragged element
  220. * @return {Boolean} isValid True if the drag drop event is valid, else false to cancel
  221. * @template
  222. */
  223. beforeDragDrop: function(target, e, id){
  224. return true;
  225. },
  226. // private
  227. onValidDrop: function(target, e, id){
  228. this.hideProxy();
  229. if(this.afterValidDrop){
  230. <span id='Ext-dd-DragSource-method-afterValidDrop'> /**
  231. </span> * An empty function by default, but provided so that you can perform a custom action
  232. * after a valid drop has occurred by providing an implementation.
  233. * @param {Object} target The target DD
  234. * @param {Event} e The event object
  235. * @param {String} id The id of the dropped element
  236. * @method afterValidDrop
  237. */
  238. this.afterValidDrop(target, e, id);
  239. }
  240. },
  241. // private
  242. getRepairXY: function(e, data){
  243. return this.el.getXY();
  244. },
  245. // private
  246. onInvalidDrop: function(target, e, id) {
  247. // This method may be called by the DragDropManager.
  248. // To preserve backwards compat, it only passes the event object
  249. // Here we correct the arguments.
  250. if (!e) {
  251. e = target;
  252. target = null;
  253. id = e.getTarget().id;
  254. }
  255. this.beforeInvalidDrop(target, e, id);
  256. if (this.cachedTarget) {
  257. if(this.cachedTarget.isNotifyTarget){
  258. this.cachedTarget.notifyOut(this, e, this.dragData);
  259. }
  260. this.cacheTarget = null;
  261. }
  262. this.proxy.repair(this.getRepairXY(e, this.dragData), this.afterRepair, this);
  263. if (this.afterInvalidDrop) {
  264. <span id='Ext-dd-DragSource-method-afterInvalidDrop'> /**
  265. </span> * An empty function by default, but provided so that you can perform a custom action
  266. * after an invalid drop has occurred by providing an implementation.
  267. * @param {Event} e The event object
  268. * @param {String} id The id of the dropped element
  269. * @method afterInvalidDrop
  270. */
  271. this.afterInvalidDrop(e, id);
  272. }
  273. },
  274. // private
  275. afterRepair: function() {
  276. var me = this;
  277. if (Ext.enableFx) {
  278. me.el.highlight(me.repairHighlightColor);
  279. }
  280. me.dragging = false;
  281. },
  282. <span id='Ext-dd-DragSource-method-beforeInvalidDrop'> /**
  283. </span> * An empty function by default, but provided so that you can perform a custom action after an invalid
  284. * drop has occurred.
  285. * @param {Ext.dd.DragDrop} target The drop target
  286. * @param {Event} e The event object
  287. * @param {String} id The id of the dragged element
  288. * @return {Boolean} isValid True if the invalid drop should proceed, else false to cancel
  289. * @template
  290. */
  291. beforeInvalidDrop: function(target, e, id) {
  292. return true;
  293. },
  294. // private
  295. handleMouseDown: function(e) {
  296. if (this.dragging) {
  297. return;
  298. }
  299. var data = this.getDragData(e);
  300. if (data &amp;&amp; this.onBeforeDrag(data, e) !== false) {
  301. this.dragData = data;
  302. this.proxy.stop();
  303. this.callParent(arguments);
  304. }
  305. },
  306. <span id='Ext-dd-DragSource-method-onBeforeDrag'> /**
  307. </span> * An empty function by default, but provided so that you can perform a custom action before the initial
  308. * drag event begins and optionally cancel it.
  309. * @param {Object} data An object containing arbitrary data to be shared with drop targets
  310. * @param {Event} e The event object
  311. * @return {Boolean} isValid True if the drag event is valid, else false to cancel
  312. * @template
  313. */
  314. onBeforeDrag: function(data, e){
  315. return true;
  316. },
  317. <span id='Ext-dd-DragSource-method-onStartDrag'> /**
  318. </span> * An empty function by default, but provided so that you can perform a custom action once the initial
  319. * drag event has begun. The drag cannot be canceled from this function.
  320. * @param {Number} x The x position of the click on the dragged object
  321. * @param {Number} y The y position of the click on the dragged object
  322. * @method
  323. * @template
  324. */
  325. onStartDrag: Ext.emptyFn,
  326. alignElWithMouse: function() {
  327. this.proxy.ensureAttachedToBody(true);
  328. return this.callParent(arguments);
  329. },
  330. // private override
  331. startDrag: function(x, y) {
  332. this.proxy.reset();
  333. this.proxy.hidden = false;
  334. this.dragging = true;
  335. this.proxy.update(&quot;&quot;);
  336. this.onInitDrag(x, y);
  337. this.proxy.show();
  338. },
  339. // private
  340. onInitDrag: function(x, y) {
  341. var clone = this.el.dom.cloneNode(true);
  342. clone.id = Ext.id(); // prevent duplicate ids
  343. this.proxy.update(clone);
  344. this.onStartDrag(x, y);
  345. return true;
  346. },
  347. <span id='Ext-dd-DragSource-method-getProxy'> /**
  348. </span> * Returns the drag source's underlying {@link Ext.dd.StatusProxy}
  349. * @return {Ext.dd.StatusProxy} proxy The StatusProxy
  350. */
  351. getProxy: function() {
  352. return this.proxy;
  353. },
  354. <span id='Ext-dd-DragSource-method-hideProxy'> /**
  355. </span> * Hides the drag source's {@link Ext.dd.StatusProxy}
  356. */
  357. hideProxy: function() {
  358. this.proxy.hide();
  359. this.proxy.reset(true);
  360. this.dragging = false;
  361. },
  362. // private
  363. triggerCacheRefresh: function() {
  364. Ext.dd.DDM.refreshCache(this.groups);
  365. },
  366. // private - override to prevent hiding
  367. b4EndDrag: function(e) {
  368. },
  369. // private - override to prevent moving
  370. endDrag : function(e){
  371. this.onEndDrag(this.dragData, e);
  372. },
  373. // private
  374. onEndDrag : function(data, e){
  375. },
  376. // private - pin to cursor
  377. autoOffset : function(x, y) {
  378. this.setDelta(-12, -20);
  379. },
  380. destroy: function(){
  381. this.callParent();
  382. Ext.destroy(this.proxy);
  383. }
  384. });
  385. </pre>
  386. </body>
  387. </html>