import AbstractSearchOption from "./abstract_search_option.js";
import noteAutocompleteService from "../../services/note_autocomplete.js";
import { t } from "../../services/i18n.js";
const TPL = `
${t("ancestor.label")}:
${t("ancestor.depth_label")}:
${t("ancestor.depth_doesnt_matter")}
${t("ancestor.depth_eq", { count: 1 })} (${t("ancestor.direct_children")})
${t("ancestor.depth_eq", { count: 2 })}
${t("ancestor.depth_eq", { count: 3 })}
${t("ancestor.depth_eq", { count: 4 })}
${t("ancestor.depth_eq", { count: 5 })}
${t("ancestor.depth_eq", { count: 6 })}
${t("ancestor.depth_eq", { count: 7 })}
${t("ancestor.depth_eq", { count: 8 })}
${t("ancestor.depth_eq", { count: 9 })}
${t("ancestor.depth_gt", { count: 0 })}
${t("ancestor.depth_gt", { count: 1 })}
${t("ancestor.depth_gt", { count: 2 })}
${t("ancestor.depth_gt", { count: 3 })}
${t("ancestor.depth_gt", { count: 4 })}
${t("ancestor.depth_gt", { count: 5 })}
${t("ancestor.depth_gt", { count: 6 })}
${t("ancestor.depth_gt", { count: 7 })}
${t("ancestor.depth_gt", { count: 8 })}
${t("ancestor.depth_gt", { count: 9 })}
${t("ancestor.depth_lt", { count: 2 })}
${t("ancestor.depth_lt", { count: 3 })}
${t("ancestor.depth_lt", { count: 4 })}
${t("ancestor.depth_lt", { count: 5 })}
${t("ancestor.depth_lt", { count: 6 })}
${t("ancestor.depth_lt", { count: 7 })}
${t("ancestor.depth_lt", { count: 8 })}
${t("ancestor.depth_lt", { count: 9 })}
`;
export default class Ancestor extends AbstractSearchOption {
static get optionName() {
return "ancestor";
}
static get attributeType() {
return "relation";
}
static async create(noteId) {
await AbstractSearchOption.setAttribute(noteId, "relation", "ancestor", "root");
}
doRender() {
const $option = $(TPL);
const $ancestor = $option.find(".ancestor");
const $ancestorDepth = $option.find(".ancestor-depth");
noteAutocompleteService.initNoteAutocomplete($ancestor);
$ancestor.on("autocomplete:closed", async () => {
const ancestorNoteId = $ancestor.getSelectedNoteId();
if (ancestorNoteId) {
await this.setAttribute("relation", "ancestor", ancestorNoteId);
}
});
$ancestorDepth.on("change", async () => {
const ancestorDepth = $ancestorDepth.val();
if (ancestorDepth) {
await this.setAttribute("label", "ancestorDepth", ancestorDepth);
} else {
await this.deleteAttribute("label", "ancestorDepth");
}
});
const ancestorNoteId = this.note.getRelationValue("ancestor");
if (ancestorNoteId !== "root") {
$ancestor.setNote(ancestorNoteId);
}
const ancestorDepth = this.note.getLabelValue("ancestorDepth");
if (ancestorDepth) {
$ancestorDepth.val(ancestorDepth);
}
return $option;
}
async deleteOption() {
await this.deleteAttribute("label", "ancestorDepth");
await super.deleteOption();
}
}