Last active
December 12, 2015 06:49
-
-
Save isochronous/4732423 to your computer and use it in GitHub Desktop.
taitems toggleswitch converted to AMD-compliant widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* jQuery UI Toggle Switch | |
* | |
* http://taitems.tumblr.com/post/17853682273/jquery-ui-toggleswitch-ux-lab-005 | |
* | |
* Depends: | |
* jquery.ui.slider.js | |
*/ | |
(function (factory) { | |
if (typeof exports === 'object') { | |
module.exports = factory(require('jquery'), require('jqueryui/widget'), require('jqueryui/slider')); | |
} else if (typeof define === 'function' && define.amd) { | |
define(['jquery', 'jqueryui/widget', 'jqueryui/slider'], factory); | |
} else { | |
factory(jQuery); | |
} | |
}(function ($) { | |
"use strict"; | |
$.widget("triton.toggleSwitch", { | |
options: { | |
highlight: true, | |
width: 25, | |
change: null | |
}, | |
_create: function () { | |
// create containing element | |
this.nodes = {}; | |
var that = this; | |
var ctrl = this.nodes.control = $(this.element); | |
var $contain = this.nodes.container = $("<div />").addClass("ui-toggle-switch"); | |
var ctrlOptions = this.nodes.options = ctrl.find("option"); | |
// generate labels | |
ctrl.addClass("ui-toggle-switch"); | |
ctrlOptions.each(function (i, item) { | |
var $el = $(item); | |
var label = $("<label>", { | |
"data-value": $el.val() | |
}).text($el.text()); | |
$contain.append(label); | |
}); | |
this.nodes.labels = $contain.find("label"); | |
// generate slider with established options | |
var $slider = this.nodes.slider = $("<div>").slider({ | |
min: 0, | |
max: 100, | |
animate: "fast", | |
change: this.options.change, | |
stop: function (e, ui) { | |
var roundedVal = Math.round(ui.value / 100); | |
var boundToggle = that._toggleValue.bind(that); | |
window.setTimeout(function () { | |
boundToggle(roundedVal); | |
}, 11); | |
}, | |
range: (this.options.highlight && !ctrl.data("hideHighlight")) ? "max" : null | |
}).width(this.options.width); | |
// put slider in the middle | |
$slider.insertAfter( | |
$contain.children().eq(0) | |
); | |
this._addEventHandlers(); | |
// initialise selected option | |
$contain.find("label").eq(ctrl[0].selectedIndex).click(); | |
// add to DOM | |
$(ctrl).parent().append($contain); | |
}, | |
_toggleValue: function(index) { | |
var container = this.nodes.container; | |
container.find("label").eq(index).addClass("ui-state-active").siblings("label").removeClass("ui-state-active"); | |
container.parent().find("option").prop("selected", false).eq(index).prop("selected", true); | |
container.find(".ui-slider").slider("value", index * 100); | |
this.nodes.control.change(); | |
}, | |
_addEventHandlers: function () { | |
var labels = this.nodes.labels; | |
this._on(labels, { | |
click: function (event) { | |
var label = $(event.target); | |
if (label.hasClass("ui-state-active")) { | |
return; | |
} | |
var labelIndex = label.is(":first-child") ? 0 : 1; | |
this._toggleValue(labelIndex); | |
} | |
}); | |
}, | |
setValue: function (value) { | |
var matchedOption = this.nodes.labels.find('[data-value="' + value + '"]'); | |
var index; | |
if (matchedOption.length === 0) { | |
matchedOption = this.nodes.labels.find(':contains(' + value + ')'); | |
} | |
if (matchedOption.length) { | |
index = matchedOption.is(":first-child") ? 0 : 1; | |
this._toggleValue(index); | |
} | |
}, | |
getValue: function () { | |
var selected = this.nodes.options.find(":selected"); | |
if (selected.length) { | |
return selected.val(); | |
} | |
return null; | |
} | |
}); | |
return $; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment