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