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          * When a butotn is pressed
44          * @param {Roo.EventObject} e
45          */
46         "click" : true,
47          /**
48          * @event toggle
49          * After the button has been toggles
50          * @param {Roo.EventObject} e
51          * @param {boolean} pressed (also available as button.pressed)
52          */
53         "toggle" : true
54     });
55 };
56
57 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
58     html: false,
59     active: false,
60     weight: '',
61     size: '',
62     tag: 'button',
63     href: '',
64     disabled: false,
65     isClose: false,
66     glyphicon: '',
67     badge: '',
68     theme: 'default',
69     inverse: false,
70     
71     toggle: false,
72     ontext: 'ON',
73     offtext: 'OFF',
74     defaulton: true,
75     preventDefault: true,
76     removeClass: false,
77     name: false,
78     target: false,
79     
80     
81     pressed : null,
82     
83     
84     getAutoCreate : function(){
85         
86         var cfg = {
87             tag : 'button',
88             cls : 'roo-button',
89             html: ''
90         };
91         
92         if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
93             throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
94             this.tag = 'button';
95         } else {
96             cfg.tag = this.tag;
97         }
98         cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
99         
100         if (this.toggle == true) {
101             cfg={
102                 tag: 'div',
103                 cls: 'slider-frame roo-button',
104                 cn: [
105                     {
106                         tag: 'span',
107                         'data-on-text':'ON',
108                         'data-off-text':'OFF',
109                         cls: 'slider-button',
110                         html: this.offtext
111                     }
112                 ]
113             };
114             
115             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
116                 cfg.cls += ' '+this.weight;
117             }
118             
119             return cfg;
120         }
121         
122         if (this.isClose) {
123             cfg.cls += ' close';
124             
125             cfg["aria-hidden"] = true;
126             
127             cfg.html = "&times;";
128             
129             return cfg;
130         }
131         
132          
133         if (this.theme==='default') {
134             cfg.cls = 'btn roo-button';
135             
136             //if (this.parentType != 'Navbar') {
137             this.weight = this.weight.length ?  this.weight : 'default';
138             //}
139             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
140                 
141                 cfg.cls += ' btn-' + this.weight;
142             }
143         } else if (this.theme==='glow') {
144             
145             cfg.tag = 'a';
146             cfg.cls = 'btn-glow roo-button';
147             
148             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
149                 
150                 cfg.cls += ' ' + this.weight;
151             }
152         }
153    
154         
155         if (this.inverse) {
156             this.cls += ' inverse';
157         }
158         
159         
160         if (this.active) {
161             cfg.cls += ' active';
162         }
163         
164         if (this.disabled) {
165             cfg.disabled = 'disabled';
166         }
167         
168         if (this.items) {
169             Roo.log('changing to ul' );
170             cfg.tag = 'ul';
171             this.glyphicon = 'caret';
172         }
173         
174         cfg.cls += this.size.length ? (' btn-' + this.size) : '';
175          
176         //gsRoo.log(this.parentType);
177         if (this.parentType === 'Navbar' && !this.parent().bar) {
178             Roo.log('changing to li?');
179             
180             cfg.tag = 'li';
181             
182             cfg.cls = '';
183             cfg.cn =  [{
184                 tag : 'a',
185                 cls : 'roo-button',
186                 html : this.html,
187                 href : this.href || '#'
188             }];
189             if (this.menu) {
190                 cfg.cn[0].html = this.html  + ' <span class="caret"></span>';
191                 cfg.cls += ' dropdown';
192             }   
193             
194             delete cfg.html;
195             
196         }
197         
198        cfg.cls += this.parentType === 'Navbar' ?  ' navbar-btn' : '';
199         
200         if (this.glyphicon) {
201             cfg.html = ' ' + cfg.html;
202             
203             cfg.cn = [
204                 {
205                     tag: 'span',
206                     cls: 'glyphicon glyphicon-' + this.glyphicon
207                 }
208             ];
209         }
210         
211         if (this.badge) {
212             cfg.html += ' ';
213             
214             cfg.tag = 'a';
215             
216 //            cfg.cls='btn roo-button';
217             
218             cfg.href=this.href;
219             
220             var value = cfg.html;
221             
222             if(this.glyphicon){
223                 value = {
224                             tag: 'span',
225                             cls: 'glyphicon glyphicon-' + this.glyphicon,
226                             html: this.html
227                         };
228                 
229             }
230             
231             cfg.cn = [
232                 value,
233                 {
234                     tag: 'span',
235                     cls: 'badge',
236                     html: this.badge
237                 }
238             ];
239             
240             cfg.html='';
241         }
242         
243         if (this.menu) {
244             cfg.cls += ' dropdown';
245             cfg.html = typeof(cfg.html) != 'undefined' ? cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
246         }
247         
248         if (cfg.tag !== 'a' && this.href !== '') {
249             throw "Tag must be a to set href.";
250         } else if (this.href.length > 0) {
251             cfg.href = this.href;
252         }
253         
254         if(this.removeClass){
255             cfg.cls = '';
256         }
257         
258         if(this.target){
259             cfg.target = this.target;
260         }
261         
262         return cfg;
263     },
264     initEvents: function() {
265        // Roo.log('init events?');
266 //        Roo.log(this.el.dom);
267        if (this.el.hasClass('roo-button')) {
268             this.el.on('click', this.onClick, this);
269        } else {
270             this.el.select('.roo-button').on('click', this.onClick, this);
271        }
272        
273        
274         
275     },
276     onClick : function(e)
277     {
278         if (this.disabled) {
279             return;
280         }
281         
282         Roo.log('button on click ');
283         if(this.preventDefault){
284             e.preventDefault();
285         }
286         if (this.pressed === true || this.pressed === false) {
287             this.pressed = !this.pressed;
288             this.el[this.pressed ? 'addClass' : 'removeClass']('active');
289             this.fireEvent('toggle', this, e, this.pressed);
290         }
291         
292         
293         this.fireEvent('click', this, e);
294     },
295     
296     /**
297      * Enables this button
298      */
299     enable : function()
300     {
301         this.disabled = false;
302         this.el.removeClass('disabled');
303     },
304     
305     /**
306      * Disable this button
307      */
308     disable : function()
309     {
310         this.disabled = true;
311         this.el.addClass('disabled');
312     },
313      /**
314      * sets the active state on/off, 
315      * @param {Boolean} state (optional) Force a particular state
316      */
317     setActive : function(v) {
318         
319         this.el[v ? 'addClass' : 'removeClass']('active');
320     },
321      /**
322      * toggles the current active state 
323      */
324     toggleActive : function()
325     {
326        var active = this.el.hasClass('active');
327        this.setActive(!active);
328        
329         
330     },
331     setText : function(str)
332     {
333         this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
334     }
335     
336     
337     
338 });
339
340