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