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