Draggable.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-ux-DataView-Draggable'>/**
  19. </span> * @class Ext.ux.DataView.Draggable
  20. * @extends Object
  21. * @author Ed Spencer
  22. *
  23. &lt;pre&gt;&lt;code&gt;
  24. Ext.create('Ext.view.View', {
  25. mixins: {
  26. draggable: 'Ext.ux.DataView.Draggable'
  27. },
  28. initComponent: function() {
  29. this.mixins.draggable.init(this, {
  30. ddConfig: {
  31. ddGroup: 'someGroup'
  32. }
  33. });
  34. this.callParent(arguments);
  35. }
  36. });
  37. &lt;/code&gt;&lt;/pre&gt;
  38. *
  39. */
  40. Ext.define('Ext.ux.DataView.Draggable', {
  41. requires: 'Ext.dd.DragZone',
  42. <span id='Ext-ux-DataView-Draggable-cfg-ghostCls'> /**
  43. </span> * @cfg {String} ghostCls The CSS class added to the outermost element of the created ghost proxy
  44. * (defaults to 'x-dataview-draggable-ghost')
  45. */
  46. ghostCls: 'x-dataview-draggable-ghost',
  47. <span id='Ext-ux-DataView-Draggable-cfg-ghostTpl'> /**
  48. </span> * @cfg {Ext.XTemplate/Array} ghostTpl The template used in the ghost DataView
  49. */
  50. ghostTpl: [
  51. '&lt;tpl for=&quot;.&quot;&gt;',
  52. '{title}',
  53. '&lt;/tpl&gt;'
  54. ],
  55. <span id='Ext-ux-DataView-Draggable-cfg-ddConfig'> /**
  56. </span> * @cfg {Object} ddConfig Config object that is applied to the internally created DragZone
  57. */
  58. <span id='Ext-ux-DataView-Draggable-cfg-ghostConfig'> /**
  59. </span> * @cfg {String} ghostConfig Config object that is used to configure the internally created DataView
  60. */
  61. init: function(dataview, config) {
  62. <span id='Ext-ux-DataView-Draggable-property-dataview'> /**
  63. </span> * @property dataview
  64. * @type Ext.view.View
  65. * The Ext.view.View instance that this DragZone is attached to
  66. */
  67. this.dataview = dataview;
  68. dataview.on('render', this.onRender, this);
  69. Ext.apply(this, {
  70. itemSelector: dataview.itemSelector,
  71. ghostConfig : {}
  72. }, config || {});
  73. Ext.applyIf(this.ghostConfig, {
  74. itemSelector: 'img',
  75. cls: this.ghostCls,
  76. tpl: this.ghostTpl
  77. });
  78. },
  79. <span id='Ext-ux-DataView-Draggable-method-onRender'> /**
  80. </span> * @private
  81. * Called when the attached DataView is rendered. Sets up the internal DragZone
  82. */
  83. onRender: function() {
  84. var config = Ext.apply({}, this.ddConfig || {}, {
  85. dvDraggable: this,
  86. dataview : this.dataview,
  87. getDragData: this.getDragData,
  88. getTreeNode: this.getTreeNode,
  89. afterRepair: this.afterRepair,
  90. getRepairXY: this.getRepairXY
  91. });
  92. <span id='Ext-ux-DataView-Draggable-property-dragZone'> /**
  93. </span> * @property dragZone
  94. * @type Ext.dd.DragZone
  95. * The attached DragZone instane
  96. */
  97. this.dragZone = Ext.create('Ext.dd.DragZone', this.dataview.getEl(), config);
  98. },
  99. getDragData: function(e) {
  100. var draggable = this.dvDraggable,
  101. dataview = this.dataview,
  102. selModel = dataview.getSelectionModel(),
  103. target = e.getTarget(draggable.itemSelector),
  104. selected, dragData;
  105. if (target) {
  106. if (!dataview.isSelected(target)) {
  107. selModel.select(dataview.getRecord(target));
  108. }
  109. selected = dataview.getSelectedNodes();
  110. dragData = {
  111. copy: true,
  112. nodes: selected,
  113. records: selModel.getSelection(),
  114. item: true
  115. };
  116. if (selected.length == 1) {
  117. dragData.single = true;
  118. dragData.ddel = target;
  119. } else {
  120. dragData.multi = true;
  121. dragData.ddel = draggable.prepareGhost(selModel.getSelection()).dom;
  122. }
  123. return dragData;
  124. }
  125. return false;
  126. },
  127. getTreeNode: function() {
  128. // console.log('test');
  129. },
  130. afterRepair: function() {
  131. this.dragging = false;
  132. var nodes = this.dragData.nodes,
  133. length = nodes.length,
  134. i;
  135. //FIXME: Ext.fly does not work here for some reason, only frames the last node
  136. for (i = 0; i &lt; length; i++) {
  137. Ext.get(nodes[i]).frame('#8db2e3', 1);
  138. }
  139. },
  140. <span id='Ext-ux-DataView-Draggable-method-getRepairXY'> /**
  141. </span> * @private
  142. * Returns the x and y co-ordinates that the dragged item should be animated back to if it was dropped on an
  143. * invalid drop target. If we're dragging more than one item we don't animate back and just allow afterRepair
  144. * to frame each dropped item.
  145. */
  146. getRepairXY: function(e) {
  147. if (this.dragData.multi) {
  148. return false;
  149. } else {
  150. var repairEl = Ext.get(this.dragData.ddel),
  151. repairXY = repairEl.getXY();
  152. //take the item's margins and padding into account to make the repair animation line up perfectly
  153. repairXY[0] += repairEl.getPadding('t') + repairEl.getMargin('t');
  154. repairXY[1] += repairEl.getPadding('l') + repairEl.getMargin('l');
  155. return repairXY;
  156. }
  157. },
  158. <span id='Ext-ux-DataView-Draggable-method-prepareGhost'> /**
  159. </span> * Updates the internal ghost DataView by ensuring it is rendered and contains the correct records
  160. * @param {Array} records The set of records that is currently selected in the parent DataView
  161. * @return {Ext.view.View} The Ghost DataView
  162. */
  163. prepareGhost: function(records) {
  164. var ghost = this.createGhost(records),
  165. store = ghost.store;
  166. store.removeAll();
  167. store.add(records);
  168. return ghost.getEl();
  169. },
  170. <span id='Ext-ux-DataView-Draggable-method-createGhost'> /**
  171. </span> * @private
  172. * Creates the 'ghost' DataView that follows the mouse cursor during the drag operation. This div is usually a
  173. * lighter-weight representation of just the nodes that are selected in the parent DataView.
  174. */
  175. createGhost: function(records) {
  176. if (!this.ghost) {
  177. var ghostConfig = Ext.apply({}, this.ghostConfig, {
  178. store: Ext.create('Ext.data.Store', {
  179. model: records[0].modelName
  180. })
  181. });
  182. this.ghost = Ext.create('Ext.view.View', ghostConfig);
  183. this.ghost.render(document.createElement('div'));
  184. }
  185. return this.ghost;
  186. }
  187. });
  188. </pre>
  189. </body>
  190. </html>