File size: 5,446 Bytes
b5ba7a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(() => {
	const saveWorkspaceBtn = document.getElementById("save-workspace-btn");
	const renameWorkspaceBtn = document.getElementById("rename-workspace-btn");
	const moreWorkspaceBtn = document.getElementById("more-workspace-btn");
	const expandedWorkspaceMenu = document.getElementById("more-workspace-menu");
	const exportWorkspaceBtn = document.getElementById("export-workspace-btn");
	const importWorkspaceBtn = document.getElementById("import-workspace-btn");
	const deleteWorkspaceBtn = document.getElementById("delete-workspace-btn");

	moreWorkspaceBtn.addEventListener("click", () => {
		expandedWorkspaceMenu.classList.toggle("collapsed");
	});

	const workspaceAutocomplete = createAutoComplete(
		"Workspace",
		document.getElementById("workspace-select")
	);

	workspaceAutocomplete.options = [{name: "Default", value: "default"}];
	workspaceAutocomplete.value = "default";
	renameWorkspaceBtn.disabled = true;
	deleteWorkspaceBtn.disabled = true;

	workspaceAutocomplete.onchange.on(async ({name, value}) => {
		if (value === "default") {
			renameWorkspaceBtn.disabled = true;
			deleteWorkspaceBtn.disabled = true;
			await commands.clear();
			return;
		}
		renameWorkspaceBtn.disabled = false;
		deleteWorkspaceBtn.disabled = false;

		const workspaces = db
			.transaction("workspaces", "readonly")
			.objectStore("workspaces");

		workspaces.get(value).onsuccess = (e) => {
			console.debug("[workspace.populate] Loading workspace");

			const res = e.target.result;
			const {name, workspace} = res;
			importWorkspaceState(workspace);
			notifications.notify(`Loaded workspace '${name}'`, {
				type: NotificationType.SUCCESS,
			});
		};
	});

	/**
	 * Updates Workspace selection list
	 */
	const listWorkspaces = async (value = undefined) => {
		const options = [{name: "Default", value: "default"}];

		const workspaces = db
			.transaction("workspaces", "readonly")
			.objectStore("workspaces");

		workspaces.openCursor().onsuccess = (e) => {
			/** @type {IDBCursor} */
			const c = e.target.result;
			if (c) {
				options.push({name: c.value.name, value: c.key});
				c.continue();
			} else {
				const previousValue = workspaceAutocomplete.value;

				workspaceAutocomplete.options = options;
				workspaceAutocomplete.value = value ?? previousValue;
			}
		};
	};

	const saveWorkspaceToDB = async (value) => {
		const workspace = await exportWorkspaceState();

		const workspaces = db
			.transaction("workspaces", "readwrite")
			.objectStore("workspaces");

		let id = value;
		if (value === "default" && commands._history.length > 0) {
			// If Workspace is the Default
			const name = (prompt("Please enter the workspace name") ?? "").trim();

			if (name) {
				id = guid();
				workspaces.add({id, name, workspace}).onsuccess = () => {
					listWorkspaces(id);
					notifications.notify(`Workspace saved as '${name}'`, {
						type: "success",
					});
				};
			}
		} else {
			workspaces.get(id).onsuccess = (e) => {
				const ws = e.target.result;
				if (ws) {
					var name = ws.name;
					workspaces.delete(id).onsuccess = () => {
						workspaces.add({id, name, workspace}).onsuccess = () => {
							notifications.notify(`Workspace saved as '${name}'`, {
								type: "success",
							});
						}; //workspaces.put is failing, delete and re-add?
						listWorkspaces();
					};
				}
			};
		}
	};

	// Normal Workspace Export/Import
	exportWorkspaceBtn.addEventListener("click", () => saveWorkspaceToFile());
	importWorkspaceBtn.addEventListener("click", () => {
		const input = document.createElement("input");
		input.type = "file";
		input.accept = "application/json";
		input.addEventListener("change", async (evn) => {
			let files = Array.from(input.files);
			const json = await files[0].text();

			await importWorkspaceState(JSON.parse(json));
			saveWorkspaceToDB("default");
		});
		input.click();
	});

	const onDatabaseLoad = async () => {
		// Get workspaces from database
		listWorkspaces();

		// Save Workspace Button
		saveWorkspaceBtn.addEventListener("click", () =>
			saveWorkspaceToDB(workspaceAutocomplete.value)
		);

		// Rename Workspace
		renameWorkspaceBtn.addEventListener("click", () => {
			const workspaces = db
				.transaction("workspaces", "readwrite")
				.objectStore("workspaces");

			let id = workspaceAutocomplete.value;

			workspaces.get(id).onsuccess = (e) => {
				const workspace = e.target.result;
				const name = prompt(
					`Please enter the new workspace name.<br>Original is '${workspace.name}'`
				).trim();

				if (!name) return;

				workspace.name = name;

				workspaces.put(workspace).onsuccess = () => {
					notifications.notify(
						`Workspace name was updated to '${workspace.name}'`,
						{type: NotificationType.SUCCESS}
					);
					listWorkspaces();
				};
			};
		});
		// Delete Workspace
		deleteWorkspaceBtn.addEventListener("click", () => {
			const workspaces = db
				.transaction("workspaces", "readwrite")
				.objectStore("workspaces");

			let id = workspaceAutocomplete.value;

			workspaces.get(id).onsuccess = async (e) => {
				const workspace = e.target.result;

				if (
					await notifications.dialog(
						"Delete Workspace",
						`Do you really want to delete the workspace '${workspace.name}'?`
					)
				) {
					workspaces.delete(id).onsuccess = (e) => {
						listWorkspaces("default");
					};
				}
			};
		});
	};

	if (db) onDatabaseLoad();
	else ondatabaseload.on(onDatabaseLoad);
})();