Notes/src/public/app/widgets/editability_select.js

98 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import attributeService from "../services/attributes.js";
2021-06-26 17:08:50 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import { t } from "../services/i18n.js";
2021-06-26 17:08:50 +02:00
const TPL = `
<div class="dropdown editability-select-widget">
<style>
.editability-dropdown {
width: 300px;
}
.editability-dropdown .dropdown-item {
display: block !important;
}
2021-06-26 17:08:50 +02:00
.editability-dropdown .dropdown-item div {
font-size: small;
color: var(--muted-text-color);
white-space: normal;
}
</style>
<button type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-sm dropdown-toggle editability-button">
<span class="editability-active-desc">${t("editability_select.auto")}</span>
2021-06-26 17:08:50 +02:00
<span class="caret"></span>
</button>
<div class="editability-dropdown dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#" data-editability="auto">
<span class="check">&check;</span>
${t("editability_select.auto")}
<div>${t("editability_select.note_is_editable")}</div>
2021-06-26 17:08:50 +02:00
</a>
<a class="dropdown-item" href="#" data-editability="readOnly">
<span class="check">&check;</span>
${t("editability_select.read_only")}
<div>${t("editability_select.note_is_read_only")}</div>
2021-06-26 17:08:50 +02:00
</a>
<a class="dropdown-item" href="#" data-editability="autoReadOnlyDisabled">
<span class="check">&check;</span>
${t("editability_select.always_editable")}
<div>${t("editability_select.note_is_always_editable")}</div>
2021-06-26 17:08:50 +02:00
</a>
</div>
</div>
`;
export default class EditabilitySelectWidget extends NoteContextAwareWidget {
doRender() {
this.$widget = $(TPL);
this.dropdown = bootstrap.Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']"));
2021-06-26 17:08:50 +02:00
this.$editabilityActiveDesc = this.$widget.find(".editability-active-desc");
2025-01-09 18:07:02 +02:00
this.$widget.on("click", ".dropdown-item", async (e) => {
this.dropdown.toggle();
2021-06-26 17:08:50 +02:00
2025-01-09 18:07:02 +02:00
const editability = $(e.target).closest("[data-editability]").attr("data-editability");
2021-06-26 17:08:50 +02:00
2025-01-09 18:07:02 +02:00
for (const ownedAttr of this.note.getOwnedLabels()) {
if (["readOnly", "autoReadOnlyDisabled"].includes(ownedAttr.name)) {
await attributeService.removeAttributeById(this.noteId, ownedAttr.attributeId);
2021-06-26 17:08:50 +02:00
}
2025-01-09 18:07:02 +02:00
}
2021-06-26 17:08:50 +02:00
2025-01-09 18:07:02 +02:00
if (editability !== "auto") {
await attributeService.addLabel(this.noteId, editability);
}
});
2021-06-26 17:08:50 +02:00
}
async refreshWithNote(note) {
2025-01-09 18:07:02 +02:00
let editability = "auto";
2025-01-09 18:07:02 +02:00
if (this.note.isLabelTruthy("readOnly")) {
editability = "readOnly";
} else if (this.note.isLabelTruthy("autoReadOnlyDisabled")) {
editability = "autoReadOnlyDisabled";
}
const labels = {
2025-01-09 18:07:02 +02:00
auto: t("editability_select.auto"),
readOnly: t("editability_select.read_only"),
autoReadOnlyDisabled: t("editability_select.always_editable")
};
2025-01-09 18:07:02 +02:00
this.$widget.find(".dropdown-item").removeClass("selected");
this.$widget.find(`.dropdown-item[data-editability='${editability}']`).addClass("selected");
this.$editabilityActiveDesc.text(labels[editability]);
2021-06-26 17:08:50 +02:00
}
entitiesReloadedEvent({ loadResults }) {
2025-01-09 18:07:02 +02:00
if (loadResults.getAttributeRows().find((attr) => attr.noteId === this.noteId)) {
2021-06-26 17:08:50 +02:00
this.refresh();
}
}
}