Ver Fonte

Merge pull request #1 from BdMdesigN/Test

Merge Test branch to Master
BdMdesigN há 12 anos atrás
pai
commit
fa9a0daa4e

+ 80 - 0
README.md

@@ -1,6 +1,9 @@
 Bootstrap-switch
 ========================
 
+You can now also use radio buttons as switches.
+
+
 Demo
 ----
 http://www.larentis.eu/switch/
@@ -27,59 +30,136 @@ lessc static/less/bootstrapSwitch.less static/stylesheets/bootstrapSwitch.css
 
 Basic Example
 -------------
+checkboxes:
+
 ``` html
 <div class="switch">
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch">
+    <input type="radio">
+</div>
+```
+
+
 Large, small or mini
 --------------------
+checkboxes:
+
 ``` html
 <div class="switch switch-large">  <!-- switch-large, switch-small or switch-mini -->
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch switch-large">  <!-- switch-large, switch-small or switch-mini -->
+    <input type="radio">
+</div>
+```
+
+
 Colors
 ------
+checkboxes:
+
 ``` html
 <div class="switch" data-on="danger" data-off="warning">  <!-- primary, info, success, warning and danger -->
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch" data-on="danger" data-off="warning">  <!-- primary, info, success, warning and danger -->
+    <input type="radio">
+</div>
+```
+
+
 Animation
 ---------
+checkboxes:
+
 ``` html
 <div class="switch" data-animated="false">
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch" data-animated="false">
+    <input type="radio">
+</div>
+```
+
+
 Text
 -----
+checkboxes:
+
 ``` html
 <div class="switch" data-on-label="SI" data-off-label="NO">
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch" data-on-label="SI" data-off-label="NO">
+    <input type="radio">
+</div>
+```
+
+
 HTML Text
 ----------
+checkboxes:
+
 ``` html
 <div class="switch" data-on-label="<i class='icon-ok icon-white'></i>" data-off-label="<i class='icon-remove'></i>">
     <input type="checkbox">
 </div>
 ```
 
+radioboxes:
+
+``` html
+<div class="switch" data-on-label="<i class='icon-ok icon-white'></i>" data-off-label="<i class='icon-remove'></i>">
+    <input type="radio">
+</div>
+```
+
+
 Initial values
 --------------
+checkboxes:
+
 ``` html
 <div class="switch">
     <input type="checkbox" checked="checked" disabled="disabled">
 </div>
 ```
+radioboxes:
+
+
+``` html
+<div class="switch">
+    <input type="radio" checked="checked" disabled="disabled">
+</div>
+```
+
 
 Event handler
 -------------

+ 154 - 1
examples/index.html

@@ -2,8 +2,9 @@
 <html lang="en">
 <head>
     <meta charset="utf-8">
-    <title>Bootstrap switch - by Mattia Larentis</title>
+    <title>Bootstrap switch - by Mattia Larentis, enhanced by BdMdesigN aka Peter Stein</title>
     <meta name="author" content="Mattia Larentis">
+    <meta name="subauthor" content="Peter Stein">
     <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8;" />
 
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
@@ -303,6 +304,139 @@ $('#disable-switch').bootstrapSwitch('setActive', false);  // true || false</pre
     </div>
 </div>
 
+
+<div class="row-fluid">
+    <div class="span12">
+        <h3>Radio <small>- radioboxes 1</small></h3>
+
+        <div class="bs-docs-example">
+               <div class="control-group exemple1">
+                  <br /><br />
+                    <div id="radio1" class="controls radio1">
+                        <label class="control-label" for="yes">Yes</label>
+                        <div id="yes" class="switch yes" data-on-label="Yes" data-off-label="No" data-on="success" data-off="danger">
+                            <input type="radio" name="yes" class="check demo-1" value="1" checked="checked" />
+                        </div>
+                        <label class="control-label" for="no">No</label>
+                        <div id="no" class="switch no" data-on-label="Yes" data-off-label="No" data-on="success" data-off="danger">
+                            <input type="radio" name="no" class="check demo-1" value="0" />
+                        </div>
+                    </div>
+                </div>
+
+            <div class="clearfix"></div>
+
+        </div>
+        <pre class="prettyprint linenums">
+    &lt;div class="control-group exemple1">
+      &lt;br />&lt;br />
+        &lt;div id="radio1" class="controls radio1">
+            &lt;label class="control-label" for="yes">Yes&lt;/label>
+            &lt;div id="yes" class="switch yes" data-on-label="Yes" data-off-label="No" data-on="success" data-off="danger">
+                &lt;input type="radio" name="yes" class="check demo-1" value="1" checked="checked" />
+            &lt;/div>
+            &lt;label class="control-label" for="no">No&lt;/label>
+            &lt;div id="no" class="switch no" data-on-label="Yes" data-off-label="No" data-on="success" data-off="danger">
+                &lt;input type="radio" name="no" class="check demo-1" value="0" />
+            &lt;/div>
+        &lt;/div>
+    &lt;/div>
+&lt;script>
+$(document).ready(function() {
+    $('#yes').on('click', function () {
+        if($('input[name=yes]').val() == 1) {
+        $('.no').bootstrapSwitch('toggleState');
+        }
+    });
+    $('#no').on('click', function () {
+        if($('input[name=no]').val() == 0) {
+        $('.yes').bootstrapSwitch('toggleState');
+        }
+    });
+});
+&lt;/script></pre>
+    </div>
+</div>
+
+
+<div class="row-fluid">
+    <div class="span12">
+        <h3>Radio <small>- radioboxes 2</small></h3>
+
+        <div class="bs-docs-example">
+            <div id="exemple2" class="control-group exemple2">
+              <br /><br />
+                <div class="control-group" style="margin-bottom: 2%;">
+                    <div style="width: 10%;float: left;">
+                      <label class="control-label" for="option1">Option 1</label>
+                    </div>
+                    <div class="controls radio2">
+                        <div id="option1" class="switch option1">
+                            <input id="radio1" type="radio" name="radio" value="option1" />
+                        </div>
+                    </div>
+                </div>
+                <div class="control-group" style="margin-bottom: 2%;">
+                    <div style="width: 10%;float: left;">
+                      <label class="control-label" for="option2">Option 2</label>
+                    </div>
+                    <div class="controls radio2">
+                        <div id="option2" class="switch option2">
+                            <input id="radio2" type="radio" name="radio" value="option2" />
+                        </div>
+                    </div>
+                </div>
+                <div class="control-group" style="margin-bottom: 2%;">
+                    <div style="width: 10%;float: left;">
+                      <label class="control-label" for="option3">Option 3</label>
+                    </div>
+                    <div class="controls radio2">
+                        <div id="option3" class="switch option3">
+                            <input id="radio3" type="radio" name="radio" value="option3" />
+                        </div>
+                    </div>
+                </div>
+            <div class="clearfix"></div>
+
+        </div>
+        <pre class="prettyprint linenums">
+    &lt;div id="exemple2" class="control-group exemple2">
+        &lt;br />&lt;br />
+        &lt;div class="control-group" style="margin-bottom: 2%;">
+          &lt;div style="width: 10%;float: left;">
+              &lt;label class="control-label" for="option1">Option 1</label>
+          &lt;/div>
+            &lt;div class="controls radio2">
+              &lt;div id="option1" class="switch option1">
+                &lt;input id="radio1" type="radio" name="radio" value="option1" />
+              &lt;/div>
+            &lt;/div>
+        &lt;/div>
+        &lt;div class="control-group" style="margin-bottom: 2%;">
+            &lt;div style="width: 10%;float: left;">
+              &lt;label class="control-label" for="option2">Option 2&lt;/label>
+            &lt;/div>
+            &lt;div class="controls radio2">
+              &lt;div id="option2" class="switch option2">
+                &lt;input id="radio2" type="radio" name="radio" value="option2" />
+              &lt;/div>
+            &lt;/div>
+        &lt;/div>
+        &lt;div class="control-group" style="margin-bottom: 2%;">
+            &lt;div style="width: 10%;float: left;">
+              &lt;label class="control-label" for="option3">Option 3&lt;/label>
+            &lt;/div>
+            &lt;div class="controls radio2">
+              &lt;div id="option3" class="switch option3">
+                &lt;input id="radio3" type="radio" name="radio" value="option3" />
+              &lt;/div>
+            &lt;/div>
+        &lt;/div>
+    &lt;/div></pre>
+    </div>
+</div>
+
+
 <div class="row-fluid">
     <div class="span12">
         <h3>Form <small>- try to use tab and space</small></h3>
@@ -411,6 +545,7 @@ $('#disable-switch').bootstrapSwitch('setActive', false);  // true || false</pre
 <script src="http://twitter.github.com/bootstrap/assets/js/google-code-prettify/prettify.js"></script>
 <script src="../static/js/bootstrapSwitch.js"></script>
 <script>
+$(document).ready(function() {
     window.prettyPrint && prettyPrint();
     $('#mySwitch').on('switch-change', function (e, data) {
         var $el = $(data.el)
@@ -455,6 +590,24 @@ $('#disable-switch').bootstrapSwitch('setActive', false);  // true || false</pre
     $('#btn-activate-switch').on('click', function () {
         $('#disable-switch').bootstrapSwitch('setActive', true);
     });
+
+    $('#yes').on('click', function () {
+
+        if($('input[name=yes]').val() == 1) {
+
+        $('.no').bootstrapSwitch('toggleState');
+        }
+    });
+
+    $('#no').on('click', function () {
+
+        if($('input[name=no]').val() == 0) {
+
+        $('.yes').bootstrapSwitch('toggleState');
+        }
+    });
+
+});
 </script>
 </body>
 </html>

+ 260 - 0
static/js/bootstrapSwitch-1.3.1.js

@@ -0,0 +1,260 @@
+/* ============================================================
+ * bootstrapSwitch v1.3.1 by Larentis Mattia @spiritualGuru
+ * http://www.larentis.eu/switch/
+ * 
+ * enhanced by BdMdesigN
+ * http://www.bdmdesign.org
+ * ============================================================
+ * Licensed under the Apache License, Version 2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * ============================================================ */
+
+!function ($) {
+  "use strict";
+
+  $.fn['bootstrapSwitch'] = function (method) {
+    var methods = {
+      init: function () {
+        return this.each(function () {
+            var $element = $(this)
+              , $div
+              , $switchLeft
+              , $switchRight
+              , $label
+              , myClasses = ""
+              , classes = $element.attr('class')
+              , color
+              , moving
+              , onLabel = "ON"
+              , offLabel = "OFF"
+              , icon = false;
+
+            $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
+              if (classes.indexOf(el) >= 0)
+                myClasses = el;
+            });
+
+            $element.addClass('has-switch');
+
+            if ($element.data('on') !== undefined)
+              color = "switch-" + $element.data('on');
+
+            if ($element.data('on-label') !== undefined)
+              onLabel = $element.data('on-label');
+
+            if ($element.data('off-label') !== undefined)
+              offLabel = $element.data('off-label');
+
+            if ($element.data('icon') !== undefined)
+              icon = $element.data('icon');
+
+            $switchLeft = $('<span>')
+              .addClass("switch-left")
+              .addClass(myClasses)
+              .addClass(color)
+              .html(onLabel);
+
+            color = '';
+            if ($element.data('off') !== undefined)
+              color = "switch-" + $element.data('off');
+
+            $switchRight = $('<span>')
+              .addClass("switch-right")
+              .addClass(myClasses)
+              .addClass(color)
+              .html(offLabel);
+
+            $label = $('<label>')
+              .html("&nbsp;")
+              .addClass(myClasses)
+              .attr('for', $element.find('input').attr('id'));
+
+            if (icon) {
+              $label.html('<i class="icon icon-' + icon + '"></i>');
+            }
+
+            $div = $element.find('input[type=radio],input[type=checkbox]').wrap($('<div>')).parent().data('animated', false);
+
+            if ($element.data('animated') !== false)
+              $div.addClass('switch-animate').data('animated', true);
+
+            $div
+              .append($switchLeft)
+              .append($label)
+              .append($switchRight);
+
+            $element.find('>div').addClass(
+              $element.find('input[type=radio],input[type=checkbox]').is(':checked') ? 'switch-on' : 'switch-off'
+            );
+
+            if ($element.find('input[type=radio],input[type=checkbox]').is(':disabled'))
+              $(this).addClass('deactivate');
+
+            var changeStatus = function ($this) {
+              $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
+            };
+
+            $element.on('keydown', function (e) {
+              if (e.keyCode === 32) {
+                e.stopImmediatePropagation();
+                e.preventDefault();
+                changeStatus($(e.target).find('span:first'));
+              }
+            });
+
+            $switchLeft.on('click', function (e) {
+              changeStatus($(this));
+            });
+
+            $switchRight.on('click', function (e) {
+              changeStatus($(this));
+            });
+
+            $element.find('input[type=radio],input[type=checkbox]').on('change', function (e, skipOnChange) {
+              var $this = $(this)
+                , $element = $this.parent()
+                , thisState = $this.is(':checked')
+                , state = $element.is('.switch-off');
+
+              e.preventDefault();
+
+              $element.css('left', '');
+
+              if (state === thisState) {
+
+                if (thisState)
+                  $element.removeClass('switch-off').addClass('switch-on');
+                else $element.removeClass('switch-on').addClass('switch-off');
+
+                if ($element.data('animated') !== false)
+                  $element.addClass("switch-animate");
+
+                if (typeof skipOnChange === 'boolean' && skipOnChange)
+                  return;
+
+                $element.parent().trigger('switch-change', {'el': $this, 'value': thisState})
+              }
+            });
+
+            $element.find('label').on('mousedown touchstart', function (e) {
+              var $this = $(this);
+              moving = false;
+
+              e.preventDefault();
+              e.stopImmediatePropagation();
+
+              $this.closest('div').removeClass('switch-animate');
+
+              if ($this.closest('.has-switch').is('.deactivate'))
+                $this.unbind('click');
+              else {
+                $this.on('mousemove touchmove', function (e) {
+                  var $element = $(this).closest('.switch')
+                    , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
+                    , percent = (relativeX / $element.width()) * 100
+                    , left = 25
+                    , right = 75;
+
+                  moving = true;
+
+                  if (percent < left)
+                    percent = left;
+                  else if (percent > right)
+                    percent = right;
+
+                  $element.find('>div').css('left', (percent - right) + "%")
+                });
+
+                $this.on('click touchend', function (e) {
+                  var $this = $(this)
+                    , $target = $(e.target)
+                    , $myRadioCheckBox = $target.siblings('input[type=radio],input[type=checkbox]');
+
+                  e.stopImmediatePropagation();
+                  e.preventDefault();
+
+                  $this.unbind('mouseleave');
+
+                  if (moving)
+
+                    $myRadioCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
+
+                  else
+                    $myRadioCheckBox.prop("checked", !$myRadioCheckBox.is(":checked"));
+
+                  moving = false;
+                  $myRadioCheckBox.trigger('change');
+                });
+
+                $this.on('mouseleave', function (e) {
+                  var $this = $(this)
+                    , $myInputBox = $this.siblings('input[type=radio],input[type=checkbox]');
+
+                  e.preventDefault();
+                  e.stopImmediatePropagation();
+
+                  $this.unbind('mouseleave');
+                  $this.trigger('mouseup');
+
+                  $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
+                });
+
+                $this.on('mouseup', function (e) {
+                  e.stopImmediatePropagation();
+                  e.preventDefault();
+
+                  $(this).unbind('mousemove');
+                });
+              }
+            });
+          }
+        );
+      },
+      toggleActivation: function () {
+        $(this).toggleClass('deactivate');
+      },
+      isActive: function () {
+        return !$(this).hasClass('deactivate');
+      },
+      setActive: function (active) {
+        if (active)
+          $(this).removeClass('deactivate');
+        else $(this).addClass('deactivate');
+      },
+      toggleState: function (skipOnChange) {
+        var $input = $(this).find('input[type=radio],input[type=checkbox]');
+        $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
+      },
+      setState: function (value, skipOnChange) {
+        $(this).find('input[type=radio],input[type=checkbox]').prop('checked', value).trigger('change', skipOnChange);
+      },
+      status: function () {
+        return $(this).find('input[type=radio],input[type=checkbox]').is(':checked');
+      },
+      destroy: function () {
+        var $div = $(this).find('div')
+          , $inputbox;
+
+        $div.find(':not(input:radio),:not(input:checkbox').remove();
+
+        $inputbox = $div.children();
+        $inputbox.unwrap().unwrap();
+
+        $inputbox.unbind('change');
+
+        return $inputbox;
+      }
+    };
+
+    if (methods[method])
+      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
+    else if (typeof method === 'object' || !method)
+      return methods.init.apply(this, arguments);
+    else
+      $.error('Method ' + method + ' does not exist!');
+  };
+}(jQuery);
+
+$(function () {
+  $('.switch')['bootstrapSwitch']();
+});

+ 26 - 20
static/js/bootstrapSwitch.js

@@ -1,11 +1,16 @@
 /* ============================================================
  * bootstrapSwitch v1.3 by Larentis Mattia @spiritualGuru
  * http://www.larentis.eu/switch/
+ * 
+ * bootstrapSwitch v1.3.1 by Stein, Peter @BdMdesigN
+ * 
+ * enhanced by BdMdesigN
+ * http://www.bdmdesign.org
  * ============================================================
  * Licensed under the Apache License, Version 2.0
  * http://www.apache.org/licenses/LICENSE-2.0
  * ============================================================ */
-
+ 
 !function ($) {
   "use strict";
 
@@ -70,7 +75,7 @@
               $label.html('<i class="icon icon-' + icon + '"></i>');
             }
 
-            $div = $element.find(':checkbox').wrap($('<div>')).parent().data('animated', false);
+            $div = $element.find('input[type=radio],input[type=checkbox]').wrap($('<div>')).parent().data('animated', false);
 
             if ($element.data('animated') !== false)
               $div.addClass('switch-animate').data('animated', true);
@@ -81,10 +86,10 @@
               .append($switchRight);
 
             $element.find('>div').addClass(
-              $element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
+              $element.find('input[type=radio],input[type=checkbox]').is(':checked') ? 'switch-on' : 'switch-off'
             );
 
-            if ($element.find('input').is(':disabled'))
+            if ($element.find('input[type=radio],input[type=checkbox]').is(':disabled'))
               $(this).addClass('deactivate');
 
             var changeStatus = function ($this) {
@@ -107,7 +112,7 @@
               changeStatus($(this));
             });
 
-            $element.find('input').on('change', function (e, skipOnChange) {
+            $element.find('input[type=radio],input[type=checkbox]').on('change', function (e, skipOnChange) {
               var $this = $(this)
                 , $element = $this.parent()
                 , thisState = $this.is(':checked')
@@ -165,7 +170,7 @@
                 $this.on('click touchend', function (e) {
                   var $this = $(this)
                     , $target = $(e.target)
-                    , $myCheckBox = $target.siblings('input');
+                    , $myRadioCheckBox = $target.siblings('input[type=radio],input[type=checkbox]');
 
                   e.stopImmediatePropagation();
                   e.preventDefault();
@@ -173,16 +178,17 @@
                   $this.unbind('mouseleave');
 
                   if (moving)
-                    $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
-                  else $myCheckBox.prop("checked", !$myCheckBox.is(":checked"));
+                    $myRadioCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
+                  else
+                    $myRadioCheckBox.prop("checked", !$myRadioCheckBox.is(":checked"));
 
                   moving = false;
-                  $myCheckBox.trigger('change');
+                  $myRadioCheckBox.trigger('change');
                 });
 
                 $this.on('mouseleave', function (e) {
                   var $this = $(this)
-                    , $myCheckBox = $this.siblings('input');
+                    , $myInputBox = $this.siblings('input[type=radio],input[type=checkbox]');
 
                   e.preventDefault();
                   e.stopImmediatePropagation();
@@ -190,7 +196,7 @@
                   $this.unbind('mouseleave');
                   $this.trigger('mouseup');
 
-                  $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
+                  $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
                 });
 
                 $this.on('mouseup', function (e) {
@@ -216,27 +222,27 @@
         else $(this).addClass('deactivate');
       },
       toggleState: function (skipOnChange) {
-        var $input = $(this).find('input:checkbox');
+        var $input = $(this).find('input[type=radio],input[type=checkbox]');
         $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
       },
       setState: function (value, skipOnChange) {
-        $(this).find('input:checkbox').prop('checked', value).trigger('change', skipOnChange);
+        $(this).find('input[type=radio],input[type=checkbox]').prop('checked', value).trigger('change', skipOnChange);
       },
       status: function () {
-        return $(this).find('input:checkbox').is(':checked');
+        return $(this).find('input[type=radio],input[type=checkbox]').is(':checked');
       },
       destroy: function () {
         var $div = $(this).find('div')
-          , $checkbox;
+          , $inputbox;
 
-        $div.find(':not(input:checkbox)').remove();
+        $div.find(':not(input:radio),:not(input:checkbox').remove();
 
-        $checkbox = $div.children();
-        $checkbox.unwrap().unwrap();
+        $inputbox = $div.children();
+        $inputbox.unwrap().unwrap();
 
-        $checkbox.unbind('change');
+        $inputbox.unbind('change');
 
-        return $checkbox;
+        return $inputbox;
       }
     };
 

+ 6 - 0
static/less/bootstrapSwitch.less

@@ -1,6 +1,11 @@
 /* ============================================================
  * bootstrapSwitch v1.3 by Larentis Mattia @spiritualGuru
  * http://www.larentis.eu/switch/
+ * 
+ * bootstrapSwitch v1.3.1 by Stein, Peter @BdMdesigN
+ * 
+ * enhanced by BdMdesigN
+ * http://www.bdmdesign.org
  * ============================================================
  * Licensed under the Apache License, Version 2.0
  * http://www.apache.org/licenses/LICENSE-2.0
@@ -58,6 +63,7 @@
       left: 0%;
     }
   }
+  input[type=radio],
   input[type=checkbox] {
     //debug
     display: none;

+ 7 - 1
static/stylesheets/bootstrapSwitch.css

@@ -1,6 +1,11 @@
 /* ============================================================
- * bootstrapSwitch v1.2 by Larentis Mattia @spiritualGuru
+ * bootstrapSwitch v1.3 by Larentis Mattia @spiritualGuru
  * http://www.larentis.eu/switch/
+ * 
+ * bootstrapSwitch v1.3.1 by Stein, Peter @BdMdesigN
+ * 
+ * enhanced by BdMdesigN
+ * http://www.bdmdesign.org
  * ============================================================
  * Licensed under the Apache License, Version 2.0
  * http://www.apache.org/licenses/LICENSE-2.0
@@ -60,6 +65,7 @@
 .has-switch > div.switch-on {
   left: 0%;
 }
+.has-switch input[type=radio],
 .has-switch input[type=checkbox] {
   display: none;
 }