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
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     
69     getAutoCreate : function(){
70         
71         var cfg = {
72             tag : 'button',
73             cls : 'roo-button',
74             html: 'hello'
75         };
76         
77         if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
78             throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
79             this.tag = 'button';
80         } else {
81             cfg.tag = this.tag;
82         }
83         cfg.html = this.html || cfg.html;
84         
85         if (this.toggle===true) {
86             cfg={
87                 tag: 'div',
88                 cls: 'slider-frame roo-button',
89                 cn: [
90                     {
91                         tag: 'span',
92                         'data-on-text':'ON',
93                         'data-off-text':'OFF',
94                         cls: 'slider-button',
95                         html: this.offtext
96                     }
97                 ]
98             };
99             
100             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
101                 cfg.cls += ' '+this.weight;
102             }
103             
104             return cfg;
105         }
106         
107         if (this.isClose) {
108             cfg.cls += ' close';
109             
110             cfg["aria-hidden"] = true;
111             
112             cfg.html = "&times;";
113             
114             return cfg;
115         }
116         
117          
118         if (this.theme==='default') {
119             cfg.cls = 'btn roo-button';
120             
121             if (this.parentType != 'Navbar') {
122                 this.weight = this.weight.length ?  this.weight : 'default';
123             }
124             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
125                 
126                 cfg.cls += ' btn-' + this.weight;
127             }
128         } else if (this.theme==='glow') {
129             
130             cfg.tag = 'a';
131             cfg.cls = 'btn-glow roo-button';
132             
133             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
134                 
135                 cfg.cls += ' ' + this.weight;
136             }
137         }
138    
139         
140         if (this.inverse) {
141             this.cls += ' inverse';
142         }
143         
144         
145         if (this.active) {
146             cfg.cls += ' active';
147         }
148         
149         cfg.cls += this.size.length ? (' btn-' + this.size) : '';
150          
151         //gsRoo.log(this.parentType);
152         if (this.parentType === 'Navbar') {
153             cfg.tag = 'li';
154             
155             cfg.cls = '';
156             cfg.cn =  [{
157                 tag : 'a',
158                 cls : 'roo-button',
159                 html : this.html,
160                 href : this.href || '#'
161             }];
162             if (this.menu) {
163                 cfg.cn[0].html = this.html  + ' <span class="caret"></span>';
164                 cfg.cls += ' dropdown';
165             }   
166             
167             delete cfg.html;
168             
169         } else if (this.menu) {
170             cfg.tag = 'a';
171             cfg.cls += ' dropdown test';
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         if (this.glyphicon) {
185             cfg.html = ' ' + cfg.html;
186             
187             cfg.cn = [
188                 {
189                     tag: 'span',
190                     cls: 'glyphicon glyphicon-' + this.glyphicon
191                 }
192             ];
193         }
194         
195         if (this.badge) {
196             cfg.html += ' ';
197             
198             cfg.tag = 'a';
199             
200 //            cfg.cls='btn roo-button';
201             
202             cfg.href=this.href;
203             
204             var value = cfg.html;
205             
206             if(this.glyphicon){
207                 value = {
208                             tag: 'span',
209                             cls: 'glyphicon glyphicon-' + this.glyphicon,
210                             html: this.html
211                         };
212                 
213             }
214             
215             cfg.cn = [
216                 value,
217                 {
218                     tag: 'span',
219                     cls: 'badge',
220                     html: this.badge
221                 }
222             ];
223             
224             cfg.html='';
225         }
226         
227         if (cfg.tag !== 'a' && this.href !== '') {
228             throw "Tag must be a to set href.";
229         } else if (this.href.length > 0) {
230             cfg.href = this.href;
231         }
232         
233         return cfg;
234     },
235     initEvents: function() {
236        // Roo.log('init events?');
237 //        Roo.log(this.el.dom);
238        if (this.el.hasClass('roo-button')) {
239             this.el.on('click', this.onClick, this);
240        } else {
241             this.el.select('.roo-button').on('click', this.onClick, this);
242        }
243        
244        
245         
246     },
247     onClick : function(e)
248     {
249         Roo.log('button on click ');
250         if(this.preventDefault){
251             e.preventDefault();
252         }
253         
254         this.fireEvent('click', this, e);
255     }
256     
257     
258 });
259
260