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