From 8ae3665fc788ddc125e504d1c3e8dd36e63d6e26 Mon Sep 17 00:00:00 2001 From: Erwin Matthijssen Date: Sat, 25 Jan 2014 18:59:48 +0100 Subject: [PATCH 1/2] Added two public methods. Toggle allows toggling the button via js. SilentToggle does the same only without triggering a change event. --- jquery.switchButton.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/jquery.switchButton.js b/jquery.switchButton.js index d7f49ed..fce73f9 100755 --- a/jquery.switchButton.js +++ b/jquery.switchButton.js @@ -246,13 +246,17 @@ this._toggleSwitch(); }, - _toggleSwitch: function() { + _toggleSwitch: function(skipChangeEvent) { this.options.checked = !this.options.checked; var newLeft = ""; if (this.options.checked) { // Update the underlying checkbox state this.element.prop("checked", true); - this.element.change(); + + // Trigger change event unless toggle is silent + if (!skipChangeEvent == true) { + this.element.change(); + } var dLeft = this.options.width - this.options.button_width; newLeft = "+=" + dLeft; @@ -275,7 +279,11 @@ else { // Update the underlying checkbox state this.element.prop("checked", false); - this.element.change(); + + // Trigger change event unless toggle is silent + if (!skipChangeEvent == true) { + this.element.change(); + } newLeft = "-1px"; // Update labels states @@ -295,8 +303,13 @@ } // Animate the switch this.button.animate({ left: newLeft }, 250, "easeInOutCubic"); + }, + silentToggle: function() { + this._toggleSwitch(true); + }, + toggle: function() { + this._toggleSwitch(false); } - }); -})(jQuery); +})(jQuery); \ No newline at end of file From 712feb354497225e279a96367eaf7bfc001c2ae5 Mon Sep 17 00:00:00 2001 From: Erwin Matthijssen Date: Sat, 25 Jan 2014 19:15:07 +0100 Subject: [PATCH 2/2] Added redraw function that brings button and checkbox back in sync. --- jquery.switchButton.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jquery.switchButton.js b/jquery.switchButton.js index fce73f9..c516cfb 100755 --- a/jquery.switchButton.js +++ b/jquery.switchButton.js @@ -309,7 +309,12 @@ }, toggle: function() { this._toggleSwitch(false); + }, + redraw: function() { + if (!this.element.prop("checked") == this.button_bg.hasClass("checked")) { + this._toggleSwitch(true); + } } }); -})(jQuery); \ No newline at end of file +})(jQuery);