Sfoglia il codice sorgente

fix onchange event, it will be triggered only if needed

Mattia Larentis 12 anni fa
parent
commit
362de96b09
2 ha cambiato i file con 16 aggiunte e 15 eliminazioni
  1. 15 14
      static/js/bootstrapSwitch.js
  2. 1 1
      static/less/bootstrapSwitch.less

+ 15 - 14
static/js/bootstrapSwitch.js

@@ -1,5 +1,5 @@
 /* ============================================================
- * bootstrapSwitch v1.2 by Larentis Mattia @spiritualGuru
+ * bootstrapSwitch v1.3 by Larentis Mattia @spiritualGuru
  * http://www.larentis.eu/switch/
  * ============================================================
  * Licensed under the Apache License, Version 2.0
@@ -67,7 +67,7 @@
               .attr('for', $element.find('input').attr('id'));
 
             if (icon) {
-              $label.html('<i class="icon icon-'+icon+'"></i>');
+              $label.html('<i class="icon icon-' + icon + '"></i>');
             }
 
             $div = $element.find(':checkbox').wrap($('<div>')).parent().data('animated', false);
@@ -108,25 +108,26 @@
             });
 
             $element.find('input').on('change', function (e) {
-              var $element = $(this).parent();
+              var $this = $(this)
+                , $element = $this.parent()
+                , thisState = $this.is(':checked')
+                , state = $element.is('.switch-off');
 
               e.preventDefault();
-              // don't stop propagation of the
-              // change event, there's no need to do so
-              // and we don't want to break other modules
-              // that might depend on this event.
-              // e.stopImmediatePropagation();
 
               $element.css('left', '');
 
-              if ($(this).is(':checked'))
-                $element.removeClass('switch-off').addClass('switch-on');
-              else $element.removeClass('switch-on').addClass('switch-off');
+              if (state === thisState) {
 
-              if ($element.data('animated') !== false)
-                $element.addClass("switch-animate");
+                if (thisState)
+                  $element.removeClass('switch-off').addClass('switch-on');
+                else $element.removeClass('switch-on').addClass('switch-off');
 
-              $element.parent().trigger('switch-change', {'el': $(this), 'value': $(this).is(':checked')})
+                if ($element.data('animated') !== false)
+                  $element.addClass("switch-animate");
+
+                $element.parent().trigger('switch-change', {'el': $this, 'value': thisState})
+              }
             });
 
             $element.find('label').on('mousedown touchstart', function (e) {

+ 1 - 1
static/less/bootstrapSwitch.less

@@ -1,5 +1,5 @@
 /* ============================================================
- * bootstrapSwitch v1.2 by Larentis Mattia @spiritualGuru
+ * bootstrapSwitch v1.3 by Larentis Mattia @spiritualGuru
  * http://www.larentis.eu/switch/
  * ============================================================
  * Licensed under the Apache License, Version 2.0