-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdata-template.component.ts
More file actions
603 lines (539 loc) · 22.6 KB
/
data-template.component.ts
File metadata and controls
603 lines (539 loc) · 22.6 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import {Component, computed, effect, EventEmitter, inject, Input, OnDestroy, OnInit, Signal, signal, untracked, WritableSignal} from '@angular/core';
import {RelationalResult, Result, UiColumnDefinition} from '../models/result-set.model';
import {WebuiSettingsService} from '../../../services/webui-settings.service';
import {CatalogService} from '../../../services/catalog.service';
import {EntityModel, EntityType} from '../../../models/catalog.model';
import {WebSocket} from '../../../services/webSocket';
import {EntityConfig} from '../data-table/entity-config';
import {ActivatedRoute, Router} from '@angular/router';
import {Subscription} from 'rxjs';
import {toSignal} from '@angular/core/rxjs-interop';
import {LeftSidebarService} from '../../left-sidebar/left-sidebar.service';
import {CrudService} from '../../../services/crud.service';
import {PaginationElement} from '../models/pagination-element.model';
import {DataModel, DeleteRequest, EntityRequest, Method, QueryRequest} from '../../../models/ui-request.model';
import {ToastDuration, ToasterService} from '../../toast-exposer/toaster.service';
import {SortState} from '../models/sort-state.model';
import {HttpEventType} from '@angular/common/http';
import {DbmsTypesService} from '../../../services/dbms-types.service';
import * as $ from 'jquery';
import {CombinedResult} from '../data-view.model';
const INITIAL_TYPE = 'BIGINT';
@Component({
selector: 'data-template',
templateUrl: './data-template.component.html',
styleUrls: ['./data-template.component.scss']
})
export abstract class DataTemplateComponent implements OnInit, OnDestroy {
protected readonly _settings: WebuiSettingsService = inject(WebuiSettingsService);
protected readonly _router: Router = inject(Router);
protected readonly _route: ActivatedRoute = inject(ActivatedRoute);
protected readonly _sidebar: LeftSidebarService = inject(LeftSidebarService);
protected readonly _catalog: CatalogService = inject(CatalogService);
protected readonly _crud: CrudService = inject(CrudService);
protected readonly _toast: ToasterService = inject(ToasterService);
protected readonly _types: DbmsTypesService = inject(DbmsTypesService);
protected readonly webSocket: WebSocket;
@Input() set inputConfig(config: EntityConfig) {
if (!config) {
return;
}
this.entityConfig.set(config);
}
protected readonly entityConfig: WritableSignal<EntityConfig> = signal({
create: true,
search: true,
sort: true,
update: true,
delete: true,
exploring: false,
hideCreateView: false,
cardRelWidth: false
});
protected readonly currentRoute: WritableSignal<string> = signal(this._route.snapshot.paramMap.get('id'));
protected readonly routeParams = toSignal(this._route.params);
protected readonly entity: Signal<EntityModel>;
protected readonly $result: WritableSignal<CombinedResult> = signal(null);
protected readonly loading: WritableSignal<boolean> = signal(false);
protected readonly subscriptions = new Subscription();
@Input() set result(result: Result<any, any>) {
if (!result) {
return;
}
this.$result.set(CombinedResult.from(result));
}
pagination: PaginationElement[] = [];
currentPage: WritableSignal<number> = signal(1);
editing = -1;//-1 if not editing any row, else the index of that row
sortStates = new Map<string, SortState>();
filter = new Map<string, string>();
protected focusId: string;
insertValues = new Map<string, any>();
insertDirty = new Map<string, boolean>();//check if field has been edited (if yes, it is "dirty")
updateValues = new Map<string, any>();
/** -1 if not uploading, 0 or 100: striped, else: showing progress */
uploadProgress = -1;
downloadProgress = -1;
downloadingIthRow = -1;
confirm = -1;
protected constructor() {
this.webSocket = new WebSocket();
this._route.params.subscribe(route => {
this.currentRoute.set(route['id']);
this.stopEditing();
});
this.entity = computed(() => {
const catalog = this._catalog.listener();
if (!this.currentRoute || !this.currentRoute()) {
return null;
}
const route = this.currentRoute();
if (!route) {
return null;
}
const splits = route.split('.');
return catalog.getEntityFromName(splits[0], splits[1]);
});
// to get the correct insert defaults
effect(() => {
if (!this.$result || !this.$result() || this.$result().error) {
return;
}
untracked(() => {
this.buildInsertObject();
this.setPagination();
});
});
}
ngOnInit() {
//this._sidebar.open();
//listen to results
this.initWebsocket();
this.currentPage.set(+this._route.snapshot.paramMap.get('page') || 1);
if (this.$result && this.$result()) {
this.$result().currentPage = this.currentPage();
}
//listen to parameter changes
this._route.params.subscribe((params) => {
this.currentPage.set(+this._route.snapshot.paramMap.get('page') || 1);
this.$result?.update(res => {
if (!res) {
return res;
}
res.currentPage = this.currentPage();
return res;
});
});
}
ngOnDestroy() {
this.subscriptions.unsubscribe();
this.webSocket.close();
}
protected initWebsocket() {
const sub = this.webSocket.onMessage().subscribe({
next: (result: Result<any, any>) => {
if (!result) {
return;
}
//go to the highest page if you are "lost" (if you are on a page that is higher than the highest possible page)
if (this.$result && +this._route.snapshot.paramMap.get('page') > this.$result()?.highestPage) {
this._router.navigate(['/views/data-table/' + this.entity()?.name + '/' + this.$result().highestPage]).then(null);
}
this.editing = -1;
this.buildInsertObject();
this.entityConfig.update(conf => {
if (this.entity().entityType === EntityType.ENTITY) {
conf.create = true;
conf.update = true;
conf.delete = true;
} else {
conf.create = false;
conf.update = false;
conf.delete = false;
}
return conf;
});
this.$result.set(CombinedResult.from(result));
this.loading.set(false);
}, error: err => {
console.log(err);
this.loading.set(false);
this.$result.set(CombinedResult.fromRelational(new RelationalResult('Server is not available')));
}
});
this.subscriptions.add(sub);
}
removeNull(dataType: string) {
return dataType.replace(' NOT NULL', '');
}
setPagination() {
if (!this.$result()) {
return;
}
const activePage = this.$result().currentPage;
const highestPage = this.$result().highestPage;
this.pagination = [];
if (highestPage < 2) {
return;
}
if (!this.entity || !this.entity()) {
return;
}
const entity = this.entity();
const entityId = entity.id;
const neighbors = 1;//from active page, show n neighbors to the left and n neighbors to the right.
const prev = new PaginationElement().withPage(entityId, Math.max(1, activePage - 1)).withLabel('<');
if (activePage === 1) {
prev.setDisabled();
}
this.pagination.push(prev);
if (activePage === 1) {
this.pagination.push(new PaginationElement().withPage(entityId, 1).setActive());
} else {
this.pagination.push(new PaginationElement().withPage(entityId, 1));
}
if (activePage - neighbors > 2) {
this.pagination.push(new PaginationElement().withLabel('..').setDisabled());
}
let counter = Math.max(2, activePage - neighbors);
while (counter <= activePage + neighbors && counter <= highestPage) {
if (counter === activePage) {
this.pagination.push(new PaginationElement().withPage(entityId, counter).setActive());
} else {
this.pagination.push(new PaginationElement().withPage(entityId, counter));
}
counter++;
}
counter--;
if (counter < highestPage) {
if (counter + neighbors < highestPage) {
this.pagination.push(new PaginationElement().withLabel('..').setDisabled());
}
this.pagination.push(new PaginationElement().withPage(entityId, highestPage));
}
const next = new PaginationElement().withPage(entityId, Math.min(highestPage, activePage + 1)).withLabel('>');
if (activePage === highestPage) {
next.setDisabled();
}
this.pagination.push(next);
return this.pagination;
}
paginate(p: PaginationElement) {
this.$result().currentPage = p.page;
this.getEntityData();
}
public getEntityData() {
const filterObj = this.mapToObject(this.filter);
const sortState = {};
this.$result()?.header?.forEach((h: UiColumnDefinition) => {
this.sortStates.set(h.name, h.sort);
sortState[h.name] = h.sort;
});
const request = new EntityRequest(this.entity()?.id, this._catalog.getNamespaceFromId(this.entity()?.namespaceId).name, this.currentPage(), filterObj, sortState);
if (!this._crud.getEntityData(this.webSocket, request)) {
this.$result.set(CombinedResult.fromRelational(new RelationalResult('Could not establish a connection with the server.')));
}
}
mapToObject(map: Map<any, any>) {
const obj = {};
map.forEach((v, k) => {
obj[k] = v;
});
return obj;
}
deleteRow(values: string[], i: number | null) {
if (i !== null && this.confirm !== i) { // confirm functionality may be delegated to app-delete-confirm component
this.confirm = i;
return;
}
if (this.$result().dataModel === DataModel.DOCUMENT) {
this.adjustDocument(Method.DROP, values[0]);
return;
}
const rowMap = new Map<string, string>();
values.forEach((val, key) => {
rowMap.set(this.$result().header[key].name, val);
});
const row = this.mapToObject(rowMap);
const request = new DeleteRequest(this.entity()?.id, row);
const emitResult = new EventEmitter<RelationalResult>();
this._crud.deleteTuple(request).subscribe({
next: (result: RelationalResult) => {
emitResult.emit(result);
if (result.error) {
this._toast.exception(result, 'Could not delete this tuple:');
} else {
this.getEntityData();
}
}, error: err => {
this._toast.error('Could not delete this tuple.');
console.log(err);
emitResult.emit(new RelationalResult('Could not delete this tuple.'));
}
});
return emitResult;
}
/**
* In the card and carousel view, show mm data first (only image, video and audio columns)
*/
showFirst(dataType: string) {
switch (dataType) {
case 'IMAGE':
case 'VIDEO':
case 'AUDIO':
return true;
}
return false;
}
getFileLink(data: string) {
return this._crud.getFileUrl(data);
}
getFile(data: string, index: number) {
this.downloadingIthRow = index;
this.downloadProgress = 0;
this._crud.getFile(data).subscribe({
next: res => {
if (res.type && res.type === HttpEventType.DownloadProgress) {
this.downloadProgress = Math.round(100 * res.loaded / res.total);
} else if (res.type === HttpEventType.Response) {
//see https://stackoverflow.com/questions/51960172/
const url = window.URL.createObjectURL(<any>res.body);
window.open(url);
}
},
error: err => {
console.log(err);
}
}).add(() => {
this.downloadingIthRow = -1;
this.downloadProgress = -1;
});
}
triggerEditing(i) {
if (this.confirm !== -1) {
//when double-clicking the delete btn
return;
}
if (this.entityConfig().update) {
this.updateValues.clear();
this.$result().data[i].forEach((v, k) => {
if (this.$result().header[k].dataType === 'bool') {
this.updateValues.set(this.$result().header[k].name, this.getBoolean(v));
}
//assign multimedia types: null if the item is NULL, else undefined
//null items will be submitted and updated, undefined items will not be part of the UPDATE statement
else if (this._types.isMultimedia(this.$result().header[k].dataType)) {
if (v === null) {
this.updateValues.set(this.$result().header[k].name, null);
} else {
this.updateValues.set(this.$result().header[k].name, undefined);
}
} else {
this.updateValues.set(this.$result().header[k].name, v);
}
});
this.editing = i;
}
}
stopEditing() {
this.editing = -1;
}
getBoolean(value: any): Boolean {
switch (value) {
case true:
case 'true':
case 't':
case 1:
case '1':
case 'on':
case 'yes':
return true;
case 'null':
case 'NULL':
case null:
return null;
default:
return false;
}
}
inputChange(name: string, e) {
this.insertValues.set(name, e);
this.insertDirty.set(name, true);
}
insertTuple() {
if (this.$result().dataModel === DataModel.DOCUMENT) {
this.adjustDocument(Method.ADD);
return;
}
const formData = new FormData();
this.insertValues.forEach((v, k) => {
//only values with dirty state will be submitted. Columns that are not nullable are already set dirty
if (this.insertDirty.get(k) === true && v !== null) { // null check prevents null being inserted as string "null"
let value;
if (isNaN(v)) {
value = v;
} else {
value = String(v);
}
formData.append(k, value);
}
});
formData.append('entityId', String(this.entity().id));
this.uploadProgress = 100;//show striped progressbar
const emitResult = new EventEmitter<RelationalResult>();
this._crud.insertTuple(formData).subscribe({
next: res => {
if (res.type && res.type === HttpEventType.UploadProgress) {
this.uploadProgress = Math.round(100 * res.loaded / res.total);
} else if (res.type === HttpEventType.Response) {
this.uploadProgress = -1;
const result = <RelationalResult>res.body;
emitResult.emit(result);
if (result.error) {
if (result.error.includes('PRIMARY KEY')) {
this._toast.warn(`Insert failed: Duplicate primary key value.`);
} else {
this._toast.exception(result, 'Insert failed:');
}
} else if (result.affectedTuples === 1) {
$('.insert-input').val('');
this.insertValues.clear();
this.buildInsertObject();
this.getEntityData();
}
}
},
error: err => {
this._toast.error('Insert failed.');
console.log(err);
emitResult.emit(new RelationalResult('Insert failed.'));
}
}).add(() => this.uploadProgress = -1);
return emitResult;
}
private adjustDocument(method: Method, initialData: string = '') {
const entity = this.entity();
switch (method) {
case Method.ADD:
const data = this.insertValues.get('_id');
const add = `db.${entity.name}.insert(${data})`;
this._crud.anyQuery(this.webSocket, new QueryRequest(add, false, true, 'mql', this.$result().namespace));
this.insertValues.clear();
this.getEntityData();
break;
case Method.MODIFY:
const values = new Map<string, string>();//previous values
for (let i = 0; i < this.$result().header.length; i++) {
values.set(this.$result().header[i].name, this.$result().data[this.editing][i]);
i++;
}
const updated = this.updateValues.get('_id');
const parsed = JSON.parse(updated);
if (parsed.hasOwnProperty('_id')) {
const modify = `db.${entity.name}.updateMany({"_id": "${parsed['_id']}"}, {"$set": ${updated}})`;
this._crud.anyQuery(this.webSocket, new QueryRequest(modify, false, true, 'mql', this.$result().namespace));
this.insertValues.clear();
this.getEntityData();
}
break;
case Method.DROP:
const parsedDelete = JSON.parse(initialData);
if (parsedDelete.hasOwnProperty('_id')) {
const modify = `db.${entity.name}.deleteMany({"_id": "${parsedDelete['_id']}" })`;
this._crud.anyQuery(this.webSocket, new QueryRequest(modify, false, true, 'mql', this.$result().namespace));
this.insertValues.clear();
this.getEntityData();
}
break;
}
}
buildInsertObject() {
if (this.entityConfig && !this.entityConfig().create || !this.$result()) {
return;
}
this.insertValues.clear();
this.insertDirty.clear();
if (this.$result().header) {
for (const g of this.$result().header) {
//set insertDirty
if (!g.nullable && g.dataType !== 'serial' && g.defaultValue === undefined) {
//set dirty if not nullable, so it will be submitted, except if it has autoincrement (dataType 'serial') or a default value
this.insertDirty.set(g.name, true);
} else {
this.insertDirty.set(g.name, false);
}
//set insertValues
if (g.nullable) {
this.insertValues.set(g.name, null);
continue;
}
if (this._types.isNumeric((g.dataType))) {
this.insertValues.set(g.name, 0);
} else if (this._types.isBoolean(g.dataType)) {
this.insertValues.set(g.name, false);
} else {
this.insertValues.set(g.name, '');
}
}
}
}
newUpdateValue(key, val) {
this.updateValues.set(key, val);
}
updateTuple() {
if (this.$result().dataModel === DataModel.DOCUMENT) {
this.adjustDocument(Method.MODIFY);
return;
}
const oldValues = new Map<string, string>();//previous values
for (let i = 0; i < this.$result().header.length; i++) {
oldValues.set(this.$result().header[i].name, this.$result().data[this.editing][i]);
i++;
}
const formData = new FormData();
formData.append('entityId', String(this.entity()?.id));
formData.append('oldValues', JSON.stringify(this.mapToObject(oldValues)));
for (const [k, v] of this.updateValues) {
if (v === undefined) {
//don't add undefined file inputs, but if they are null, they need to be added
continue;
}
if (!(v instanceof File)) {
//stringify to distinguish between null and 'null'
formData.append(k, JSON.stringify(v));
} else {
formData.append(k, v);
}
}
this.uploadProgress = 100;//show striped progressbar
//const req = new UpdateRequest(this.resultSet.table, this.mapToObject(this.updateValues), this.mapToObject(oldValues));
this._crud.updateTuple(formData).subscribe({
next: res => {
if (res.type && res.type === HttpEventType.UploadProgress) {
this.uploadProgress = Math.round(100 * res.loaded / res.total);
} else if (res.type === HttpEventType.Response) {
this.uploadProgress = -1;
const result = <RelationalResult>res.body;
if (result.affectedTuples) {
this.getEntityData();
let rows = ' tuples';
if (result.affectedTuples === 1) {
rows = ' tuple';
}
this._toast.success('Updated ' + result.affectedTuples + rows, result.query, 'update', ToastDuration.SHORT);
} else if (result.error) {
if (result.error.includes('PRIMARY KEY')) {
this._toast.warn(`Update failed: Duplicate primary key value.`);
} else {
this._toast.exception(result, 'Update failed:');
}
}
}
},
error: err => {
this._toast.error('Could not update the data.');
console.log(err);
}
}).add(() => this.uploadProgress = -1);
}
}