namibia
90d6a07e7e4539815d3e24e48c81d37282fb4e71
namibia / public / js / vendor / jquery-ui.toggleSwitch.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
jQuery.fn.toggleSwitch = function (params) { var defaults = { highlight: true, width: 36, change: null }; var options = $.extend({}, defaults, params); $(this).each(function (i, item) { generateToggle(item); }); function generateToggle(selectObj) { // create containing element var $contain = $("<div />").addClass("ui-toggle-switch"); // generate labels $(selectObj).find("option").each(function (i, item) { $contain.append("<label>" + $(item).text() + "</label>"); }).end().addClass("ui-toggle-switch"); // generate slider with established options var $slider = $("<div />").slider({ min: 0, max: 100, animate: "fast", change: options.change, stop: function (e, ui) { var roundedVal = Math.round(ui.value / 100); var self = this; window.setTimeout(function () { toggleValue(self.parentNode, roundedVal); }, 11); }, range: (options.highlight && !$(selectObj).data("hideHighlight")) ? "max" : null }).width(options.width); // put slider in the middle $slider.insertAfter( $contain.children().eq(0) ); // bind interaction $contain.delegate("label", "click", function () { if ($(this).hasClass("ui-state-active")) { return; } var labelIndex = ($(this).is(":first-child")) ? 0 : 1; toggleValue(this.parentNode, labelIndex); }); function toggleValue(slideContain, index) { $(slideContain).find("label").eq(index).addClass("ui-state-active").siblings("label").removeClass("ui-state-active"); $(slideContain).parent().find("option").eq(index).attr("selected", true); $(slideContain).find(".ui-slider").slider("value", index * 100); } // initialise selected option $contain.find("label").eq(selectObj.selectedIndex).click(); // add to DOM $(selectObj).parent().append($contain); } }; |
Commits for namibia/public/js/vendor/jquery-ui.toggleSwitch.js
Revision | Author | Commited | Message |
---|---|---|---|
df0489 ... | Mark | Fri 14 Oct, 2016 10:01:00 +0000 | initial commit |