generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
226 lines (187 loc) · 5.78 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {App, Modal, Plugin, PluginSettingTab, Setting} from 'obsidian';
import {VIEW_TYPE_WIENER_LINIEN, WienerLinienView} from './view';
import stationsPerLine from "./data/stations-per-line.json";
type LineID = keyof typeof stationsPerLine;
// Remember to rename these classes and interfaces!
const SETTINGS_INDEX = 0;
export interface WienerLinienSettings {
numTrains: number;
rblNumbers: string[];
trains: LineID[];
showRelatedLines: boolean;
}
const DEFAULT_SETTINGS: WienerLinienSettings = {
numTrains: 1,
rblNumbers: ['4437'],
trains: ['301'],
showRelatedLines: true,
}
const options = () => Object.fromEntries(
Object.entries(stationsPerLine).map(([key, value]) => [key, value?.name])
);
let stations: Record<LineID, Record<string, string>>;
let stationOptions: Record<number, Record<string, string>>;
stations = Object.fromEntries(
(Object.keys(stationsPerLine) as LineID[]).map(
line => (
[
line,
Object.fromEntries(
stationsPerLine[line]
.stations
.filter(station => station.id && station.name)
.sort((a, b) =>
((maybeZero, fallback) => maybeZero === 0 ? fallback : maybeZero)(
a.direction.localeCompare(b.direction) * 10,
a.name.localeCompare(b.name),
)
)
.map(station => [station.id, `${station.name} (Direction ${station.direction})`])
)
]
)
)
) as Record<LineID, Record<string, string>>;
function refreshStations(trains: LineID[]) {
stationOptions = Object.fromEntries(
trains.map((train, index) => [index, stations[train]])
);
}
export default class WienerLinienPlugin extends Plugin {
settings?: WienerLinienSettings;
async onload() {
await this.loadSettings();
this.addRibbonIcon('train', 'Wiener Linien', () => {
// Called when the user clicks the icon.
this.openView();
});
this.registerView(
VIEW_TYPE_WIENER_LINIEN,
leaf => new WienerLinienView(leaf, this.settings)
);
this.addCommand({
id: 'open-wiener-linien-view',
name: 'Open Wiener Linien view',
callback: () => {
this.openView();
}
})
this.addCommand({
id: 'refresh-departures',
name: 'Refresh departures',
checkCallback: (checking: boolean) => {
// Conditions to check
const viewOfType = this.app.workspace.getActiveViewOfType(WienerLinienView);
if (viewOfType) {
// Only run in WienerLinienView
if (!checking) {
// TODO: Implement command
new Modal(this.app).open();
}
// This command will only show up in Command Palette when the check function returns true
return true;
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));
}
onunload() {
}
async openView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_WIENER_LINIEN);
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_WIENER_LINIEN,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE_WIENER_LINIEN)[0]
);
}
async reloadView() {
const view = this.app.workspace.getActiveViewOfType(WienerLinienView);
if (view !== null) {
// There is an active view, so simply create a new one
return this.openView()
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings(reload?: boolean|undefined) {
await this.saveData(this.settings);
if (reload === true) {
await this.reloadView();
}
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: WienerLinienPlugin;
constructor(app: App, plugin: WienerLinienPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
refreshStations(this.plugin.settings!.trains)
new Setting(containerEl)
.setName('Station number')
.setDesc('Should be a valid RBL number')
.addText(text => text
.setPlaceholder('4437')
.setValue(this.plugin.settings!.rblNumbers[SETTINGS_INDEX])
.onChange(async (value) => {
this.plugin.settings!.rblNumbers[SETTINGS_INDEX] = value;
await this.plugin.saveSettings(true);
this.display();
})
);
new Setting(containerEl)
.setName('Number of stations')
.setDesc('How many stations should be displayed (1 though 5)? Settings might need to be reloaded to take effect.')
.addText(text => text
.setPlaceholder('1')
.setValue(this.plugin.settings!.numTrains.toString())
.onChange(async (value) => {
this.plugin.settings!.numTrains = Math.max(1, Math.min(parseInt(value), 5));
await this.plugin.saveSettings();
this.display();
})
);
for (let i = 0; i < this.plugin.settings!.numTrains; i++) {
new Setting(containerEl)
.setName(`Line number ${i + 1}`)
.setDesc("Select your train/tram/bus. Settings might need to be reloaded to take effect.")
.addDropdown(dropdown => dropdown
.addOptions(options())
.setValue(this.plugin.settings!.trains[i])
.onChange(async (value: string) => {
this.plugin.settings!.trains[i] = value as LineID;
await this.plugin.saveSettings();
refreshStations(this.plugin.settings!.trains);
this.display();
}
)
)
.addDropdown(dropdown => dropdown
.addOptions(stationOptions[i])
.setValue(this.plugin.settings!.rblNumbers[i])
.onChange(async (value) => {
this.plugin.settings!.rblNumbers[i] = value;
await this.plugin.saveSettings(true);
})
);
}
new Setting(containerEl)
.setName('Show related lines')
.setDesc('Show departures for related lines')
.addToggle(toggle => toggle
.setValue(this.plugin.settings!.showRelatedLines)
.onChange(async (value) => {
this.plugin.settings!.showRelatedLines = value;
await this.plugin.saveSettings(true);
}
));
}
}