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