Roo/bootstrap/Button.js
[roojs1] / Roo / bootstrap / Button.js
index bec1d57..fda7977 100644 (file)
@@ -40,10 +40,17 @@ Roo.bootstrap.Button = function(config){
         // raw events
         /**
          * @event click
-         * The raw click event for the entire grid.
+         * When a butotn is pressed
          * @param {Roo.EventObject} e
          */
-        "click" : true
+        "click" : true,
+         /**
+         * @event toggle
+         * After the button has been toggles
+         * @param {Roo.EventObject} e
+         * @param {boolean} pressed (also available as button.pressed)
+         */
+        "toggle" : true
     });
 };
 
@@ -70,6 +77,10 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
     name: false,
     target: false,
     
+    
+    pressed : null,
+    
+    
     getAutoCreate : function(){
         
         var cfg = {
@@ -86,7 +97,7 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
         }
         cfg.html = this.html || cfg.html;
         
-        if (this.toggle===true) {
+        if (this.toggle == true) {
             cfg={
                 tag: 'div',
                 cls: 'slider-frame roo-button',
@@ -122,9 +133,9 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
         if (this.theme==='default') {
             cfg.cls = 'btn roo-button';
             
-            if (this.parentType != 'Navbar') {
-                this.weight = this.weight.length ?  this.weight : 'default';
-            }
+            //if (this.parentType != 'Navbar') {
+            this.weight = this.weight.length ?  this.weight : 'default';
+            //}
             if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
                 
                 cfg.cls += ' btn-' + this.weight;
@@ -150,10 +161,22 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
             cfg.cls += ' active';
         }
         
+        if (this.disabled) {
+            cfg.disabled = 'disabled';
+        }
+        
+        if (this.items) {
+            Roo.log('changing to ul' );
+            cfg.tag = 'ul';
+            this.glyphicon = 'caret';
+        }
+        
         cfg.cls += this.size.length ? (' btn-' + this.size) : '';
          
         //gsRoo.log(this.parentType);
-        if (this.parentType === 'Navbar') {
+        if (this.parentType === 'Navbar' && !this.parent().bar) {
+            Roo.log('changing to li?');
+            
             cfg.tag = 'li';
             
             cfg.cls = '';
@@ -170,21 +193,9 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
             
             delete cfg.html;
             
-        } 
-        else if (this.menu) {
-            cfg.cls += ' dropdown';
-            cfg.html = this.html  + ' <span class="caret"></span>';
         }
         
-        if (this.disabled) {
-            cfg.disabled = 'disabled';
-        }
-        //????
-        if (this.items) {
-            Roo.log('changing to ul' );
-            cfg.tag = 'ul';
-            this.glyphicon = 'caret';
-        }
+       cfg.cls += this.parentType === 'Navbar' ?  ' navbar-btn' : '';
         
         if (this.glyphicon) {
             cfg.html = ' ' + cfg.html;
@@ -229,6 +240,11 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
             cfg.html='';
         }
         
+        if (this.menu) {
+            cfg.cls += ' dropdown';
+            cfg.html = typeof(cfg.html) != 'undefined' ? cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
+        }
+        
         if (cfg.tag !== 'a' && this.href !== '') {
             throw "Tag must be a to set href.";
         } else if (this.href.length > 0) {
@@ -259,15 +275,39 @@ Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component,  {
     },
     onClick : function(e)
     {
+        if (this.disabled) {
+            return;
+        }
+        
         Roo.log('button on click ');
         if(this.preventDefault){
             e.preventDefault();
         }
+        if (this.pressed === true || this.pressed === false) {
+            this.pressed = !this.pressed;
+            this.el[this.pressed ? 'addClass' : 'removeClass']('active');
+            this.fireEvent('toggle', this, e, this.pressed);
+        }
+        
         
         this.fireEvent('click', this, e);
+    },
+    enable : function()
+    {
+        this.disabled = false;
+        this.el.removeClass('disabled');
+    },
+    disable : function()
+    {
+        this.disabled = true;
+        this.el.addClass('disabled');
+    },
+    setActive : function(v) {
+        
+        this[v ? 'addClass' : 'removeClass']('active');
+        
+        
     }
-    
-    
 });
 
  
\ No newline at end of file