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