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.

1168 lines
38 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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.Button
  17. * @extends Ext.BoxComponent
  18. * Simple Button class
  19. * @cfg {String} text The button text to be used as innerHTML (html tags are accepted)
  20. * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
  21. * CSS property of the button by default, so if you want a mixed icon/text button, set cls:'x-btn-text-icon')
  22. * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event).
  23. * The handler is passed the following parameters:<div class="mdetail-params"><ul>
  24. * <li><code>b</code> : Button<div class="sub-desc">This Button.</div></li>
  25. * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
  26. * </ul></div>
  27. * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width).
  28. * See also {@link Ext.Panel}.<tt>{@link Ext.Panel#minButtonWidth minButtonWidth}</tt>.
  29. * @cfg {String/Object} tooltip The tooltip for the button - can be a string to be used as innerHTML (html tags are accepted) or QuickTips config object
  30. * @cfg {Boolean} hidden True to start hidden (defaults to false)
  31. * @cfg {Boolean} disabled True to start disabled (defaults to false)
  32. * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
  33. * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed)
  34. * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
  35. * a {@link Ext.util.ClickRepeater ClickRepeater} config object (defaults to false).
  36. * @constructor
  37. * Create a new button
  38. * @param {Object} config The config object
  39. * @xtype button
  40. */
  41. Ext.Button = Ext.extend(Ext.BoxComponent, {
  42. /**
  43. * Read-only. True if this button is hidden
  44. * @type Boolean
  45. */
  46. hidden : false,
  47. /**
  48. * Read-only. True if this button is disabled
  49. * @type Boolean
  50. */
  51. disabled : false,
  52. /**
  53. * Read-only. True if this button is pressed (only if enableToggle = true)
  54. * @type Boolean
  55. */
  56. pressed : false,
  57. /**
  58. * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
  59. */
  60. /**
  61. * @cfg {Boolean} allowDepress
  62. * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
  63. */
  64. /**
  65. * @cfg {Boolean} enableToggle
  66. * True to enable pressed/not pressed toggling (defaults to false)
  67. */
  68. enableToggle : false,
  69. /**
  70. * @cfg {Function} toggleHandler
  71. * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
  72. * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
  73. * <li><b>state</b> : Boolean<div class="sub-desc">The next state of the Button, true means pressed.</div></li>
  74. * </ul>
  75. */
  76. /**
  77. * @cfg {Mixed} menu
  78. * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
  79. */
  80. /**
  81. * @cfg {String} menuAlign
  82. * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
  83. */
  84. menuAlign : 'tl-bl?',
  85. /**
  86. * @cfg {String} overflowText If used in a {@link Ext.Toolbar Toolbar}, the
  87. * text to be used if this item is shown in the overflow menu. See also
  88. * {@link Ext.Toolbar.Item}.<code>{@link Ext.Toolbar.Item#overflowText overflowText}</code>.
  89. */
  90. /**
  91. * @cfg {String} iconCls
  92. * A css class which sets a background image to be used as the icon for this button
  93. */
  94. /**
  95. * @cfg {String} type
  96. * submit, reset or button - defaults to 'button'
  97. */
  98. type : 'button',
  99. // private
  100. menuClassTarget : 'tr:nth(2)',
  101. /**
  102. * @cfg {String} clickEvent
  103. * The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
  104. * Defaults to <tt>'click'</tt>.
  105. */
  106. clickEvent : 'click',
  107. /**
  108. * @cfg {Boolean} handleMouseEvents
  109. * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
  110. */
  111. handleMouseEvents : true,
  112. /**
  113. * @cfg {String} tooltipType
  114. * The type of tooltip to use. Either 'qtip' (default) for QuickTips or 'title' for title attribute.
  115. */
  116. tooltipType : 'qtip',
  117. /**
  118. * @cfg {String} buttonSelector
  119. * <p>(Optional) A {@link Ext.DomQuery DomQuery} selector which is used to extract the active, clickable element from the
  120. * DOM structure created.</p>
  121. * <p>When a custom {@link #template} is used, you must ensure that this selector results in the selection of
  122. * a focussable element.</p>
  123. * <p>Defaults to <b><tt>'button:first-child'</tt></b>.</p>
  124. */
  125. buttonSelector : 'button:first-child',
  126. /**
  127. * @cfg {String} scale
  128. * <p>(Optional) The size of the Button. Three values are allowed:</p>
  129. * <ul class="mdetail-params">
  130. * <li>'small'<div class="sub-desc">Results in the button element being 16px high.</div></li>
  131. * <li>'medium'<div class="sub-desc">Results in the button element being 24px high.</div></li>
  132. * <li>'large'<div class="sub-desc">Results in the button element being 32px high.</div></li>
  133. * </ul>
  134. * <p>Defaults to <b><tt>'small'</tt></b>.</p>
  135. */
  136. scale : 'small',
  137. /**
  138. * @cfg {Object} scope The scope (<tt><b>this</b></tt> reference) in which the
  139. * <code>{@link #handler}</code> and <code>{@link #toggleHandler}</code> is
  140. * executed. Defaults to this Button.
  141. */
  142. /**
  143. * @cfg {String} iconAlign
  144. * <p>(Optional) The side of the Button box to render the icon. Four values are allowed:</p>
  145. * <ul class="mdetail-params">
  146. * <li>'top'<div class="sub-desc"></div></li>
  147. * <li>'right'<div class="sub-desc"></div></li>
  148. * <li>'bottom'<div class="sub-desc"></div></li>
  149. * <li>'left'<div class="sub-desc"></div></li>
  150. * </ul>
  151. * <p>Defaults to <b><tt>'left'</tt></b>.</p>
  152. */
  153. iconAlign : 'left',
  154. /**
  155. * @cfg {String} arrowAlign
  156. * <p>(Optional) The side of the Button box to render the arrow if the button has an associated {@link #menu}.
  157. * Two values are allowed:</p>
  158. * <ul class="mdetail-params">
  159. * <li>'right'<div class="sub-desc"></div></li>
  160. * <li>'bottom'<div class="sub-desc"></div></li>
  161. * </ul>
  162. * <p>Defaults to <b><tt>'right'</tt></b>.</p>
  163. */
  164. arrowAlign : 'right',
  165. /**
  166. * @cfg {Ext.Template} template (Optional)
  167. * <p>A {@link Ext.Template Template} used to create the Button's DOM structure.</p>
  168. * Instances, or subclasses which need a different DOM structure may provide a different
  169. * template layout in conjunction with an implementation of {@link #getTemplateArgs}.
  170. * @type Ext.Template
  171. * @property template
  172. */
  173. /**
  174. * @cfg {String} cls
  175. * A CSS class string to apply to the button's main element.
  176. */
  177. /**
  178. * @property menu
  179. * @type Menu
  180. * The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #menu} config option.
  181. */
  182. /**
  183. * @cfg {Boolean} autoWidth
  184. * By default, if a width is not specified the button will attempt to stretch horizontally to fit its content.
  185. * If the button is being managed by a width sizing layout (hbox, fit, anchor), set this to false to prevent
  186. * the button from doing this automatic sizing.
  187. * Defaults to <tt>undefined</tt>.
  188. */
  189. initComponent : function(){
  190. if(this.menu){
  191. // If array of items, turn it into an object config so we
  192. // can set the ownerCt property in the config
  193. if (Ext.isArray(this.menu)){
  194. this.menu = { items: this.menu };
  195. }
  196. // An object config will work here, but an instance of a menu
  197. // will have already setup its ref's and have no effect
  198. if (Ext.isObject(this.menu)){
  199. this.menu.ownerCt = this;
  200. }
  201. this.menu = Ext.menu.MenuMgr.get(this.menu);
  202. this.menu.ownerCt = undefined;
  203. }
  204. Ext.Button.superclass.initComponent.call(this);
  205. this.addEvents(
  206. /**
  207. * @event click
  208. * Fires when this button is clicked
  209. * @param {Button} this
  210. * @param {EventObject} e The click event
  211. */
  212. 'click',
  213. /**
  214. * @event toggle
  215. * Fires when the 'pressed' state of this button changes (only if enableToggle = true)
  216. * @param {Button} this
  217. * @param {Boolean} pressed
  218. */
  219. 'toggle',
  220. /**
  221. * @event mouseover
  222. * Fires when the mouse hovers over the button
  223. * @param {Button} this
  224. * @param {Event} e The event object
  225. */
  226. 'mouseover',
  227. /**
  228. * @event mouseout
  229. * Fires when the mouse exits the button
  230. * @param {Button} this
  231. * @param {Event} e The event object
  232. */
  233. 'mouseout',
  234. /**
  235. * @event menushow
  236. * If this button has a menu, this event fires when it is shown
  237. * @param {Button} this
  238. * @param {Menu} menu
  239. */
  240. 'menushow',
  241. /**
  242. * @event menuhide
  243. * If this button has a menu, this event fires when it is hidden
  244. * @param {Button} this
  245. * @param {Menu} menu
  246. */
  247. 'menuhide',
  248. /**
  249. * @event menutriggerover
  250. * If this button has a menu, this event fires when the mouse enters the menu triggering element
  251. * @param {Button} this
  252. * @param {Menu} menu
  253. * @param {EventObject} e
  254. */
  255. 'menutriggerover',
  256. /**
  257. * @event menutriggerout
  258. * If this button has a menu, this event fires when the mouse leaves the menu triggering element
  259. * @param {Button} this
  260. * @param {Menu} menu
  261. * @param {EventObject} e
  262. */
  263. 'menutriggerout'
  264. );
  265. if(Ext.isString(this.toggleGroup)){
  266. this.enableToggle = true;
  267. }
  268. },
  269. /**
  270. * <p>This method returns an Array which provides substitution parameters for the {@link #template Template} used
  271. * to create this Button's DOM structure.</p>
  272. * <p>Instances or subclasses which use a different Template to create a different DOM structure may need to provide their
  273. * own implementation of this method.</p>
  274. * <p>The default implementation which provides data for the default {@link #template} returns an Array containing the
  275. * following items:</p><div class="mdetail-params"><ul>
  276. * <li>The &lt;button&gt;'s {@link #type}</li>
  277. * <li>A CSS class name applied to the Button's main &lt;tbody&gt; element which determines the button's scale and icon alignment.</li>
  278. * <li>A CSS class to determine the presence and position of an arrow icon. (<code>'x-btn-arrow'</code> or <code>'x-btn-arrow-bottom'</code> or <code>''</code>)</li>
  279. * <li>The {@link #cls} CSS class name applied to the button's wrapping &lt;table&gt; element.</li>
  280. * <li>The Component id which is applied to the button's wrapping &lt;table&gt; element.</li>
  281. * </ul></div>
  282. * @return {Array} Substitution data for a Template.
  283. */
  284. getTemplateArgs : function(){
  285. return [this.type, 'x-btn-' + this.scale + ' x-btn-icon-' + this.scale + '-' + this.iconAlign, this.getMenuClass(), this.cls, this.id];
  286. },
  287. // private
  288. setButtonClass : function(){
  289. if(this.useSetClass){
  290. if(!Ext.isEmpty(this.oldCls)){
  291. this.el.removeClass([this.oldCls, 'x-btn-pressed']);
  292. }
  293. this.oldCls = (this.iconCls || this.icon) ? (this.text ? 'x-btn-text-icon' : 'x-btn-icon') : 'x-btn-noicon';
  294. this.el.addClass([this.oldCls, this.pressed ? 'x-btn-pressed' : null]);
  295. }
  296. },
  297. // protected
  298. getMenuClass : function(){
  299. return this.menu ? (this.arrowAlign != 'bottom' ? 'x-btn-arrow' : 'x-btn-arrow-bottom') : '';
  300. },
  301. // private
  302. onRender : function(ct, position){
  303. if(!this.template){
  304. if(!Ext.Button.buttonTemplate){
  305. // hideous table template
  306. Ext.Button.buttonTemplate = new Ext.Template(
  307. '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
  308. '<tr><td class="x-btn-tl"><i>&#160;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&#160;</i></td></tr>',
  309. '<tr><td class="x-btn-ml"><i>&#160;</i></td><td class="x-btn-mc"><em class="{2} x-unselectable" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i>&#160;</i></td></tr>',
  310. '<tr><td class="x-btn-bl"><i>&#160;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&#160;</i></td></tr>',
  311. '</tbody></table>');
  312. Ext.Button.buttonTemplate.compile();
  313. }
  314. this.template = Ext.Button.buttonTemplate;
  315. }
  316. var btn, targs = this.getTemplateArgs();
  317. if(position){
  318. btn = this.template.insertBefore(position, targs, true);
  319. }else{
  320. btn = this.template.append(ct, targs, true);
  321. }
  322. /**
  323. * An {@link Ext.Element Element} encapsulating the Button's clickable element. By default,
  324. * this references a <tt>&lt;button&gt;</tt> element. Read only.
  325. * @type Ext.Element
  326. * @property btnEl
  327. */
  328. this.btnEl = btn.child(this.buttonSelector);
  329. this.mon(this.btnEl, {
  330. scope: this,
  331. focus: this.onFocus,
  332. blur: this.onBlur
  333. });
  334. this.initButtonEl(btn, this.btnEl);
  335. Ext.ButtonToggleMgr.register(this);
  336. },
  337. // private
  338. initButtonEl : function(btn, btnEl){
  339. this.el = btn;
  340. this.setIcon(this.icon);
  341. this.setText(this.text);
  342. this.setIconClass(this.iconCls);
  343. if(Ext.isDefined(this.tabIndex)){
  344. btnEl.dom.tabIndex = this.tabIndex;
  345. }
  346. if(this.tooltip){
  347. this.setTooltip(this.tooltip, true);
  348. }
  349. if(this.handleMouseEvents){
  350. this.mon(btn, {
  351. scope: this,
  352. mouseover: this.onMouseOver,
  353. mousedown: this.onMouseDown
  354. });
  355. // new functionality for monitoring on the document level
  356. //this.mon(btn, 'mouseout', this.onMouseOut, this);
  357. }
  358. if(this.menu){
  359. this.mon(this.menu, {
  360. scope: this,
  361. show: this.onMenuShow,
  362. hide: this.onMenuHide
  363. });
  364. }
  365. if(this.repeat){
  366. var repeater = new Ext.util.ClickRepeater(btn, Ext.isObject(this.repeat) ? this.repeat : {});
  367. this.mon(repeater, 'click', this.onRepeatClick, this);
  368. }else{
  369. this.mon(btn, this.clickEvent, this.onClick, this);
  370. }
  371. },
  372. // private
  373. afterRender : function(){
  374. Ext.Button.superclass.afterRender.call(this);
  375. this.useSetClass = true;
  376. this.setButtonClass();
  377. this.doc = Ext.getDoc();
  378. this.doAutoWidth();
  379. },
  380. /**
  381. * Sets the CSS class that provides a background image to use as the button's icon. This method also changes
  382. * the value of the {@link iconCls} config internally.
  383. * @param {String} cls The CSS class providing the icon image
  384. * @return {Ext.Button} this
  385. */
  386. setIconClass : function(cls){
  387. this.iconCls = cls;
  388. if(this.el){
  389. this.btnEl.dom.className = '';
  390. this.btnEl.addClass(['x-btn-text', cls || '']);
  391. this.setButtonClass();
  392. }
  393. return this;
  394. },
  395. /**
  396. * Sets the tooltip for this Button.
  397. * @param {String/Object} tooltip. This may be:<div class="mdesc-details"><ul>
  398. * <li><b>String</b> : A string to be used as innerHTML (html tags are accepted) to show in a tooltip</li>
  399. * <li><b>Object</b> : A configuration object for {@link Ext.QuickTips#register}.</li>
  400. * </ul></div>
  401. * @return {Ext.Button} this
  402. */
  403. setTooltip : function(tooltip, /* private */ initial){
  404. if(this.rendered){
  405. if(!initial){
  406. this.clearTip();
  407. }
  408. if(Ext.isObject(tooltip)){
  409. Ext.QuickTips.register(Ext.apply({
  410. target: this.btnEl.id
  411. }, tooltip));
  412. this.tooltip = tooltip;
  413. }else{
  414. this.btnEl.dom[this.tooltipType] = tooltip;
  415. }
  416. }else{
  417. this.tooltip = tooltip;
  418. }
  419. return this;
  420. },
  421. // private
  422. clearTip : function(){
  423. if(Ext.isObject(this.tooltip)){
  424. Ext.QuickTips.unregister(this.btnEl);
  425. }
  426. },
  427. // private
  428. beforeDestroy : function(){
  429. if(this.rendered){
  430. this.clearTip();
  431. }
  432. if(this.menu && this.destroyMenu !== false) {
  433. Ext.destroy(this.btnEl, this.menu);
  434. }
  435. Ext.destroy(this.repeater);
  436. },
  437. // private
  438. onDestroy : function(){
  439. if(this.rendered){
  440. this.doc.un('mouseover', this.monitorMouseOver, this);
  441. this.doc.un('mouseup', this.onMouseUp, this);
  442. delete this.doc;
  443. delete this.btnEl;
  444. Ext.ButtonToggleMgr.unregister(this);
  445. }
  446. Ext.Button.superclass.onDestroy.call(this);
  447. },
  448. // private
  449. doAutoWidth : function(){
  450. if(this.autoWidth !== false && this.el && this.text && this.width === undefined){
  451. this.el.setWidth('auto');
  452. if(Ext.isIE7 && Ext.isStrict){
  453. var ib = this.btnEl;
  454. if(ib && ib.getWidth() > 20){
  455. ib.clip();
  456. ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
  457. }
  458. }
  459. if(this.minWidth){
  460. if(this.el.getWidth() < this.minWidth){
  461. this.el.setWidth(this.minWidth);
  462. }
  463. }
  464. }
  465. },
  466. /**
  467. * Assigns this Button's click handler
  468. * @param {Function} handler The function to call when the button is clicked
  469. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
  470. * Defaults to this Button.
  471. * @return {Ext.Button} this
  472. */
  473. setHandler : function(handler, scope){
  474. this.handler = handler;
  475. this.scope = scope;
  476. return this;
  477. },
  478. /**
  479. * Sets this Button's text
  480. * @param {String} text The button text
  481. * @return {Ext.Button} this
  482. */
  483. setText : function(text){
  484. this.text = text;
  485. if(this.el){
  486. this.btnEl.update(text || '&#160;');
  487. this.setButtonClass();
  488. }
  489. this.doAutoWidth();
  490. return this;
  491. },
  492. /**
  493. * Sets the background image (inline style) of the button. This method also changes
  494. * the value of the {@link icon} config internally.
  495. * @param {String} icon The path to an image to display in the button
  496. * @return {Ext.Button} this
  497. */
  498. setIcon : function(icon){
  499. this.icon = icon;
  500. if(this.el){
  501. this.btnEl.setStyle('background-image', icon ? 'url(' + icon + ')' : '');
  502. this.setButtonClass();
  503. }
  504. return this;
  505. },
  506. /**
  507. * Gets the text for this Button
  508. * @return {String} The button text
  509. */
  510. getText : function(){
  511. return this.text;
  512. },
  513. /**
  514. * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
  515. * @param {Boolean} state (optional) Force a particular state
  516. * @param {Boolean} supressEvent (optional) True to stop events being fired when calling this method.
  517. * @return {Ext.Button} this
  518. */
  519. toggle : function(state, suppressEvent){
  520. state = state === undefined ? !this.pressed : !!state;
  521. if(state != this.pressed){
  522. if(this.rendered){
  523. this.el[state ? 'addClass' : 'removeClass']('x-btn-pressed');
  524. }
  525. this.pressed = state;
  526. if(!suppressEvent){
  527. this.fireEvent('toggle', this, state);
  528. if(this.toggleHandler){
  529. this.toggleHandler.call(this.scope || this, this, state);
  530. }
  531. }
  532. }
  533. return this;
  534. },
  535. // private
  536. onDisable : function(){
  537. this.onDisableChange(true);
  538. },
  539. // private
  540. onEnable : function(){
  541. this.onDisableChange(false);
  542. },
  543. onDisableChange : function(disabled){
  544. if(this.el){
  545. if(!Ext.isIE6 || !this.text){
  546. this.el[disabled ? 'addClass' : 'removeClass'](this.disabledClass);
  547. }
  548. this.el.dom.disabled = disabled;
  549. }
  550. this.disabled = disabled;
  551. },
  552. /**
  553. * Show this button's menu (if it has one)
  554. */
  555. showMenu : function(){
  556. if(this.rendered && this.menu){
  557. if(this.tooltip){
  558. Ext.QuickTips.getQuickTip().cancelShow(this.btnEl);
  559. }
  560. if(this.menu.isVisible()){
  561. this.menu.hide();
  562. }
  563. this.menu.ownerCt = this;
  564. this.menu.show(this.el, this.menuAlign);
  565. }
  566. return this;
  567. },
  568. /**
  569. * Hide this button's menu (if it has one)
  570. */
  571. hideMenu : function(){
  572. if(this.hasVisibleMenu()){
  573. this.menu.hide();
  574. }
  575. return this;
  576. },
  577. /**
  578. * Returns true if the button has a menu and it is visible
  579. * @return {Boolean}
  580. */
  581. hasVisibleMenu : function(){
  582. return this.menu && this.menu.ownerCt == this && this.menu.isVisible();
  583. },
  584. // private
  585. onRepeatClick : function(repeat, e){
  586. this.onClick(e);
  587. },
  588. // private
  589. onClick : function(e){
  590. if(e){
  591. e.preventDefault();
  592. }
  593. if(e.button !== 0){
  594. return;
  595. }
  596. if(!this.disabled){
  597. this.doToggle();
  598. if(this.menu && !this.hasVisibleMenu() && !this.ignoreNextClick){
  599. this.showMenu();
  600. }
  601. this.fireEvent('click', this, e);
  602. if(this.handler){
  603. //this.el.removeClass('x-btn-over');
  604. this.handler.call(this.scope || this, this, e);
  605. }
  606. }
  607. },
  608. // private
  609. doToggle: function(){
  610. if (this.enableToggle && (this.allowDepress !== false || !this.pressed)) {
  611. this.toggle();
  612. }
  613. },
  614. // private
  615. isMenuTriggerOver : function(e, internal){
  616. return this.menu && !internal;
  617. },
  618. // private
  619. isMenuTriggerOut : function(e, internal){
  620. return this.menu && !internal;
  621. },
  622. // private
  623. onMouseOver : function(e){
  624. if(!this.disabled){
  625. var internal = e.within(this.el, true);
  626. if(!internal){
  627. this.el.addClass('x-btn-over');
  628. if(!this.monitoringMouseOver){
  629. this.doc.on('mouseover', this.monitorMouseOver, this);
  630. this.monitoringMouseOver = true;
  631. }
  632. this.fireEvent('mouseover', this, e);
  633. }
  634. if(this.isMenuTriggerOver(e, internal)){
  635. this.fireEvent('menutriggerover', this, this.menu, e);
  636. }
  637. }
  638. },
  639. // private
  640. monitorMouseOver : function(e){
  641. if(e.target != this.el.dom && !e.within(this.el)){
  642. if(this.monitoringMouseOver){
  643. this.doc.un('mouseover', this.monitorMouseOver, this);
  644. this.monitoringMouseOver = false;
  645. }
  646. this.onMouseOut(e);
  647. }
  648. },
  649. // private
  650. onMouseOut : function(e){
  651. var internal = e.within(this.el) && e.target != this.el.dom;
  652. this.el.removeClass('x-btn-over');
  653. this.fireEvent('mouseout', this, e);
  654. if(this.isMenuTriggerOut(e, internal)){
  655. this.fireEvent('menutriggerout', this, this.menu, e);
  656. }
  657. },
  658. focus : function() {
  659. this.btnEl.focus();
  660. },
  661. blur : function() {
  662. this.btnEl.blur();
  663. },
  664. // private
  665. onFocus : function(e){
  666. if(!this.disabled){
  667. this.el.addClass('x-btn-focus');
  668. }
  669. },
  670. // private
  671. onBlur : function(e){
  672. this.el.removeClass('x-btn-focus');
  673. },
  674. // private
  675. getClickEl : function(e, isUp){
  676. return this.el;
  677. },
  678. // private
  679. onMouseDown : function(e){
  680. if(!this.disabled && e.button === 0){
  681. this.getClickEl(e).addClass('x-btn-click');
  682. this.doc.on('mouseup', this.onMouseUp, this);
  683. }
  684. },
  685. // private
  686. onMouseUp : function(e){
  687. if(e.button === 0){
  688. this.getClickEl(e, true).removeClass('x-btn-click');
  689. this.doc.un('mouseup', this.onMouseUp, this);
  690. }
  691. },
  692. // private
  693. onMenuShow : function(e){
  694. if(this.menu.ownerCt == this){
  695. this.menu.ownerCt = this;
  696. this.ignoreNextClick = 0;
  697. this.el.addClass('x-btn-menu-active');
  698. this.fireEvent('menushow', this, this.menu);
  699. }
  700. },
  701. // private
  702. onMenuHide : function(e){
  703. if(this.menu.ownerCt == this){
  704. this.el.removeClass('x-btn-menu-active');
  705. this.ignoreNextClick = this.restoreClick.defer(250, this);
  706. this.fireEvent('menuhide', this, this.menu);
  707. delete this.menu.ownerCt;
  708. }
  709. },
  710. // private
  711. restoreClick : function(){
  712. this.ignoreNextClick = 0;
  713. }
  714. /**
  715. * @cfg {String} autoEl @hide
  716. */
  717. /**
  718. * @cfg {String/Object} html @hide
  719. */
  720. /**
  721. * @cfg {String} contentEl @hide
  722. */
  723. /**
  724. * @cfg {Mixed} data @hide
  725. */
  726. /**
  727. * @cfg {Mixed} tpl @hide
  728. */
  729. /**
  730. * @cfg {String} tplWriteMode @hide
  731. */
  732. });
  733. Ext.reg('button', Ext.Button);
  734. // Private utility class used by Button
  735. Ext.ButtonToggleMgr = function(){
  736. var groups = {};
  737. function toggleGroup(btn, state){
  738. if(state){
  739. var g = groups[btn.toggleGroup];
  740. for(var i = 0, l = g.length; i < l; i++){
  741. if(g[i] != btn){
  742. g[i].toggle(false);
  743. }
  744. }
  745. }
  746. }
  747. return {
  748. register : function(btn){
  749. if(!btn.toggleGroup){
  750. return;
  751. }
  752. var g = groups[btn.toggleGroup];
  753. if(!g){
  754. g = groups[btn.toggleGroup] = [];
  755. }
  756. g.push(btn);
  757. btn.on('toggle', toggleGroup);
  758. },
  759. unregister : function(btn){
  760. if(!btn.toggleGroup){
  761. return;
  762. }
  763. var g = groups[btn.toggleGroup];
  764. if(g){
  765. g.remove(btn);
  766. btn.un('toggle', toggleGroup);
  767. }
  768. },
  769. /**
  770. * Gets the pressed button in the passed group or null
  771. * @param {String} group
  772. * @return Button
  773. */
  774. getPressed : function(group){
  775. var g = groups[group];
  776. if(g){
  777. for(var i = 0, len = g.length; i < len; i++){
  778. if(g[i].pressed === true){
  779. return g[i];
  780. }
  781. }
  782. }
  783. return null;
  784. }
  785. };
  786. }();
  787. /**
  788. * @class Ext.SplitButton
  789. * @extends Ext.Button
  790. * A split button that provides a built-in dropdown arrow that can fire an event separately from the default
  791. * click event of the button. Typically this would be used to display a dropdown menu that provides additional
  792. * options to the primary button action, but any custom handler can provide the arrowclick implementation. Example usage:
  793. * <pre><code>
  794. // display a dropdown menu:
  795. new Ext.SplitButton({
  796. renderTo: 'button-ct', // the container id
  797. text: 'Options',
  798. handler: optionsHandler, // handle a click on the button itself
  799. menu: new Ext.menu.Menu({
  800. items: [
  801. // these items will render as dropdown menu items when the arrow is clicked:
  802. {text: 'Item 1', handler: item1Handler},
  803. {text: 'Item 2', handler: item2Handler}
  804. ]
  805. })
  806. });
  807. // Instead of showing a menu, you provide any type of custom
  808. // functionality you want when the dropdown arrow is clicked:
  809. new Ext.SplitButton({
  810. renderTo: 'button-ct',
  811. text: 'Options',
  812. handler: optionsHandler,
  813. arrowHandler: myCustomHandler
  814. });
  815. </code></pre>
  816. * @cfg {Function} arrowHandler A function called when the arrow button is clicked (can be used instead of click event)
  817. * @cfg {String} arrowTooltip The title attribute of the arrow
  818. * @constructor
  819. * Create a new menu button
  820. * @param {Object} config The config object
  821. * @xtype splitbutton
  822. */
  823. Ext.SplitButton = Ext.extend(Ext.Button, {
  824. // private
  825. arrowSelector : 'em',
  826. split: true,
  827. // private
  828. initComponent : function(){
  829. Ext.SplitButton.superclass.initComponent.call(this);
  830. /**
  831. * @event arrowclick
  832. * Fires when this button's arrow is clicked
  833. * @param {MenuButton} this
  834. * @param {EventObject} e The click event
  835. */
  836. this.addEvents("arrowclick");
  837. },
  838. // private
  839. onRender : function(){
  840. Ext.SplitButton.superclass.onRender.apply(this, arguments);
  841. if(this.arrowTooltip){
  842. this.el.child(this.arrowSelector).dom[this.tooltipType] = this.arrowTooltip;
  843. }
  844. },
  845. /**
  846. * Sets this button's arrow click handler.
  847. * @param {Function} handler The function to call when the arrow is clicked
  848. * @param {Object} scope (optional) Scope for the function passed above
  849. */
  850. setArrowHandler : function(handler, scope){
  851. this.arrowHandler = handler;
  852. this.scope = scope;
  853. },
  854. getMenuClass : function(){
  855. return 'x-btn-split' + (this.arrowAlign == 'bottom' ? '-bottom' : '');
  856. },
  857. isClickOnArrow : function(e){
  858. if (this.arrowAlign != 'bottom') {
  859. var visBtn = this.el.child('em.x-btn-split');
  860. var right = visBtn.getRegion().right - visBtn.getPadding('r');
  861. return e.getPageX() > right;
  862. } else {
  863. return e.getPageY() > this.btnEl.getRegion().bottom;
  864. }
  865. },
  866. // private
  867. onClick : function(e, t){
  868. e.preventDefault();
  869. if(!this.disabled){
  870. if(this.isClickOnArrow(e)){
  871. if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
  872. this.showMenu();
  873. }
  874. this.fireEvent("arrowclick", this, e);
  875. if(this.arrowHandler){
  876. this.arrowHandler.call(this.scope || this, this, e);
  877. }
  878. }else{
  879. this.doToggle();
  880. this.fireEvent("click", this, e);
  881. if(this.handler){
  882. this.handler.call(this.scope || this, this, e);
  883. }
  884. }
  885. }
  886. },
  887. // private
  888. isMenuTriggerOver : function(e){
  889. return this.menu && e.target.tagName == this.arrowSelector;
  890. },
  891. // private
  892. isMenuTriggerOut : function(e, internal){
  893. return this.menu && e.target.tagName != this.arrowSelector;
  894. }
  895. });
  896. Ext.reg('splitbutton', Ext.SplitButton);/**
  897. * @class Ext.CycleButton
  898. * @extends Ext.SplitButton
  899. * A specialized SplitButton that contains a menu of {@link Ext.menu.CheckItem} elements. The button automatically
  900. * cycles through each menu item on click, raising the button's {@link #change} event (or calling the button's
  901. * {@link #changeHandler} function, if supplied) for the active menu item. Clicking on the arrow section of the
  902. * button displays the dropdown menu just like a normal SplitButton. Example usage:
  903. * <pre><code>
  904. var btn = new Ext.CycleButton({
  905. showText: true,
  906. prependText: 'View as ',
  907. items: [{
  908. text:'text only',
  909. iconCls:'view-text',
  910. checked:true
  911. },{
  912. text:'HTML',
  913. iconCls:'view-html'
  914. }],
  915. changeHandler:function(btn, item){
  916. Ext.Msg.alert('Change View', item.text);
  917. }
  918. });
  919. </code></pre>
  920. * @constructor
  921. * Create a new split button
  922. * @param {Object} config The config object
  923. * @xtype cycle
  924. */
  925. Ext.CycleButton = Ext.extend(Ext.SplitButton, {
  926. /**
  927. * @cfg {Array} items An array of {@link Ext.menu.CheckItem} <b>config</b> objects to be used when creating the
  928. * button's menu items (e.g., {text:'Foo', iconCls:'foo-icon'})
  929. */
  930. /**
  931. * @cfg {Boolean} showText True to display the active item's text as the button text (defaults to false)
  932. */
  933. /**
  934. * @cfg {String} prependText A static string to prepend before the active item's text when displayed as the
  935. * button's text (only applies when showText = true, defaults to '')
  936. */
  937. /**
  938. * @cfg {Function} changeHandler A callback function that will be invoked each time the active menu
  939. * item in the button's menu has changed. If this callback is not supplied, the SplitButton will instead
  940. * fire the {@link #change} event on active item change. The changeHandler function will be called with the
  941. * following argument list: (SplitButton this, Ext.menu.CheckItem item)
  942. */
  943. /**
  944. * @cfg {String} forceIcon A css class which sets an image to be used as the static icon for this button. This
  945. * icon will always be displayed regardless of which item is selected in the dropdown list. This overrides the
  946. * default behavior of changing the button's icon to match the selected item's icon on change.
  947. */
  948. /**
  949. * @property menu
  950. * @type Menu
  951. * The {@link Ext.menu.Menu Menu} object used to display the {@link Ext.menu.CheckItem CheckItems} representing the available choices.
  952. */
  953. // private
  954. getItemText : function(item){
  955. if(item && this.showText === true){
  956. var text = '';
  957. if(this.prependText){
  958. text += this.prependText;
  959. }
  960. text += item.text;
  961. return text;
  962. }
  963. return undefined;
  964. },
  965. /**
  966. * Sets the button's active menu item.
  967. * @param {Ext.menu.CheckItem} item The item to activate
  968. * @param {Boolean} suppressEvent True to prevent the button's change event from firing (defaults to false)
  969. */
  970. setActiveItem : function(item, suppressEvent){
  971. if(!Ext.isObject(item)){
  972. item = this.menu.getComponent(item);
  973. }
  974. if(item){
  975. if(!this.rendered){
  976. this.text = this.getItemText(item);
  977. this.iconCls = item.iconCls;
  978. }else{
  979. var t = this.getItemText(item);
  980. if(t){
  981. this.setText(t);
  982. }
  983. this.setIconClass(item.iconCls);
  984. }
  985. this.activeItem = item;
  986. if(!item.checked){
  987. item.setChecked(true, suppressEvent);
  988. }
  989. if(this.forceIcon){
  990. this.setIconClass(this.forceIcon);
  991. }
  992. if(!suppressEvent){
  993. this.fireEvent('change', this, item);
  994. }
  995. }
  996. },
  997. /**
  998. * Gets the currently active menu item.
  999. * @return {Ext.menu.CheckItem} The active item
  1000. */
  1001. getActiveItem : function(){
  1002. return this.activeItem;
  1003. },
  1004. // private
  1005. initComponent : function(){
  1006. this.addEvents(
  1007. /**
  1008. * @event change
  1009. * Fires after the button's active menu item has changed. Note that if a {@link #changeHandler} function
  1010. * is set on this CycleButton, it will be called instead on active item change and this change event will
  1011. * not be fired.
  1012. * @param {Ext.CycleButton} this
  1013. * @param {Ext.menu.CheckItem} item The menu item that was selected
  1014. */
  1015. "change"
  1016. );
  1017. if(this.changeHandler){
  1018. this.on('change', this.changeHandler, this.scope||this);
  1019. delete this.changeHandler;
  1020. }
  1021. this.itemCount = this.items.length;
  1022. this.menu = {cls:'x-cycle-menu', items:[]};
  1023. var checked = 0;
  1024. Ext.each(this.items, function(item, i){
  1025. Ext.apply(item, {
  1026. group: item.group || this.id,
  1027. itemIndex: i,
  1028. checkHandler: this.checkHandler,
  1029. scope: this,
  1030. checked: item.checked || false
  1031. });
  1032. this.menu.items.push(item);
  1033. if(item.checked){
  1034. checked = i;
  1035. }
  1036. }, this);
  1037. Ext.CycleButton.superclass.initComponent.call(this);
  1038. this.on('click', this.toggleSelected, this);
  1039. this.setActiveItem(checked, true);
  1040. },
  1041. // private
  1042. checkHandler : function(item, pressed){
  1043. if(pressed){
  1044. this.setActiveItem(item);
  1045. }
  1046. },
  1047. /**
  1048. * This is normally called internally on button click, but can be called externally to advance the button's
  1049. * active item programmatically to the next one in the menu. If the current item is the last one in the menu
  1050. * the active item will be set to the first item in the menu.
  1051. */
  1052. toggleSelected : function(){
  1053. var m = this.menu;
  1054. m.render();
  1055. // layout if we haven't before so the items are active
  1056. if(!m.hasLayout){
  1057. m.doLayout();
  1058. }
  1059. var nextIdx, checkItem;
  1060. for (var i = 1; i < this.itemCount; i++) {
  1061. nextIdx = (this.activeItem.itemIndex + i) % this.itemCount;
  1062. // check the potential item
  1063. checkItem = m.items.itemAt(nextIdx);
  1064. // if its not disabled then check it.
  1065. if (!checkItem.disabled) {
  1066. checkItem.setChecked(true);
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. });
  1072. Ext.reg('cycle', Ext.CycleButton);