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