ToolbarDroppable.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 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 published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.ToolbarDroppable
  11. * @extends Object
  12. * Plugin which allows items to be dropped onto a toolbar and be turned into new Toolbar items.
  13. * To use the plugin, you just need to provide a createItem implementation that takes the drop
  14. * data as an argument and returns an object that can be placed onto the toolbar. Example:
  15. * <pre>
  16. * Ext.create('Ext.ux.ToolbarDroppable', {
  17. * createItem: function(data) {
  18. * return Ext.create('Ext.Button', {text: data.text});
  19. * }
  20. * });
  21. * </pre>
  22. * The afterLayout function can also be overridden, and is called after a new item has been
  23. * created and inserted into the Toolbar. Use this for any logic that needs to be run after
  24. * the item has been created.
  25. */
  26. Ext.define('Ext.ux.ToolbarDroppable', {
  27. extend: 'Object',
  28. /**
  29. * @constructor
  30. */
  31. constructor: function(config) {
  32. Ext.apply(this, config);
  33. },
  34. /**
  35. * Initializes the plugin and saves a reference to the toolbar
  36. * @param {Ext.toolbar.Toolbar} toolbar The toolbar instance
  37. */
  38. init: function(toolbar) {
  39. /**
  40. * @property toolbar
  41. * @type Ext.toolbar.Toolbar
  42. * The toolbar instance that this plugin is tied to
  43. */
  44. this.toolbar = toolbar;
  45. this.toolbar.on({
  46. scope : this,
  47. render: this.createDropTarget
  48. });
  49. },
  50. /**
  51. * Creates a drop target on the toolbar
  52. */
  53. createDropTarget: function() {
  54. /**
  55. * @property dropTarget
  56. * @type Ext.dd.DropTarget
  57. * The drop target attached to the toolbar instance
  58. */
  59. this.dropTarget = Ext.create('Ext.dd.DropTarget', this.toolbar.getEl(), {
  60. notifyOver: Ext.Function.bind(this.notifyOver, this),
  61. notifyDrop: Ext.Function.bind(this.notifyDrop, this)
  62. });
  63. },
  64. /**
  65. * Adds the given DD Group to the drop target
  66. * @param {String} ddGroup The DD Group
  67. */
  68. addDDGroup: function(ddGroup) {
  69. this.dropTarget.addToGroup(ddGroup);
  70. },
  71. /**
  72. * Calculates the location on the toolbar to create the new sorter button based on the XY of the
  73. * drag event
  74. * @param {Ext.EventObject} e The event object
  75. * @return {Number} The index at which to insert the new button
  76. */
  77. calculateEntryIndex: function(e) {
  78. var entryIndex = 0,
  79. toolbar = this.toolbar,
  80. items = toolbar.items.items,
  81. count = items.length,
  82. xTotal = toolbar.getEl().getXY()[0],
  83. xHover = e.getXY()[0] - xTotal;
  84. for (var index = 0; index < count; index++) {
  85. var item = items[index],
  86. width = item.getEl().getWidth(),
  87. midpoint = xTotal + width / 2;
  88. xTotal += width;
  89. if (xHover < midpoint) {
  90. entryIndex = index;
  91. break;
  92. } else {
  93. entryIndex = index + 1;
  94. }
  95. }
  96. return entryIndex;
  97. },
  98. /**
  99. * Returns true if the drop is allowed on the drop target. This function can be overridden
  100. * and defaults to simply return true
  101. * @param {Object} data Arbitrary data from the drag source
  102. * @return {Boolean} True if the drop is allowed
  103. */
  104. canDrop: function(data) {
  105. return true;
  106. },
  107. /**
  108. * Custom notifyOver method which will be used in the plugin's internal DropTarget
  109. * @return {String} The CSS class to add
  110. */
  111. notifyOver: function(dragSource, event, data) {
  112. return this.canDrop.apply(this, arguments) ? this.dropTarget.dropAllowed : this.dropTarget.dropNotAllowed;
  113. },
  114. /**
  115. * Called when the drop has been made. Creates the new toolbar item, places it at the correct location
  116. * and calls the afterLayout callback.
  117. */
  118. notifyDrop: function(dragSource, event, data) {
  119. var canAdd = this.canDrop(dragSource, event, data),
  120. tbar = this.toolbar;
  121. if (canAdd) {
  122. var entryIndex = this.calculateEntryIndex(event);
  123. tbar.insert(entryIndex, this.createItem(data));
  124. tbar.doLayout();
  125. this.afterLayout();
  126. }
  127. return canAdd;
  128. },
  129. /**
  130. * Creates the new toolbar item based on drop data. This method must be implemented by the plugin instance
  131. * @param {Object} data Arbitrary data from the drop
  132. * @return {Mixed} An item that can be added to a toolbar
  133. */
  134. createItem: function(data) {
  135. //<debug>
  136. Ext.Error.raise("The createItem method must be implemented in the ToolbarDroppable plugin");
  137. //</debug>
  138. },
  139. /**
  140. * Called after a new button has been created and added to the toolbar. Add any required cleanup logic here
  141. */
  142. afterLayout: Ext.emptyFn
  143. });