ToolbarDroppable.html 6.1 KB

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