129 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-09-22 22:25:39 +02:00
import NoteMapWidget from "../note_map.js";
import { t } from "../../services/i18n.js";
import RightPanelWidget from "../right_panel_widget.js";
2021-05-28 23:19:11 +02:00
const TPL = `
<div class="note-map-ribbon-widget">
2021-05-28 23:52:42 +02:00
<style>
.note-map-ribbon-widget {
2021-05-28 23:52:42 +02:00
position: relative;
}
2025-01-19 21:21:13 +02:00
.note-map-ribbon-widget .note-map-container {
2021-05-30 23:21:34 +02:00
height: 300px;
}
2025-01-19 21:21:13 +02:00
2021-05-31 21:20:30 +02:00
.open-full-button, .collapse-button {
2021-05-28 23:52:42 +02:00
position: absolute;
2021-05-31 21:20:30 +02:00
right: 5px;
bottom: 5px;
2021-05-28 23:52:42 +02:00
z-index: 1000;
}
2025-01-19 21:21:13 +02:00
2021-06-02 21:23:40 +02:00
.style-resolver {
color: var(--muted-text-color);
display: none;
}
2021-05-28 23:52:42 +02:00
</style>
<button class="bx bx-arrow-to-bottom icon-action open-full-button" title="${t("note_map.open_full")}"></button>
<button class="bx bx-arrow-to-top icon-action collapse-button" style="display: none;" title="${t("note_map.collapse")}"></button>
2021-05-28 23:52:42 +02:00
<div class="note-map-container"></div>
2021-05-28 23:19:11 +02:00
</div>`;
export default class NoteMapRibbonWidget extends RightPanelWidget {
2025-01-19 21:21:13 +02:00
private openState!: "small" | "full";
private noteMapWidget: NoteMapWidget;
private $container!: JQuery<HTMLElement>;
private $openFullButton!: JQuery<HTMLElement>;
private $collapseButton!: JQuery<HTMLElement>;
2021-09-22 22:25:39 +02:00
constructor() {
super();
2025-01-09 18:07:02 +02:00
this.noteMapWidget = new NoteMapWidget("ribbon");
2021-09-22 22:25:39 +02:00
this.child(this.noteMapWidget);
}
2021-06-27 12:53:05 +02:00
get name() {
return "noteMap";
2021-06-27 12:53:05 +02:00
}
get toggleCommand() {
return "toggleRibbonTabNoteMap";
}
get widgetTitle() {
return t("note_map.title");
2021-05-28 23:19:11 +02:00
}
async doRenderBody() {
this.$body.empty().append($(TPL));
2021-06-13 22:55:31 +02:00
this.contentSized();
this.$container = this.$body.find(".note-map-container");
2021-09-22 22:25:39 +02:00
this.$container.append(this.noteMapWidget.render());
2021-05-31 21:20:30 +02:00
2025-01-09 18:07:02 +02:00
this.openState = "small";
2021-05-31 23:38:47 +02:00
this.$openFullButton = this.$body.find(".open-full-button");
2025-01-09 18:07:02 +02:00
this.$openFullButton.on("click", () => {
2021-05-31 23:38:47 +02:00
this.setFullHeight();
2021-05-31 21:20:30 +02:00
this.$openFullButton.hide();
this.$collapseButton.show();
2021-05-31 23:38:47 +02:00
2025-01-09 18:07:02 +02:00
this.openState = "full";
2021-09-22 22:25:39 +02:00
this.noteMapWidget.setDimensions();
2021-05-31 21:20:30 +02:00
});
this.$collapseButton = this.$body.find(".collapse-button");
2025-01-09 18:07:02 +02:00
this.$collapseButton.on("click", () => {
2021-05-31 23:38:47 +02:00
this.setSmallSize();
2021-05-31 21:20:30 +02:00
this.$openFullButton.show();
this.$collapseButton.hide();
2021-05-31 23:38:47 +02:00
2025-01-09 18:07:02 +02:00
this.openState = "small";
2021-06-02 21:23:40 +02:00
this.noteMapWidget.setDimensions();
2021-09-22 22:25:39 +02:00
});
2021-05-31 23:38:47 +02:00
2023-04-06 20:59:09 +08:00
const handleResize = () => {
2025-01-09 18:07:02 +02:00
if (!this.noteMapWidget.graph) {
// no graph has been even rendered
2021-05-31 23:38:47 +02:00
return;
}
2025-01-09 18:07:02 +02:00
if (this.openState === "full") {
2021-05-31 23:38:47 +02:00
this.setFullHeight();
2025-01-09 18:07:02 +02:00
} else if (this.openState === "small") {
2021-05-31 23:38:47 +02:00
this.setSmallSize();
}
2025-01-09 18:07:02 +02:00
};
2023-04-06 20:59:09 +08:00
new ResizeObserver(handleResize).observe(this.$body[0]);
2021-05-31 23:38:47 +02:00
}
setSmallSize() {
const SMALL_SIZE_HEIGHT = 300;
const width = this.$body.width() ?? 0;
2021-05-31 23:38:47 +02:00
this.$body.find(".note-map-container").height(SMALL_SIZE_HEIGHT).width(width);
2021-05-31 23:38:47 +02:00
}
setFullHeight() {
const { top } = this.$body[0].getBoundingClientRect();
2021-05-31 23:38:47 +02:00
2025-01-19 21:21:13 +02:00
const height = ($(window).height() ?? 0) - top;
const width = (this.$body.width() ?? 0);
2021-05-31 23:38:47 +02:00
this.$body.find(".note-map-container")
2025-01-19 21:21:13 +02:00
.height(height)
.width(width);
2021-05-28 23:19:11 +02:00
}
}