2025-01-09 18:07:02 +02:00
|
|
|
import treeService from "../../services/tree.js";
|
|
|
|
import server from "../../services/server.js";
|
2022-06-14 23:07:42 +02:00
|
|
|
import froca from "../../services/froca.js";
|
|
|
|
import toastService from "../../services/toast.js";
|
|
|
|
import utils from "../../services/utils.js";
|
|
|
|
import BasicWidget from "../basic_widget.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../../components/app_context.js";
|
2024-07-22 17:31:54 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-02-21 20:41:00 +01:00
|
|
|
import { Modal } from "bootstrap";
|
2022-06-14 23:07:42 +02:00
|
|
|
|
|
|
|
const TPL = `<div class="branch-prefix-dialog modal fade mx-auto" tabindex="-1" role="dialog">
|
|
|
|
<div class="modal-dialog modal-lg" role="document">
|
|
|
|
<form class="branch-prefix-form">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5 class="modal-title flex-grow-1">${t("branch_prefix.edit_branch_prefix")}</h5>
|
|
|
|
<button class="help-button" type="button" data-help-page="tree-concepts.html#prefix" title="${t("branch_prefix.help_on_tree_prefix")}">?</button>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("branch_prefix.close")}"></button>
|
2022-06-14 23:07:42 +02:00
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
|
|
<div class="form-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<label for="branch-prefix-input">${t("branch_prefix.prefix")}</label>
|
2022-06-14 23:07:42 +02:00
|
|
|
|
|
|
|
<div class="input-group">
|
|
|
|
<input class="branch-prefix-input form-control">
|
2025-03-19 13:59:36 +01:00
|
|
|
<div class="branch-prefix-note-title input-group-text"></div>
|
2022-06-14 23:07:42 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2025-01-09 18:07:02 +02:00
|
|
|
<button class="btn btn-primary btn-sm">${t("branch_prefix.save")}</button>
|
2022-06-14 23:07:42 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class BranchPrefixDialog extends BasicWidget {
|
2025-03-19 13:59:36 +01:00
|
|
|
private modal!: Modal;
|
|
|
|
private $form!: JQuery<HTMLElement>;
|
|
|
|
private $treePrefixInput!: JQuery<HTMLElement>;
|
|
|
|
private $noteTitle!: JQuery<HTMLElement>;
|
|
|
|
private branchId: string | null = null;
|
|
|
|
|
2025-03-19 14:22:40 +01:00
|
|
|
doRender() {
|
2022-06-14 23:07:42 +02:00
|
|
|
this.$widget = $(TPL);
|
2025-03-19 13:59:36 +01:00
|
|
|
this.modal = Modal.getOrCreateInstance(this.$widget[0]);
|
2022-06-14 23:07:42 +02:00
|
|
|
this.$form = this.$widget.find(".branch-prefix-form");
|
|
|
|
this.$treePrefixInput = this.$widget.find(".branch-prefix-input");
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$noteTitle = this.$widget.find(".branch-prefix-note-title");
|
2022-06-14 23:07:42 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$form.on("submit", () => {
|
2022-06-14 23:07:42 +02:00
|
|
|
this.savePrefix();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.on("shown.bs.modal", () => this.$treePrefixInput.trigger("focus"));
|
2022-06-14 23:07:42 +02:00
|
|
|
}
|
|
|
|
|
2025-03-19 13:59:36 +01:00
|
|
|
async refresh(notePath: string) {
|
2024-09-03 18:15:10 +02:00
|
|
|
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
|
2022-06-14 23:07:42 +02:00
|
|
|
|
|
|
|
if (!noteId || !parentNoteId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-03-19 13:59:36 +01:00
|
|
|
const newBranchId = await froca.getBranchId(parentNoteId, noteId);
|
|
|
|
if (!newBranchId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.branchId = newBranchId;
|
2022-06-14 23:07:42 +02:00
|
|
|
|
2025-03-19 13:59:36 +01:00
|
|
|
const branch = froca.getBranch(this.branchId);
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!branch || branch.noteId === "root") {
|
2022-06-14 23:07:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentNote = await froca.getNote(branch.parentNoteId);
|
2025-03-19 13:59:36 +01:00
|
|
|
if (!parentNote || parentNote.type === "search") {
|
2022-06-14 23:07:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-03-19 13:59:36 +01:00
|
|
|
this.$treePrefixInput.val(branch.prefix || "");
|
2022-06-14 23:07:42 +02:00
|
|
|
|
|
|
|
const noteTitle = await treeService.getNoteTitle(noteId);
|
2022-12-21 15:19:05 +01:00
|
|
|
this.$noteTitle.text(` - ${noteTitle}`);
|
2022-06-14 23:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async editBranchPrefixEvent() {
|
|
|
|
const notePath = appContext.tabManager.getActiveContextNotePath();
|
2025-03-19 13:59:36 +01:00
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-14 23:07:42 +02:00
|
|
|
|
|
|
|
await this.refresh(notePath);
|
|
|
|
utils.openDialog(this.$widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
async savePrefix() {
|
|
|
|
const prefix = this.$treePrefixInput.val();
|
|
|
|
|
2025-03-19 13:59:36 +01:00
|
|
|
await server.put(`branches/${this.branchId}/set-prefix`, { prefix: prefix });
|
2022-06-14 23:07:42 +02:00
|
|
|
|
2024-09-03 18:15:10 +02:00
|
|
|
this.modal.hide();
|
2022-06-14 23:07:42 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
toastService.showMessage(t("branch_prefix.branch_prefix_saved"));
|
2022-06-14 23:07:42 +02:00
|
|
|
}
|
|
|
|
}
|