瀏覽代碼

added basic radio support

Emanuele Marchi 11 年之前
父節點
當前提交
d28fa72169
共有 3 個文件被更改,包括 33 次插入51 次删除
  1. 14 28
      build/js/bootstrap-switch.js
  2. 0 0
      build/js/bootstrap-switch.min.js
  3. 19 23
      src/coffee/bootstrap-switch.coffee

+ 14 - 28
build/js/bootstrap-switch.js

@@ -111,7 +111,7 @@
           return this.$element;
         }
         value = !!value;
-        this.$element.prop("checked", value).trigger("change", skip);
+        this.$element.prop("checked", value).trigger("change.bootstrapSwitch", skip);
         return this.$element;
       };
 
@@ -119,7 +119,7 @@
         if (this.options.disabled || this.options.readonly) {
           return this.$element;
         }
-        return this.$element.prop("checked", !this.options.state).trigger("change", skip);
+        return this.$element.prop("checked", !this.options.state).trigger("change.bootstrapSwitch", skip);
       };
 
       /*
@@ -128,9 +128,9 @@
         $element = @$element.not ":checked"
       
         if uncheck
-          $element.trigger "change", skip
+          $element.trigger "change.bootstrapSwitch", skip
         else
-          $element.prop("checked", not @$element.is ":checked").trigger "change", skip
+          $element.prop("checked", not @$element.is ":checked").trigger "change.bootstrapSwitch", skip
         @$element
       */
 
@@ -263,7 +263,7 @@
         var _this = this;
         return this.$element.on({
           "change.bootstrapSwitch": function(e, skip) {
-            var $radios, checked;
+            var checked;
             e.preventDefault();
             e.stopPropagation();
             e.stopImmediatePropagation();
@@ -273,12 +273,10 @@
             }
             _this.options.state = checked;
             _this.$wrapper.removeClass(checked ? "switch-off" : "switch-on").addClass(checked ? "switch-on" : "switch-off");
-            $radios = _this._radioSiblings();
-            console.log($radios);
-            if ($radios) {
-              $radios.prop("checked", !value).trigger("change", skip);
-            }
             if (!skip) {
+              if (_this.$element.is(":radio")) {
+                $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
+              }
               return _this.$element.trigger("switchChange", {
                 el: _this.$element,
                 value: checked
@@ -326,11 +324,11 @@
         var _this = this;
         this.$on.on("click.bootstrapSwitch", function(e) {
           _this.state(false);
-          return _this.$element.trigger("focus");
+          return _this.$element.trigger("focus.bootstrapSwitch");
         });
         return this.$off.on("click.bootstrapSwitch", function(e) {
           _this.state(true);
-          return _this.$element.trigger("focus");
+          return _this.$element.trigger("focus.bootstrapSwitch");
         });
       };
 
@@ -351,7 +349,7 @@
               percent = right;
             }
             _this.$div.css("margin-left", "" + (percent - right) + "%");
-            return _this.$element.trigger("focus");
+            return _this.$element.trigger("focus.bootstrapSwitch");
           },
           "mousedown.bootstrapSwitch": function(e) {
             if (_this.drag || _this.options.disabled || _this.options.readonly) {
@@ -361,14 +359,14 @@
             if (_this.options.animate) {
               _this.$wrapper.removeClass("switch-animate");
             }
-            return _this.$element.trigger("focus");
+            return _this.$element.trigger("focus.bootstrapSwitch");
           },
           "mouseup.bootstrapSwitch": function(e) {
             if (!_this.drag) {
               return;
             }
             _this.drag = false;
-            _this.$element.prop("checked", parseInt(_this.$div.css("margin-left"), 10) > -25).trigger("change");
+            _this.$element.prop("checked", parseInt(_this.$div.css("margin-left"), 10) > -25).trigger("change.bootstrapSwitch");
             _this.$div.css("margin-left", "");
             if (_this.options.animate) {
               return _this.$wrapper.addClass("switch-animate");
@@ -378,7 +376,7 @@
             e.preventDefault();
             e.stopImmediatePropagation();
             _this.toggleState();
-            return _this.$element.trigger("focus");
+            return _this.$element.trigger("focus.bootstrapSwitch");
           }
         });
       };
@@ -400,18 +398,6 @@
         }).data("bootstrap-switch", true);
       };
 
-      BootstrapSwitch.prototype._radioSiblings = function() {
-        var $elements;
-        if (!this.$element.is(":radio")) {
-          return false;
-        }
-        $elements = $("[name='" + (this.$element.attr('name')) + "']").not(this.$element);
-        if (!$elements.length) {
-          return false;
-        }
-        return $elements;
-      };
-
       return BootstrapSwitch;
 
     })();

File diff suppressed because it is too large
+ 0 - 0
build/js/bootstrap-switch.min.js


+ 19 - 23
src/coffee/bootstrap-switch.coffee

@@ -70,14 +70,13 @@
 
       value = not not value
 
-      @$element.prop("checked", value).trigger "change", skip
+      @$element.prop("checked", value).trigger "change.bootstrapSwitch", skip
       @$element
 
     toggleState: (skip) ->
       return @$element if @options.disabled or @options.readonly
 
-      @$element.prop("checked", not @options.state).trigger "change", skip
-
+      @$element.prop("checked", not @options.state).trigger "change.bootstrapSwitch", skip
 
     ###
     TODO: refactor
@@ -85,9 +84,9 @@
       $element = @$element.not ":checked"
 
       if uncheck
-        $element.trigger "change", skip
+        $element.trigger "change.bootstrapSwitch", skip
       else
-        $element.prop("checked", not @$element.is ":checked").trigger "change", skip
+        $element.prop("checked", not @$element.is ":checked").trigger "change.bootstrapSwitch", skip
       @$element
     ###
 
@@ -205,23 +204,24 @@
           .removeClass(if checked then "switch-off" else "switch-on")
           .addClass if checked then "switch-on" else "switch-off"
 
-          $radios = @_radioSiblings()
-          console.log $radios
-          $radios.prop("checked", not value).trigger "change", skip if $radios
+          unless skip
+            $("[name='#{@$element.attr('name')}']").not(@$element).prop("checked", false).trigger "change.bootstrapSwitch", true if @$element.is ":radio"
+            @$element.trigger "switchChange", el: @$element, value: checked
 
-          @$element.trigger "switchChange", el: @$element, value: checked if not skip
         "focus.bootstrapSwitch": (e) =>
           e.preventDefault()
           e.stopPropagation()
           e.stopImmediatePropagation()
 
           @$wrapper.addClass "switch-focused"
+
         "blur.bootstrapSwitch": (e) =>
           e.preventDefault()
           e.stopPropagation()
           e.stopImmediatePropagation()
 
           @$wrapper.removeClass "switch-focused"
+
         "keydown.bootstrapSwitch": (e) =>
           return if not e.which or @options.disabled or @options.readonly
 
@@ -248,10 +248,11 @@
     _handleHandlers: ->
       @$on.on "click.bootstrapSwitch", (e) =>
         @state false
-        @$element.trigger "focus"
+        @$element.trigger "focus.bootstrapSwitch"
+
       @$off.on "click.bootstrapSwitch", (e) =>
         @state true
-        @$element.trigger "focus"
+        @$element.trigger "focus.bootstrapSwitch"
 
     _labelHandlers: ->
       @$label.on
@@ -268,26 +269,29 @@
             percent = right
 
           @$div.css "margin-left", "#{percent - right}%"
-          @$element.trigger "focus"
+          @$element.trigger "focus.bootstrapSwitch"
+
         "mousedown.bootstrapSwitch": (e) =>
           return if @drag or @options.disabled or @options.readonly
 
           @drag = true
           @$wrapper.removeClass "switch-animate" if @options.animate
-          @$element.trigger "focus"
+          @$element.trigger "focus.bootstrapSwitch"
+
         "mouseup.bootstrapSwitch": (e) =>
           return unless @drag
 
           @drag = false
-          @$element.prop("checked", (parseInt(@$div.css("margin-left"), 10) > -25)).trigger "change"
+          @$element.prop("checked", (parseInt(@$div.css("margin-left"), 10) > -25)).trigger "change.bootstrapSwitch"
           @$div.css "margin-left", ""
           @$wrapper.addClass "switch-animate" if @options.animate
+
         "click.bootstrapSwitch": (e) =>
           e.preventDefault()
           e.stopImmediatePropagation()
 
           @toggleState()
-          @$element.trigger "focus"
+          @$element.trigger "focus.bootstrapSwitch"
 
     _formHandler: ->
       $form = @$element.closest "form"
@@ -304,14 +308,6 @@
         , 1
       .data "bootstrap-switch", true
 
-    _radioSiblings: ->
-      return false unless @$element.is ":radio"
-
-      $elements = $("[name='#{@$element.attr('name')}']").not @$element
-
-      return false unless $elements.length
-      $elements
-
   $.fn.extend bootstrapSwitch: (option, args...) ->
     ret = @
     @each ->

Some files were not shown because too many files changed in this diff