You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.4 KiB

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