-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_editor.js
More file actions
349 lines (294 loc) · 11.8 KB
/
db_editor.js
File metadata and controls
349 lines (294 loc) · 11.8 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* PCM Database Editor
* A browser-based SQLite editor for Pro Cycling Manager database files
*/
import { PCMFileHandler } from './db_file_handler.js';
import Choices from 'choices.js';
import * as TaskProcessor from './task_processor.js';
import { renderConsole, escapeHtml } from './sql_console.js';
const CHOICES_CONFIG = {
searchEnabled: true,
shouldSort: false,
itemSelectText: ''
};
const CHOICES_CONFIG_NO_SEARCH = {
searchEnabled: false,
shouldSort: false,
itemSelectText: ''
};
class EditorState {
constructor() {
this.config = {
tasks: [],
tasksDirectory: 'tasks'
};
this.tasks = [];
this.db = null;
this.currentTask = null;
this.currentRecordId = null;
this.taskSelectChoices = null;
this.recordSelectChoices = null;
this.fieldSelectChoices = [];
// SQL Console state
this.sqlConsole = {
isActive: false,
queryHistory: [],
writeMode: false,
currentQuery: '',
maxHistorySize: 20,
rowLimit: 200,
currentOffset: 0,
cachedResult: null
};
}
destroyChoices() {
if (this.recordSelectChoices) {
this.recordSelectChoices.destroy();
this.recordSelectChoices = null;
}
this.fieldSelectChoices.forEach(choice => choice.destroy());
this.fieldSelectChoices = [];
}
}
const state = new EditorState();
async function init(userConfig) {
state.config = { ...state.config, ...userConfig };
await loadTasks();
setupEventHandlers();
}
async function loadTasks() {
try {
state.tasks = await TaskProcessor.loadTaskDefinitions(state.config);
} catch (error) {
console.error('Error loading tasks:', error);
showStatus('Error loading tasks', 'error');
}
}
function populateTaskDropdown() {
const options = '<option value="">-- Choose a task --</option>' +
'<option value="__sql_console__">SQL Console</option>' +
state.tasks.map((task, index) => `<option value="${index}">${escapeHtml(task.name)}</option>`).join('');
document.getElementById('taskSelect').innerHTML = options;
}
function setupEventHandlers() {
document.getElementById('taskSelect').addEventListener('change', (e) => {
const taskIndex = e.target.value;
if (taskIndex === '') {
document.getElementById('record-section').classList.remove('active');
document.getElementById('form-section').classList.remove('active');
state.destroyChoices();
state.currentTask = null;
state.currentRecordId = null;
state.sqlConsole.isActive = false;
return;
}
// Check if SQL Console was selected
if (taskIndex === '__sql_console__') {
document.getElementById('record-section').classList.remove('active');
state.destroyChoices();
state.currentTask = null;
state.currentRecordId = null;
state.sqlConsole.isActive = true;
renderConsole(state.db, state.sqlConsole);
return;
}
document.getElementById('form-section').classList.remove('active');
state.currentRecordId = null;
state.sqlConsole.isActive = false;
if (state.recordSelectChoices) {
state.recordSelectChoices.destroy();
state.recordSelectChoices = null;
}
document.getElementById('recordSelect').innerHTML = '<option value="">-- Choose a record --</option>';
state.currentTask = state.tasks[taskIndex];
loadRecords();
});
document.getElementById('recordSelect').addEventListener('change', (e) => {
state.currentRecordId = e.target.value;
if (state.currentRecordId) loadEditForm();
});
}
function onDatabaseLoaded(dbInfo) {
state.db = dbInfo.db;
populateTaskDropdown();
document.getElementById('drop-section').classList.remove('active');
document.getElementById('task-section').classList.add('active');
state.taskSelectChoices = new Choices('#taskSelect', CHOICES_CONFIG_NO_SEARCH);
showStatus('Database loaded successfully', 'success');
}
function onError(message) {
showStatus(message, 'error');
}
function onLoadingStateChange(isLoading, message) {
const loadingOverlay = document.getElementById('loadingOverlay');
if (isLoading) {
document.getElementById('loadingMessage').textContent = message || 'Loading...';
loadingOverlay.classList.add('active');
} else {
loadingOverlay.classList.remove('active');
}
}
function loadRecords() {
const result = state.db.exec(TaskProcessor.buildRecordQuery(state.currentTask));
if (result.length === 0) {
showStatus('No records found in table', 'error');
return;
}
const columns = result[0].columns;
const rows = result[0].values;
const recordSelector = TaskProcessor.getRecordSelector(state.currentTask);
const idIndex = columns.indexOf(recordSelector);
const displayIndices = TaskProcessor.findDisplayIndices(state.currentTask, columns);
const options = rows.map(row =>
`<option value="${escapeHtml(String(row[idIndex]))}">${escapeHtml(TaskProcessor.formatRecordLabel(state.currentTask, row, columns, idIndex, displayIndices))}</option>`
).join('');
document.getElementById('recordSelect').innerHTML = '<option value="">-- Choose a record --</option>' + options;
document.getElementById('record-section').classList.add('active');
state.recordSelectChoices = new Choices('#recordSelect', CHOICES_CONFIG);
}
function loadEditForm() {
const query = TaskProcessor.buildSelectRecordQuery(state.currentTask, state.currentRecordId);
const stmt = state.db.prepare(query.sql);
stmt.bind(query.params);
stmt.step();
const row = stmt.getAsObject();
stmt.free();
// Destroy previous field choices
state.fieldSelectChoices.forEach(choice => choice.destroy());
state.fieldSelectChoices = [];
document.getElementById('formFields').innerHTML = '';
const fields = TaskProcessor.buildFieldDefinitions(state.currentTask, state.db, row);
fields.forEach(field => {
const fieldDiv = document.createElement('div');
fieldDiv.classList.add('field-group');
const label = document.createElement('label');
label.textContent = field.label;
label.setAttribute('for', field.column);
switch (field.type) {
case 'readonly':
const input = document.createElement('input');
input.type = 'text';
input.id = field.column;
input.name = field.column;
input.value = field.currentValue || '';
input.readOnly = true;
input.style.background = '#f5f5f5';
fieldDiv.appendChild(label);
fieldDiv.appendChild(input);
break;
case 'enum':
case 'query':
case 'join':
const select = document.createElement('select');
select.id = field.column;
select.name = field.column;
field.options.forEach(item => select.appendChild(createOption(item, field.currentValue)));
fieldDiv.appendChild(label);
fieldDiv.appendChild(select);
initChoices(select, field.column);
break;
case 'list':
if (field.isConsecutiveIntegers) {
// Use slider for consecutive integers
const min = field.options[0];
const max = field.options[field.options.length - 1];
const slider = document.createElement('input');
slider.type = 'range';
slider.id = field.column;
slider.name = field.column;
slider.min = min;
slider.max = max;
slider.step = 1;
slider.value = field.currentValue || min;
const valueDisplay = document.createElement('span');
valueDisplay.classList.add('range-value');
valueDisplay.textContent = field.currentValue || min;
slider.addEventListener('input', (e) => {
valueDisplay.textContent = e.target.value;
});
slider.addEventListener('change', (e) => {
saveField(field.column, e.target.value);
});
fieldDiv.appendChild(label);
fieldDiv.appendChild(slider);
fieldDiv.appendChild(valueDisplay);
} else {
const listSelect = document.createElement('select');
listSelect.id = field.column;
listSelect.name = field.column;
field.options.forEach(item => listSelect.appendChild(createOption(item, field.currentValue)));
fieldDiv.appendChild(label);
fieldDiv.appendChild(listSelect);
initChoices(listSelect, field.column);
}
break;
}
document.getElementById('formFields').appendChild(fieldDiv);
});
document.getElementById('form-section').classList.add('active');
}
function saveField(column, value) {
try {
const query = TaskProcessor.buildUpdateQuery(state.currentTask, column, state.currentRecordId, value);
state.db.run(query.sql, query.params);
showStatus('Saved', 'success');
} catch (error) {
showStatus('Error: ' + error.message, 'error');
}
}
function createOption(item, currentValue) {
const option = document.createElement('option');
if (item instanceof Date) {
const year = item.getFullYear();
const month = String(item.getMonth() + 1).padStart(2, '0');
const day = String(item.getDate()).padStart(2, '0');
const yyyymmdd = parseInt(`${year}${month}${day}`);
option.value = yyyymmdd;
option.textContent = `${year}-${month}-${day}`;
if (yyyymmdd == currentValue) option.selected = true;
} else if (typeof item === 'number' && item >= 19000101 && item <= 21991231) {
const dateStr = item.toString();
const year = dateStr.substring(0, 4);
const month = dateStr.substring(4, 6);
const day = dateStr.substring(6, 8);
option.value = item;
option.textContent = `${year}-${month}-${day}`;
if (item == currentValue) option.selected = true;
} else if (typeof item === 'object' && item !== null && 'value' in item) {
option.value = item.value;
option.textContent = item.display;
if (item.value == currentValue) option.selected = true;
} else {
option.value = item;
option.textContent = item;
if (item == currentValue) option.selected = true;
}
return option;
}
function initChoices(select, column) {
setTimeout(() => {
const choices = new Choices(select, CHOICES_CONFIG);
state.fieldSelectChoices.push(choices);
select.addEventListener('change', (e) => saveField(column, e.target.value));
}, 0);
}
function showStatus(message, type) {
const status = document.getElementById('status');
status.textContent = message;
status.className = 'status ' + type;
setTimeout(() => {
status.className = 'status';
}, 3000);
}
async function initializeApp(editorConfig) {
await init(editorConfig);
await PCMFileHandler.init({
onDatabaseLoaded: onDatabaseLoaded,
onError: onError,
onLoadingStateChange: onLoadingStateChange,
getDatabase: () => state.db
});
}
export const PCMEditor = {
initializeApp: initializeApp
};