Skip to content

Commit

Permalink
fixed missing width on init (close #376), fixed indeterminate removal…
Browse files Browse the repository at this point in the history
…, added examples,
  • Loading branch information
Emanuele Marchi committed Nov 15, 2014
1 parent 5920b51 commit 3a1caad
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 189 deletions.
71 changes: 42 additions & 29 deletions dist/js/bootstrap-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
if (this.options.indeterminate) {
this.$element.prop("indeterminate", true);
}
this._width();
this._initWidth();
this._containerPosition(this.options.state, (function(_this) {
return function() {
if (_this.options.animate) {
Expand All @@ -134,7 +134,7 @@
if (this.options.disabled || this.options.readonly) {
return this.$element;
}
if (this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {
if (this.options.state && !this.options.radioAllOff && this.$element.is(":radio")) {
return this.$element;
}
if (this.options.indeterminate) {
Expand Down Expand Up @@ -179,9 +179,10 @@
return this.options.animate;
}
value = !!value;
this.options.animate = value;
this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-animate");
return this.$element;
if (value === this.options.animate) {
return this.$element;
}
return this.toggleAnimate();
};

BootstrapSwitch.prototype.toggleAnimate = function() {
Expand All @@ -195,10 +196,10 @@
return this.options.disabled;
}
value = !!value;
this.options.disabled = value;
this.$element.prop("disabled", value);
this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-disabled");
return this.$element;
if (value === this.options.disabled) {
return this.$element;
}
return this.toggleDisabled();
};

BootstrapSwitch.prototype.toggleDisabled = function() {
Expand All @@ -213,10 +214,10 @@
return this.options.readonly;
}
value = !!value;
this.options.readonly = value;
this.$element.prop("readonly", value);
this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-readonly");
return this.$element;
if (value === this.options.readonly) {
return this.$element;
}
return this.toggleReadonly();
};

BootstrapSwitch.prototype.toggleReadonly = function() {
Expand All @@ -231,36 +232,29 @@
return this.options.indeterminate;
}
value = !!value;
this.options.indeterminate = value;
this.$element.prop("indeterminate", value);
this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-indeterminate");
this._containerPosition();
return this.$element;
if (value === this.options.indeterminate) {
return this.$element;
}
return this.toggleIndeterminate();
};

BootstrapSwitch.prototype.toggleIndeterminate = function() {
this.options.indeterminate = !this.options.indeterminate;
this.$element.prop("indeterminate", !this.options.indeterminate);
this.$element.prop("indeterminate", this.options.indeterminate);
this.$wrapper.toggleClass("" + this.options.baseClass + "-indeterminate");
this._containerPosition();
return this.$element;
};

BootstrapSwitch.prototype.inverse = function(value) {
var $off, $on;
if (typeof value === "undefined") {
return this.options.inverse;
}
value = !!value;
this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-inverse");
$on = this.$on.clone(true);
$off = this.$off.clone(true);
this.$on.replaceWith($off);
this.$off.replaceWith($on);
this.$on = $off;
this.$off = $on;
this.options.inverse = value;
return this.$element;
if (value === this.options.inverse) {
return this.$element;
}
return this.toggleInverse();
};

BootstrapSwitch.prototype.toggleInverse = function() {
Expand Down Expand Up @@ -377,6 +371,10 @@
if (typeof value === "undefined") {
return this.options.radioAllOff;
}
value = !!value;
if (value === this.options.radioAllOff) {
return this.$element;
}
this.options.radioAllOff = value;
return this.$element;
};
Expand Down Expand Up @@ -438,6 +436,21 @@
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;
Expand Down
2 changes: 1 addition & 1 deletion dist/js/bootstrap-switch.min.js

Large diffs are not rendered by default.

38 changes: 18 additions & 20 deletions docs/js/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$(function() {
var $window = $(window);
var sectionTop = $('.top').outerHeight() + 20;
var $createDestroy = $('#switch-create-destroy');

// initialize highlight.js
hljs.initHighlightingOnLoad();
Expand All @@ -27,33 +28,29 @@ $(function() {
});

// initialize all the inputs
$('input[type="checkbox"],[type="radio"]')
.not('#create-switch')
.not('#events-switch')
.not('#switch-modal')
.bootstrapSwitch();
$('input[type="checkbox"], input[type="radio"]:not("#switch-create-destroy, #switch-modal")').bootstrapSwitch();

$('[data-get]').on("click", function() {
var type = $(this).data('get');
$('[data-switch-get]').on("click", function() {
var type = $(this).data('switch-get');

alert($('#switch-' + type).bootstrapSwitch(type));
});

$('[data-set]').on('click', function() {
var type = $(this).data('set');
$('[data-switch-set]').on('click', function() {
var type = $(this).data('switch-set');

$('#switch-' + type).bootstrapSwitch(type, $(this).data('value'));
$('#switch-' + type).bootstrapSwitch(type, $(this).data('switch-value'));
});

$('[data-toggle]').on('click', function() {
var type = $(this).data('toggle');
$('[data-switch-toggle]').on('click', function() {
var type = $(this).data('switch-toggle');

$('#switch-' + type).bootstrapSwitch('toggle' + type.charAt(0).toUpperCase() + type.slice(1));
});

$('[data-set-value]').on('input', function(event) {
$('[data-switch-set-value]').on('input', function(event) {
event.preventDefault();
var type = $(this).data('set-value');
var type = $(this).data('switch-set-value');
var value = $.trim($(this).val());

if ($(this).data('value') == value) {
Expand All @@ -63,11 +60,12 @@ $(function() {
$('#switch-' + type).bootstrapSwitch(type, value);
});

$('#modal-switch')
.on("shown.bs.modal", function() {
$('#switch-modal').bootstrapSwitch();
})
.on("hidden.bs.modal", function() {
$('#switch-modal').bootstrapSwitch('destroy');
$('[data-switch-create-destroy]').on('click', function() {
var isSwitch = $createDestroy.data('bootstrap-switch');

$createDestroy.bootstrapSwitch(isSwitch ? 'destroy' : null);
$(this).button(isSwitch ? 'reset' : 'destroy');
});

$('#modal-switch');
});
Loading

0 comments on commit 3a1caad

Please sign in to comment.