This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdm-sdk.js
More file actions
504 lines (413 loc) · 12.1 KB
/
dm-sdk.js
File metadata and controls
504 lines (413 loc) · 12.1 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
/** @global LSF */
/**
* @typedef {{
* hiddenColumns?: {
* labeling?: string[],
* explore?: string[],
* },
* visibleColumns?: {
* labeling?: string[],
* explore?: string[],
* }
* }} TableConfig
*/
/**
* @typedef {{
* root: HTMLElement,
* polling: boolean,
* apiGateway: string | URL,
* apiEndpoints: import("../utils/api-proxy").Endpoints,
* apiMockDisabled: boolean,
* apiHeaders?: Dict<string>,
* settings: Dict<any>,
* labelStudio: Dict<any>,
* env: "development" | "production",
* mode: "labelstream" | "explorer",
* table: TableConfig,
* links: Dict<string|null>,
* showPreviews: boolean,
* projectId?: number,
* datasetId?: number,
* interfaces: Dict<boolean>,
* instruments: Dict<any>,
* toolbar?: string,
* panels?: Record<string, any>[]
* spinner?: import("react").ReactNode
* apiTransform?: Record<string, Record<string, Function>
* tabControls?: { add?: boolean, delete?: boolean, edit?: boolean, duplicate?: boolean },
* }} DMConfig
*/
import { inject, observer } from "mobx-react";
import { destroy } from "mobx-state-tree";
import { unmountComponentAtNode } from "react-dom";
import { toCamelCase } from "strman";
import { instruments } from "../components/DataManager/Toolbar/instruments";
import { APIProxy } from "../utils/api-proxy";
import { objectToMap } from "../utils/helpers";
import { packJSON } from "../utils/packJSON";
import { isDefined } from "../utils/utils";
import { APIConfig } from "./api-config";
import { createApp } from "./app-create";
import { LSFWrapper } from "./lsf-sdk";
import { taskToLSFormat } from "./lsf-utils";
const DEFAULT_TOOLBAR = "actions columns filters ordering label-button loading-possum error-box | refresh import-button export-button view-toggle";
const prepareInstruments = (instruments) => {
const result = Object
.entries(instruments)
.map(([name, builder]) => [name, builder({ inject, observer })]);
return objectToMap(Object.fromEntries(result));
};
export class DataManager {
/** @type {HTMLElement} */
root = null;
/** @type {APIProxy} */
api = null;
/** @type {import("./lsf-sdk").LSFWrapper} */
lsf = null;
/** @type {Dict} */
settings = {};
/** @type {import("../stores/AppStore").AppStore} */
store = null;
/** @type {Dict<any>} */
labelStudioOptions = {};
/** @type {"development" | "production"} */
env = "development";
/** @type {"explorer" | "labelstream"} */
mode = "explorer";
/** @type {TableConfig} */
tableConfig = {};
/** @type {Dict<string|null>} */
links = {
import: "/import",
export: "/export",
settings: "./settings",
};
/**
* @private
* @type {Map<String, Set<Function>>}
*/
callbacks = new Map();
/**
* @private
* @type {Map<String, Set<Function>>}
*/
actions = new Map();
/** @type {Number} */
apiVersion = 1;
/** @type {boolean} */
showPreviews = false;
/** @type {boolean} */
polling = true;
/** @type {boolean} */
started = false;
instruments = new Map();
/**
* @type {DMConfig.tabControls}
*/
tabControls = {
add: true,
delete: true,
edit: true,
duplicate: true,
}
/** @type {"dm" | "labelops"} */
type = "dm";
/**
* Constructor
* @param {DMConfig} config
*/
constructor(config) {
this.root = config.root;
this.project = config.project;
this.projectId = config.projectId;
this.dataset = config.dataset;
this.datasetId = config.datasetId;
this.settings = config.settings;
this.labelStudioOptions = config.labelStudio;
this.env = config.env ?? process.env.NODE_ENV ?? this.env;
this.mode = config.mode ?? this.mode;
this.tableConfig = config.table ?? {};
this.apiVersion = config?.apiVersion ?? 1;
this.links = Object.assign(this.links, config.links ?? {});
this.showPreviews = config.showPreviews ?? false;
this.polling = config.polling;
this.toolbar = config.toolbar ?? DEFAULT_TOOLBAR;
this.panels = config.panels;
this.spinner = config.spinner;
this.spinnerSize = config.spinnerSize;
this.instruments = prepareInstruments(config.instruments ?? {}),
this.apiTransform = config.apiTransform ?? {};
this.preload = config.preload ?? {};
this.interfaces = objectToMap({
tabs: true,
toolbar: true,
import: true,
export: true,
labelButton: true,
backButton: true,
labelingHeader: true,
groundTruth: false,
instruction: false,
autoAnnotation: false,
...config.interfaces,
});
this.api = new APIProxy(
this.apiConfig({
apiGateway: config.apiGateway,
apiEndpoints: config.apiEndpoints,
apiMockDisabled: config.apiMockDisabled,
apiSharedParams: config.apiSharedParams,
apiHeaders: config.apiHeaders,
}),
);
Object.assign(this.tabControls, config.tabControls ?? {});
if (config.actions) {
config.actions.forEach(([action, callback]) => {
if (!isDefined(action.id)) {
throw new Error("Every action must provide a unique ID");
}
this.actions.set(action.id, { action, callback });
});
}
this.type = config.type ?? "dm";
this.initApp();
}
get isExplorer() {
return this.mode === "labeling";
}
get isLabelStream() {
return this.mode === "labelstream";
}
get projectId() {
return (this._projectId = this._projectId ?? this.root?.dataset?.projectId);
}
set projectId(value) {
this._projectId = value;
}
apiConfig({
apiGateway,
apiEndpoints,
apiMockDisabled,
apiSharedParams,
apiHeaders,
}) {
const config = Object.assign({}, APIConfig);
config.gateway = apiGateway ?? config.gateway;
config.mockDisabled = apiMockDisabled;
config.commonHeaders = apiHeaders;
Object.assign(config.endpoints, apiEndpoints ?? {});
const sharedParams = {};
if (!isNaN(this.projectId)) {
sharedParams.project = this.projectId;
}
if (!isNaN(this.datasetId)) {
sharedParams.dataset = this.datasetId;
}
Object.assign(config, {
sharedParams: {
...sharedParams,
...(apiSharedParams ?? {}),
},
});
return config;
}
/**
* @param {impotr("../stores/Action.js").Action} action
*/
addAction(action, callback) {
const { id } = action;
if (!id) throw new Error("Action must provide a unique ID");
this.actions.set(id, { action, callback });
this.store.addActions(action);
}
removeAction(id) {
this.actions.delete(id);
this.store.removeAction(id);
}
getAction(id) {
return this.actions.get(id)?.callback;
}
installActions() {
this.actions.forEach(({ action, callback }) => {
this.addAction(action, callback);
});
}
registerInstrument(name, initializer) {
if (instruments[name]) {
return console.warn(`Can't override native instrument ${name}`);
}
this.instruments.set(name, initializer({
store: this.store,
observer,
inject,
}));
this.store.updateInstruments();
}
/**
* Assign an event handler
* @param {string} eventName
* @param {Function} callback
*/
on(eventName, callback) {
if (this.lsf && eventName.startsWith('lsf:')) {
const evt = toCamelCase(eventName.replace(/^lsf:/, ''));
this.lsf?.lsfInstance?.on(evt, callback);
}
const events = this.getEventCallbacks(eventName);
events.add(callback);
this.callbacks.set(eventName, events);
}
/**
* Remove an event handler
* If no callback provided, all assigned callbacks will be removed
* @param {string} eventName
* @param {Function?} callback
*/
off(eventName, callback) {
if (this.lsf && eventName.startsWith('lsf:')) {
const evt = toCamelCase(eventName.replace(/^lsf:/, ''));
this.lsf?.lsfInstance?.off(evt, callback);
}
const events = this.getEventCallbacks(eventName);
if (callback) {
events.delete(callback);
} else {
events.clear();
}
}
removeAllListeners() {
const lsfEvents = Array.from(this.callbacks.keys()).filter(evt => evt.startsWith('lsf:'));
lsfEvents.forEach(evt => {
const callbacks = Array.from(this.getEventCallbacks(evt));
const eventName = toCamelCase(evt.replace(/^lsf:/, ''));
callbacks.forEach(clb => this.lsf?.lsfInstance?.off(eventName, clb));
});
this.callbacks.clear();
}
/**
* Check if an event has at least one handler
* @param {string} eventName Name of the event to check
*/
hasHandler(eventName) {
return this.getEventCallbacks(eventName).size > 0;
}
/**
* Check if interface is enabled
* @param {string} name Name of the interface
*/
interfaceEnabled(name) {
return this.store.interfaceEnabled(name);
}
/**
*
* @param {"explorer" | "labelstream"} mode
*/
setMode(mode) {
const modeChanged = mode !== this.mode;
this.mode = mode;
this.store.setMode(mode);
if (modeChanged) this.invoke('modeChanged', this.mode);
}
/**
* Invoke handlers assigned to an event
* @param {string} eventName
* @param {any[]} args
*/
async invoke(eventName, ...args) {
if (eventName.startsWith('lsf:')) return;
this.getEventCallbacks(eventName).forEach((callback) =>
callback.apply(this, args),
);
}
/**
* Get callbacks set for a particular event
* @param {string} eventName
*/
getEventCallbacks(eventName) {
return this.callbacks.get(eventName) ?? new Set();
}
/** @private */
async initApp() {
this.store = await createApp(this.root, this);
this.invoke('ready', [this]);
}
initLSF(element) {
if (this.lsf) return;
this.lsf = new LSFWrapper(this, element, {
...this.labelStudioOptions,
task: this.store.taskStore.selected,
preload: this.preload,
// annotation: this.store.annotationStore.selected,
isLabelStream: this.mode === 'labelstream',
});
}
/**
* Initialize LSF or use already initialized instance.
* Render LSF interface and load task for labeling.
* @param {HTMLElement} element Root element LSF will be rendered into
* @param {import("../stores/Tasks").TaskModel} task
*/
async startLabeling() {
if (!this.lsf) return;
let [task, annotation] = [
this.store.taskStore.selected,
this.store.annotationStore.selected,
];
const isLabelStream = this.mode === 'labelstream';
const taskExists = isDefined(this.lsf.task) && isDefined(task);
const taskSelected = this.lsf.task?.id === task?.id;
// do nothing if the task is already selected
if (taskExists && taskSelected) {
return;
}
if (!isLabelStream && (!taskSelected || isDefined(annotation))) {
const annotationID = annotation?.id ?? task.lastAnnotation?.id;
// this.lsf.loadTask(task.id, annotationID);
await this.lsf.selectTask(task, annotationID);
}
}
destroyLSF() {
this.lsf?.destroy();
this.lsf = undefined;
}
destroy(detachCallbacks = true) {
unmountComponentAtNode(this.root);
if (this.store) {
destroy(this.store);
}
if (detachCallbacks) {
this.callbacks.forEach((callbacks) => callbacks.clear());
this.callbacks.clear();
}
}
reload() {
this.destroy(false);
this.initApp();
this.installActions();
}
async apiCall(...args) {
return this.store.apiCall(...args);
}
getInstrument(name) {
return instruments[name] ?? this.instruments.get(name) ?? null;
}
hasInterface(name) {
return this.interfaces.get(name) === true;
}
get toolbarInstruments() {
const sections = this.toolbar.split("|").map(s => s.trim());
const instrumentsList = sections.map(section => {
return section.split(" ").filter((instrument) => {
const nativeInstrument = !!instruments[instrument];
const customInstrument = !!this.instruments.has(instrument);
if (!nativeInstrument && !customInstrument) {
console.warn(`Unknwown instrument detected: ${instrument}. Did you forget to register it?`);
}
return nativeInstrument || customInstrument;
});
});
return instrumentsList;
}
static packJSON = packJSON;
static taskToLSFormat = taskToLSFormat
}