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