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