1
0
Fork 0
mirror of synced 2024-07-05 22:51:24 +12:00
appwrite/public/scripts/views/forms/switch.js

38 lines
911 B
JavaScript
Raw Normal View History

(function(window) {
"use strict";
2019-05-09 18:54:39 +12:00
window.ls.container.get("view").add({
selector: "data-forms-switch",
controller: function(element) {
let input = window.document.createElement("input");
input.type = "checkbox";
2020-03-15 17:54:55 +13:00
input.className = "button switch";
2019-05-09 18:54:39 +12:00
let syncA = function() {
2020-03-17 07:41:56 +13:00
let value = input.checked ? "true" : "false"
let old = element.value;
element.value = value;
if(value !== old) {
element.dispatchEvent(new Event('change'));
}
};
2019-05-09 18:54:39 +12:00
let syncB = function() {
2020-03-17 07:41:56 +13:00
input.checked = (element.value === "true");
};
2019-05-09 18:54:39 +12:00
input.addEventListener("input", syncA);
input.addEventListener("change", syncA);
2019-05-09 18:54:39 +12:00
element.addEventListener("input", syncB);
element.addEventListener("change", syncB);
2019-05-09 18:54:39 +12:00
syncA();
2019-05-09 18:54:39 +12:00
element.parentNode.insertBefore(input, element);
}
});
})(window);