Cycle.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-button-Cycle'>/**
  19. </span> * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
  20. * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
  21. * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
  22. * button displays the dropdown menu just like a normal SplitButton. Example usage:
  23. *
  24. * @example
  25. * Ext.create('Ext.button.Cycle', {
  26. * showText: true,
  27. * prependText: 'View as ',
  28. * renderTo: Ext.getBody(),
  29. * menu: {
  30. * id: 'view-type-menu',
  31. * items: [{
  32. * text: 'text only',
  33. * iconCls: 'view-text',
  34. * checked: true
  35. * },{
  36. * text: 'HTML',
  37. * iconCls: 'view-html'
  38. * }]
  39. * },
  40. * changeHandler: function(cycleBtn, activeItem) {
  41. * Ext.Msg.alert('Change View', activeItem.text);
  42. * }
  43. * });
  44. */
  45. Ext.define('Ext.button.Cycle', {
  46. /* Begin Definitions */
  47. alias: 'widget.cycle',
  48. extend: 'Ext.button.Split',
  49. alternateClassName: 'Ext.CycleButton',
  50. /* End Definitions */
  51. <span id='Ext-button-Cycle-cfg-items'> /**
  52. </span> * @cfg {Object[]} items
  53. * An array of {@link Ext.menu.CheckItem} **config** objects to be used when creating the button's menu items (e.g.,
  54. * `{text:'Foo', iconCls:'foo-icon'}`)
  55. *
  56. * @deprecated 4.0 Use the {@link #cfg-menu} config instead. All menu items will be created as
  57. * {@link Ext.menu.CheckItem CheckItems}.
  58. */
  59. <span id='Ext-button-Cycle-cfg-showText'> /**
  60. </span> * @cfg {Boolean} [showText=false]
  61. * True to display the active item's text as the button text. The Button will show its
  62. * configured {@link #text} if this config is omitted.
  63. */
  64. <span id='Ext-button-Cycle-cfg-prependText'> /**
  65. </span> * @cfg {String} [prependText='']
  66. * A static string to prepend before the active item's text when displayed as the button's text (only applies when
  67. * showText = true).
  68. */
  69. <span id='Ext-button-Cycle-cfg-changeHandler'> /**
  70. </span> * @cfg {Function} changeHandler
  71. * A callback function that will be invoked each time the active menu item in the button's menu has changed. If this
  72. * callback is not supplied, the SplitButton will instead fire the {@link #change} event on active item change. The
  73. * changeHandler function will be called with the following argument list: (SplitButton this, Ext.menu.CheckItem
  74. * item)
  75. */
  76. <span id='Ext-button-Cycle-cfg-forceIcon'> /**
  77. </span> * @cfg {String} forceIcon
  78. * A css class which sets an image to be used as the static icon for this button. This icon will always be displayed
  79. * regardless of which item is selected in the dropdown list. This overrides the default behavior of changing the
  80. * button's icon to match the selected item's icon on change.
  81. */
  82. <span id='Ext-button-Cycle-property-menu'> /**
  83. </span> * @property {Ext.menu.Menu} menu
  84. * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the
  85. * available choices.
  86. */
  87. // private
  88. getButtonText: function(item) {
  89. var me = this,
  90. text = '';
  91. if (item &amp;&amp; me.showText === true) {
  92. if (me.prependText) {
  93. text += me.prependText;
  94. }
  95. text += item.text;
  96. return text;
  97. }
  98. return me.text;
  99. },
  100. <span id='Ext-button-Cycle-method-setActiveItem'> /**
  101. </span> * Sets the button's active menu item.
  102. * @param {Ext.menu.CheckItem} item The item to activate
  103. * @param {Boolean} [suppressEvent=false] True to prevent the button's change event from firing.
  104. */
  105. setActiveItem: function(item, suppressEvent) {
  106. var me = this;
  107. if (!Ext.isObject(item)) {
  108. item = me.menu.getComponent(item);
  109. }
  110. if (item) {
  111. if (!me.rendered) {
  112. me.text = me.getButtonText(item);
  113. me.iconCls = item.iconCls;
  114. } else {
  115. me.setText(me.getButtonText(item));
  116. me.setIconCls(item.iconCls);
  117. }
  118. me.activeItem = item;
  119. if (!item.checked) {
  120. item.setChecked(true, false);
  121. }
  122. if (me.forceIcon) {
  123. me.setIconCls(me.forceIcon);
  124. }
  125. if (!suppressEvent) {
  126. me.fireEvent('change', me, item);
  127. }
  128. }
  129. },
  130. <span id='Ext-button-Cycle-method-getActiveItem'> /**
  131. </span> * Gets the currently active menu item.
  132. * @return {Ext.menu.CheckItem} The active item
  133. */
  134. getActiveItem: function() {
  135. return this.activeItem;
  136. },
  137. // private
  138. initComponent: function() {
  139. var me = this,
  140. checked = 0,
  141. items,
  142. i, iLen, item;
  143. me.addEvents(
  144. <span id='Ext-button-Cycle-event-change'> /**
  145. </span> * @event change
  146. * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function is
  147. * set on this CycleButton, it will be called instead on active item change and this change event will not
  148. * be fired.
  149. * @param {Ext.button.Cycle} this
  150. * @param {Ext.menu.CheckItem} item The menu item that was selected
  151. */
  152. &quot;change&quot;
  153. );
  154. if (me.changeHandler) {
  155. me.on('change', me.changeHandler, me.scope || me);
  156. delete me.changeHandler;
  157. }
  158. // Allow them to specify a menu config which is a standard Button config.
  159. // Remove direct use of &quot;items&quot; in 5.0.
  160. items = (me.menu.items || []).concat(me.items || []);
  161. me.menu = Ext.applyIf({
  162. cls: Ext.baseCSSPrefix + 'cycle-menu',
  163. items: []
  164. }, me.menu);
  165. iLen = items.length;
  166. // Convert all items to CheckItems
  167. for (i = 0; i &lt; iLen; i++) {
  168. item = items[i];
  169. item = Ext.applyIf({
  170. group : me.id,
  171. itemIndex : i,
  172. checkHandler : me.checkHandler,
  173. scope : me,
  174. checked : item.checked || false
  175. }, item);
  176. me.menu.items.push(item);
  177. if (item.checked) {
  178. checked = i;
  179. }
  180. }
  181. me.itemCount = me.menu.items.length;
  182. me.callParent(arguments);
  183. me.on('click', me.toggleSelected, me);
  184. me.setActiveItem(checked, me);
  185. // If configured with a fixed width, the cycling will center a different child item's text each click. Prevent this.
  186. if (me.width &amp;&amp; me.showText) {
  187. me.addCls(Ext.baseCSSPrefix + 'cycle-fixed-width');
  188. }
  189. },
  190. // private
  191. checkHandler: function(item, pressed) {
  192. if (pressed) {
  193. this.setActiveItem(item);
  194. }
  195. },
  196. <span id='Ext-button-Cycle-method-toggleSelected'> /**
  197. </span> * This is normally called internally on button click, but can be called externally to advance the button's active
  198. * item programmatically to the next one in the menu. If the current item is the last one in the menu the active
  199. * item will be set to the first item in the menu.
  200. */
  201. toggleSelected: function() {
  202. var me = this,
  203. m = me.menu,
  204. checkItem;
  205. checkItem = me.activeItem.next(':not([disabled])') || m.items.getAt(0);
  206. checkItem.setChecked(true);
  207. }
  208. });</pre>
  209. </body>
  210. </html>