-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-wizard-page.ts
96 lines (78 loc) · 3.36 KB
/
export-wizard-page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//import { Setting } from "obsidian";
export abstract class WizardPage {
protected _containerEl: HTMLElement;
private _backButtonEl: HTMLButtonElement | null = null;
private _backButtonText: string;
private _nextButtonEl: HTMLButtonElement | null = null;
private _nextButtonText: string;
protected _animalID: string;
private _onSuccess?: (...args: any[]) => void;
private _onCancelled?: () => void;
constructor(parentEl: HTMLElement, animalID: string, backButtonText: string = "Back", nextButtonText: string = "Next") {
this._containerEl = parentEl.createDiv({ cls: "wizard-page" });
this._animalID = animalID;
this._backButtonText = backButtonText;
if (!this._backButtonText) {
this._backButtonText = "Back";
}
this._nextButtonText = nextButtonText;
if (!this._nextButtonText) {
this._nextButtonText = "Next";
}
}
// Setup callbacks
onSuccess(callback: (...args: any[]) => void): void {
this._onSuccess = callback;
}
onCancelled(callback: () => void): void {
this._onCancelled = callback;
}
protected triggerSuccess(...args: any[]): void {
if (this._onSuccess) {
this._onSuccess(...args);
} else {
console.warn("No onSuccess handler is set.");
}
}
protected triggerCancelled(): void {
if (this._onCancelled) {
this._onCancelled();
} else {
console.warn("No onCancelled handler is set.");
}
}
// Initialize the page with specific content and buttons
public renderPage(contentElements: HTMLElement[], showBack: boolean, showNext: boolean) {
// Clear any previous content
this._containerEl.empty();
const outerContainer = this._containerEl.createDiv({ cls: "wizard-page-container" });
const contentContainer = outerContainer.createDiv({ cls: "wizard-content-container" });
// Always render top title
const titleEl = contentContainer.createEl("h2", { text: `Export ${this._animalID}` });
titleEl.classList.add("export-wizard-top-title");
// Append dynamic content
contentElements.forEach((el) => contentContainer.appendChild(el));
// Add Back and Next buttons
this.renderButtons(outerContainer, showBack, showNext);
}
private renderButtons(outerContainer: HTMLElement, showBack: boolean, showNext: boolean) {
const buttonsContainer = outerContainer.createDiv({ cls: "wizard-buttons-container" });
// Back Button
if (showBack) {
this._backButtonEl = buttonsContainer.createEl("button", { text: this._backButtonText });
this._backButtonEl.classList.add("wizard-button");
this._backButtonEl.classList.add("left-aligned");
this._backButtonEl.onclick = () => this.onBack();
}
// Next Button
if (showNext) {
this._nextButtonEl = buttonsContainer.createEl("button", { text: this._nextButtonText });
this._nextButtonEl.classList.add("wizard-button");
this._nextButtonEl.classList.add("right-aligned");
this._nextButtonEl.onclick = () => this.onNext();
}
}
// Abstract methods for specific handling in child pages
protected abstract onBack(): void;
protected abstract onNext(): void;
}