email-dns-tools/assets/scripts/ui/creator.js

57 lines
1.4 KiB
JavaScript

import { DmarcRecord } from "../records/DmarcRecord.js";
import { SpfRecord } from "../records/SpfRecord.js";
const records = {
"/dmarc-creator": DmarcRecord,
"/spf-creator": SpfRecord,
};
const Record = records[location.pathname];
const inputs = [];
addInputs(document.getElementById("form"), Record.fields.filter(field => !field.categoryName));
const categories = Record.fields.map(field => field.categoryName).filter(isUnique).filter(val => val);
for (const category of categories) {
const details = document.createElement("details");
details.innerHTML = `<summary>${Record.categories[category]}</summary>`;
document.getElementById("form").appendChild(details);
addInputs(details, Record.fields.filter(field => field.categoryName === category));
}
function addInputs(elem, fields) {
for (const field of fields) {
if (field.isDisabled) continue;
const input = field.createInput(elem);
inputs.push(input);
}
}
document.getElementById("form").onchange = generate;
generate();
function generate() {
const items = [];
for (const input of inputs) {
items.push(...input.items);
}
const record = Record.createFromFieldInputItems(items);
document.getElementById("record").value = record.text;
}
document.getElementById("record").onclick = (e) => {
e.target.select();
document.execCommand("copy");
}
function isUnique(value, index, array) {
return array.indexOf(value) === index;
}