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.

206 lines
7.9 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-CycleButton-method-constructor'><span id='Ext-CycleButton'>/**
  19. </span></span> * @class Ext.CycleButton
  20. * @extends Ext.SplitButton
  21. * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
  22. * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
  23. * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
  24. * button displays the dropdown menu just like a normal SplitButton. Example usage:
  25. * &lt;pre&gt;&lt;code&gt;
  26. var btn = new Ext.CycleButton({
  27. showText: true,
  28. prependText: 'View as ',
  29. items: [{
  30. text:'text only',
  31. iconCls:'view-text',
  32. checked:true
  33. },{
  34. text:'HTML',
  35. iconCls:'view-html'
  36. }],
  37. changeHandler:function(btn, item){
  38. Ext.Msg.alert('Change View', item.text);
  39. }
  40. });
  41. &lt;/code&gt;&lt;/pre&gt;
  42. * @constructor
  43. * Create a new split button
  44. * @param {Object} config The config object
  45. * @xtype cycle
  46. */
  47. Ext.CycleButton = Ext.extend(Ext.SplitButton, {
  48. <span id='Ext-CycleButton-cfg-items'> /**
  49. </span> * @cfg {Array} items An array of {@link Ext.menu.CheckItem} &lt;b&gt;config&lt;/b&gt; objects to be used when creating the
  50. * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
  51. */
  52. <span id='Ext-CycleButton-cfg-showText'> /**
  53. </span> * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
  54. */
  55. <span id='Ext-CycleButton-cfg-prependText'> /**
  56. </span> * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
  57. * button's text (only applies when showText = true, defaults to '')
  58. */
  59. <span id='Ext-CycleButton-cfg-changeHandler'> /**
  60. </span> * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
  61. * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
  62. * fire the {@link #change} event on active item change. The changeHandler function will be called with the
  63. * following argument list: (SplitButton this, Ext.menu.CheckItem item)
  64. */
  65. <span id='Ext-CycleButton-cfg-forceIcon'> /**
  66. </span> * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
  67. * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
  68. * default behavior of changing the button's icon to match the selected item's icon on change.
  69. */
  70. <span id='Ext-CycleButton-property-menu'> /**
  71. </span> * @property menu
  72. * @type Menu
  73. * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
  74. */
  75. <span id='Ext-CycleButton-method-getItemText'> // private
  76. </span> getItemText : function(item){
  77. if(item &amp;&amp; this.showText === true){
  78. var text = '';
  79. if(this.prependText){
  80. text += this.prependText;
  81. }
  82. text += item.text;
  83. return text;
  84. }
  85. return undefined;
  86. },
  87. <span id='Ext-CycleButton-method-setActiveItem'> /**
  88. </span> * Sets the button's active menu item.
  89. * @param {Ext.menu.CheckItem} item The item to activate
  90. * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
  91. */
  92. setActiveItem : function(item, suppressEvent){
  93. if(!Ext.isObject(item)){
  94. item = this.menu.getComponent(item);
  95. }
  96. if(item){
  97. if(!this.rendered){
  98. this.text = this.getItemText(item);
  99. this.iconCls = item.iconCls;
  100. }else{
  101. var t = this.getItemText(item);
  102. if(t){
  103. this.setText(t);
  104. }
  105. this.setIconClass(item.iconCls);
  106. }
  107. this.activeItem = item;
  108. if(!item.checked){
  109. item.setChecked(true, suppressEvent);
  110. }
  111. if(this.forceIcon){
  112. this.setIconClass(this.forceIcon);
  113. }
  114. if(!suppressEvent){
  115. this.fireEvent('change', this, item);
  116. }
  117. }
  118. },
  119. <span id='Ext-CycleButton-method-getActiveItem'> /**
  120. </span> * Gets the currently active menu item.
  121. * @return {Ext.menu.CheckItem} The active item
  122. */
  123. getActiveItem : function(){
  124. return this.activeItem;
  125. },
  126. <span id='Ext-CycleButton-method-initComponent'> // private
  127. </span> initComponent : function(){
  128. this.addEvents(
  129. <span id='Ext-CycleButton-event-change'> /**
  130. </span> * @event change
  131. * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
  132. * is set on this CycleButton, it will be called instead on active item change and this change event will
  133. * not be fired.
  134. * @param {Ext.CycleButton} this
  135. * @param {Ext.menu.CheckItem} item The menu item that was selected
  136. */
  137. &quot;change&quot;
  138. );
  139. if(this.changeHandler){
  140. this.on('change', this.changeHandler, this.scope||this);
  141. delete this.changeHandler;
  142. }
  143. this.itemCount = this.items.length;
  144. this.menu = {cls:'x-cycle-menu', items:[]};
  145. var checked = 0;
  146. Ext.each(this.items, function(item, i){
  147. Ext.apply(item, {
  148. group: item.group || this.id,
  149. itemIndex: i,
  150. checkHandler: this.checkHandler,
  151. scope: this,
  152. checked: item.checked || false
  153. });
  154. this.menu.items.push(item);
  155. if(item.checked){
  156. checked = i;
  157. }
  158. }, this);
  159. Ext.CycleButton.superclass.initComponent.call(this);
  160. this.on('click', this.toggleSelected, this);
  161. this.setActiveItem(checked, true);
  162. },
  163. <span id='Ext-CycleButton-method-checkHandler'> // private
  164. </span> checkHandler : function(item, pressed){
  165. if(pressed){
  166. this.setActiveItem(item);
  167. }
  168. },
  169. <span id='Ext-CycleButton-method-toggleSelected'> /**
  170. </span> * This is normally called internally on button click, but can be called externally to advance the button's
  171. * active item programmatically to the next one in the menu. If the current item is the last one in the menu
  172. * the active item will be set to the first item in the menu.
  173. */
  174. toggleSelected : function(){
  175. var m = this.menu;
  176. m.render();
  177. // layout if we haven't before so the items are active
  178. if(!m.hasLayout){
  179. m.doLayout();
  180. }
  181. var nextIdx, checkItem;
  182. for (var i = 1; i &lt; this.itemCount; i++) {
  183. nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
  184. // check the potential item
  185. checkItem = m.items.itemAt(nextIdx);
  186. // if its not disabled then check it.
  187. if (!checkItem.disabled) {
  188. checkItem.setChecked(true);
  189. break;
  190. }
  191. }
  192. }
  193. });
  194. Ext.reg('cycle', Ext.CycleButton);</pre>
  195. </body>
  196. </html>