Uncommited changes synced
[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 | primary | secondary | success | info | warning | danger | link ) default
14  * @cfg {String} badge_weight (default | primary | secondary | success | info | warning | danger | link ) default (same as button)
15  * @cfg {Boolean} outline default false (except for weight=default which emulates old behaveiour with an outline)
16  * @cfg {String} size ( lg | sm | xs)
17  * @cfg {String} tag ( a | input | submit)
18  * @cfg {String} href empty or href
19  * @cfg {Boolean} disabled default false;
20  * @cfg {Boolean} isClose default false;
21  * @cfg {String} glyphicon (| 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)
22  * @cfg {String} badge text for badge
23  * @cfg {String} theme (default|glow)  
24  * @cfg {Boolean} inverse dark themed version
25  * @cfg {Boolean} toggle is it a slidy toggle button
26  * @cfg {Boolean} pressed (true|false) default null - if the button ahs active state
27  * @cfg {String} ontext text for on slidy toggle state
28  * @cfg {String} offtext text for off slidy toggle state
29  * @cfg {Boolean} preventDefault  default true (stop click event triggering the URL if it's a link.)
30  * @cfg {Boolean} removeClass remove the standard class..
31  * @cfg {String} target  target for a href. (_self|_blank|_parent|_top| other)
32  * 
33  * @constructor
34  * Create a new button
35  * @param {Object} config The config object
36  */
37
38
39 Roo.bootstrap.Button = function(config){
40     Roo.bootstrap.Button.superclass.constructor.call(this, config);
41     this.weightClass = ["btn-default btn-outline-secondary", 
42                        "btn-primary", 
43                        "btn-success", 
44                        "btn-info", 
45                        "btn-warning",
46                        "btn-danger",
47                        "btn-link"
48                       ],  
49     this.addEvents({
50         // raw events
51         /**
52          * @event click
53          * When a butotn is pressed
54          * @param {Roo.bootstrap.Button} btn
55          * @param {Roo.EventObject} e
56          */
57         "click" : true,
58          /**
59          * @event toggle
60          * After the button has been toggles
61          * @param {Roo.bootstrap.Button} btn
62          * @param {Roo.EventObject} e
63          * @param {boolean} pressed (also available as button.pressed)
64          */
65         "toggle" : true
66     });
67 };
68
69 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
70     html: false,
71     active: false,
72     weight: '',
73     badge_weight: '',
74     outline : false,
75     size: '',
76     tag: 'button',
77     href: '',
78     disabled: false,
79     isClose: false,
80     glyphicon: '',
81     badge: '',
82     theme: 'default',
83     inverse: false,
84     
85     toggle: false,
86     ontext: 'ON',
87     offtext: 'OFF',
88     defaulton: true,
89     preventDefault: true,
90     removeClass: false,
91     name: false,
92     target: false,
93      
94     pressed : null,
95      
96     
97     getAutoCreate : function(){
98         
99         var cfg = {
100             tag : 'button',
101             cls : 'roo-button',
102             html: ''
103         };
104         
105         if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
106             throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
107             this.tag = 'button';
108         } else {
109             cfg.tag = this.tag;
110         }
111         cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
112         
113         if (this.toggle == true) {
114             cfg={
115                 tag: 'div',
116                 cls: 'slider-frame roo-button',
117                 cn: [
118                     {
119                         tag: 'span',
120                         'data-on-text':'ON',
121                         'data-off-text':'OFF',
122                         cls: 'slider-button',
123                         html: this.offtext
124                     }
125                 ]
126             };
127             
128             if (['default', 'secondary' , 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
129                 cfg.cls += ' '+this.weight;
130             }
131             
132             return cfg;
133         }
134         
135         if (this.isClose) {
136             cfg.cls += ' close';
137             
138             cfg["aria-hidden"] = true;
139             
140             cfg.html = "&times;";
141             
142             return cfg;
143         }
144         
145          
146         if (this.theme==='default') {
147             cfg.cls = 'btn roo-button';
148             
149             //if (this.parentType != 'Navbar') {
150             this.weight = this.weight.length ?  this.weight : 'default';
151             //}
152             if (['default', 'primary', 'secondary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
153                 
154                 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
155                 var weight = this.weight == 'default' ? 'secondary' : this.weight;
156                 cfg.cls += ' btn-' + outline + weight;
157                 if (this.weight == 'default') {
158                     // BC
159                     cfg.cls += ' btn-' + this.weight;
160                 }
161             }
162         } else if (this.theme==='glow') {
163             
164             cfg.tag = 'a';
165             cfg.cls = 'btn-glow roo-button';
166             
167             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
168                 
169                 cfg.cls += ' ' + this.weight;
170             }
171         }
172    
173         
174         if (this.inverse) {
175             this.cls += ' inverse';
176         }
177         
178         
179         if (this.active || this.pressed === true) {
180             cfg.cls += ' active';
181         }
182         
183         if (this.disabled) {
184             cfg.disabled = 'disabled';
185         }
186         
187         if (this.items) {
188             Roo.log('changing to ul' );
189             cfg.tag = 'ul';
190             this.glyphicon = 'caret';
191         }
192         
193         cfg.cls += this.size.length ? (' btn-' + this.size) : '';
194          
195         //gsRoo.log(this.parentType);
196         if (this.parentType === 'Navbar' && !this.parent().bar) {
197             Roo.log('changing to li?');
198             
199             cfg.tag = 'li';
200             
201             cfg.cls = '';
202             cfg.cn =  [{
203                 tag : 'a',
204                 cls : 'roo-button',
205                 html : this.html,
206                 href : this.href || '#'
207             }];
208             if (this.menu) {
209                 cfg.cn[0].html = this.html  + ' <span class="caret"></span>';
210                 cfg.cls += ' dropdown';
211             }   
212             
213             delete cfg.html;
214             
215         }
216         
217        cfg.cls += this.parentType === 'Navbar' ?  ' navbar-btn' : '';
218         
219         if (this.glyphicon) {
220             cfg.html = ' ' + cfg.html;
221             
222             cfg.cn = [
223                 {
224                     tag: 'span',
225                     cls: 'glyphicon glyphicon-' + this.glyphicon
226                 }
227             ];
228         }
229         
230         if (this.badge) {
231             cfg.html += ' ';
232             
233             cfg.tag = 'a';
234             
235 //            cfg.cls='btn roo-button';
236             
237             cfg.href=this.href;
238             
239             var value = cfg.html;
240             
241             if(this.glyphicon){
242                 value = {
243                             tag: 'span',
244                             cls: 'glyphicon glyphicon-' + this.glyphicon,
245                             html: this.html
246                         };
247                 
248             }
249             var bw = this.badge_weight.length ? this.badge_weight :
250                 (this.weight.length ? this.weight : 'secondary');
251             bw = bw == 'default' ? 'secondary' : bw;
252             
253             cfg.cn = [
254                 value,
255                 {
256                     tag: 'span',
257                     cls: 'badge badge-' + bw,
258                     html: this.badge
259                 }
260             ];
261             
262             cfg.html='';
263         }
264         
265         if (this.menu) {
266             cfg.cls += ' dropdown';
267             cfg.html = typeof(cfg.html) != 'undefined' ?
268                     cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
269         }
270         
271         if (cfg.tag !== 'a' && this.href !== '') {
272             throw "Tag must be a to set href.";
273         } else if (this.href.length > 0) {
274             cfg.href = this.href;
275         }
276         
277         if(this.removeClass){
278             cfg.cls = '';
279         }
280         
281         if(this.target){
282             cfg.target = this.target;
283         }
284         
285         return cfg;
286     },
287     initEvents: function() {
288        // Roo.log('init events?');
289 //        Roo.log(this.el.dom);
290         // add the menu...
291         
292         if (typeof (this.menu) != 'undefined') {
293             this.menu.parentType = this.xtype;
294             this.menu.triggerEl = this.el;
295             this.addxtype(Roo.apply({}, this.menu));
296         }
297
298
299        if (this.el.hasClass('roo-button')) {
300             this.el.on('click', this.onClick, this);
301        } else {
302             this.el.select('.roo-button').on('click', this.onClick, this);
303        }
304        
305        if(this.removeClass){
306            this.el.on('click', this.onClick, this);
307        }
308        
309        this.el.enableDisplayMode();
310         
311     },
312     onClick : function(e)
313     {
314         if (this.disabled) {
315             return;
316         }
317         
318         Roo.log('button on click ');
319         if(this.preventDefault){
320             e.preventDefault();
321         }
322         
323         if (this.pressed === true || this.pressed === false) {
324             this.toggleActive(e);
325         }
326         
327         
328         this.fireEvent('click', this, e);
329     },
330     
331     /**
332      * Enables this button
333      */
334     enable : function()
335     {
336         this.disabled = false;
337         this.el.removeClass('disabled');
338     },
339     
340     /**
341      * Disable this button
342      */
343     disable : function()
344     {
345         this.disabled = true;
346         this.el.addClass('disabled');
347     },
348      /**
349      * sets the active state on/off, 
350      * @param {Boolean} state (optional) Force a particular state
351      */
352     setActive : function(v) {
353         
354         this.el[v ? 'addClass' : 'removeClass']('active');
355         this.pressed = v;
356     },
357      /**
358      * toggles the current active state 
359      */
360     toggleActive : function(e)
361     {
362         this.setActive(!this.pressed);
363         this.fireEvent('toggle', this, e, !this.pressed);
364     },
365      /**
366      * get the current active state
367      * @return {boolean} true if it's active
368      */
369     isActive : function()
370     {
371         return this.el.hasClass('active');
372     },
373     /**
374      * set the text of the first selected button
375      */
376     setText : function(str)
377     {
378         this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
379     },
380     /**
381      * get the text of the first selected button
382      */
383     getText : function()
384     {
385         return this.el.select('.roo-button-text',true).first().dom.innerHTML;
386     },
387     
388     setWeight : function(str)
389     {
390         this.el.removeClass(this.weightClass);
391         this.weight = str;
392         var outline = this.outline ? 'outline-' : '';
393         if (str == 'default') {
394             this.el.addClass('btn-default btn-outline-secondary');        
395             return;
396         }
397         this.el.addClass('btn-' + outline + str);        
398     }
399     
400     
401 });
402
403