Sfoglia il codice sorgente

fixed missing delayed call of container position if element is not visible on init #383

Emanuele Marchi 10 anni fa
parent
commit
97b28eab76

+ 11 - 20
dist/js/bootstrap-switch.js

@@ -27,6 +27,7 @@
     var BootstrapSwitch;
     BootstrapSwitch = (function() {
       function BootstrapSwitch(element, options) {
+        var initInterval;
         if (options == null) {
           options = {};
         }
@@ -109,14 +110,19 @@
         if (this.options.indeterminate) {
           this.$element.prop("indeterminate", true);
         }
-        this._initWidth();
-        this._containerPosition(this.options.state, (function(_this) {
+        initInterval = window.setInterval((function(_this) {
           return function() {
-            if (_this.options.animate) {
-              return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+            if (_this.$wrapper.is(":visible")) {
+              _this._width();
+              _this._containerPosition(null, function() {
+                if (_this.options.animate) {
+                  return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+                }
+              });
+              return window.clearInterval(initInterval);
             }
           };
-        })(this));
+        })(this), 50);
         this._elementHandlers();
         this._handleHandlers();
         this._labelHandlers();
@@ -436,21 +442,6 @@
         return this.$wrapper.width(this._handleWidth + this._labelWidth);
       };
 
-      BootstrapSwitch.prototype._initWidth = function() {
-        var widthInterval;
-        if (this.$wrapper.is(":visible")) {
-          return this._width();
-        }
-        return widthInterval = window.setInterval((function(_this) {
-          return function() {
-            if (_this.$wrapper.is(":visible")) {
-              _this._width();
-              return window.clearInterval(widthInterval);
-            }
-          };
-        })(this), 50);
-      };
-
       BootstrapSwitch.prototype._containerPosition = function(state, callback) {
         if (state == null) {
           state = this.options.state;

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


+ 8 - 15
src/coffee/bootstrap-switch.coffee

@@ -64,12 +64,14 @@ do ($ = window.jQuery, window) ->
       # indeterminate state
       @$element.prop "indeterminate", true  if @options.indeterminate
 
-      # normalize handles width
-      @_initWidth()
-
-      # set container position
-      @_containerPosition @options.state, =>
-        @$wrapper.addClass "#{@options.baseClass}-animate"  if @options.animate
+      # normalize handles width and set container position
+      initInterval = window.setInterval =>
+        if @$wrapper.is ":visible"
+          @_width()
+          @_containerPosition null, =>
+            @$wrapper.addClass "#{@options.baseClass}-animate"  if @options.animate
+          window.clearInterval initInterval
+      , 50
 
       # initialise handlers
       @_elementHandlers()
@@ -329,15 +331,6 @@ do ($ = window.jQuery, window) ->
       @$container.width (@_handleWidth * 2) + @_labelWidth
       @$wrapper.width @_handleWidth + @_labelWidth
 
-    _initWidth: ->
-      return @_width()  if @$wrapper.is ":visible"
-
-      widthInterval = window.setInterval =>
-        if @$wrapper.is ":visible"
-          @_width()
-          window.clearInterval widthInterval
-      , 50
-
     _containerPosition: (state = @options.state, callback) ->
       @$container
       .css "margin-left", =>

+ 11 - 20
test/bootstrap-switch.js

@@ -27,6 +27,7 @@
     var BootstrapSwitch;
     BootstrapSwitch = (function() {
       function BootstrapSwitch(element, options) {
+        var initInterval;
         if (options == null) {
           options = {};
         }
@@ -109,14 +110,19 @@
         if (this.options.indeterminate) {
           this.$element.prop("indeterminate", true);
         }
-        this._initWidth();
-        this._containerPosition(this.options.state, (function(_this) {
+        initInterval = window.setInterval((function(_this) {
           return function() {
-            if (_this.options.animate) {
-              return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+            if (_this.$wrapper.is(":visible")) {
+              _this._width();
+              _this._containerPosition(null, function() {
+                if (_this.options.animate) {
+                  return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
+                }
+              });
+              return window.clearInterval(initInterval);
             }
           };
-        })(this));
+        })(this), 50);
         this._elementHandlers();
         this._handleHandlers();
         this._labelHandlers();
@@ -436,21 +442,6 @@
         return this.$wrapper.width(this._handleWidth + this._labelWidth);
       };
 
-      BootstrapSwitch.prototype._initWidth = function() {
-        var widthInterval;
-        if (this.$wrapper.is(":visible")) {
-          return this._width();
-        }
-        return widthInterval = window.setInterval((function(_this) {
-          return function() {
-            if (_this.$wrapper.is(":visible")) {
-              _this._width();
-              return window.clearInterval(widthInterval);
-            }
-          };
-        })(this), 50);
-      };
-
       BootstrapSwitch.prototype._containerPosition = function(state, callback) {
         if (state == null) {
           state = this.options.state;

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