Bladeren bron

restored touch events

Emanuele Marchi 11 jaren geleden
bovenliggende
commit
c9a45db667
3 gewijzigde bestanden met toevoegingen van 19 en 25 verwijderingen
  1. 8 13
      build/js/bootstrap-switch.js
  2. 0 0
      build/js/bootstrap-switch.min.js
  3. 11 12
      src/coffee/bootstrap-switch.coffee

+ 8 - 13
build/js/bootstrap-switch.js

@@ -345,13 +345,14 @@
 
       BootstrapSwitch.prototype._labelHandlers = function() {
         return this.$label.on({
-          "mousemove.bootstrapSwitch": (function(_this) {
+          "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
             return function(e) {
               var left, percent, right;
               if (!_this.drag) {
                 return;
               }
-              percent = ((e.pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
+              e.preventDefault();
+              percent = (((e.pageX || e.originalEvent.touches[0].pageX) - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
               left = 25;
               right = 75;
               if (percent < left) {
@@ -363,11 +364,12 @@
               return _this.$element.trigger("focus.bootstrapSwitch");
             };
           })(this),
-          "mousedown.bootstrapSwitch": (function(_this) {
+          "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
             return function(e) {
               if (_this.drag || _this.options.disabled || _this.options.readonly) {
                 return;
               }
+              e.preventDefault();
               _this.drag = true;
               if (_this.options.animate) {
                 _this.$wrapper.removeClass("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
@@ -375,13 +377,14 @@
               return _this.$element.trigger("focus.bootstrapSwitch");
             };
           })(this),
-          "mouseup.bootstrapSwitch": (function(_this) {
+          "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
             return function(e) {
               if (!_this.drag) {
                 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.$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 + "-" + _this.options.animateModifierClass);
@@ -392,14 +395,6 @@
             return function(e) {
               return _this.$label.trigger("mouseup.bootstrapSwitch");
             };
-          })(this),
-          "click.bootstrapSwitch": (function(_this) {
-            return function(e) {
-              e.preventDefault();
-              e.stopImmediatePropagation();
-              _this.toggleState();
-              return _this.$element.trigger("focus.bootstrapSwitch");
-            };
           })(this)
         });
       };

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


+ 11 - 12
src/coffee/bootstrap-switch.coffee

@@ -255,10 +255,12 @@ do ($ = window.jQuery, window) ->
 
     _labelHandlers: ->
       @$label.on
-        "mousemove.bootstrapSwitch": (e) =>
+        "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (e) =>
           return unless @drag
 
-          percent = ((e.pageX - @$wrapper.offset().left) / @$wrapper.width()) * 100
+          e.preventDefault()
+
+          percent = (((e.pageX or e.originalEvent.touches[0].pageX) - @$wrapper.offset().left) / @$wrapper.width()) * 100
           left = 25
           right = 75
 
@@ -270,31 +272,28 @@ do ($ = window.jQuery, window) ->
           @$container.css "margin-left", "#{percent - right}%"
           @$element.trigger "focus.bootstrapSwitch"
 
-        "mousedown.bootstrapSwitch": (e) =>
+        "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (e) =>
           return if @drag or @options.disabled or @options.readonly
 
+          e.preventDefault()
+
           @drag = true
           @$wrapper.removeClass "#{@options.baseClass}-#{@options.animateModifierClass}" if @options.animate
           @$element.trigger "focus.bootstrapSwitch"
 
-        "mouseup.bootstrapSwitch": (e) =>
+        "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (e) =>
           return unless @drag
 
+          e.preventDefault()
+
           @drag = false
-          @$element.prop("checked", parseInt(@$container.css("margin-left"), 10) < -(@$container.width() / 6)).trigger "change.bootstrapSwitch"
+          @$element.prop("checked", parseInt(@$container.css("margin-left"), 10) > -(@$container.width() / 6)).trigger "change.bootstrapSwitch"
           @$container.css "margin-left", ""
           @$wrapper.addClass "#{@options.baseClass}-#{@options.animateModifierClass}" if @options.animate
 
         "mouseleave.bootstrapSwitch": (e) =>
           @$label.trigger "mouseup.bootstrapSwitch"
 
-        "click.bootstrapSwitch": (e) =>
-          e.preventDefault()
-          e.stopImmediatePropagation()
-
-          @toggleState()
-          @$element.trigger "focus.bootstrapSwitch"
-
     _formHandler: ->
       $form = @$element.closest "form"
 

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