lib/super_settings/application/scripts.js in super_settings-2.0.0 vs lib/super_settings/application/scripts.js in super_settings-2.0.1
- old
+ new
@@ -230,13 +230,16 @@
return row
}
// Create a table row with form elements for creating a new setting.
- function newSettingRow() {
+ function newSettingRow(key) {
+ if (!key) {
+ key = "";
+ }
const randomId = "new" + Math.floor((Math.random() * 0xFFFFFFFFFFFFFF)).toString(16);
- const setting = {id: randomId, key: "", value: "", value_type: "string", new_record: true}
+ const setting = {id: randomId, key: key, value: "", value_type: "string", new_record: true}
row = editSettingRow(setting);
return row;
}
// Add a setting table row the table of settings.
@@ -454,10 +457,37 @@
}
});
return found;
}
+ // Find a setting by key.
+ function findSettingByKey(key) {
+ let found = null;
+ activeSettings.forEach(function(setting) {
+ if (setting.key === key) {
+ found = setting;
+ return;
+ }
+ });
+ return found;
+ }
+
+ // Add a new setting.
+ function addSetting(key) {
+ const row = addRowToTable(newSettingRow(key));
+ row.querySelector(".super-settings-key input").focus();
+ }
+
+ function editSetting(setting) {
+ const row = addRowToTable(editSettingRow(setting));
+ if (row.querySelector(".super-settings-value .js-date-input")) {
+ row.querySelector(".super-settings-value .js-date-input").focus();
+ } else {
+ row.querySelector(".super-settings-value .js-setting-value").focus();
+ }
+ }
+
/*** Event Listeners ***/
// Listener for showing the setting history modal.
function showHistoryModal(event) {
event.preventDefault();
@@ -540,27 +570,21 @@
const date = new Date(Date.parse(dateValue + "T" + timeValue));
parentNode.querySelector(".js-setting-value").value = date.toISOString();
}
// Listener for the add setting button.
- function addSetting(event) {
+ function addSettingListener(event) {
event.preventDefault();
- const row = addRowToTable(newSettingRow());
- row.querySelector(".super-settings-key input").focus();
+ addSetting();
}
// Listener for the edit setting button.
- function editSetting(event) {
+ function editSettingListener(event) {
event.preventDefault();
const id = event.target.closest("tr").dataset.id;
- const setting = findSetting(id);
- const row = addRowToTable(editSettingRow(setting));
- if (row.querySelector(".super-settings-value .js-date-input")) {
- row.querySelector(".super-settings-value .js-date-input").focus();
- } else {
- row.querySelector(".super-settings-value .js-setting-value").focus();
- }
+ setting = findSetting(id);
+ editSetting(setting);
}
// Listener for the restore setting button.
function restoreSetting(event) {
event.preventDefault();
@@ -654,10 +678,22 @@
url += "?filter=" + escape(filter);
}
window.location = url;
}
+ // Open the setting if the URL hash includes #edit=setting.
+ function fetchEditHash() {
+ const hash = window.location.hash;
+ if (hash.startsWith("#edit=")) {
+ const name = hash.replace("#edit=", "");
+ window.location.hash = "";
+ return name;
+ } else {
+ return null;
+ }
+ }
+
// Support integration into single page applications where OAuth2 access tokens are used.
// The access token can be passed either in the access_token query parameter per the
// OAuth2 standard, or in the URL hash. Passing it in the hash will prevent it from ever
// being sent to the backend and is a bit more secure since there's no chance a web server
// will accidentally log it with the request URL.
@@ -692,11 +728,11 @@
}
// Bind event listeners for setting controls on a setting table row.
function bindSettingControlEvents(parent) {
addListener(parent.querySelectorAll(".js-remove-setting"), "click", removeSetting);
- addListener(parent.querySelectorAll(".js-edit-setting"), "click", editSetting);
+ addListener(parent.querySelectorAll(".js-edit-setting"), "click", editSettingListener);
addListener(parent.querySelectorAll(".js-restore-setting"), "click", restoreSetting);
addListener(parent.querySelectorAll(".js-show-history"), "click", showHistoryModal);
addListener(parent.querySelectorAll(".js-no-op"), "click", noOp);
addListener(parent.querySelectorAll(".js-setting-value-type"), "change", changeSettingType);
addListener(parent.querySelectorAll(".js-date-input"), "change", changeDateTime);
@@ -800,36 +836,46 @@
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
- function fetchActiveSettings() {
+ function fetchActiveSettings(editKey) {
SuperSettingsAPI.fetchSettings(function(settings_hash) {
const settings = settings_hash["settings"];
activeSettings = settings;
renderSettingsTable(settings);
+ if (editKey) {
+ const setting = findSettingByKey(editKey);
+ if (setting) {
+ editSetting(setting);
+ } else {
+ addSetting(editKey);
+ }
+ }
enableSaveButton();
});
}
let activeSettings = [];
docReady(function() {
storeAccessToken();
addListener(document.querySelector("#super-settings-filter"), "input", filterListener);
- addListener(document.querySelector("#super-settings-add-setting"), "click", addSetting);
+ addListener(document.querySelector("#super-settings-add-setting"), "click", addSettingListener);
addListener(document.querySelector("#super-settings-discard-changes"), "click", refreshPage);
addListener(document.querySelector("#super-settings-save-settings"), "click", updateSettings);
addListener(document.querySelector("#super-settings-modal"), "click", closeModal);
addListener(document.querySelectorAll(".super-settings-sort-control"), "click", setSortOrder);
+ const editKey = fetchEditHash();
+
const queryParams = new URLSearchParams(window.location.search);
applyFilter(queryParams.get("filter"));
selectSortElement(document.querySelector(".super-settings-sort-control[data-selected=true]"), true);
- fetchActiveSettings();
+ fetchActiveSettings(editKey);
window.onbeforeunload = promptUnsavedChanges;
})
})();