@myrmidon/cadmus-state 16.0.1 → 20.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1374 +1,1154 @@
1
- import * as i0 from '@angular/core';
2
- import { Injectable, signal, ChangeDetectionStrategy, Component } from '@angular/core';
3
- import { BehaviorSubject, forkJoin, take, map } from 'rxjs';
4
- import * as i1 from '@myrmidon/cadmus-api';
5
- import * as i4 from '@myrmidon/cadmus-core';
6
- import { TokenLocation } from '@myrmidon/cadmus-core';
7
- import { deepCopy } from '@myrmidon/ngx-tools';
8
- import * as i1$1 from '@angular/router';
9
- import * as i2 from '@angular/material/snack-bar';
10
-
11
- /**
12
- * App state repository.
13
- */
14
- class AppRepository {
15
- _facetService;
16
- _flagService;
17
- _thesaurusService;
18
- _previewService;
19
- _settingService;
20
- // all the available facets definitions
21
- _facets$;
22
- // all the available flags definitions
23
- _flags$;
24
- // the thesaurus for model-types. This (if present) is used to display
25
- // human-friendly part types names from their IDs. Otherwise, the raw
26
- // IDs are displayed.
27
- _typeThesaurus$;
28
- // the items browsers thesaurus. This (if present) is used to display
29
- // the items browsers menu.
30
- _itemBrowserThesaurus$;
31
- // the preview JSON renderers keys. Empty when preview is disabled.
32
- _previewJKeys$;
33
- // the preview text flatteners keys. Empty when preview is disabled.
34
- _previewFKeys$;
35
- // the preview item composer keys. Empty when preview is disabled.
36
- _previewCKeys$;
37
- // editor settings cache, lazily loaded
38
- _settingsCache;
39
- get facets$() {
40
- return this._facets$;
41
- }
42
- get flags$() {
43
- return this._flags$;
44
- }
45
- get typeThesaurus$() {
46
- return this._typeThesaurus$;
47
- }
48
- get itemBrowserThesaurus$() {
49
- return this._itemBrowserThesaurus$;
50
- }
51
- get previewJKeys$() {
52
- return this._previewJKeys$;
53
- }
54
- get previewFKeys$() {
55
- return this._previewFKeys$;
56
- }
57
- get previewCKeys$() {
58
- return this._previewCKeys$;
59
- }
60
- constructor(_facetService, _flagService, _thesaurusService, _previewService, _settingService) {
61
- this._facetService = _facetService;
62
- this._flagService = _flagService;
63
- this._thesaurusService = _thesaurusService;
64
- this._previewService = _previewService;
65
- this._settingService = _settingService;
66
- this._facets$ = new BehaviorSubject([]);
67
- this._flags$ = new BehaviorSubject([]);
68
- this._typeThesaurus$ = new BehaviorSubject(undefined);
69
- this._itemBrowserThesaurus$ = new BehaviorSubject(undefined);
70
- this._previewJKeys$ = new BehaviorSubject([]);
71
- this._previewFKeys$ = new BehaviorSubject([]);
72
- this._previewCKeys$ = new BehaviorSubject([]);
73
- this._settingsCache = new Map();
74
- }
75
- /**
76
- * Get the loaded model types thesaurus.
77
- * @returns Thesaurus or undefined if not loaded.
78
- */
79
- getTypeThesaurus() {
80
- return this._typeThesaurus$.value;
81
- }
82
- /**
83
- * Get the loaded items facets.
84
- * @returns The facets.
85
- */
86
- getFacets() {
87
- return this._facets$.value;
88
- }
89
- /**
90
- * Get the loaded items flags.
91
- * @returns The flags.
92
- */
93
- getFlags() {
94
- return this._flags$.value;
95
- }
96
- /**
97
- * Load app data from the API. If the data is already loaded, this
98
- * does nothing unless refresh is true.
99
- *
100
- * @param refresh True to force a refresh.
101
- * @returns Promise.
102
- */
103
- load(refresh = false) {
104
- if (this._facets$.value.length && !refresh) {
105
- return Promise.resolve();
106
- }
107
- const facets$ = this._facetService.getFacets();
108
- const flags$ = this._flagService.getFlags();
109
- const thesauri$ = this._thesaurusService.getThesauriSet([
110
- 'model-types@en',
111
- 'item-browsers@en',
112
- ]);
113
- const jKeys$ = this._previewService.getKeys('J');
114
- const fKeys$ = this._previewService.getKeys('F');
115
- const cKeys$ = this._previewService.getKeys('C');
116
- return new Promise((resolve, reject) => {
117
- forkJoin({
118
- facets: facets$,
119
- flags: flags$,
120
- thesauri: thesauri$,
121
- jKeys: jKeys$,
122
- fKeys: fKeys$,
123
- cKeys: cKeys$,
124
- }).subscribe({
125
- next: (result) => {
126
- this._facets$.next(result.facets);
127
- this._flags$.next(result.flags);
128
- this._typeThesaurus$.next(result.thesauri['model-types']);
129
- this._itemBrowserThesaurus$.next(result.thesauri['item-browsers']);
130
- this._previewJKeys$.next(result.jKeys);
131
- this._previewFKeys$.next(result.fKeys);
132
- this._previewCKeys$.next(result.cKeys);
133
- console.log(`AppRepository loaded (facets: ${result.facets.length}, flags: ${result.flags.length})`);
134
- resolve();
135
- },
136
- error: (err) => {
137
- reject(err);
138
- },
139
- });
140
- });
141
- }
142
- /**
143
- * Clear the repository.
144
- */
145
- clear() {
146
- this._facets$.next([]);
147
- this._flags$.next([]);
148
- this._typeThesaurus$.next(undefined);
149
- this._itemBrowserThesaurus$.next(undefined);
150
- this._previewJKeys$.next([]);
151
- this._previewFKeys$.next([]);
152
- this._previewCKeys$.next([]);
153
- this._settingsCache.clear();
154
- }
155
- /**
156
- * Load the model types and item browsers thesauri.
157
- */
158
- loadThesauri() {
159
- return new Promise((resolve, reject) => {
160
- this._thesaurusService
161
- .getThesauriSet(['model-types@en', 'item-browsers@en'])
162
- .subscribe({
163
- next: (thesauri) => {
164
- this._typeThesaurus$.next(thesauri['model-types']);
165
- this._itemBrowserThesaurus$.next(thesauri['item-browsers']);
166
- resolve();
167
- },
168
- error: (err) => {
169
- reject(err);
170
- },
171
- });
172
- });
173
- }
174
- /**
175
- * Get the color defined for the specified part type ID, reading it
176
- * from the facets definitions.
177
- * @param typeId The part type ID.
178
- * @param roleId The optional role type ID.
179
- * @returns The color key, or undefined if not found.
180
- */
181
- getPartColor(typeId, roleId) {
182
- // for each facet, find typeId and roleId in its part definitions
183
- for (let facet of this._facets$.value) {
184
- for (let part of facet.partDefinitions) {
185
- if (part.typeId === typeId && (!roleId || part.roleId === roleId)) {
186
- return part.colorKey;
187
- }
188
- }
189
- }
190
- return undefined;
191
- }
192
- /**
193
- * Load the flags definitions.
194
- */
195
- loadFlags() {
196
- return new Promise((resolve, reject) => {
197
- this._flagService.getFlags().subscribe({
198
- next: (flags) => {
199
- this._flags$.next(flags);
200
- resolve();
201
- },
202
- error: (err) => {
203
- reject(err);
204
- },
205
- });
206
- });
207
- }
208
- /**
209
- * Get the editor setting with the specified ID.
210
- * @param id The setting ID.
211
- * @param reload True to force reloading from server.
212
- * @returns Promise with the settings object.
213
- */
214
- getSetting(id, reload = false) {
215
- return new Promise((resolve, reject) => {
216
- if (this._settingsCache.has(id) && !reload) {
217
- resolve(this._settingsCache.get(id));
218
- return;
219
- }
220
- this._settingService.getSetting(id).subscribe({
221
- next: (setting) => {
222
- this._settingsCache.set(id, setting);
223
- resolve(setting);
224
- },
225
- error: (err) => {
226
- reject(err);
227
- },
228
- });
229
- });
230
- }
231
- /**
232
- * Set the editor setting with the specified ID.
233
- * @param id The setting ID.
234
- * @param setting The setting to store.
235
- */
236
- setSetting(id, setting) {
237
- this._settingService.addSetting(id, setting).subscribe({
238
- next: () => {
239
- this._settingsCache.set(id, setting);
240
- },
241
- error: (err) => {
242
- console.error(`Error setting setting ${id}:`, err);
243
- },
244
- });
245
- }
246
- /**
247
- * Get the editor setting for the specified part or fragment type ID,
248
- * optionally having the specified role ID.
249
- * @param typeId The part or fragment type ID.
250
- * @param roleId The optional role ID.
251
- * @param reload True to force reloading from server.
252
- * @returns Promise with settings object.
253
- */
254
- getSettingFor(typeId, roleId, reload = false) {
255
- const id = roleId ? `${typeId}_${roleId}` : typeId;
256
- return this.getSetting(id, reload);
257
- }
258
- /**
259
- * Set the editor setting for the specified part or fragment type ID,
260
- * optionally having the specified role ID.
261
- * @param typeId The part or fragment type ID.
262
- * @param setting The setting to store.
263
- * @param roleId The optional role ID.
264
- */
265
- setSettingFor(typeId, setting, roleId) {
266
- const id = roleId ? `${typeId}_${roleId}` : typeId;
267
- this.setSetting(id, setting);
268
- }
269
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: AppRepository, deps: [{ token: i1.FacetService }, { token: i1.FlagService }, { token: i1.ThesaurusService }, { token: i1.PreviewService }, { token: i1.EditorSettingsService }], target: i0.ɵɵFactoryTarget.Injectable });
270
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: AppRepository, providedIn: 'root' });
271
- }
272
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: AppRepository, decorators: [{
273
- type: Injectable,
274
- args: [{ providedIn: 'root' }]
275
- }], ctorParameters: () => [{ type: i1.FacetService }, { type: i1.FlagService }, { type: i1.ThesaurusService }, { type: i1.PreviewService }, { type: i1.EditorSettingsService }] });
276
-
277
- /**
278
- * Service used by layer part editor wrappers to load part and thesauri
279
- * or save parts.
280
- */
281
- class FragmentEditorService {
282
- _itemService;
283
- _thesaurusService;
284
- _loading$;
285
- _saving$;
286
- loading$;
287
- saving$;
288
- constructor(_itemService, _thesaurusService) {
289
- this._itemService = _itemService;
290
- this._thesaurusService = _thesaurusService;
291
- this._loading$ = new BehaviorSubject(false);
292
- this.loading$ = this._loading$.asObservable();
293
- this._saving$ = new BehaviorSubject(false);
294
- this.saving$ = this._saving$.asObservable();
295
- }
296
- getFragmentFromPart(part, loc) {
297
- const fr = part.fragments.find((f) => f.location === loc);
298
- return (fr ?? {
299
- location: loc,
300
- });
301
- }
302
- loadWithThesauri(identity, thesauriIds) {
303
- // remove trailing ! from IDs if any
304
- const unscopedIds = thesauriIds.map((id) => {
305
- return this._thesaurusService.getScopedId(id);
306
- });
307
- this._loading$.next(true);
308
- return new Promise((resolve, reject) => {
309
- // load part and thesauri
310
- forkJoin({
311
- part: this._itemService.getPart(identity.partId || ''),
312
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
313
- t: this._itemService.getBaseTextPart(identity.itemId),
314
- }).subscribe({
315
- next: (result) => {
316
- const layerPart = result.part;
317
- const thesauri = result.thesauri;
318
- const fr = this.getFragmentFromPart(layerPart, identity.loc);
319
- // if the loaded part has a thesaurus scope, reload the thesauri
320
- if (layerPart.thesaurusScope) {
321
- const scopedIds = thesauriIds.map((id) => {
322
- return this._thesaurusService.getScopedId(id, layerPart.thesaurusScope);
323
- });
324
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
325
- next: (scopedThesauri) => {
326
- this._loading$.next(false);
327
- resolve({
328
- value: fr,
329
- layerPart: layerPart,
330
- baseText: result.t.text,
331
- thesauri: scopedThesauri,
332
- });
333
- },
334
- error: (error) => {
335
- this._loading$.next(false);
336
- console.error(error);
337
- reject({
338
- message: 'Error loading thesauri ' + scopedIds.join(', '),
339
- error: error,
340
- });
341
- },
342
- });
343
- }
344
- else {
345
- // else just use the loaded thesauri
346
- this._loading$.next(false);
347
- resolve({
348
- value: fr,
349
- layerPart: layerPart,
350
- baseText: result.t.text,
351
- thesauri: thesauri,
352
- });
353
- }
354
- },
355
- error: (error) => {
356
- this._loading$.next(false);
357
- console.error(error);
358
- reject({
359
- message: 'Error loading thesauri ' + unscopedIds.join(', '),
360
- error: error,
361
- });
362
- },
363
- });
364
- });
365
- }
366
- loadWithoutThesauri(identity) {
367
- this._loading$.next(true);
368
- return new Promise((resolve, reject) => {
369
- // load layer part and base text
370
- forkJoin({
371
- t: this._itemService.getBaseTextPart(identity.itemId),
372
- p: this._itemService.getPart(identity.partId),
373
- }).subscribe({
374
- next: (result) => {
375
- this._loading$.next(false);
376
- const layerPart = result.p;
377
- resolve({
378
- value: this.getFragmentFromPart(layerPart, identity.loc),
379
- layerPart: layerPart,
380
- baseText: result.t.text,
381
- thesauri: {},
382
- });
383
- },
384
- error: (error) => {
385
- this._loading$.next(false);
386
- console.error(error);
387
- reject({
388
- message: 'Error loading layer part ' + identity.partId,
389
- error: error,
390
- });
391
- },
392
- });
393
- });
394
- }
395
- /**
396
- * Load into the state the part with the specified ID
397
- * and its thesauri.
398
- * If the ID is not specified, it's a new part; in this case,
399
- * it will get a null ID, but use the specified itemId and roleId.
400
- *
401
- * @param identity The part identity.
402
- * @param thesauriIds The thesauri IDs array.
403
- */
404
- load(identity, thesauriIds) {
405
- if (thesauriIds?.length) {
406
- return this.loadWithThesauri(identity, thesauriIds);
407
- }
408
- else {
409
- return this.loadWithoutThesauri(identity);
410
- }
411
- }
412
- /**
413
- * Save the fragments in their layer part.
414
- *
415
- * @param part The part.
416
- * @returns Promise which when successful returns the saved part.
417
- */
418
- save(part) {
419
- this._saving$.next(true);
420
- return new Promise((resolve, reject) => {
421
- this._itemService.addPart(part).subscribe({
422
- next: (saved) => {
423
- this._saving$.next(false);
424
- resolve(saved);
425
- },
426
- error: (error) => {
427
- this._saving$.next(false);
428
- console.error(error);
429
- reject({
430
- message: 'Error saving part ' + part.id,
431
- error: error,
432
- });
433
- },
434
- });
435
- });
436
- }
437
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: FragmentEditorService, deps: [{ token: i1.ItemService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
438
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: FragmentEditorService, providedIn: 'root' });
439
- }
440
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: FragmentEditorService, decorators: [{
441
- type: Injectable,
442
- args: [{
443
- providedIn: 'root',
444
- }]
445
- }], ctorParameters: () => [{ type: i1.ItemService }, { type: i1.ThesaurusService }] });
446
-
447
- /**
448
- * Base class for fragment feature editors.
449
- * This is similar to the EditPartFeatureBase class.
450
- */
451
- class EditFragmentFeatureBase {
452
- _router;
453
- snackbar;
454
- editorService;
455
- _libraryRouteService;
456
- _loadingSub;
457
- _savingSub;
458
- /**
459
- * The fragment being edited.
460
- */
461
- data = signal(undefined, /* @ts-ignore */
462
- ...(ngDevMode ? [{ debugName: "data" }] : /* istanbul ignore next */ []));
463
- /**
464
- * The identity of the fragment being edited. This gets built from the current
465
- * route.
466
- */
467
- identity = signal({
468
- itemId: '',
469
- typeId: '',
470
- partId: '',
471
- roleId: null,
472
- frTypeId: '',
473
- frRoleId: null,
474
- loc: '',
475
- }, /* @ts-ignore */
476
- ...(ngDevMode ? [{ debugName: "identity" }] : /* istanbul ignore next */ []));
477
- /**
478
- * True when the wrapped editor component data is dirty.
479
- */
480
- dirty = signal(false, /* @ts-ignore */
481
- ...(ngDevMode ? [{ debugName: "dirty" }] : /* istanbul ignore next */ []));
482
- /**
483
- * True when loading data.
484
- */
485
- loading = signal(undefined, /* @ts-ignore */
486
- ...(ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []));
487
- /**
488
- * True when saving data.
489
- */
490
- saving = signal(undefined, /* @ts-ignore */
491
- ...(ngDevMode ? [{ debugName: "saving" }] : /* istanbul ignore next */ []));
492
- /**
493
- * The location of the fragment being edited.
494
- */
495
- frLoc = signal(undefined, /* @ts-ignore */
496
- ...(ngDevMode ? [{ debugName: "frLoc" }] : /* istanbul ignore next */ []));
497
- constructor(_router, route, snackbar, editorService, _libraryRouteService) {
498
- this._router = _router;
499
- this.snackbar = snackbar;
500
- this.editorService = editorService;
501
- this._libraryRouteService = _libraryRouteService;
502
- // fragment route:
503
- // /items/<iid>/@partGroup/fragment/<pid>/@frTypeId/<loc>?frrid
504
- const itemId = route.snapshot.params['iid'];
505
- const partId = route.snapshot.params['pid'];
506
- const frTypeId = route.snapshot.url[2]?.path;
507
- const loc = route.snapshot.params['loc'];
508
- const frRoleId = route.snapshot.queryParams['frrid'];
509
- this.identity.set({
510
- itemId: itemId,
511
- typeId: '',
512
- partId: partId,
513
- roleId: frTypeId,
514
- frTypeId: frTypeId,
515
- frRoleId: frRoleId,
516
- loc: loc,
517
- });
518
- // subscriptions
519
- this._loadingSub = this.editorService.loading$.subscribe((loading) => this.loading.set(loading));
520
- this._savingSub = this.editorService.saving$.subscribe((saving) => this.saving.set(saving));
521
- }
522
- /**
523
- * Override in derived classes to return the IDs of the thesauri requested
524
- * for this editor.
525
- *
526
- * @return Array with thesauri IDs, eventually empty.
527
- */
528
- getReqThesauriIds() {
529
- return [];
530
- }
531
- /**
532
- * Called once data has been loaded.
533
- */
534
- onDataLoaded() { }
535
- ngOnInit() {
536
- this.loading.set(true);
537
- this.frLoc.set(TokenLocation.parse(this.identity().loc) ?? undefined);
538
- const thesIds = this.getReqThesauriIds();
539
- this.editorService
540
- .load(this.identity(), thesIds)
541
- .then((data) => {
542
- if (data) {
543
- this.data.set(data);
544
- this.onDataLoaded();
545
- }
546
- })
547
- .catch((reason) => {
548
- this.snackbar.open(reason.message, 'OK');
549
- });
550
- }
551
- ngOnDestroy() {
552
- this._loadingSub?.unsubscribe();
553
- this._savingSub?.unsubscribe();
554
- }
555
- canDeactivate() {
556
- return !this.dirty();
557
- }
558
- /**
559
- * Called when the wrapped editor dirty state changes.
560
- * In the HTML template, you MUST bind this handler to the dirtyChange event
561
- * emitted by your wrapped editor (i.e. (dirtyChange)="onDirtyChange($event)").
562
- *
563
- * @param value The value of the dirty state.
564
- */
565
- onDirtyChange(value) {
566
- console.log('fragment dirty change (from editor): ' + value);
567
- this.dirty.set(value);
568
- }
569
- /**
570
- * Save the fragment.
571
- *
572
- * @param fragment The fragment to be saved.
573
- */
574
- save(fragment) {
575
- // update a copy of the edited part with new fragment; a shallow spread
576
- // would leave part.fragments pointing at the same array as
577
- // data().layerPart.fragments, so the splice/push below would mutate
578
- // state still referenced by the data signal.
579
- const part = deepCopy(this.data().layerPart);
580
- const frIndex = part.fragments.findIndex((f) => f.location === this.identity().loc);
581
- if (frIndex > -1) {
582
- part.fragments.splice(frIndex, 1, fragment);
583
- }
584
- else {
585
- part.fragments.push(fragment);
586
- }
587
- // save the new layer part with the replaced fragment
588
- this.editorService.save(part).then((p) => {
589
- console.log(p.id);
590
- this.snackbar.open('Fragment saved', 'OK', {
591
- duration: 3000,
592
- });
593
- }, (error) => {
594
- // restore dirty: save failed, the editor data was not persisted
595
- this.dirty.set(true);
596
- console.error(error);
597
- this.snackbar.open('Error saving fragment', 'OK');
598
- });
599
- }
600
- close() {
601
- // /items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>
602
- const part = this.data().layerPart;
603
- const editorKey = this._libraryRouteService.getEditorKeyFromPartType(part.typeId, part.roleId);
604
- const url = `/items/${this.identity().itemId}/${editorKey.partKey}/${part.typeId}/${this.identity().partId}`;
605
- this._router.navigate([url], {
606
- queryParams: {
607
- rid: this.identity().frTypeId,
608
- },
609
- });
610
- }
611
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditFragmentFeatureBase, deps: [{ token: i1$1.Router }, { token: i1$1.ActivatedRoute }, { token: i2.MatSnackBar }, { token: FragmentEditorService }, { token: i4.LibraryRouteService }], target: i0.ɵɵFactoryTarget.Component });
612
- static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "22.0.1", type: EditFragmentFeatureBase, isStandalone: false, selector: "ng-component", ngImport: i0, template: '', isInline: true, changeDetection: i0.ChangeDetectionStrategy.Eager });
613
- }
614
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditFragmentFeatureBase, decorators: [{
615
- type: Component,
616
- args: [{
617
- template: '',
618
- changeDetection: ChangeDetectionStrategy.Eager,
619
- standalone: false,
620
- }]
621
- }], ctorParameters: () => [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: FragmentEditorService }, { type: i4.LibraryRouteService }] });
622
-
623
- /**
624
- * Service used by part editor wrappers to load part and thesauri
625
- * or save parts.
626
- */
627
- class PartEditorService {
628
- _itemService;
629
- _thesaurusService;
630
- _loading$;
631
- _saving$;
632
- loading$;
633
- saving$;
634
- constructor(_itemService, _thesaurusService) {
635
- this._itemService = _itemService;
636
- this._thesaurusService = _thesaurusService;
637
- this._loading$ = new BehaviorSubject(false);
638
- this.loading$ = this._loading$.asObservable();
639
- this._saving$ = new BehaviorSubject(false);
640
- this.saving$ = this._saving$.asObservable();
641
- }
642
- loadWithThesauri(identity, thesauriIds) {
643
- // remove trailing ! from IDs if any
644
- const unscopedIds = thesauriIds.map((id) => {
645
- return this._thesaurusService.getScopedId(id);
646
- });
647
- // return a promise
648
- this._loading$.next(true);
649
- return new Promise((resolve, reject) => {
650
- // load part and thesauri
651
- forkJoin({
652
- part: this._itemService.getPart(identity.partId || ''),
653
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
654
- }).subscribe({
655
- next: (result) => {
656
- const part = result.part;
657
- const thesauri = result.thesauri;
658
- // if the loaded part has a thesaurus scope, reload the thesauri
659
- if (part?.thesaurusScope) {
660
- const scopedIds = thesauriIds.map((id) => {
661
- return this._thesaurusService.getScopedId(id, part.thesaurusScope);
662
- });
663
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
664
- next: (scopedThesauri) => {
665
- this._loading$.next(false);
666
- resolve({
667
- value: part,
668
- thesauri: scopedThesauri,
669
- });
670
- },
671
- error: (error) => {
672
- this._loading$.next(false);
673
- console.error(error);
674
- reject({
675
- message: 'Error loading thesauri ' + scopedIds.join(', '),
676
- error: error,
677
- });
678
- },
679
- });
680
- }
681
- else {
682
- // else just use the loaded thesauri
683
- this._loading$.next(false);
684
- resolve({
685
- value: part,
686
- thesauri: thesauri,
687
- });
688
- }
689
- },
690
- error: (error) => {
691
- this._loading$.next(false);
692
- console.error(error);
693
- reject({
694
- message: 'Error loading thesauri ' + unscopedIds.join(', '),
695
- error: error,
696
- });
697
- },
698
- });
699
- });
700
- }
701
- loadWithoutThesauri(partId) {
702
- return new Promise((resolve, reject) => {
703
- this._loading$.next(true);
704
- this._itemService
705
- .getPart(partId)
706
- .pipe(take(1), map((part) => {
707
- return part
708
- ? {
709
- value: part,
710
- thesauri: {},
711
- }
712
- : null;
713
- }))
714
- .subscribe({
715
- next: (result) => {
716
- this._loading$.next(false);
717
- resolve(result);
718
- },
719
- error: (error) => {
720
- this._loading$.next(false);
721
- console.error(error);
722
- reject({
723
- message: 'Error loading part ' + partId,
724
- error: error,
725
- });
726
- },
727
- });
728
- });
729
- }
730
- /**
731
- * Load into the state the part with the specified ID
732
- * and its thesauri.
733
- * If the ID is not specified, it's a new part; in this case,
734
- * it will get a null ID, but use the specified itemId and roleId.
735
- *
736
- * @param identity The part identity.
737
- * @param thesauriIds The thesauri IDs array.
738
- */
739
- load(identity, thesauriIds) {
740
- if (thesauriIds?.length) {
741
- return this.loadWithThesauri(identity, thesauriIds);
742
- }
743
- else {
744
- return this.loadWithoutThesauri(identity.partId);
745
- }
746
- }
747
- /**
748
- * Save the part.
749
- *
750
- * @param part The part.
751
- * @returns Promise which when successful returns the saved part.
752
- */
753
- save(part) {
754
- this._saving$.next(true);
755
- return new Promise((resolve, reject) => {
756
- this._itemService.addPart(part).subscribe({
757
- next: (saved) => {
758
- this._saving$.next(false);
759
- resolve(saved);
760
- },
761
- error: (error) => {
762
- this._saving$.next(false);
763
- console.error(error);
764
- reject({
765
- message: 'Error saving part ' + part.id,
766
- error: error,
767
- });
768
- },
769
- });
770
- });
771
- }
772
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: PartEditorService, deps: [{ token: i1.ItemService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
773
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: PartEditorService, providedIn: 'root' });
774
- }
775
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: PartEditorService, decorators: [{
776
- type: Injectable,
777
- args: [{
778
- providedIn: 'root',
779
- }]
780
- }], ctorParameters: () => [{ type: i1.ItemService }, { type: i1.ThesaurusService }] });
781
-
782
- /**
783
- * Base class for part feature editors.
784
- * A part feature editor is a wrapper component around a model editor component,
785
- * used to separate the editor's logic from its host infrastructure.
786
- */
787
- class EditPartFeatureBase {
788
- router;
789
- snackbar;
790
- itemService;
791
- thesaurusService;
792
- editorService;
793
- _loadingSub;
794
- _savingSub;
795
- /**
796
- * The part being edited.
797
- */
798
- data = signal(undefined, /* @ts-ignore */
799
- ...(ngDevMode ? [{ debugName: "data" }] : /* istanbul ignore next */ []));
800
- /**
801
- * The identity of the part being edited. This gets built from the current
802
- * route.
803
- */
804
- identity = signal({
805
- itemId: '',
806
- typeId: '',
807
- partId: null,
808
- roleId: null,
809
- }, /* @ts-ignore */
810
- ...(ngDevMode ? [{ debugName: "identity" }] : /* istanbul ignore next */ []));
811
- /**
812
- * True when the wrapped editor component data is dirty.
813
- */
814
- dirty = signal(false, /* @ts-ignore */
815
- ...(ngDevMode ? [{ debugName: "dirty" }] : /* istanbul ignore next */ []));
816
- /**
817
- * True when loading data.
818
- */
819
- loading = signal(undefined, /* @ts-ignore */
820
- ...(ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []));
821
- /**
822
- * True when saving data.
823
- */
824
- saving = signal(undefined, /* @ts-ignore */
825
- ...(ngDevMode ? [{ debugName: "saving" }] : /* istanbul ignore next */ []));
826
- /**
827
- * Set to true when you want the role ID (defined by identity) to affect
828
- * the ID of the thesauri to load. You should set this in your editor
829
- * constructor, or in your getReqThesauriIds override. When true, the role ID
830
- * will be suffixed (preceded by an underscore) to the thesaurus ID,
831
- * before its language identifier, i.e. for instance "categories@en"
832
- * with role "sample" becomes "categories_sample@en". When this option is
833
- * enabled, and some of your thesauri do not need to be suffixed, just
834
- * provide an alias for the suffixed thesaurus in the backend.
835
- */
836
- roleIdInThesauri;
837
- constructor(router, route, snackbar, itemService, thesaurusService, editorService) {
838
- this.router = router;
839
- this.snackbar = snackbar;
840
- this.itemService = itemService;
841
- this.thesaurusService = thesaurusService;
842
- this.editorService = editorService;
843
- // part route:
844
- // /items/<iid>/@partGroup/@partTypeId/<pid>?rid
845
- // item ID
846
- const itemId = route.snapshot.params['iid'];
847
- // type ID (from "@partTypeId/:pid")
848
- const typeId = route.snapshot.routeConfig.path.substring(0, route.snapshot.routeConfig.path.indexOf('/'));
849
- // part ID or null (if "new")
850
- let partId = route.snapshot.params['pid'];
851
- if (partId === 'new') {
852
- partId = null;
853
- }
854
- // role ID or null (if "default")
855
- let roleId = route.snapshot.queryParams['rid'];
856
- if (roleId === 'default') {
857
- roleId = null;
858
- }
859
- this.identity.set({
860
- itemId: itemId,
861
- typeId: typeId,
862
- partId: partId,
863
- roleId: roleId,
864
- });
865
- // subscriptions
866
- this._loadingSub = this.editorService.loading$.subscribe((loading) => this.loading.set(loading));
867
- this._savingSub = this.editorService.saving$.subscribe((saving) => this.saving.set(saving));
868
- }
869
- /**
870
- * Override in derived classes to return the IDs of the thesauri requested
871
- * for this editor.
872
- *
873
- * @return Array with thesauri IDs, eventually empty.
874
- */
875
- getReqThesauriIds() {
876
- return [];
877
- }
878
- /**
879
- * Called once data has been loaded.
880
- */
881
- onDataLoaded() { }
882
- suffixThesaurusId(id, suffix) {
883
- const i = id.lastIndexOf('@');
884
- return i > -1
885
- ? `${id.substring(0, i)}_${suffix}${id.substring(i)}`
886
- : id + '_' + suffix;
887
- }
888
- suffixThesauriIds(ids) {
889
- if (!this.roleIdInThesauri || !ids.length || !this.identity().roleId) {
890
- return ids;
891
- }
892
- return ids.map((id) => this.suffixThesaurusId(id, this.identity().roleId));
893
- }
894
- ngOnInit() {
895
- // load data
896
- this.loading.set(true);
897
- const thesIds = this.getReqThesauriIds();
898
- const sfxThesIds = this.suffixThesauriIds(thesIds);
899
- this.editorService
900
- .load(this.identity(), sfxThesIds)
901
- .then((data) => {
902
- if (data) {
903
- // if we loaded suffixed thesauri, add an unsuffixed alias to each
904
- if (this.roleIdInThesauri && this.identity().roleId) {
905
- for (let i = 0; i < sfxThesIds.length; i++) {
906
- data.thesauri[thesIds[i]] = data.thesauri[sfxThesIds[i]];
907
- }
908
- }
909
- this.data.set(data);
910
- this.onDataLoaded();
911
- }
912
- })
913
- .catch((reason) => {
914
- this.snackbar.open(reason.message, 'OK');
915
- });
916
- }
917
- ngOnDestroy() {
918
- this._loadingSub?.unsubscribe();
919
- this._savingSub?.unsubscribe();
920
- }
921
- canDeactivate() {
922
- return !this.dirty();
923
- }
924
- /**
925
- * Called when the wrapped editor dirty state changes.
926
- * In the HTML template, you MUST bind this handler to the dirtyChange event
927
- * emitted by your wrapped editor (i.e. (dirtyChange)="onDirtyChange($event)").
928
- *
929
- * @param value The value of the dirty state.
930
- */
931
- onDirtyChange(value) {
932
- console.log('part dirty change (from editor): ' + value);
933
- this.dirty.set(value);
934
- }
935
- /**
936
- * Save the part.
937
- *
938
- * @param part The part to be saved.
939
- */
940
- save(part) {
941
- console.log('Saving part ID: ' + part.id);
942
- this.editorService.save(part).then((saved) => {
943
- // update the route-defined part ID if it was null (new part)
944
- console.log('Saved part ID: ' + saved.id);
945
- if (!this.identity().partId) {
946
- this.identity.set({ ...this.identity(), partId: saved.id });
947
- console.log('Updated identity: ', this.identity());
948
- }
949
- console.log('Part saved: ' + saved.id);
950
- this.snackbar.open('Part saved', 'OK', {
951
- duration: 3000,
952
- });
953
- }, (reason) => {
954
- // restore dirty: save failed, the editor data was not persisted
955
- this.dirty.set(true);
956
- this.snackbar.open(reason.message, 'OK');
957
- });
958
- }
959
- /**
960
- * Close the editor by navigating back to the part's item.
961
- */
962
- close() {
963
- this.router.navigate(['items', this.identity().itemId]);
964
- }
965
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditPartFeatureBase, deps: [{ token: i1$1.Router }, { token: i1$1.ActivatedRoute }, { token: i2.MatSnackBar }, { token: i1.ItemService }, { token: i1.ThesaurusService }, { token: PartEditorService }], target: i0.ɵɵFactoryTarget.Component });
966
- static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "22.0.1", type: EditPartFeatureBase, isStandalone: false, selector: "ng-component", ngImport: i0, template: '', isInline: true, changeDetection: i0.ChangeDetectionStrategy.Eager });
967
- }
968
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditPartFeatureBase, decorators: [{
969
- type: Component,
970
- args: [{
971
- template: '',
972
- changeDetection: ChangeDetectionStrategy.Eager,
973
- standalone: false,
974
- }]
975
- }], ctorParameters: () => [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: i1.ItemService }, { type: i1.ThesaurusService }, { type: PartEditorService }] });
976
-
977
- class EditedThesaurusRepository {
978
- _thesaurusService;
979
- _thesaurus$;
980
- _loading$;
981
- _saving$;
982
- loading$;
983
- saving$;
984
- get thesaurus$() {
985
- return this._thesaurus$.asObservable();
986
- }
987
- constructor(_thesaurusService) {
988
- this._thesaurusService = _thesaurusService;
989
- this._thesaurus$ = new BehaviorSubject(undefined);
990
- this._loading$ = new BehaviorSubject(false);
991
- this.loading$ = this._loading$.asObservable();
992
- this._saving$ = new BehaviorSubject(false);
993
- this.saving$ = this._saving$.asObservable();
994
- }
995
- getThesaurus() {
996
- return this._thesaurus$.value;
997
- }
998
- /**
999
- * Load the specified thesaurus.
1000
- *
1001
- * @param id The thesaurus ID or undefined to load a new thesaurus.
1002
- */
1003
- load(id) {
1004
- this._loading$.next(true);
1005
- if (id) {
1006
- this._thesaurusService.getThesaurus(id, true).subscribe({
1007
- next: (thesaurus) => {
1008
- this._loading$.next(false);
1009
- this._thesaurus$.next(thesaurus);
1010
- },
1011
- error: (error) => {
1012
- this._loading$.next(false);
1013
- console.error('Error loding thesaurus', error);
1014
- },
1015
- });
1016
- }
1017
- else {
1018
- this._loading$.next(false);
1019
- this._thesaurus$.next({
1020
- id: '',
1021
- language: 'en',
1022
- entries: [],
1023
- });
1024
- }
1025
- }
1026
- /**
1027
- * Save the specified thesaurus.
1028
- *
1029
- * @param thesaurus The thesaurus to save.
1030
- * @returns Promise with saved thesaurus.
1031
- */
1032
- save(thesaurus) {
1033
- this._saving$.next(true);
1034
- return new Promise((resolve, reject) => {
1035
- this._thesaurusService.addThesaurus(thesaurus).subscribe({
1036
- next: (saved) => {
1037
- this._saving$.next(false);
1038
- this.load(saved.id);
1039
- resolve(saved);
1040
- },
1041
- error: (error) => {
1042
- this._saving$.next(false);
1043
- console.error('Error loding thesaurus', error);
1044
- reject(error);
1045
- },
1046
- });
1047
- });
1048
- }
1049
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedThesaurusRepository, deps: [{ token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
1050
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedThesaurusRepository, providedIn: 'root' });
1051
- }
1052
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedThesaurusRepository, decorators: [{
1053
- type: Injectable,
1054
- args: [{ providedIn: 'root' }]
1055
- }], ctorParameters: () => [{ type: i1.ThesaurusService }] });
1056
-
1057
- /**
1058
- * Edited layer part repository.
1059
- */
1060
- class EditedLayerRepository {
1061
- _itemService;
1062
- _facetService;
1063
- _thesaurusService;
1064
- // the layer part (=collection of fragments) being edited
1065
- _part$;
1066
- // the base text rendered into a plain string, whatever its original model.
1067
- // This is used for reference (e.g. show it to the user while editing),
1068
- // even if in some cases it can be enough to work with the base text in the
1069
- // layer part editor itself (this is the case of the token-based text,
1070
- // but not e.g. for the tiles-based text).
1071
- _baseText$;
1072
- // the base text part
1073
- _baseTextPart$;
1074
- // the fragments locations, collected from all the fragments
1075
- _locations$;
1076
- // the estimated chance of broken fragments in this layer: 0=safe,
1077
- // 1=potentially broken, 2=broken
1078
- _breakChance$;
1079
- // the layer fragments reconciliation hints. There is one hint for each
1080
- // fragment in the layer.
1081
- _layerHints$;
1082
- _thesauriSet;
1083
- _loading$;
1084
- _saving$;
1085
- get loading$() {
1086
- return this._loading$.asObservable();
1087
- }
1088
- get saving$() {
1089
- return this._saving$.asObservable();
1090
- }
1091
- get part$() {
1092
- return this._part$.asObservable();
1093
- }
1094
- get baseText$() {
1095
- return this._baseText$.asObservable();
1096
- }
1097
- get baseTextPart$() {
1098
- return this._baseTextPart$.asObservable();
1099
- }
1100
- get locations$() {
1101
- return this._locations$.asObservable();
1102
- }
1103
- get breakChance$() {
1104
- return this._breakChance$.asObservable();
1105
- }
1106
- get layerHints$() {
1107
- return this._layerHints$.asObservable();
1108
- }
1109
- get thesauriSet$() {
1110
- return this._thesauriSet.asObservable();
1111
- }
1112
- constructor(_itemService, _facetService, _thesaurusService) {
1113
- this._itemService = _itemService;
1114
- this._facetService = _facetService;
1115
- this._thesaurusService = _thesaurusService;
1116
- this._part$ = new BehaviorSubject(undefined);
1117
- this._baseText$ = new BehaviorSubject(undefined);
1118
- this._baseTextPart$ = new BehaviorSubject(undefined);
1119
- this._locations$ = new BehaviorSubject([]);
1120
- this._breakChance$ = new BehaviorSubject(-1);
1121
- this._layerHints$ = new BehaviorSubject([]);
1122
- this._thesauriSet = new BehaviorSubject(undefined);
1123
- this._loading$ = new BehaviorSubject(false);
1124
- this._saving$ = new BehaviorSubject(false);
1125
- }
1126
- reset() {
1127
- this._part$.next(undefined);
1128
- this._baseText$.next(undefined);
1129
- this._baseTextPart$.next(undefined);
1130
- this._locations$.next([]);
1131
- this._breakChance$.next(-1);
1132
- this._layerHints$.next([]);
1133
- }
1134
- getPart() {
1135
- return this._part$.value;
1136
- }
1137
- getBaseText() {
1138
- return this._baseText$.value;
1139
- }
1140
- getLocations() {
1141
- return this._locations$.value;
1142
- }
1143
- getPartLocations(part) {
1144
- const locations = [];
1145
- if (part && part.fragments) {
1146
- part.fragments.forEach((p) => {
1147
- locations.push(TokenLocation.parse(p.location));
1148
- });
1149
- }
1150
- return locations;
1151
- }
1152
- loadWithThesauri(itemId, partId, thesauriIds) {
1153
- // remove trailing ! from IDs if any
1154
- const unscopedIds = thesauriIds.map((id) => {
1155
- return this._thesaurusService.getScopedId(id);
1156
- });
1157
- this._loading$.next(true);
1158
- forkJoin({
1159
- // TODO: eventually optimize by adding method param to load only fragments locations
1160
- layerPart: this._itemService.getPart(partId),
1161
- baseText: this._itemService.getBaseTextPart(itemId),
1162
- layers: this._facetService.getFacetParts(itemId, true),
1163
- breakChance: this._itemService.getLayerPartBreakChance(partId),
1164
- layerHints: this._itemService.getLayerPartHints(partId),
1165
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
1166
- }).subscribe({
1167
- next: (result) => {
1168
- // if the loaded part has a thesaurus scope, reload the thesauri
1169
- if (result.layerPart?.thesaurusScope) {
1170
- const scopedIds = thesauriIds.map((id) => {
1171
- return this._thesaurusService.getScopedId(id, result.layerPart.thesaurusScope);
1172
- });
1173
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
1174
- next: (thesauri) => {
1175
- this._loading$.next(false);
1176
- this._part$.next(result.layerPart);
1177
- this._baseText$.next(result.baseText.text);
1178
- this._baseTextPart$.next(result.baseText.part);
1179
- this._locations$.next(this.getPartLocations(result.layerPart));
1180
- this._breakChance$.next(result.breakChance.chance);
1181
- this._layerHints$.next(result.layerHints);
1182
- this._thesauriSet.next(thesauri);
1183
- },
1184
- error: (error) => {
1185
- this._loading$.next(false);
1186
- console.error('Error loading thesauri', error);
1187
- },
1188
- });
1189
- }
1190
- else {
1191
- this._loading$.next(false);
1192
- this._part$.next(result.layerPart);
1193
- this._baseText$.next(result.baseText.text);
1194
- this._baseTextPart$.next(result.baseText.part);
1195
- this._locations$.next(this.getPartLocations(result.layerPart));
1196
- this._breakChance$.next(result.breakChance.chance);
1197
- this._layerHints$.next(result.layerHints);
1198
- this._thesauriSet.next(result.thesauri);
1199
- }
1200
- },
1201
- error: (error) => {
1202
- this._loading$.next(false);
1203
- console.error('Error loading text layer part', error);
1204
- },
1205
- });
1206
- }
1207
- loadWithoutThesauri(itemId, partId) {
1208
- this._loading$.next(true);
1209
- forkJoin({
1210
- // TODO: eventually optimize by adding method param to load only fragments locations
1211
- layerPart: this._itemService.getPart(partId),
1212
- baseText: this._itemService.getBaseTextPart(itemId),
1213
- layers: this._facetService.getFacetParts(itemId, true),
1214
- breakChance: this._itemService.getLayerPartBreakChance(partId),
1215
- layerHints: this._itemService.getLayerPartHints(partId),
1216
- }).subscribe({
1217
- next: (result) => {
1218
- this._loading$.next(false);
1219
- this._part$.next(result.layerPart);
1220
- this._baseText$.next(result.baseText.text);
1221
- this._baseTextPart$.next(result.baseText.part);
1222
- this._locations$.next(this.getPartLocations(result.layerPart));
1223
- this._breakChance$.next(result.breakChance.chance);
1224
- this._layerHints$.next(result.layerHints);
1225
- },
1226
- error: (error) => {
1227
- this._loading$.next(false);
1228
- console.error('Error loading text layer part', error);
1229
- },
1230
- });
1231
- }
1232
- /**
1233
- * Load the state for editing layer part(s).
1234
- *
1235
- * @param itemId The item ID the layer part belongs to.
1236
- * @param partId The layer part ID.
1237
- * @param thesauriIds The optional thesauri IDs to load.
1238
- */
1239
- load(itemId, partId, thesauriIds) {
1240
- if (thesauriIds?.length) {
1241
- this.loadWithThesauri(itemId, partId, thesauriIds);
1242
- }
1243
- else {
1244
- this.loadWithoutThesauri(itemId, partId);
1245
- }
1246
- }
1247
- /**
1248
- * Refresh the layer part break chance.
1249
- */
1250
- refreshBreakChance() {
1251
- const part = this._part$.value;
1252
- if (!part) {
1253
- return;
1254
- }
1255
- this._itemService.getLayerPartBreakChance(part.id).subscribe({
1256
- next: (result) => {
1257
- this._breakChance$.next(result.chance);
1258
- },
1259
- error: (error) => {
1260
- console.error('Error calculating break chance', error);
1261
- this._breakChance$.next(-1);
1262
- },
1263
- });
1264
- }
1265
- applyLayerPatches(partId, patches) {
1266
- this._saving$.next(true);
1267
- this._itemService.applyLayerPatches(partId, patches).subscribe({
1268
- next: (part) => {
1269
- this._saving$.next(false);
1270
- this.load(part.itemId, partId);
1271
- },
1272
- error: (error) => {
1273
- this._saving$.next(false);
1274
- console.error('Error patching text layer part', error);
1275
- },
1276
- });
1277
- }
1278
- /**
1279
- * Delete the fragment at the specified location.
1280
- *
1281
- * @param loc The fragment's location.
1282
- */
1283
- deleteFragment(loc) {
1284
- // find the fragment
1285
- let part = this._part$.value;
1286
- if (!part) {
1287
- return;
1288
- }
1289
- const i = part.fragments.findIndex((p) => {
1290
- return TokenLocation.parse(p.location)?.overlaps(loc);
1291
- });
1292
- if (i === -1) {
1293
- return;
1294
- }
1295
- this._saving$.next(true);
1296
- // remove it from the part
1297
- // work on a copy, as store objects are immutable
1298
- part = deepCopy(part);
1299
- part.fragments.splice(i, 1);
1300
- // update the part and reload state once done
1301
- this._itemService.addPart(part).subscribe({
1302
- next: (_) => {
1303
- this._saving$.next(false);
1304
- this.load(part.itemId, part.id);
1305
- },
1306
- error: (error) => {
1307
- this._saving$.next(false);
1308
- console.error(error);
1309
- console.error(`Error deleting fragment at ${loc} in part ${part.id}`, error);
1310
- },
1311
- });
1312
- }
1313
- /**
1314
- * Save the specified fragment.
1315
- *
1316
- * @param fragment The fragment.
1317
- */
1318
- saveFragment(fragment) {
1319
- // find the fragment
1320
- let part = this._part$.value;
1321
- if (!part) {
1322
- return;
1323
- }
1324
- // add or replace it
1325
- // work on a copy, as store objects are immutable
1326
- this._saving$.next(true);
1327
- part = deepCopy(part);
1328
- // replace all the overlapping fragments with the new one
1329
- const newLoc = TokenLocation.parse(fragment.location);
1330
- let insertAt = 0;
1331
- for (let i = part.fragments.length - 1; i > -1; i--) {
1332
- const frLoc = TokenLocation.parse(part.fragments[i].location);
1333
- if (newLoc.compareTo(frLoc) >= 0) {
1334
- insertAt = i + 1;
1335
- }
1336
- if (newLoc.overlaps(frLoc)) {
1337
- part.fragments.splice(i, 1);
1338
- if (insertAt > i && insertAt > 0) {
1339
- insertAt--;
1340
- }
1341
- }
1342
- }
1343
- // add the new fragment
1344
- part.fragments.splice(insertAt, 0, fragment);
1345
- // update the part and reload once done
1346
- this._itemService.addPart(part).subscribe({
1347
- next: (_) => {
1348
- this._saving$.next(false);
1349
- this.load(part.itemId, part.id);
1350
- },
1351
- error: (error) => {
1352
- this._saving$.next(false);
1353
- console.error(`Error saving fragment at ${fragment.location} in part ${part.id}`, error);
1354
- },
1355
- });
1356
- }
1357
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedLayerRepository, deps: [{ token: i1.ItemService }, { token: i1.FacetService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
1358
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedLayerRepository, providedIn: 'root' });
1359
- }
1360
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.1", ngImport: i0, type: EditedLayerRepository, decorators: [{
1361
- type: Injectable,
1362
- args: [{ providedIn: 'root' }]
1363
- }], ctorParameters: () => [{ type: i1.ItemService }, { type: i1.FacetService }, { type: i1.ThesaurusService }] });
1364
-
1365
- /*
1366
- * Public API Surface of cadmus-state
1367
- */
1368
-
1369
- /**
1370
- * Generated bundle index. Do not edit.
1371
- */
1372
-
1
+ import * as i0 from "@angular/core";
2
+ import { ChangeDetectionStrategy, Component, Injectable, signal } from "@angular/core";
3
+ import { BehaviorSubject, forkJoin, map, take } from "rxjs";
4
+ import * as i1$1 from "@myrmidon/cadmus-api";
5
+ import * as i4 from "@myrmidon/cadmus-core";
6
+ import { TokenLocation } from "@myrmidon/cadmus-core";
7
+ import { deepCopy } from "@myrmidon/ngx-tools";
8
+ import * as i1 from "@angular/router";
9
+ import * as i2 from "@angular/material/snack-bar";
10
+ var AppRepository = class AppRepository {
11
+ _facetService;
12
+ _flagService;
13
+ _thesaurusService;
14
+ _previewService;
15
+ _settingService;
16
+ _facets$;
17
+ _flags$;
18
+ _typeThesaurus$;
19
+ _itemBrowserThesaurus$;
20
+ _previewJKeys$;
21
+ _previewFKeys$;
22
+ _previewCKeys$;
23
+ _settingsCache;
24
+ get facets$() {
25
+ return this._facets$;
26
+ }
27
+ get flags$() {
28
+ return this._flags$;
29
+ }
30
+ get typeThesaurus$() {
31
+ return this._typeThesaurus$;
32
+ }
33
+ get itemBrowserThesaurus$() {
34
+ return this._itemBrowserThesaurus$;
35
+ }
36
+ get previewJKeys$() {
37
+ return this._previewJKeys$;
38
+ }
39
+ get previewFKeys$() {
40
+ return this._previewFKeys$;
41
+ }
42
+ get previewCKeys$() {
43
+ return this._previewCKeys$;
44
+ }
45
+ constructor(_facetService, _flagService, _thesaurusService, _previewService, _settingService) {
46
+ this._facetService = _facetService;
47
+ this._flagService = _flagService;
48
+ this._thesaurusService = _thesaurusService;
49
+ this._previewService = _previewService;
50
+ this._settingService = _settingService;
51
+ this._facets$ = new BehaviorSubject([]);
52
+ this._flags$ = new BehaviorSubject([]);
53
+ this._typeThesaurus$ = new BehaviorSubject(void 0);
54
+ this._itemBrowserThesaurus$ = new BehaviorSubject(void 0);
55
+ this._previewJKeys$ = new BehaviorSubject([]);
56
+ this._previewFKeys$ = new BehaviorSubject([]);
57
+ this._previewCKeys$ = new BehaviorSubject([]);
58
+ this._settingsCache = /* @__PURE__ */ new Map();
59
+ }
60
+ getTypeThesaurus() {
61
+ return this._typeThesaurus$.value;
62
+ }
63
+ getFacets() {
64
+ return this._facets$.value;
65
+ }
66
+ getFlags() {
67
+ return this._flags$.value;
68
+ }
69
+ load(refresh = false) {
70
+ if (this._facets$.value.length && !refresh) return Promise.resolve();
71
+ const facets$ = this._facetService.getFacets();
72
+ const flags$ = this._flagService.getFlags();
73
+ const thesauri$ = this._thesaurusService.getThesauriSet(["model-types@en", "item-browsers@en"]);
74
+ const jKeys$ = this._previewService.getKeys("J");
75
+ const fKeys$ = this._previewService.getKeys("F");
76
+ const cKeys$ = this._previewService.getKeys("C");
77
+ return new Promise((resolve, reject) => {
78
+ forkJoin({
79
+ facets: facets$,
80
+ flags: flags$,
81
+ thesauri: thesauri$,
82
+ jKeys: jKeys$,
83
+ fKeys: fKeys$,
84
+ cKeys: cKeys$
85
+ }).subscribe({
86
+ next: (result) => {
87
+ this._facets$.next(result.facets);
88
+ this._flags$.next(result.flags);
89
+ this._typeThesaurus$.next(result.thesauri["model-types"]);
90
+ this._itemBrowserThesaurus$.next(result.thesauri["item-browsers"]);
91
+ this._previewJKeys$.next(result.jKeys);
92
+ this._previewFKeys$.next(result.fKeys);
93
+ this._previewCKeys$.next(result.cKeys);
94
+ console.log(`AppRepository loaded (facets: ${result.facets.length}, flags: ${result.flags.length})`);
95
+ resolve();
96
+ },
97
+ error: (err) => {
98
+ reject(err);
99
+ }
100
+ });
101
+ });
102
+ }
103
+ clear() {
104
+ this._facets$.next([]);
105
+ this._flags$.next([]);
106
+ this._typeThesaurus$.next(void 0);
107
+ this._itemBrowserThesaurus$.next(void 0);
108
+ this._previewJKeys$.next([]);
109
+ this._previewFKeys$.next([]);
110
+ this._previewCKeys$.next([]);
111
+ this._settingsCache.clear();
112
+ }
113
+ loadThesauri() {
114
+ return new Promise((resolve, reject) => {
115
+ this._thesaurusService.getThesauriSet(["model-types@en", "item-browsers@en"]).subscribe({
116
+ next: (thesauri) => {
117
+ this._typeThesaurus$.next(thesauri["model-types"]);
118
+ this._itemBrowserThesaurus$.next(thesauri["item-browsers"]);
119
+ resolve();
120
+ },
121
+ error: (err) => {
122
+ reject(err);
123
+ }
124
+ });
125
+ });
126
+ }
127
+ getPartColor(typeId, roleId) {
128
+ for (let facet of this._facets$.value) for (let part of facet.partDefinitions) if (part.typeId === typeId && (!roleId || part.roleId === roleId)) return part.colorKey;
129
+ }
130
+ loadFlags() {
131
+ return new Promise((resolve, reject) => {
132
+ this._flagService.getFlags().subscribe({
133
+ next: (flags) => {
134
+ this._flags$.next(flags);
135
+ resolve();
136
+ },
137
+ error: (err) => {
138
+ reject(err);
139
+ }
140
+ });
141
+ });
142
+ }
143
+ getSetting(id, reload = false) {
144
+ return new Promise((resolve, reject) => {
145
+ if (this._settingsCache.has(id) && !reload) {
146
+ resolve(this._settingsCache.get(id));
147
+ return;
148
+ }
149
+ this._settingService.getSetting(id).subscribe({
150
+ next: (setting) => {
151
+ this._settingsCache.set(id, setting);
152
+ resolve(setting);
153
+ },
154
+ error: (err) => {
155
+ reject(err);
156
+ }
157
+ });
158
+ });
159
+ }
160
+ setSetting(id, setting) {
161
+ this._settingService.addSetting(id, setting).subscribe({
162
+ next: () => {
163
+ this._settingsCache.set(id, setting);
164
+ },
165
+ error: (err) => {
166
+ console.error(`Error setting setting ${id}:`, err);
167
+ }
168
+ });
169
+ }
170
+ getSettingFor(typeId, roleId, reload = false) {
171
+ const id = roleId ? `${typeId}_${roleId}` : typeId;
172
+ return this.getSetting(id, reload);
173
+ }
174
+ setSettingFor(typeId, setting, roleId) {
175
+ const id = roleId ? `${typeId}_${roleId}` : typeId;
176
+ this.setSetting(id, setting);
177
+ }
178
+ static ɵfac = i0.ɵɵngDeclareFactory({
179
+ minVersion: "12.0.0",
180
+ version: "22.2.0",
181
+ ngImport: i0,
182
+ type: AppRepository,
183
+ deps: [
184
+ { token: i1$1.FacetService },
185
+ { token: i1$1.FlagService },
186
+ { token: i1$1.ThesaurusService },
187
+ { token: i1$1.PreviewService },
188
+ { token: i1$1.EditorSettingsService }
189
+ ],
190
+ target: i0.ɵɵFactoryTarget.Injectable
191
+ });
192
+ static ɵprov = i0.ɵɵngDeclareInjectable({
193
+ minVersion: "12.0.0",
194
+ version: "22.2.0",
195
+ ngImport: i0,
196
+ type: AppRepository,
197
+ providedIn: "root"
198
+ });
199
+ };
200
+ i0.ɵɵngDeclareClassMetadata({
201
+ minVersion: "12.0.0",
202
+ version: "22.2.0",
203
+ ngImport: i0,
204
+ type: AppRepository,
205
+ decorators: [{
206
+ type: Injectable,
207
+ args: [{ providedIn: "root" }]
208
+ }],
209
+ ctorParameters: () => [
210
+ { type: i1$1.FacetService },
211
+ { type: i1$1.FlagService },
212
+ { type: i1$1.ThesaurusService },
213
+ { type: i1$1.PreviewService },
214
+ { type: i1$1.EditorSettingsService }
215
+ ]
216
+ });
217
+ var FragmentEditorService = class FragmentEditorService {
218
+ _itemService;
219
+ _thesaurusService;
220
+ _loading$;
221
+ _saving$;
222
+ loading$;
223
+ saving$;
224
+ constructor(_itemService, _thesaurusService) {
225
+ this._itemService = _itemService;
226
+ this._thesaurusService = _thesaurusService;
227
+ this._loading$ = new BehaviorSubject(false);
228
+ this.loading$ = this._loading$.asObservable();
229
+ this._saving$ = new BehaviorSubject(false);
230
+ this.saving$ = this._saving$.asObservable();
231
+ }
232
+ getFragmentFromPart(part, loc) {
233
+ return part.fragments.find((f) => f.location === loc) ?? { location: loc };
234
+ }
235
+ loadWithThesauri(identity, thesauriIds) {
236
+ const unscopedIds = thesauriIds.map((id) => {
237
+ return this._thesaurusService.getScopedId(id);
238
+ });
239
+ this._loading$.next(true);
240
+ return new Promise((resolve, reject) => {
241
+ forkJoin({
242
+ part: this._itemService.getPart(identity.partId || ""),
243
+ thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
244
+ t: this._itemService.getBaseTextPart(identity.itemId)
245
+ }).subscribe({
246
+ next: (result) => {
247
+ const layerPart = result.part;
248
+ const thesauri = result.thesauri;
249
+ const fr = this.getFragmentFromPart(layerPart, identity.loc);
250
+ if (layerPart.thesaurusScope) {
251
+ const scopedIds = thesauriIds.map((id) => {
252
+ return this._thesaurusService.getScopedId(id, layerPart.thesaurusScope);
253
+ });
254
+ this._thesaurusService.getThesauriSet(scopedIds).subscribe({
255
+ next: (scopedThesauri) => {
256
+ this._loading$.next(false);
257
+ resolve({
258
+ value: fr,
259
+ layerPart,
260
+ baseText: result.t.text,
261
+ thesauri: scopedThesauri
262
+ });
263
+ },
264
+ error: (error) => {
265
+ this._loading$.next(false);
266
+ console.error(error);
267
+ reject({
268
+ message: "Error loading thesauri " + scopedIds.join(", "),
269
+ error
270
+ });
271
+ }
272
+ });
273
+ } else {
274
+ this._loading$.next(false);
275
+ resolve({
276
+ value: fr,
277
+ layerPart,
278
+ baseText: result.t.text,
279
+ thesauri
280
+ });
281
+ }
282
+ },
283
+ error: (error) => {
284
+ this._loading$.next(false);
285
+ console.error(error);
286
+ reject({
287
+ message: "Error loading thesauri " + unscopedIds.join(", "),
288
+ error
289
+ });
290
+ }
291
+ });
292
+ });
293
+ }
294
+ loadWithoutThesauri(identity) {
295
+ this._loading$.next(true);
296
+ return new Promise((resolve, reject) => {
297
+ forkJoin({
298
+ t: this._itemService.getBaseTextPart(identity.itemId),
299
+ p: this._itemService.getPart(identity.partId)
300
+ }).subscribe({
301
+ next: (result) => {
302
+ this._loading$.next(false);
303
+ const layerPart = result.p;
304
+ resolve({
305
+ value: this.getFragmentFromPart(layerPart, identity.loc),
306
+ layerPart,
307
+ baseText: result.t.text,
308
+ thesauri: {}
309
+ });
310
+ },
311
+ error: (error) => {
312
+ this._loading$.next(false);
313
+ console.error(error);
314
+ reject({
315
+ message: "Error loading layer part " + identity.partId,
316
+ error
317
+ });
318
+ }
319
+ });
320
+ });
321
+ }
322
+ load(identity, thesauriIds) {
323
+ if (thesauriIds?.length) return this.loadWithThesauri(identity, thesauriIds);
324
+ else return this.loadWithoutThesauri(identity);
325
+ }
326
+ save(part) {
327
+ this._saving$.next(true);
328
+ return new Promise((resolve, reject) => {
329
+ this._itemService.addPart(part).subscribe({
330
+ next: (saved) => {
331
+ this._saving$.next(false);
332
+ resolve(saved);
333
+ },
334
+ error: (error) => {
335
+ this._saving$.next(false);
336
+ console.error(error);
337
+ reject({
338
+ message: "Error saving part " + part.id,
339
+ error
340
+ });
341
+ }
342
+ });
343
+ });
344
+ }
345
+ static ɵfac = i0.ɵɵngDeclareFactory({
346
+ minVersion: "12.0.0",
347
+ version: "22.2.0",
348
+ ngImport: i0,
349
+ type: FragmentEditorService,
350
+ deps: [{ token: i1$1.ItemService }, { token: i1$1.ThesaurusService }],
351
+ target: i0.ɵɵFactoryTarget.Injectable
352
+ });
353
+ static ɵprov = i0.ɵɵngDeclareInjectable({
354
+ minVersion: "12.0.0",
355
+ version: "22.2.0",
356
+ ngImport: i0,
357
+ type: FragmentEditorService,
358
+ providedIn: "root"
359
+ });
360
+ };
361
+ i0.ɵɵngDeclareClassMetadata({
362
+ minVersion: "12.0.0",
363
+ version: "22.2.0",
364
+ ngImport: i0,
365
+ type: FragmentEditorService,
366
+ decorators: [{
367
+ type: Injectable,
368
+ args: [{ providedIn: "root" }]
369
+ }],
370
+ ctorParameters: () => [{ type: i1$1.ItemService }, { type: i1$1.ThesaurusService }]
371
+ });
372
+ var EditFragmentFeatureBase = class EditFragmentFeatureBase {
373
+ _router;
374
+ snackbar;
375
+ editorService;
376
+ _libraryRouteService;
377
+ _loadingSub;
378
+ _savingSub;
379
+ data = signal(void 0, ...ngDevMode ? [{ debugName: "data" }] : /* istanbul ignore next */ []);
380
+ identity = signal({
381
+ itemId: "",
382
+ typeId: "",
383
+ partId: "",
384
+ roleId: null,
385
+ frTypeId: "",
386
+ frRoleId: null,
387
+ loc: ""
388
+ }, ...ngDevMode ? [{ debugName: "identity" }] : /* istanbul ignore next */ []);
389
+ dirty = signal(false, ...ngDevMode ? [{ debugName: "dirty" }] : /* istanbul ignore next */ []);
390
+ loading = signal(void 0, ...ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []);
391
+ saving = signal(void 0, ...ngDevMode ? [{ debugName: "saving" }] : /* istanbul ignore next */ []);
392
+ frLoc = signal(void 0, ...ngDevMode ? [{ debugName: "frLoc" }] : /* istanbul ignore next */ []);
393
+ constructor(_router, route, snackbar, editorService, _libraryRouteService) {
394
+ this._router = _router;
395
+ this.snackbar = snackbar;
396
+ this.editorService = editorService;
397
+ this._libraryRouteService = _libraryRouteService;
398
+ const itemId = route.snapshot.params["iid"];
399
+ const partId = route.snapshot.params["pid"];
400
+ const frTypeId = route.snapshot.url[2]?.path;
401
+ const loc = route.snapshot.params["loc"];
402
+ const frRoleId = route.snapshot.queryParams["frrid"];
403
+ this.identity.set({
404
+ itemId,
405
+ typeId: "",
406
+ partId,
407
+ roleId: frTypeId,
408
+ frTypeId,
409
+ frRoleId,
410
+ loc
411
+ });
412
+ this._loadingSub = this.editorService.loading$.subscribe((loading) => this.loading.set(loading));
413
+ this._savingSub = this.editorService.saving$.subscribe((saving) => this.saving.set(saving));
414
+ }
415
+ getReqThesauriIds() {
416
+ return [];
417
+ }
418
+ onDataLoaded() {}
419
+ ngOnInit() {
420
+ this.loading.set(true);
421
+ this.frLoc.set(TokenLocation.parse(this.identity().loc) ?? void 0);
422
+ const thesIds = this.getReqThesauriIds();
423
+ this.editorService.load(this.identity(), thesIds).then((data) => {
424
+ if (data) {
425
+ this.data.set(data);
426
+ this.onDataLoaded();
427
+ }
428
+ }).catch((reason) => {
429
+ this.snackbar.open(reason.message, "OK");
430
+ });
431
+ }
432
+ ngOnDestroy() {
433
+ this._loadingSub?.unsubscribe();
434
+ this._savingSub?.unsubscribe();
435
+ }
436
+ canDeactivate() {
437
+ return !this.dirty();
438
+ }
439
+ onDirtyChange(value) {
440
+ console.log("fragment dirty change (from editor): " + value);
441
+ this.dirty.set(value);
442
+ }
443
+ save(fragment) {
444
+ const part = deepCopy(this.data().layerPart);
445
+ const frIndex = part.fragments.findIndex((f) => f.location === this.identity().loc);
446
+ if (frIndex > -1) part.fragments.splice(frIndex, 1, fragment);
447
+ else part.fragments.push(fragment);
448
+ this.editorService.save(part).then((p) => {
449
+ console.log(p.id);
450
+ this.snackbar.open("Fragment saved", "OK", { duration: 3e3 });
451
+ }, (error) => {
452
+ this.dirty.set(true);
453
+ console.error(error);
454
+ this.snackbar.open("Error saving fragment", "OK");
455
+ });
456
+ }
457
+ close() {
458
+ const part = this.data().layerPart;
459
+ const editorKey = this._libraryRouteService.getEditorKeyFromPartType(part.typeId, part.roleId);
460
+ const url = `/items/${this.identity().itemId}/${editorKey.partKey}/${part.typeId}/${this.identity().partId}`;
461
+ this._router.navigate([url], { queryParams: { rid: this.identity().frTypeId } });
462
+ }
463
+ static ɵfac = i0.ɵɵngDeclareFactory({
464
+ minVersion: "12.0.0",
465
+ version: "22.2.0",
466
+ ngImport: i0,
467
+ type: EditFragmentFeatureBase,
468
+ deps: [
469
+ { token: i1.Router },
470
+ { token: i1.ActivatedRoute },
471
+ { token: i2.MatSnackBar },
472
+ { token: FragmentEditorService },
473
+ { token: i4.LibraryRouteService }
474
+ ],
475
+ target: i0.ɵɵFactoryTarget.Component
476
+ });
477
+ static ɵcmp = i0.ɵɵngDeclareComponent({
478
+ minVersion: "14.0.0",
479
+ version: "22.2.0",
480
+ type: EditFragmentFeatureBase,
481
+ isStandalone: false,
482
+ selector: "ng-component",
483
+ ngImport: i0,
484
+ template: "",
485
+ isInline: true,
486
+ changeDetection: i0.ChangeDetectionStrategy.Eager
487
+ });
488
+ };
489
+ i0.ɵɵngDeclareClassMetadata({
490
+ minVersion: "12.0.0",
491
+ version: "22.2.0",
492
+ ngImport: i0,
493
+ type: EditFragmentFeatureBase,
494
+ decorators: [{
495
+ type: Component,
496
+ args: [{
497
+ template: "",
498
+ changeDetection: ChangeDetectionStrategy.Eager,
499
+ standalone: false
500
+ }]
501
+ }],
502
+ ctorParameters: () => [
503
+ { type: i1.Router },
504
+ { type: i1.ActivatedRoute },
505
+ { type: i2.MatSnackBar },
506
+ { type: FragmentEditorService },
507
+ { type: i4.LibraryRouteService }
508
+ ]
509
+ });
510
+ var PartEditorService = class PartEditorService {
511
+ _itemService;
512
+ _thesaurusService;
513
+ _loading$;
514
+ _saving$;
515
+ loading$;
516
+ saving$;
517
+ constructor(_itemService, _thesaurusService) {
518
+ this._itemService = _itemService;
519
+ this._thesaurusService = _thesaurusService;
520
+ this._loading$ = new BehaviorSubject(false);
521
+ this.loading$ = this._loading$.asObservable();
522
+ this._saving$ = new BehaviorSubject(false);
523
+ this.saving$ = this._saving$.asObservable();
524
+ }
525
+ loadWithThesauri(identity, thesauriIds) {
526
+ const unscopedIds = thesauriIds.map((id) => {
527
+ return this._thesaurusService.getScopedId(id);
528
+ });
529
+ this._loading$.next(true);
530
+ return new Promise((resolve, reject) => {
531
+ forkJoin({
532
+ part: this._itemService.getPart(identity.partId || ""),
533
+ thesauri: this._thesaurusService.getThesauriSet(unscopedIds)
534
+ }).subscribe({
535
+ next: (result) => {
536
+ const part = result.part;
537
+ const thesauri = result.thesauri;
538
+ if (part?.thesaurusScope) {
539
+ const scopedIds = thesauriIds.map((id) => {
540
+ return this._thesaurusService.getScopedId(id, part.thesaurusScope);
541
+ });
542
+ this._thesaurusService.getThesauriSet(scopedIds).subscribe({
543
+ next: (scopedThesauri) => {
544
+ this._loading$.next(false);
545
+ resolve({
546
+ value: part,
547
+ thesauri: scopedThesauri
548
+ });
549
+ },
550
+ error: (error) => {
551
+ this._loading$.next(false);
552
+ console.error(error);
553
+ reject({
554
+ message: "Error loading thesauri " + scopedIds.join(", "),
555
+ error
556
+ });
557
+ }
558
+ });
559
+ } else {
560
+ this._loading$.next(false);
561
+ resolve({
562
+ value: part,
563
+ thesauri
564
+ });
565
+ }
566
+ },
567
+ error: (error) => {
568
+ this._loading$.next(false);
569
+ console.error(error);
570
+ reject({
571
+ message: "Error loading thesauri " + unscopedIds.join(", "),
572
+ error
573
+ });
574
+ }
575
+ });
576
+ });
577
+ }
578
+ loadWithoutThesauri(partId) {
579
+ return new Promise((resolve, reject) => {
580
+ this._loading$.next(true);
581
+ this._itemService.getPart(partId).pipe(take(1), map((part) => {
582
+ return part ? {
583
+ value: part,
584
+ thesauri: {}
585
+ } : null;
586
+ })).subscribe({
587
+ next: (result) => {
588
+ this._loading$.next(false);
589
+ resolve(result);
590
+ },
591
+ error: (error) => {
592
+ this._loading$.next(false);
593
+ console.error(error);
594
+ reject({
595
+ message: "Error loading part " + partId,
596
+ error
597
+ });
598
+ }
599
+ });
600
+ });
601
+ }
602
+ load(identity, thesauriIds) {
603
+ if (thesauriIds?.length) return this.loadWithThesauri(identity, thesauriIds);
604
+ else return this.loadWithoutThesauri(identity.partId);
605
+ }
606
+ save(part) {
607
+ this._saving$.next(true);
608
+ return new Promise((resolve, reject) => {
609
+ this._itemService.addPart(part).subscribe({
610
+ next: (saved) => {
611
+ this._saving$.next(false);
612
+ resolve(saved);
613
+ },
614
+ error: (error) => {
615
+ this._saving$.next(false);
616
+ console.error(error);
617
+ reject({
618
+ message: "Error saving part " + part.id,
619
+ error
620
+ });
621
+ }
622
+ });
623
+ });
624
+ }
625
+ static ɵfac = i0.ɵɵngDeclareFactory({
626
+ minVersion: "12.0.0",
627
+ version: "22.2.0",
628
+ ngImport: i0,
629
+ type: PartEditorService,
630
+ deps: [{ token: i1$1.ItemService }, { token: i1$1.ThesaurusService }],
631
+ target: i0.ɵɵFactoryTarget.Injectable
632
+ });
633
+ static ɵprov = i0.ɵɵngDeclareInjectable({
634
+ minVersion: "12.0.0",
635
+ version: "22.2.0",
636
+ ngImport: i0,
637
+ type: PartEditorService,
638
+ providedIn: "root"
639
+ });
640
+ };
641
+ i0.ɵɵngDeclareClassMetadata({
642
+ minVersion: "12.0.0",
643
+ version: "22.2.0",
644
+ ngImport: i0,
645
+ type: PartEditorService,
646
+ decorators: [{
647
+ type: Injectable,
648
+ args: [{ providedIn: "root" }]
649
+ }],
650
+ ctorParameters: () => [{ type: i1$1.ItemService }, { type: i1$1.ThesaurusService }]
651
+ });
652
+ var EditPartFeatureBase = class EditPartFeatureBase {
653
+ router;
654
+ snackbar;
655
+ itemService;
656
+ thesaurusService;
657
+ editorService;
658
+ _loadingSub;
659
+ _savingSub;
660
+ data = signal(void 0, ...ngDevMode ? [{ debugName: "data" }] : /* istanbul ignore next */ []);
661
+ identity = signal({
662
+ itemId: "",
663
+ typeId: "",
664
+ partId: null,
665
+ roleId: null
666
+ }, ...ngDevMode ? [{ debugName: "identity" }] : /* istanbul ignore next */ []);
667
+ dirty = signal(false, ...ngDevMode ? [{ debugName: "dirty" }] : /* istanbul ignore next */ []);
668
+ loading = signal(void 0, ...ngDevMode ? [{ debugName: "loading" }] : /* istanbul ignore next */ []);
669
+ saving = signal(void 0, ...ngDevMode ? [{ debugName: "saving" }] : /* istanbul ignore next */ []);
670
+ roleIdInThesauri;
671
+ constructor(router, route, snackbar, itemService, thesaurusService, editorService) {
672
+ this.router = router;
673
+ this.snackbar = snackbar;
674
+ this.itemService = itemService;
675
+ this.thesaurusService = thesaurusService;
676
+ this.editorService = editorService;
677
+ const itemId = route.snapshot.params["iid"];
678
+ const typeId = route.snapshot.routeConfig.path.substring(0, route.snapshot.routeConfig.path.indexOf("/"));
679
+ let partId = route.snapshot.params["pid"];
680
+ if (partId === "new") partId = null;
681
+ let roleId = route.snapshot.queryParams["rid"];
682
+ if (roleId === "default") roleId = null;
683
+ this.identity.set({
684
+ itemId,
685
+ typeId,
686
+ partId,
687
+ roleId
688
+ });
689
+ this._loadingSub = this.editorService.loading$.subscribe((loading) => this.loading.set(loading));
690
+ this._savingSub = this.editorService.saving$.subscribe((saving) => this.saving.set(saving));
691
+ }
692
+ getReqThesauriIds() {
693
+ return [];
694
+ }
695
+ onDataLoaded() {}
696
+ suffixThesaurusId(id, suffix) {
697
+ const i = id.lastIndexOf("@");
698
+ return i > -1 ? `${id.substring(0, i)}_${suffix}${id.substring(i)}` : id + "_" + suffix;
699
+ }
700
+ suffixThesauriIds(ids) {
701
+ if (!this.roleIdInThesauri || !ids.length || !this.identity().roleId) return ids;
702
+ return ids.map((id) => this.suffixThesaurusId(id, this.identity().roleId));
703
+ }
704
+ ngOnInit() {
705
+ this.loading.set(true);
706
+ const thesIds = this.getReqThesauriIds();
707
+ const sfxThesIds = this.suffixThesauriIds(thesIds);
708
+ this.editorService.load(this.identity(), sfxThesIds).then((data) => {
709
+ if (data) {
710
+ if (this.roleIdInThesauri && this.identity().roleId) for (let i = 0; i < sfxThesIds.length; i++) data.thesauri[thesIds[i]] = data.thesauri[sfxThesIds[i]];
711
+ this.data.set(data);
712
+ this.onDataLoaded();
713
+ }
714
+ }).catch((reason) => {
715
+ this.snackbar.open(reason.message, "OK");
716
+ });
717
+ }
718
+ ngOnDestroy() {
719
+ this._loadingSub?.unsubscribe();
720
+ this._savingSub?.unsubscribe();
721
+ }
722
+ canDeactivate() {
723
+ return !this.dirty();
724
+ }
725
+ onDirtyChange(value) {
726
+ console.log("part dirty change (from editor): " + value);
727
+ this.dirty.set(value);
728
+ }
729
+ save(part) {
730
+ console.log("Saving part ID: " + part.id);
731
+ this.editorService.save(part).then((saved) => {
732
+ console.log("Saved part ID: " + saved.id);
733
+ if (!this.identity().partId) {
734
+ this.identity.set({
735
+ ...this.identity(),
736
+ partId: saved.id
737
+ });
738
+ console.log("Updated identity: ", this.identity());
739
+ }
740
+ console.log("Part saved: " + saved.id);
741
+ this.snackbar.open("Part saved", "OK", { duration: 3e3 });
742
+ }, (reason) => {
743
+ this.dirty.set(true);
744
+ this.snackbar.open(reason.message, "OK");
745
+ });
746
+ }
747
+ close() {
748
+ this.router.navigate(["items", this.identity().itemId]);
749
+ }
750
+ static ɵfac = i0.ɵɵngDeclareFactory({
751
+ minVersion: "12.0.0",
752
+ version: "22.2.0",
753
+ ngImport: i0,
754
+ type: EditPartFeatureBase,
755
+ deps: [
756
+ { token: i1.Router },
757
+ { token: i1.ActivatedRoute },
758
+ { token: i2.MatSnackBar },
759
+ { token: i1$1.ItemService },
760
+ { token: i1$1.ThesaurusService },
761
+ { token: PartEditorService }
762
+ ],
763
+ target: i0.ɵɵFactoryTarget.Component
764
+ });
765
+ static ɵcmp = i0.ɵɵngDeclareComponent({
766
+ minVersion: "14.0.0",
767
+ version: "22.2.0",
768
+ type: EditPartFeatureBase,
769
+ isStandalone: false,
770
+ selector: "ng-component",
771
+ ngImport: i0,
772
+ template: "",
773
+ isInline: true,
774
+ changeDetection: i0.ChangeDetectionStrategy.Eager
775
+ });
776
+ };
777
+ i0.ɵɵngDeclareClassMetadata({
778
+ minVersion: "12.0.0",
779
+ version: "22.2.0",
780
+ ngImport: i0,
781
+ type: EditPartFeatureBase,
782
+ decorators: [{
783
+ type: Component,
784
+ args: [{
785
+ template: "",
786
+ changeDetection: ChangeDetectionStrategy.Eager,
787
+ standalone: false
788
+ }]
789
+ }],
790
+ ctorParameters: () => [
791
+ { type: i1.Router },
792
+ { type: i1.ActivatedRoute },
793
+ { type: i2.MatSnackBar },
794
+ { type: i1$1.ItemService },
795
+ { type: i1$1.ThesaurusService },
796
+ { type: PartEditorService }
797
+ ]
798
+ });
799
+ var EditedThesaurusRepository = class EditedThesaurusRepository {
800
+ _thesaurusService;
801
+ _thesaurus$;
802
+ _loading$;
803
+ _saving$;
804
+ loading$;
805
+ saving$;
806
+ get thesaurus$() {
807
+ return this._thesaurus$.asObservable();
808
+ }
809
+ constructor(_thesaurusService) {
810
+ this._thesaurusService = _thesaurusService;
811
+ this._thesaurus$ = new BehaviorSubject(void 0);
812
+ this._loading$ = new BehaviorSubject(false);
813
+ this.loading$ = this._loading$.asObservable();
814
+ this._saving$ = new BehaviorSubject(false);
815
+ this.saving$ = this._saving$.asObservable();
816
+ }
817
+ getThesaurus() {
818
+ return this._thesaurus$.value;
819
+ }
820
+ load(id) {
821
+ this._loading$.next(true);
822
+ if (id) this._thesaurusService.getThesaurus(id, true).subscribe({
823
+ next: (thesaurus) => {
824
+ this._loading$.next(false);
825
+ this._thesaurus$.next(thesaurus);
826
+ },
827
+ error: (error) => {
828
+ this._loading$.next(false);
829
+ console.error("Error loding thesaurus", error);
830
+ }
831
+ });
832
+ else {
833
+ this._loading$.next(false);
834
+ this._thesaurus$.next({
835
+ id: "",
836
+ language: "en",
837
+ entries: []
838
+ });
839
+ }
840
+ }
841
+ save(thesaurus) {
842
+ this._saving$.next(true);
843
+ return new Promise((resolve, reject) => {
844
+ this._thesaurusService.addThesaurus(thesaurus).subscribe({
845
+ next: (saved) => {
846
+ this._saving$.next(false);
847
+ this.load(saved.id);
848
+ resolve(saved);
849
+ },
850
+ error: (error) => {
851
+ this._saving$.next(false);
852
+ console.error("Error loding thesaurus", error);
853
+ reject(error);
854
+ }
855
+ });
856
+ });
857
+ }
858
+ static ɵfac = i0.ɵɵngDeclareFactory({
859
+ minVersion: "12.0.0",
860
+ version: "22.2.0",
861
+ ngImport: i0,
862
+ type: EditedThesaurusRepository,
863
+ deps: [{ token: i1$1.ThesaurusService }],
864
+ target: i0.ɵɵFactoryTarget.Injectable
865
+ });
866
+ static ɵprov = i0.ɵɵngDeclareInjectable({
867
+ minVersion: "12.0.0",
868
+ version: "22.2.0",
869
+ ngImport: i0,
870
+ type: EditedThesaurusRepository,
871
+ providedIn: "root"
872
+ });
873
+ };
874
+ i0.ɵɵngDeclareClassMetadata({
875
+ minVersion: "12.0.0",
876
+ version: "22.2.0",
877
+ ngImport: i0,
878
+ type: EditedThesaurusRepository,
879
+ decorators: [{
880
+ type: Injectable,
881
+ args: [{ providedIn: "root" }]
882
+ }],
883
+ ctorParameters: () => [{ type: i1$1.ThesaurusService }]
884
+ });
885
+ var EditedLayerRepository = class EditedLayerRepository {
886
+ _itemService;
887
+ _facetService;
888
+ _thesaurusService;
889
+ _part$;
890
+ _baseText$;
891
+ _baseTextPart$;
892
+ _locations$;
893
+ _breakChance$;
894
+ _layerHints$;
895
+ _thesauriSet;
896
+ _loading$;
897
+ _saving$;
898
+ get loading$() {
899
+ return this._loading$.asObservable();
900
+ }
901
+ get saving$() {
902
+ return this._saving$.asObservable();
903
+ }
904
+ get part$() {
905
+ return this._part$.asObservable();
906
+ }
907
+ get baseText$() {
908
+ return this._baseText$.asObservable();
909
+ }
910
+ get baseTextPart$() {
911
+ return this._baseTextPart$.asObservable();
912
+ }
913
+ get locations$() {
914
+ return this._locations$.asObservable();
915
+ }
916
+ get breakChance$() {
917
+ return this._breakChance$.asObservable();
918
+ }
919
+ get layerHints$() {
920
+ return this._layerHints$.asObservable();
921
+ }
922
+ get thesauriSet$() {
923
+ return this._thesauriSet.asObservable();
924
+ }
925
+ constructor(_itemService, _facetService, _thesaurusService) {
926
+ this._itemService = _itemService;
927
+ this._facetService = _facetService;
928
+ this._thesaurusService = _thesaurusService;
929
+ this._part$ = new BehaviorSubject(void 0);
930
+ this._baseText$ = new BehaviorSubject(void 0);
931
+ this._baseTextPart$ = new BehaviorSubject(void 0);
932
+ this._locations$ = new BehaviorSubject([]);
933
+ this._breakChance$ = new BehaviorSubject(-1);
934
+ this._layerHints$ = new BehaviorSubject([]);
935
+ this._thesauriSet = new BehaviorSubject(void 0);
936
+ this._loading$ = new BehaviorSubject(false);
937
+ this._saving$ = new BehaviorSubject(false);
938
+ }
939
+ reset() {
940
+ this._part$.next(void 0);
941
+ this._baseText$.next(void 0);
942
+ this._baseTextPart$.next(void 0);
943
+ this._locations$.next([]);
944
+ this._breakChance$.next(-1);
945
+ this._layerHints$.next([]);
946
+ }
947
+ getPart() {
948
+ return this._part$.value;
949
+ }
950
+ getBaseText() {
951
+ return this._baseText$.value;
952
+ }
953
+ getLocations() {
954
+ return this._locations$.value;
955
+ }
956
+ getPartLocations(part) {
957
+ const locations = [];
958
+ if (part && part.fragments) part.fragments.forEach((p) => {
959
+ locations.push(TokenLocation.parse(p.location));
960
+ });
961
+ return locations;
962
+ }
963
+ loadWithThesauri(itemId, partId, thesauriIds) {
964
+ const unscopedIds = thesauriIds.map((id) => {
965
+ return this._thesaurusService.getScopedId(id);
966
+ });
967
+ this._loading$.next(true);
968
+ forkJoin({
969
+ layerPart: this._itemService.getPart(partId),
970
+ baseText: this._itemService.getBaseTextPart(itemId),
971
+ layers: this._facetService.getFacetParts(itemId, true),
972
+ breakChance: this._itemService.getLayerPartBreakChance(partId),
973
+ layerHints: this._itemService.getLayerPartHints(partId),
974
+ thesauri: this._thesaurusService.getThesauriSet(unscopedIds)
975
+ }).subscribe({
976
+ next: (result) => {
977
+ if (result.layerPart?.thesaurusScope) {
978
+ const scopedIds = thesauriIds.map((id) => {
979
+ return this._thesaurusService.getScopedId(id, result.layerPart.thesaurusScope);
980
+ });
981
+ this._thesaurusService.getThesauriSet(scopedIds).subscribe({
982
+ next: (thesauri) => {
983
+ this._loading$.next(false);
984
+ this._part$.next(result.layerPart);
985
+ this._baseText$.next(result.baseText.text);
986
+ this._baseTextPart$.next(result.baseText.part);
987
+ this._locations$.next(this.getPartLocations(result.layerPart));
988
+ this._breakChance$.next(result.breakChance.chance);
989
+ this._layerHints$.next(result.layerHints);
990
+ this._thesauriSet.next(thesauri);
991
+ },
992
+ error: (error) => {
993
+ this._loading$.next(false);
994
+ console.error("Error loading thesauri", error);
995
+ }
996
+ });
997
+ } else {
998
+ this._loading$.next(false);
999
+ this._part$.next(result.layerPart);
1000
+ this._baseText$.next(result.baseText.text);
1001
+ this._baseTextPart$.next(result.baseText.part);
1002
+ this._locations$.next(this.getPartLocations(result.layerPart));
1003
+ this._breakChance$.next(result.breakChance.chance);
1004
+ this._layerHints$.next(result.layerHints);
1005
+ this._thesauriSet.next(result.thesauri);
1006
+ }
1007
+ },
1008
+ error: (error) => {
1009
+ this._loading$.next(false);
1010
+ console.error("Error loading text layer part", error);
1011
+ }
1012
+ });
1013
+ }
1014
+ loadWithoutThesauri(itemId, partId) {
1015
+ this._loading$.next(true);
1016
+ forkJoin({
1017
+ layerPart: this._itemService.getPart(partId),
1018
+ baseText: this._itemService.getBaseTextPart(itemId),
1019
+ layers: this._facetService.getFacetParts(itemId, true),
1020
+ breakChance: this._itemService.getLayerPartBreakChance(partId),
1021
+ layerHints: this._itemService.getLayerPartHints(partId)
1022
+ }).subscribe({
1023
+ next: (result) => {
1024
+ this._loading$.next(false);
1025
+ this._part$.next(result.layerPart);
1026
+ this._baseText$.next(result.baseText.text);
1027
+ this._baseTextPart$.next(result.baseText.part);
1028
+ this._locations$.next(this.getPartLocations(result.layerPart));
1029
+ this._breakChance$.next(result.breakChance.chance);
1030
+ this._layerHints$.next(result.layerHints);
1031
+ },
1032
+ error: (error) => {
1033
+ this._loading$.next(false);
1034
+ console.error("Error loading text layer part", error);
1035
+ }
1036
+ });
1037
+ }
1038
+ load(itemId, partId, thesauriIds) {
1039
+ if (thesauriIds?.length) this.loadWithThesauri(itemId, partId, thesauriIds);
1040
+ else this.loadWithoutThesauri(itemId, partId);
1041
+ }
1042
+ refreshBreakChance() {
1043
+ const part = this._part$.value;
1044
+ if (!part) return;
1045
+ this._itemService.getLayerPartBreakChance(part.id).subscribe({
1046
+ next: (result) => {
1047
+ this._breakChance$.next(result.chance);
1048
+ },
1049
+ error: (error) => {
1050
+ console.error("Error calculating break chance", error);
1051
+ this._breakChance$.next(-1);
1052
+ }
1053
+ });
1054
+ }
1055
+ applyLayerPatches(partId, patches) {
1056
+ this._saving$.next(true);
1057
+ this._itemService.applyLayerPatches(partId, patches).subscribe({
1058
+ next: (part) => {
1059
+ this._saving$.next(false);
1060
+ this.load(part.itemId, partId);
1061
+ },
1062
+ error: (error) => {
1063
+ this._saving$.next(false);
1064
+ console.error("Error patching text layer part", error);
1065
+ }
1066
+ });
1067
+ }
1068
+ deleteFragment(loc) {
1069
+ let part = this._part$.value;
1070
+ if (!part) return;
1071
+ const i = part.fragments.findIndex((p) => {
1072
+ return TokenLocation.parse(p.location)?.overlaps(loc);
1073
+ });
1074
+ if (i === -1) return;
1075
+ this._saving$.next(true);
1076
+ part = deepCopy(part);
1077
+ part.fragments.splice(i, 1);
1078
+ this._itemService.addPart(part).subscribe({
1079
+ next: (_) => {
1080
+ this._saving$.next(false);
1081
+ this.load(part.itemId, part.id);
1082
+ },
1083
+ error: (error) => {
1084
+ this._saving$.next(false);
1085
+ console.error(error);
1086
+ console.error(`Error deleting fragment at ${loc} in part ${part.id}`, error);
1087
+ }
1088
+ });
1089
+ }
1090
+ saveFragment(fragment) {
1091
+ let part = this._part$.value;
1092
+ if (!part) return;
1093
+ this._saving$.next(true);
1094
+ part = deepCopy(part);
1095
+ const newLoc = TokenLocation.parse(fragment.location);
1096
+ let insertAt = 0;
1097
+ for (let i = part.fragments.length - 1; i > -1; i--) {
1098
+ const frLoc = TokenLocation.parse(part.fragments[i].location);
1099
+ if (newLoc.compareTo(frLoc) >= 0) insertAt = i + 1;
1100
+ if (newLoc.overlaps(frLoc)) {
1101
+ part.fragments.splice(i, 1);
1102
+ if (insertAt > i && insertAt > 0) insertAt--;
1103
+ }
1104
+ }
1105
+ part.fragments.splice(insertAt, 0, fragment);
1106
+ this._itemService.addPart(part).subscribe({
1107
+ next: (_) => {
1108
+ this._saving$.next(false);
1109
+ this.load(part.itemId, part.id);
1110
+ },
1111
+ error: (error) => {
1112
+ this._saving$.next(false);
1113
+ console.error(`Error saving fragment at ${fragment.location} in part ${part.id}`, error);
1114
+ }
1115
+ });
1116
+ }
1117
+ static ɵfac = i0.ɵɵngDeclareFactory({
1118
+ minVersion: "12.0.0",
1119
+ version: "22.2.0",
1120
+ ngImport: i0,
1121
+ type: EditedLayerRepository,
1122
+ deps: [
1123
+ { token: i1$1.ItemService },
1124
+ { token: i1$1.FacetService },
1125
+ { token: i1$1.ThesaurusService }
1126
+ ],
1127
+ target: i0.ɵɵFactoryTarget.Injectable
1128
+ });
1129
+ static ɵprov = i0.ɵɵngDeclareInjectable({
1130
+ minVersion: "12.0.0",
1131
+ version: "22.2.0",
1132
+ ngImport: i0,
1133
+ type: EditedLayerRepository,
1134
+ providedIn: "root"
1135
+ });
1136
+ };
1137
+ i0.ɵɵngDeclareClassMetadata({
1138
+ minVersion: "12.0.0",
1139
+ version: "22.2.0",
1140
+ ngImport: i0,
1141
+ type: EditedLayerRepository,
1142
+ decorators: [{
1143
+ type: Injectable,
1144
+ args: [{ providedIn: "root" }]
1145
+ }],
1146
+ ctorParameters: () => [
1147
+ { type: i1$1.ItemService },
1148
+ { type: i1$1.FacetService },
1149
+ { type: i1$1.ThesaurusService }
1150
+ ]
1151
+ });
1373
1152
  export { AppRepository, EditFragmentFeatureBase, EditPartFeatureBase, EditedLayerRepository, EditedThesaurusRepository, FragmentEditorService, PartEditorService };
1374
- //# sourceMappingURL=myrmidon-cadmus-state.mjs.map
1153
+
1154
+ //# sourceMappingURL=myrmidon-cadmus-state.mjs.map