Roo/bootstrap/Button.js
[roojs1] / Roo / bootstrap / Button.js
1 /*
2  * - LGPL
3  *
4  * button
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Button
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Button class
12  * @cfg {String} html The button content
13  * @cfg {String} weight default (or empty) | primary | success | info | warning | danger | link
14  * @cfg {String} size empty | lg | sm | xs
15  * @cfg {String} tag empty | a | input | submit
16  * @cfg {String} href empty or href
17  * @cfg {Boolean} disabled false | true
18  * @cfg {Boolean} isClose false | true
19  * @cfg {String} glyphicon empty | adjust | align-center | align-justify | align-left | align-right | arrow-down | arrow-left | arrow-right | arrow-up | asterisk | backward | ban-circle | barcode | bell | bold | book | bookmark | briefcase | bullhorn | calendar | camera | certificate | check | chevron-down | chevron-left | chevron-right | chevron-up | circle-arrow-down | circle-arrow-left | circle-arrow-right | circle-arrow-up | cloud | cloud-download | cloud-upload | cog | collapse-down | collapse-up | comment | compressed | copyright-mark | credit-card | cutlery | dashboard | download | download-alt | earphone | edit | eject | envelope | euro | exclamation-sign | expand | export | eye-close | eye-open | facetime-video | fast-backward | fast-forward | file | film | filter | fire | flag | flash | floppy-disk | floppy-open | floppy-remove | floppy-save | floppy-saved | folder-close | folder-open | font | forward | fullscreen | gbp | gift | glass | globe | hand-down | hand-left | hand-right | hand-up | hd-video | hdd | header | headphones | heart | heart-empty | home | import | inbox | indent-left | indent-right | info-sign | italic | leaf | link | list | list-alt | lock | log-in | log-out | magnet | map-marker | minus | minus-sign | move | music | new-window | off | ok | ok-circle | ok-sign | open | paperclip | pause | pencil | phone | phone-alt | picture | plane | play | play-circle | plus | plus-sign | print | pushpin | qrcode | question-sign | random | record | refresh | registration-mark | remove | remove-circle | remove-sign | repeat | resize-full | resize-horizontal | resize-small | resize-vertical | retweet | road | save | saved | screenshot | sd-video | search | send | share | share-alt | shopping-cart | signal | sort | sort-by-alphabet | sort-by-alphabet-alt | sort-by-attributes | sort-by-attributes-alt | sort-by-order | sort-by-order-alt | sound-5-1 | sound-6-1 | sound-7-1 | sound-dolby | sound-stereo | star | star-empty | stats | step-backward | step-forward | stop | subtitles | tag | tags | tasks | text-height | text-width | th | th-large | th-list | thumbs-down | thumbs-up | time | tint | tower | transfer | trash | tree-conifer | tree-deciduous | unchecked | upload | usd | user | volume-down | volume-off | volume-up | warning-sign | wrench | zoom-in | zoom-out
20  * @cfg {String} badge text for badge
21  * @cfg {String} theme default (or empty) | glow
22  * @cfg {Boolean} inverse false | true
23  * @cfg {Boolean} toggle false | true
24  * @cfg {String} ontext text for on toggle state
25  * @cfg {String} offtext text for off toggle state
26  * @cfg {Boolean} defaulton true | false
27  * @cfg {Boolean} preventDefault (true | false) default true
28  * @cfg {Boolean} removeClass true | false remove the standard class..
29  * @cfg {String} target (_self|_blank|_parent|_top)target for a href.
30  * 
31  * @constructor
32  * Create a new button
33  * @param {Object} config The config object
34  */
35
36
37 Roo.bootstrap.Button = function(config){
38     Roo.bootstrap.Button.superclass.constructor.call(this, config);
39     this.addEvents({
40         // raw events
41         /**
42          * @event click
43          * The raw click event for the entire grid.
44          * @param {Roo.EventObject} e
45          */
46         "click" : true
47     });
48 };
49
50 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
51     html: false,
52     active: false,
53     weight: '',
54     size: '',
55     tag: 'button',
56     href: '',
57     disabled: false,
58     isClose: false,
59     glyphicon: '',
60     badge: '',
61     theme: 'default',
62     inverse: false,
63     
64     toggle: false,
65     ontext: 'ON',
66     offtext: 'OFF',
67     defaulton: true,
68     preventDefault: true,
69     removeClass: false,
70     name: false,
71     target: false,
72     
73     pressed : false,
74     
75     
76     getAutoCreate : function(){
77         
78         var cfg = {
79             tag : 'button',
80             cls : 'roo-button',
81             html: ''
82         };
83         
84         if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
85             throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
86             this.tag = 'button';
87         } else {
88             cfg.tag = this.tag;
89         }
90         cfg.html = this.html || cfg.html;
91         
92         if (this.toggle===true) {
93             cfg={
94                 tag: 'div',
95                 cls: 'slider-frame roo-button',
96                 cn: [
97                     {
98                         tag: 'span',
99                         'data-on-text':'ON',
100                         'data-off-text':'OFF',
101                         cls: 'slider-button',
102                         html: this.offtext
103                     }
104                 ]
105             };
106             
107             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
108                 cfg.cls += ' '+this.weight;
109             }
110             
111             return cfg;
112         }
113         
114         if (this.isClose) {
115             cfg.cls += ' close';
116             
117             cfg["aria-hidden"] = true;
118             
119             cfg.html = "&times;";
120             
121             return cfg;
122         }
123         
124          
125         if (this.theme==='default') {
126             cfg.cls = 'btn roo-button';
127             
128             //if (this.parentType != 'Navbar') {
129             this.weight = this.weight.length ?  this.weight : 'default';
130             //}
131             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
132                 
133                 cfg.cls += ' btn-' + this.weight;
134             }
135         } else if (this.theme==='glow') {
136             
137             cfg.tag = 'a';
138             cfg.cls = 'btn-glow roo-button';
139             
140             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
141                 
142                 cfg.cls += ' ' + this.weight;
143             }
144         }
145    
146         
147         if (this.inverse) {
148             this.cls += ' inverse';
149         }
150         
151         
152         if (this.active) {
153             cfg.cls += ' active';
154         }
155         
156         if (this.disabled) {
157             cfg.disabled = 'disabled';
158         }
159         
160         if (this.items) {
161             Roo.log('changing to ul' );
162             cfg.tag = 'ul';
163             this.glyphicon = 'caret';
164         }
165         
166         cfg.cls += this.size.length ? (' btn-' + this.size) : '';
167          
168         //gsRoo.log(this.parentType);
169         if (this.parentType === 'Navbar' && !this.parent().bar) {
170             Roo.log('changing to li?');
171             
172             cfg.tag = 'li';
173             
174             cfg.cls = '';
175             cfg.cn =  [{
176                 tag : 'a',
177                 cls : 'roo-button',
178                 html : this.html,
179                 href : this.href || '#'
180             }];
181             if (this.menu) {
182                 cfg.cn[0].html = this.html  + ' <span class="caret"></span>';
183                 cfg.cls += ' dropdown';
184             }   
185             
186             delete cfg.html;
187             
188         }
189         
190        cfg.cls += this.parentType === 'Navbar' ?  ' navbar-btn' : '';
191         
192         if (this.glyphicon) {
193             cfg.html = ' ' + cfg.html;
194             
195             cfg.cn = [
196                 {
197                     tag: 'span',
198                     cls: 'glyphicon glyphicon-' + this.glyphicon
199                 }
200             ];
201         }
202         
203         if (this.badge) {
204             cfg.html += ' ';
205             
206             cfg.tag = 'a';
207             
208 //            cfg.cls='btn roo-button';
209             
210             cfg.href=this.href;
211             
212             var value = cfg.html;
213             
214             if(this.glyphicon){
215                 value = {
216                             tag: 'span',
217                             cls: 'glyphicon glyphicon-' + this.glyphicon,
218                             html: this.html
219                         };
220                 
221             }
222             
223             cfg.cn = [
224                 value,
225                 {
226                     tag: 'span',
227                     cls: 'badge',
228                     html: this.badge
229                 }
230             ];
231             
232             cfg.html='';
233         }
234         
235         if (this.menu) {
236             cfg.cls += ' dropdown';
237             cfg.html = typeof(cfg.html) != 'undefined' ? cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
238         }
239         
240         if (cfg.tag !== 'a' && this.href !== '') {
241             throw "Tag must be a to set href.";
242         } else if (this.href.length > 0) {
243             cfg.href = this.href;
244         }
245         
246         if(this.removeClass){
247             cfg.cls = '';
248         }
249         
250         if(this.target){
251             cfg.target = this.target;
252         }
253         
254         return cfg;
255     },
256     initEvents: function() {
257        // Roo.log('init events?');
258 //        Roo.log(this.el.dom);
259        if (this.el.hasClass('roo-button')) {
260             this.el.on('click', this.onClick, this);
261        } else {
262             this.el.select('.roo-button').on('click', this.onClick, this);
263        }
264        
265        
266         
267     },
268     onClick : function(e)
269     {
270         Roo.log('button on click ');
271         if(this.preventDefault){
272             e.preventDefault();
273         }
274         
275         this.fireEvent('click', this, e);
276     }
277     
278     
279 });
280
281