-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.js
1652 lines (1443 loc) · 50 KB
/
button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* renderTo: Ext.getBody(),
* alert('You clicked the button!');
* }
* });
*
* The {@link #handler} configuration can also be updated dynamically using the {@link #setHandler}
* method. Example usage:
*
* @example
* Ext.create('Ext.Button', {
* text : 'Dynamic Handler Button',
* renderTo: Ext.getBody(),
* handler : function() {
* // this button will spit out a different number every time you click it.
* // so firstly we must check if that number is already set:
* if (this.clickCount) {
* // looks like the property is already set, so lets just add 1 to that number and alert the user
* this.clickCount++;
* alert('You have clicked the button "' + this.clickCount + '" times.\n\nTry clicking it again..');
* } else {
* // if the clickCount property is not set, we will set it and alert the user
* this.clickCount = 1;
* alert('You just clicked the button for the first time!\n\nTry pressing it again..');
* }
* }
* });
*
* A button within a container:
*
* @example
* Ext.create('Ext.Container', {
* renderTo: Ext.getBody(),
* items : [
* {
* xtype: 'button',
* text : 'My Button'
* }
* ]
* });
*
* A useful option of Button is the {@link #scale} configuration. This configuration has three different options:
*
* - `'small'`
* - `'medium'`
* - `'large'`
*
* Example usage:
*
* @example
* Ext.create('Ext.Button', {
* renderTo: document.body,
* text : 'Click me',
* scale : 'large'
* });
*
* Buttons can also be toggled. To enable this, you simple set the {@link #enableToggle} property to `true`.
* Example usage:
*
* @example
* Ext.create('Ext.Button', {
* renderTo: Ext.getBody(),
* text: 'Click Me',
* enableToggle: true
* });
*
* You can assign a menu to a button by using the {@link #cfg-menu} configuration. This standard configuration
* can either be a reference to a {@link Ext.menu.Menu menu} object, a {@link Ext.menu.Menu menu} id or a
* {@link Ext.menu.Menu menu} config blob. When assigning a menu to a button, an arrow is automatically
* added to the button. You can change the alignment of the arrow using the {@link #arrowAlign} configuration
* on button. Example usage:
*
* @example
* Ext.create('Ext.Button', {
* text : 'Menu button',
* renderTo : Ext.getBody(),
* arrowAlign: 'bottom',
* menu : [
* {text: 'Item 1'},
* {text: 'Item 2'},
* {text: 'Item 3'},
* {text: 'Item 4'}
* ]
* });
*
* Using listeners, you can easily listen to events fired by any component, using the {@link #listeners}
* configuration or using the {@link #addListener} method. Button has a variety of different listeners:
*
* - `click`
* - `toggle`
* - `mouseover`
* - `mouseout`
* - `mouseshow`
* - `menuhide`
* - `menutriggerover`
* - `menutriggerout`
*
* Example usage:
*
* @example
* Ext.create('Ext.Button', {
* text : 'Button',
* renderTo : Ext.getBody(),
* listeners: {
* click: function() {
* // this == the button, as we are in the local scope
* this.setText('I was clicked!');
* },
* mouseover: function() {
* // set a new config which says we moused over, if not already set
* if (!this.mousedOver) {
* this.mousedOver = true;
* alert('You moused over a button!\n\nI wont do this again.');
* }
* }
* }
* });
*/
Ext.define('Ext.button.Button', {
/* Begin Definitions */
alias: 'widget.button',
extend: 'Ext.Component',
requires: [
'Ext.button.Manager',
'Ext.menu.Manager',
'Ext.util.ClickRepeater',
'Ext.layout.component.Button',
'Ext.util.TextMetrics',
'Ext.util.KeyMap'
],
mixins: {
queryable: 'Ext.Queryable'
},
alternateClassName: 'Ext.Button',
/* End Definitions */
/**
* @property {Boolean}
* `true` in this class to identify an object as an instantiated Button, or subclass thereof.
*/
isButton: true,
componentLayout: 'button',
/**
* @property {Boolean} hidden
* True if this button is hidden.
* @readonly
*/
hidden: false,
/**
* @property {Boolean} disabled
* True if this button is disabled.
* @readonly
*/
disabled: false,
/**
* @property {Boolean} pressed
* True if this button is pressed (only if enableToggle = true).
* @readonly
*/
pressed: false,
/**
* @cfg {String} text
* The button text to be used as innerHTML (html tags are accepted).
*/
/**
* @cfg {String} icon
* The path to an image to display in the button.
*
* There are no default icons that come with Ext JS.
*/
/**
* @cfg {Function} handler
* A function called when the button is clicked (can be used instead of click event).
* @cfg {Ext.button.Button} handler.button This button.
* @cfg {Ext.EventObject} handler.e The click event.
*/
/**
* @cfg {Number} minWidth
* The minimum width for this button (used to give a set of buttons a common width).
* See also {@link Ext.panel.Panel}.{@link Ext.panel.Panel#minButtonWidth minButtonWidth}.
*/
/**
* @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.
*/
/**
* @cfg {Boolean} [hidden=false]
* True to start hidden.
*/
/**
* @cfg {Boolean} [disabled=false]
* True to start disabled.
*/
/**
* @cfg {Boolean} [pressed=false]
* True to start pressed (only if enableToggle = true)
*/
/**
* @cfg {String} toggleGroup
* The group this toggle button is a member of (only 1 per group can be pressed). If a toggleGroup
* is specified, the {@link #enableToggle} configuration will automatically be set to true.
*/
/**
* @cfg {Boolean/Object} [repeat=false]
* True to repeat fire the click event while the mouse is down. This can also be a
* {@link Ext.util.ClickRepeater ClickRepeater} config object.
*/
/**
* @cfg {Number} tabIndex
* Sets a DOM tabIndex for this button. tabIndex may be set to `-1` in order to remove
* the button from the tab rotation.
*/
tabIndex: 0,
/**
* @cfg {Boolean} [allowDepress=true]
* False to not allow a pressed Button to be depressed. Only valid when {@link #enableToggle} is true.
*/
/**
* @cfg {Boolean} [enableToggle=false]
* True to enable pressed/not pressed toggling. If a {@link #toggleGroup} is specified, this
* option will be set to true.
*/
enableToggle: false,
/**
* @cfg {Function} toggleHandler
* Function called when a Button with {@link #enableToggle} set to true is clicked.
* @cfg {Ext.button.Button} toggleHandler.button This button.
* @cfg {Boolean} toggleHandler.state The next state of the Button, true means pressed.
*/
/**
* @cfg {Ext.menu.Menu/String/Object} menu
* Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob.
*/
/**
* @cfg {String} menuAlign
* The position to align the menu to (see {@link Ext.util.Positionable#alignTo} for more details).
*/
menuAlign: 'tl-bl?',
/**
* @cfg {Boolean} showEmptyMenu
* True to force an attached {@link #cfg-menu} with no items to be shown when clicking
* this button. By default, the menu will not show if it is empty.
*/
showEmptyMenu: false,
/**
* @cfg {String} textAlign
* The text alignment for this button (center, left, right).
*/
textAlign: 'center',
/**
* @cfg {String} overflowText
* If used in a {@link Ext.toolbar.Toolbar Toolbar}, the text to be used if this item is shown in the overflow menu.
* See also {@link Ext.toolbar.Item}.`{@link Ext.toolbar.Item#overflowText overflowText}`.
*/
/**
* @cfg {String} iconCls
* A css class which sets a background image to be used as the icon for this button.
*
* There are no default icon classes that come with Ext JS.
*/
/**
* @cfg {Number/String} glyph
* A numeric unicode character code to use as the icon for this button. The default
* font-family for glyphs can be set globally using
* {@link Ext#setGlyphFontFamily Ext.setGlyphFontFamily()}. Alternatively, this
* config option accepts a string with the charCode and font-family separated by the
* `@` symbol. For example '65@My Font Family'.
*/
/**
* @cfg {String} clickEvent
* The DOM event that will fire the handler of the button. This can be any valid event name (dblclick, contextmenu).
*/
clickEvent: 'click',
/**
* @cfg {Boolean} preventDefault
* True to prevent the default action when the {@link #clickEvent} is processed.
*/
preventDefault: true,
/**
* @cfg {Boolean} handleMouseEvents
* False to disable visual cues on mouseover, mouseout and mousedown.
*/
handleMouseEvents: true,
/**
* @cfg {String} tooltipType
* The type of tooltip to use. Either 'qtip' for QuickTips or 'title' for title attribute.
*/
tooltipType: 'qtip',
/**
* @cfg {String} [baseCls='x-btn']
* The base CSS class to add to all buttons.
*/
baseCls: Ext.baseCSSPrefix + 'btn',
/**
* @cfg {String} pressedCls
* The CSS class to add to a button when it is in the pressed state.
*/
pressedCls: 'pressed',
/**
* @cfg {String} overCls
* The CSS class to add to a button when it is in the over (hovered) state.
*/
overCls: 'over',
/**
* @cfg {String} focusCls
* The CSS class to add to a button when it is in the focussed state.
*/
focusCls: 'focus',
/**
* @cfg {String} menuActiveCls
* The CSS class to add to a button when it's menu is active.
*/
menuActiveCls: 'menu-active',
/**
* @cfg {String} href
* The URL to open when the button is clicked. Specifying this config causes the Button to be
* rendered with the specified URL as the `href` attribute of its `<a>` Element.
*
* This is better than specifying a click handler of
*
* function() { window.location = "http://www.sencha.com" }
*
* because the UI will provide meaningful hints to the user as to what to expect upon clicking
* the button, and will also allow the user to open in a new tab or window, bookmark or drag the URL, or directly save
* the URL stream to disk.
*
* See also the {@link #hrefTarget} config.
*/
/**
* @cfg {String} [hrefTarget="_blank"]
* The target attribute to use for the underlying anchor. Only used if the {@link #href}
* property is specified.
*/
hrefTarget: '_blank',
/**
* @cfg {Boolean} [destroyMenu=true]
* Whether or not to destroy any associated menu when this button is destroyed.
* In addition, a value of `true` for this config will destroy the currently bound menu when a new
* menu is set in {@link #setMenu} unless overridden by that method's destroyMenu function argument.
*/
destroyMenu: true,
/**
* @cfg {Object} baseParams
* An object literal of parameters to pass to the url when the {@link #href} property is specified.
*/
/**
* @cfg {Object} params
* An object literal of parameters to pass to the url when the {@link #href} property is specified. Any params
* override {@link #baseParams}. New params can be set using the {@link #setParams} method.
*/
ariaRole: 'button',
childEls: [
'btnEl', 'btnWrap', 'btnInnerEl', 'btnIconEl'
],
// We have to keep "unselectable" attribute on all elements because it's not inheritable.
// Without it, clicking anywhere on a button disrupts current selection and cursor position
// in HtmlEditor.
renderTpl: [
'<span id="{id}-btnWrap" role="presentation" class="{baseCls}-wrap',
'<tpl if="splitCls"> {splitCls}</tpl>',
'{childElCls}" unselectable="on">',
'<span id="{id}-btnEl" class="{baseCls}-button" role="presentation">',
'<span id="{id}-btnInnerEl" class="{baseCls}-inner {innerCls}',
'{childElCls}" unselectable="on">',
'{text}',
'</span>',
'<span role="presentation" id="{id}-btnIconEl" class="{baseCls}-icon-el {iconCls}',
'{childElCls} {glyphCls}" unselectable="on" style="',
'<tpl if="iconUrl">background-image:url({iconUrl});</tpl>',
'<tpl if="glyph && glyphFontFamily">font-family:{glyphFontFamily};</tpl>">',
'<tpl if="glyph">&#{glyph};</tpl><tpl if="iconCls || iconUrl"> </tpl>',
'</span>',
'</span>',
'</span>',
// if "closable" (tab) add a close element icon
'<tpl if="closable">',
'<span id="{id}-closeEl" role="presentation"',
' class="{baseCls}-close-btn"',
'<tpl if="closeText">',
' title="{closeText}" aria-label="{closeText}"',
'</tpl>',
'>',
'</span>',
'</tpl>'
],
/**
* @cfg {"small"/"medium"/"large"} scale
* The size of the Button. Three values are allowed:
*
* - 'small' - Results in the button element being 16px high.
* - 'medium' - Results in the button element being 24px high.
* - 'large' - Results in the button element being 32px high.
*/
scale: 'small',
/**
* @private
* An array of allowed scales.
*/
allowedScales: ['small', 'medium', 'large'],
/**
* @cfg {Object} scope
* The scope (**this** reference) in which the `{@link #handler}` and `{@link #toggleHandler}` is executed.
* Defaults to this Button.
*/
/**
* @cfg {String} iconAlign
* The side of the Button box to render the icon. Four values are allowed:
*
* - 'top'
* - 'right'
* - 'bottom'
* - 'left'
*/
iconAlign: 'left',
/**
* @cfg {String} arrowAlign
* The side of the Button box to render the arrow if the button has an associated {@link #cfg-menu}. Two
* values are allowed:
*
* - 'right'
* - 'bottom'
*/
arrowAlign: 'right',
/**
* @cfg {String} arrowCls
* The className used for the inner arrow element if the button has a menu.
*/
arrowCls: 'arrow',
/**
* @property {Ext.Template} template
* A {@link Ext.Template Template} used to create the Button's DOM structure.
*
* Instances, or subclasses which need a different DOM structure may provide a different template layout in
* conjunction with an implementation of {@link #getTemplateArgs}.
*/
/**
* @cfg {String} cls
* A CSS class string to apply to the button's main element.
*/
/**
* @property {Ext.menu.Menu} menu
* The {@link Ext.menu.Menu Menu} object associated with this Button when configured with the {@link #cfg-menu} config
* option.
*/
maskOnDisable: false,
shrinkWrap: 3,
frame: true,
autoEl: {
tag: 'a',
hidefocus: 'on',
unselectable: 'on'
},
hasFrameTable: function () {
// Instead of browser sniffing, it's easier to check for the presence of frameTable.
// If present, we know that it's a browser that doesn't support CSS3BorderRadius.
return this.href && this.frameTable;
},
frameTableListener: function () {
if (!this.disabled) {
this.doNavigate();
}
},
doNavigate: function () {
// Non-HTML5 browsers don't support a block element inside an A tag.
// http://stackoverflow.com/questions/5682048/putting-a-table-inside-a-hyperlink-not-working-in-ie
// Note use this.getHref() to append any params to the url.
if (this.hrefTarget === '_blank') {
window.open(this.getHref(), this.hrefTarget);
} else {
location.href = this.getHref();
}
},
// A reusable object used by getTriggerRegion to avoid excessive object creation.
_triggerRegion: {},
initComponent: function() {
var me = this;
// Ensure no selection happens
me.addCls(Ext.baseCSSPrefix + 'unselectable');
me.callParent(arguments);
me.addEvents(
/**
* @event click
* Fires when this button is clicked, before the configured {@link #handler} is invoked. Execution of the
* {@link #handler} may be vetoed by returning <code>false</code> to this event.
* @param {Ext.button.Button} this
* @param {Event} e The click event
*/
'click',
/**
* @event toggle
* Fires when the 'pressed' state of this button changes (only if enableToggle = true)
* @param {Ext.button.Button} this
* @param {Boolean} pressed
*/
'toggle',
/**
* @event mouseover
* Fires when the mouse hovers over the button
* @param {Ext.button.Button} this
* @param {Event} e The event object
*/
'mouseover',
/**
* @event mouseout
* Fires when the mouse exits the button
* @param {Ext.button.Button} this
* @param {Event} e The event object
*/
'mouseout',
/**
* @event menushow
* If this button has a menu, this event fires when it is shown
* @param {Ext.button.Button} this
* @param {Ext.menu.Menu} menu
*/
'menushow',
/**
* @event menuhide
* If this button has a menu, this event fires when it is hidden
* @param {Ext.button.Button} this
* @param {Ext.menu.Menu} menu
*/
'menuhide',
/**
* @event menutriggerover
* If this button has a menu, this event fires when the mouse enters the menu triggering element
* @param {Ext.button.Button} this
* @param {Ext.menu.Menu} menu
* @param {Event} e
*/
'menutriggerover',
/**
* @event menutriggerout
* If this button has a menu, this event fires when the mouse leaves the menu triggering element
* @param {Ext.button.Button} this
* @param {Ext.menu.Menu} menu
* @param {Event} e
*/
'menutriggerout',
/**
* @event textchange
* Fired when the button's text is changed by the {@link #setText} method.
* @param {Ext.button.Button} this
* @param {String} oldText
* @param {String} newText
*/
'textchange',
/**
* @event iconchange
* Fired when the button's icon is changed by the {@link #setIcon} or {@link #setIconCls} methods.
* @param {Ext.button.Button} this
* @param {String} oldIcon
* @param {String} newIcon
*/
'iconchange',
/**
* @event glyphchange
* Fired when the button's glyph is changed by the {@link #setGlyph} method.
* @param {Ext.button.Button} this
* @param {Number/String} newGlyph
* @param {Number/String} oldGlyph
*/
'glyphchange'
);
if (me.menu) {
// Flag that we'll have a splitCls
me.split = true;
me.setMenu(me.menu, /*destroyMenu*/false);
}
// Accept url as a synonym for href
if (me.url) {
me.href = me.url;
}
// preventDefault defaults to false for links
if (me.href && !me.hasOwnProperty('preventDefault')) {
me.preventDefault = false;
}
if (Ext.isString(me.toggleGroup) && me.toggleGroup !== '') {
me.enableToggle = true;
}
if (me.html && !me.text) {
me.text = me.html;
delete me.html;
}
me.glyphCls = me.baseCls + '-glyph';
},
getActionEl: function() {
return this.el;
},
getFocusEl: function() {
return this.el;
},
// @private
setComponentCls: function() {
var me = this,
cls = me.getComponentCls();
if (!Ext.isEmpty(me.oldCls)) {
me.removeClsWithUI(me.oldCls);
me.removeClsWithUI(me.pressedCls);
}
me.oldCls = cls;
me.addClsWithUI(cls);
},
getComponentCls: function() {
var me = this,
cls;
// Check whether the button has an icon or not, and if it has an icon, what is the alignment
if (me.iconCls || me.icon || me.glyph) {
cls = [me.text ? 'icon-text-' + me.iconAlign : 'icon'];
} else if (me.text) {
cls = ['noicon'];
} else {
cls = [];
}
if (me.pressed) {
cls[cls.length] = me.pressedCls;
}
return cls;
},
getElConfig: function() {
var me = this,
config = me.callParent(),
href = me.getHref(),
hrefTarget = me.hrefTarget;
if (config.tag === 'a') {
if (!me.disabled) {
config.tabIndex = me.tabIndex;
}
if (href) {
config.href = href;
if (hrefTarget) {
config.target = hrefTarget;
}
}
}
return config;
},
beforeRender: function () {
var me = this;
me.callParent();
// Add all needed classes to the protoElement.
me.oldCls = me.getComponentCls();
me.addClsWithUI(me.oldCls);
// Apply the renderData to the template args
Ext.applyIf(me.renderData, me.getTemplateArgs());
},
/**
* Sets a new menu for this button. Pass a falsy value to unset the current menu.
* To destroy the previous menu for this button, explicitly pass `false` as the second argument. If this is not set, the destroy will depend on the
* value of {@link #cfg-destroyMenu}.
*
* @param {Ext.menu.Menu/String/Object/null} menu Accepts a menu component, a menu id or a menu config.
* @param {Boolean} destroyMenu By default, will destroy the previous set menu and remove it from the menu manager. Pass `false` to prevent the destroy.
*/
setMenu: function (menu, destroyMenu) {
var me = this,
oldMenu = me.menu;
if (oldMenu && destroyMenu !== false && me.destroyMenu) {
oldMenu.destroy();
}
if (oldMenu) {
oldMenu.ownerCmp = oldMenu.ownerButton = null;
}
if (menu) {
// Retrieve menu by id or instantiate instance if needed.
menu = Ext.menu.Manager.get(menu);
// Use ownerCmp as the upward link. Menus *must have no ownerCt* - they are global floaters.
// Upward navigation is done using the up() method.
menu.ownerCmp = menu.ownerButton = me;
me.mon(menu, {
scope: me,
show: me.onMenuShow,
hide: me.onMenuHide
});
// If the button wasn't initially configured with a menu or has previously been unset then we need
// to poke the split classes onto the btnWrap dom element.
if (!oldMenu) {
me.split = true;
if (me.rendered) {
me.btnWrap.addCls(me.getSplitCls());
me.updateLayout();
}
}
me.menu = menu;
} else {
if (me.rendered) {
me.btnWrap.removeCls(me.getSplitCls());
me.updateLayout();
}
me.split = false;
me.menu = null;
}
},
// @private
onRender: function() {
var me = this,
addOnclick,
btn,
btnListeners;
me.doc = Ext.getDoc();
me.callParent(arguments);
// Set btn as a local variable for easy access
btn = me.el;
if (me.tooltip) {
me.setTooltip(me.tooltip, true);
}
// Add the mouse events to the button
if (me.handleMouseEvents) {
btnListeners = {
scope: me,
mouseover: me.onMouseOver,
mouseout: me.onMouseOut,
mousedown: me.onMouseDown
};
if (me.split) {
btnListeners.mousemove = me.onMouseMove;
}
} else {
btnListeners = {
scope: me
};
}
// Check if the button has a menu
if (me.menu) {
/***
*
*
*
*
*
* @type {Ext.util.KeyMap}
*/
me.keyMap = new Ext.util.KeyMap({
target: me.el,
key: Ext.EventObject.DOWN,
handler: me.onDownKey,
scope: me
});
}
// Check if it is a repeat button
if (me.repeat) {
me.mon(new Ext.util.ClickRepeater(btn, Ext.isObject(me.repeat) ? me.repeat: {}), 'click', me.onRepeatClick, me);
} else {
// If the activation event already has a handler, make a note to add the handler later
if (btnListeners[me.clickEvent]) {
addOnclick = true;
} else {
btnListeners[me.clickEvent] = me.onClick;
}
}
// Add whatever button listeners we need
me.mon(btn, btnListeners);
if (me.hasFrameTable()) {
me.mon(me.frameTable, 'click', me.frameTableListener, me);
}
// If the listeners object had an entry for our clickEvent, add a listener now
if (addOnclick) {
me.mon(btn, me.clickEvent, me.onClick, me);
}
Ext.button.Manager.register(me);
},
/**
* This method returns an object which provides substitution parameters for the {@link #renderTpl XTemplate} used to
* create this Button's DOM structure.
*
* Instances or subclasses which use a different Template to create a different DOM structure may need to provide
* their own implementation of this method.
* @protected
*
* @return {Object} Substitution data for a Template. The default implementation which provides data for the default
* {@link #template} returns an Object containing the following properties:
* @return {String} return.innerCls A CSS class to apply to the button's text element.
* @return {String} return.splitCls A CSS class to determine the presence and position of an arrow icon.
* (`'x-btn-arrow'` or `'x-btn-arrow-bottom'` or `''`)
* @return {String} return.iconUrl The url for the button icon.
* @return {String} return.iconCls The CSS class for the button icon.
* @return {String} return.glyph The glyph to use as the button icon.
* @return {String} return.glyphCls The CSS class to use for the glyph element.
* @return {String} return.glyphFontFamily The CSS font-family to use for the glyph element.
* @return {String} return.text The {@link #text} to display ion the Button.
*/
getTemplateArgs: function() {
var me = this,
glyph = me.glyph,
glyphFontFamily = Ext._glyphFontFamily,
glyphParts;
if (typeof glyph === 'string') {
glyphParts = glyph.split('@');
glyph = glyphParts[0];
glyphFontFamily = glyphParts[1];
}
return {
innerCls : me.getInnerCls(),
splitCls : me.getSplitCls(),
iconUrl : me.icon,
iconCls : me.iconCls,
glyph: glyph,
glyphCls: glyph ? me.glyphCls : '',
glyphFontFamily: glyphFontFamily,
text : me.text || ' ',
closeText: me.closeText
};
},
/**
* Sets the href of the embedded anchor element to the passed URL.
*
* Also appends any configured {@link #cfg-baseParams} and parameters set through {@link #setParams}.
* @param {String} href The URL to set in the anchor element.
*
*/
setHref: function(href) {
this.href = href;
this.el.dom.href = this.getHref();
},
/**
* @private
* If there is a configured href for this Button, returns the href with parameters appended.
* @return {String/Boolean} The href string with parameters appended.
*/
getHref: function() {
var me = this,
href = me.href;
return href ? Ext.urlAppend(href, Ext.Object.toQueryString(Ext.apply({}, me.params, me.baseParams))) : false;
},
/**
* Sets the href of the link dynamically according to the params passed, and any {@link #baseParams} configured.
*
* var button = Ext.create('Ext.button.Button', {
* renderTo : document.body,
* text : 'Open',
* href : 'http://www.sencha.com',
* baseParams : {
* foo : 'bar'
* }
* });
*
* button.setParams({
* company : 'Sencha'
* });
*
* When clicked, this button will open a new window with the url http://www.sencha.com/?foo=bar&company=Sencha because
* the button wased configured with the {@link #baseParams} to have `foo` = `'bar'` and then used {@link #setParams} to set the `company` parameter to `'Sencha'`.
*
* **Only valid if the Button was originally configured with a {@link #href}**
*
* @param {Object} params Parameters to use in the href URL.
*/
setParams: function(params) {
this.params = params;
this.el.dom.href = this.getHref();
},
getSplitCls: function() {
var me = this;
return me.split ? (me.baseCls + '-' + me.arrowCls) + ' ' + (me.baseCls + '-' + me.arrowCls + '-' + me.arrowAlign) : '';
},
getInnerCls: function() {
return this.textAlign ? this.baseCls + '-inner-' + this.textAlign : '';
},
/**
* Sets the background image (inline style) of the button. This method also changes the value of the {@link #icon}
* config internally.
* @param {String} icon The path to an image to display in the button
* @return {Ext.button.Button} this
*/
setIcon: function(icon) {
icon = icon || '';
var me = this,
btnIconEl = me.btnIconEl,
oldIcon = me.icon || '';
me.icon = icon;
if (icon != oldIcon) {
if (btnIconEl) {
btnIconEl.setStyle('background-image', icon ? 'url(' + icon + ')': '');