Browse Source

removed click label click handler and managed the click behaviour within the other event handlers, fixed firefox double event triggering on spacebar press #316

LostCrew 11 năm trước cách đây
mục cha
commit
0780217bd6

+ 18 - 31
dist/js/bootstrap-switch.js

@@ -322,7 +322,6 @@
             return function(e, skip) {
               var checked;
               e.preventDefault();
-              e.stopPropagation();
               e.stopImmediatePropagation();
               checked = _this.$element.is(":checked");
               if (checked === _this.options.state) {
@@ -341,16 +340,12 @@
           "focus.bootstrapSwitch": (function(_this) {
             return function(e) {
               e.preventDefault();
-              e.stopPropagation();
-              e.stopImmediatePropagation();
               return _this.$wrapper.addClass("" + _this.options.baseClass + "-focused");
             };
           })(this),
           "blur.bootstrapSwitch": (function(_this) {
             return function(e) {
               e.preventDefault();
-              e.stopPropagation();
-              e.stopImmediatePropagation();
               return _this.$wrapper.removeClass("" + _this.options.baseClass + "-focused");
             };
           })(this),
@@ -360,19 +355,12 @@
                 return;
               }
               switch (e.which) {
-                case 32:
-                  e.preventDefault();
-                  e.stopPropagation();
-                  e.stopImmediatePropagation();
-                  return _this.toggleState();
                 case 37:
                   e.preventDefault();
-                  e.stopPropagation();
                   e.stopImmediatePropagation();
                   return _this.state(false);
                 case 39:
                   e.preventDefault();
-                  e.stopPropagation();
                   e.stopImmediatePropagation();
                   return _this.state(true);
               }
@@ -401,14 +389,18 @@
           "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
             return function(e) {
               var left, pageX, percent, right;
-              if (!_this.drag) {
+              if (!_this.isLabelDragging) {
                 return;
               }
               e.preventDefault();
+              _this.isLabelDragged = true;
               pageX = e.pageX || e.originalEvent.touches[0].pageX;
               percent = ((pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
               left = 25;
               right = 75;
+              if (_this.options.animate) {
+                _this.$wrapper.removeClass("" + _this.options.baseClass + "-animate");
+              }
               if (percent < left) {
                 percent = left;
               } else if (percent > right) {
@@ -420,42 +412,37 @@
           })(this),
           "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
             return function(e) {
-              if (_this.drag || _this.options.disabled || _this.options.readonly || _this.options.indeterminate) {
+              if (_this.isLabelDragging || _this.options.disabled || _this.options.readonly || _this.options.indeterminate) {
                 return;
               }
               e.preventDefault();
-              _this.drag = true;
-              if (_this.options.animate) {
-                _this.$wrapper.removeClass("" + _this.options.baseClass + "-animate");
-              }
+              _this.isLabelDragging = true;
               return _this.$element.trigger("focus.bootstrapSwitch");
             };
           })(this),
           "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
             return function(e) {
-              if (!_this.drag) {
+              if (!_this.isLabelDragging) {
                 return;
               }
               e.preventDefault();
-              _this.drag = false;
-              _this.$element.prop("checked", parseInt(_this.$container.css("margin-left"), 10) > -(_this.$container.width() / 6)).trigger("change.bootstrapSwitch");
-              _this.$container.css("margin-left", "");
-              if (_this.options.animate) {
-                return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+              if (_this.isLabelDragged) {
+                _this.isLabelDragged = false;
+                _this.state(parseInt(_this.$container.css("margin-left"), 10) > -(_this.$container.width() / 6));
+                _this.$container.css("margin-left", "");
+                if (_this.options.animate) {
+                  _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+                }
+              } else {
+                _this.state(!_this.options.state);
               }
+              return _this.isLabelDragging = false;
             };
           })(this),
           "mouseleave.bootstrapSwitch": (function(_this) {
             return function(e) {
               return _this.$label.trigger("mouseup.bootstrapSwitch");
             };
-          })(this),
-          "click.bootstrapSwitch": (function(_this) {
-            return function(e) {
-              e.preventDefault();
-              _this.toggleState();
-              return _this.$element.trigger("focus.bootstrapSwitch");
-            };
           })(this)
         });
       };

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/js/bootstrap-switch.min.js


+ 14 - 32
src/coffee/bootstrap-switch.coffee

@@ -231,7 +231,6 @@ do ($ = window.jQuery, window) ->
       @$element.on
         "change.bootstrapSwitch": (e, skip) =>
           e.preventDefault()
-          e.stopPropagation()
           e.stopImmediatePropagation()
 
           checked = @$element.is ":checked"
@@ -253,37 +252,23 @@ do ($ = window.jQuery, window) ->
 
         "focus.bootstrapSwitch": (e) =>
           e.preventDefault()
-          e.stopPropagation()
-          e.stopImmediatePropagation()
-
           @$wrapper.addClass "#{@options.baseClass}-focused"
 
         "blur.bootstrapSwitch": (e) =>
           e.preventDefault()
-          e.stopPropagation()
-          e.stopImmediatePropagation()
-
           @$wrapper.removeClass "#{@options.baseClass}-focused"
 
         "keydown.bootstrapSwitch": (e) =>
           return if not e.which or @options.disabled or @options.readonly or @options.indeterminate
 
           switch e.which
-            when 32
-              e.preventDefault()
-              e.stopPropagation()
-              e.stopImmediatePropagation()
-
-              @toggleState()
             when 37
               e.preventDefault()
-              e.stopPropagation()
               e.stopImmediatePropagation()
 
               @state false
             when 39
               e.preventDefault()
-              e.stopPropagation()
               e.stopImmediatePropagation()
 
               @state true
@@ -300,15 +285,17 @@ do ($ = window.jQuery, window) ->
     _labelHandlers: ->
       @$label.on
         "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (e) =>
-          return unless @drag
+          return unless @isLabelDragging
 
           e.preventDefault()
 
+          @isLabelDragged = true
           pageX = e.pageX or e.originalEvent.touches[0].pageX
           percent = ((pageX - @$wrapper.offset().left) / @$wrapper.width()) * 100
           left = 25
           right = 75
 
+          @$wrapper.removeClass "#{@options.baseClass}-animate" if @options.animate
           if percent < left
             percent = left
           else if percent > right
@@ -318,35 +305,30 @@ do ($ = window.jQuery, window) ->
           @$element.trigger "focus.bootstrapSwitch"
 
         "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (e) =>
-          return if @drag or @options.disabled or @options.readonly or @options.indeterminate
+          return if @isLabelDragging or @options.disabled or @options.readonly or @options.indeterminate
 
           e.preventDefault()
 
-          @drag = true
-          @$wrapper.removeClass "#{@options.baseClass}-animate" if @options.animate
+          @isLabelDragging = true
           @$element.trigger "focus.bootstrapSwitch"
 
         "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (e) =>
-          return unless @drag
+          return unless @isLabelDragging
 
           e.preventDefault()
 
-          @drag = false
-          @$element
-          .prop("checked", parseInt(@$container.css("margin-left"), 10) > -(@$container.width() / 6))
-          .trigger "change.bootstrapSwitch"
-          @$container.css "margin-left", ""
-          @$wrapper.addClass "#{@options.baseClass}-animate" if @options.animate
+          if @isLabelDragged
+            @isLabelDragged = false
+            @state parseInt(@$container.css("margin-left"), 10) > -(@$container.width() / 6)
+            @$container.css "margin-left", ""
+            @$wrapper.addClass "#{@options.baseClass}-animate" if @options.animate
+          else
+            @state not @options.state
+          @isLabelDragging = false
 
         "mouseleave.bootstrapSwitch": (e) =>
           @$label.trigger "mouseup.bootstrapSwitch"
 
-        "click.bootstrapSwitch": (e) =>
-          e.preventDefault()
-
-          @toggleState()
-          @$element.trigger "focus.bootstrapSwitch"
-
     _formHandler: ->
       $form = @$element.closest "form"
 

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác