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.

158 lines
4.9 KiB

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