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