Skip to content

Commit 8424cf6

Browse files
committed
Added support for silent toggle (Issue #7)
1 parent 1a29543 commit 8424cf6

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9-
## [3.6.0](https://github.com/gitbrent/bootstrap4-toggle/tree/v3.6.0) 2019-??-??
9+
## [3.6.0](https://github.com/gitbrent/bootstrap4-toggle/tree/v3.6.0) 2019-10-17
1010
### [Full Changelog](https://github.com/gitbrent/bootstrap4-toggle/compare/v3.5.0...v3.6.0)
1111
### Added
12+
- Added option to change toggle without triggering onChange event (silent toggle) [\#7](https://github.com/gitbrent/bootstrap4-toggle/issue/7) ([aswin1980](https://github.com/aswin1980))
1213
- Added accessibility properties to labels [\#11](https://github.com/gitbrent/bootstrap4-toggle/issue/11) ([aproquot](https://github.com/aproquot))
1314
### Changed
1415
- Fixed URLs in js and css file top comment [\#5](https://github.com/gitbrent/bootstrap4-toggle/issue/5) ([wilecoyte78](https://github.com/wilecoyte78))

index.html

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="description" content="Bootstrap 4 Toggle is a bootstrap plugin that converts checkboxes into responsive toggles.">
77
<meta name="keywords" content="bootstrap, toggle, switch, bootstrap-toggle, bootstrap-switch, bootstrap-checkbox">
88
<meta name="author" content="https://github.com/gitbrent">
9-
<meta name="version" content="v3.5.0">
9+
<meta name="version" content="v3.6.0">
1010
<meta name="robots" content="index, follow">
1111
<meta name="revisit-after" content="1 month">
1212
<title>Bootstrap 4 Toggle Switch Button Checkbox</title>
@@ -452,8 +452,11 @@ <h2>Events</h2>
452452
<hr>
453453

454454
<h3 class="text-secondary mt-4">Event Propagation</h3>
455-
<p><span class="label label-primary">Note</span> All events are propagated to and from input element to the toggle. </p>
456-
<p>You should listen to events from the <code>&lt;input type="checkbox"></code> directly rather than look for custom events.</p>
455+
<p>
456+
<div class="label label-primary">Notes</div>
457+
&bull;&nbsp;All events are propagated to and from input element to the toggle.<br>
458+
&bull;&nbsp;Listen for events on the <code>&lt;input type="checkbox"></code> directly as the toggle stays synced with the input.
459+
</p>
457460
<div class="example">
458461
<input id="toggle-event" type="checkbox" data-toggle="toggle">
459462
<div id="console-event"></div>
@@ -465,6 +468,33 @@ <h3 class="text-secondary mt-4">Event Propagation</h3>
465468
})
466469
</script>
467470
</div>
471+
<h3 class="text-secondary mt-4">Stopping Event Propagation</h3>
472+
<p>
473+
Passing <code>true</code> to the on/off methods will enable the silent option to prevent the control from propagating the change event in
474+
cases where you want to update the controls on/off state, but do not want to fire the onChange event.
475+
</p>
476+
<div class="example">
477+
<input id="toggle-silent" type="checkbox" data-toggle="toggle" onchange="$('#chgConsole').text(new Date().toISOString()+' ... change event fired!')">
478+
<button class="btn btn-success" onclick="toggleApiOnSilent()" >On by API (silent)</button>
479+
<button class="btn btn-success" onclick="toggleApiOffSilent()">Off by API (silent)</button>
480+
<button class="btn btn-warning" onclick="toggleApiOnNotSilent()">On by API (not silent)</button>
481+
<button class="btn btn-warning" onclick="toggleApiOffNotSilent()">On by API (not silent)</button>
482+
<script>
483+
function toggleApiOnSilent() {
484+
$('#toggle-silent').bootstrapToggle('on', true);
485+
}
486+
function toggleApiOffSilent() {
487+
$('#toggle-silent').bootstrapToggle('off', true);
488+
}
489+
function toggleApiOnNotSilent() {
490+
$('#toggle-silent').bootstrapToggle('on');
491+
}
492+
function toggleApiOffNotSilent() {
493+
$('#toggle-silent').bootstrapToggle('off');
494+
}
495+
</script>
496+
<pre class="mb-0"><code id="chgConsole" class="text-info"></code></pre>
497+
</div>
468498

469499
<h3 class="text-secondary mt-4">API vs Input</h3>
470500
<p>This also means that using the API or Input to trigger events will work both ways.</p>

js/bootstrap4-toggle.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,17 @@
145145
// ========================
146146

147147
function Plugin(option) {
148+
var optArg = Array.prototype.slice.call( arguments, 1 )[0]
149+
148150
return this.each(function () {
149151
var $this = $(this)
150152
var data = $this.data('bs.toggle')
151153
var options = typeof option == 'object' && option
152154

153155
if (!data) $this.data('bs.toggle', (data = new Toggle(this, options)))
154-
if (typeof option == 'string' && data[option]) data[option]()
156+
if (typeof option === 'string' && data[option] && typeof optArg === 'boolean') data[option](optArg)
157+
else if (typeof option === 'string' && data[option]) data[option]()
158+
//else if (option && !data[option]) console.log('bootstrap-toggle: error: method `'+ option +'` does not exist!');
155159
})
156160
}
157161

0 commit comments

Comments
 (0)