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
|
// The table-editor used for exclusion rules.
const ExclusionRulesEditor = {
// When the Add rule button is clicked, use this as the pattern for the new rule. This is used by
// the action.html toolbar popup.
defaultPatternForNewRules: null,
init() {
document.querySelector("#exclusionAddButton").addEventListener("click", () => {
this.addRow(this.defaultPatternForNewRules);
this.dispatchEvent("input");
});
},
// - exclusionRules: the value obtained from settings, with the shape [{pattern, passKeys}].
setForm(exclusionRules = []) {
const rulesTable = document.querySelector("#exclusionRules");
// Remove any previous rows.
const existingRuleEls = rulesTable.querySelectorAll(".rule");
for (const el of existingRuleEls) el.remove();
const rowTemplate = document.querySelector("#exclusionRuleTemplate").content;
for (const rule of exclusionRules) {
this.addRow(rule.pattern, rule.passKeys);
}
},
// `pattern` and `passKeys` are optional.
addRow(pattern, passKeys) {
const rulesTable = document.querySelector("#exclusionRules");
const rowTemplate = document.querySelector("#exclusionRuleTemplate").content;
const rowEl = rowTemplate.cloneNode(true);
const patternEl = rowEl.querySelector("[name=pattern]");
patternEl.value = pattern ?? "";
patternEl.addEventListener("input", () => this.dispatchEvent("input"));
const keysEl = rowEl.querySelector("[name=passKeys]");
keysEl.value = passKeys ?? "";
keysEl.addEventListener("input", () => this.dispatchEvent("input"));
rowEl.querySelector(".remove").addEventListener("click", (e) => {
e.target.closest("tr").remove();
this.dispatchEvent("input");
});
rulesTable.appendChild(rowEl);
},
// Returns an array of rules, which can be stored in Settings.
getRules() {
const rows = Array.from(document.querySelectorAll("#exclusionRules tr.rule"));
const rules = rows
.map((el) => {
return {
// The ordering of these keys should match the order in defaultOptions in Settings.js.
passKeys: el.querySelector("[name=passKeys]").value.trim(),
pattern: el.querySelector("[name=pattern]").value.trim(),
};
})
// Exclude blank patterns.
.filter((rule) => rule.pattern);
return rules;
},
};
Object.assign(ExclusionRulesEditor, EventDispatcher);
|