DefaultJavascriptCode

Contains a updateSetting(input) function which automatically sends changes to the server.

enum DefaultJavascriptCode = q{<script id="_setting_script_"> var timeouts = {}; function updateSetting(input) { clearTimeout(timeouts[input]); timeouts[input] = setTimeout(function() { var form = input; while (form && form.tagName != "FORM") form = form.parentElement; var submit = form.querySelector ? form.querySelector("input[type=submit]") : undefined; if (submit) submit.disabled = false; name = input.name; function attachError(elem, content) { var label = elem; while (label && label.tagName != "LABEL") label = label.parentElement; if (label) label.classList.add("error"); var err = document.createElement("span"); err.className = "error"; err.textContent = content; err.style.padding = "4px"; elem.parentElement.insertBefore(err, elem.nextSibling); setTimeout(function() { err.parentElement.removeChild(err); }, 2500); } var label = input; while (label && label.tagName != "LABEL") label = label.parentElement; if (label) label.classList.remove("error"); var isFlags = false; var flagLabel = label; while (flagLabel) { if (flagLabel.classList.contains("flags")) { isFlags = true; break; } flagLabel = flagLabel.parentElement; } var valid = input.checkValidity ? input.checkValidity() : true; if (!valid) { attachError(input, input.title || "Please fill out this input correctly."); return; } var stillRequesting = true; setTimeout(function () { if (stillRequesting) input.disabled = true; }, 100); var xhr = new XMLHttpRequest(); var method = "{method}"; var action = "{action}"; var query = "_field=" + encodeURIComponent(name); if (input.type != "checkbox" || input.checked) query += '&' + encodeURIComponent(name) + '=' + encodeURIComponent(input.value); else if (isFlags) query += '&' + encodeURIComponent(name) + "=!" + encodeURIComponent(input.value); if (method != "POST") action += query; xhr.onload = function () { if (xhr.status != 200 && xhr.status != 204) attachError(input, input.title || "Please fill out this field correctly."); else { submit.value = "Saved!"; setTimeout(function() { submit.value = "Save"; }, 3000); } stillRequesting = false; input.disabled = false; }; xhr.onerror = function () { stillRequesting = false; input.disabled = false; submit.disabled = false; }; xhr.open(method, action); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); if (method == "POST") xhr.send(query); else xhr.send(); submit.disabled = true; }, 50); } function unlockForm(input) { var form = input; while (form && form.tagName != "FORM") form = form.parentElement; form.querySelector("input[type=submit]").disabled = false; } (document.currentScript || document.getElementById("_setting_script_")).previousSibling.querySelector("input[type=submit]").disabled = true; </script>};

Meta