Draggable.js 6.7 KB

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