@myrmidon/cadmus-state 2.0.4 → 5.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.
Files changed (34) hide show
  1. package/esm2022/lib/app.repository.mjs +76 -0
  2. package/esm2022/lib/cadmus-state.module.mjs +33 -0
  3. package/esm2022/lib/edit-fragment-feature-base.mjs +132 -0
  4. package/esm2022/lib/edit-part-feature-base.mjs +149 -0
  5. package/esm2022/lib/edited-layer.repository.mjs +281 -0
  6. package/esm2022/lib/edited-thesaurus.repository.mjs +81 -0
  7. package/esm2022/lib/fragment-editor.service.mjs +168 -0
  8. package/esm2022/lib/part-editor.service.mjs +158 -0
  9. package/{esm2020 → esm2022}/myrmidon-cadmus-state.mjs +4 -4
  10. package/{esm2020 → esm2022}/public-api.mjs +11 -11
  11. package/{fesm2020 → fesm2022}/myrmidon-cadmus-state.mjs +1011 -993
  12. package/fesm2022/myrmidon-cadmus-state.mjs.map +1 -0
  13. package/index.d.ts +5 -5
  14. package/lib/app.repository.d.ts +63 -63
  15. package/lib/cadmus-state.module.d.ts +10 -10
  16. package/lib/edit-fragment-feature-base.d.ts +76 -76
  17. package/lib/edit-part-feature-base.d.ts +91 -78
  18. package/lib/edited-layer.repository.d.ts +91 -91
  19. package/lib/edited-thesaurus.repository.d.ts +34 -34
  20. package/lib/fragment-editor.service.d.ts +40 -40
  21. package/lib/part-editor.service.d.ts +39 -39
  22. package/package.json +8 -14
  23. package/public-api.d.ts +8 -8
  24. package/esm2020/lib/app.repository.mjs +0 -75
  25. package/esm2020/lib/cadmus-state.module.mjs +0 -32
  26. package/esm2020/lib/edit-fragment-feature-base.mjs +0 -131
  27. package/esm2020/lib/edit-part-feature-base.mjs +0 -129
  28. package/esm2020/lib/edited-layer.repository.mjs +0 -280
  29. package/esm2020/lib/edited-thesaurus.repository.mjs +0 -80
  30. package/esm2020/lib/fragment-editor.service.mjs +0 -167
  31. package/esm2020/lib/part-editor.service.mjs +0 -157
  32. package/fesm2015/myrmidon-cadmus-state.mjs +0 -1000
  33. package/fesm2015/myrmidon-cadmus-state.mjs.map +0 -1
  34. package/fesm2020/myrmidon-cadmus-state.mjs.map +0 -1
@@ -1,1000 +0,0 @@
1
- import * as i0 from '@angular/core';
2
- import { Injectable, Component, NgModule } from '@angular/core';
3
- import { createStore, withProps, select, setProp } from '@ngneat/elf';
4
- import { withRequestsStatus, updateRequestStatus } from '@ngneat/elf-requests';
5
- import { forkJoin, BehaviorSubject, take, map } from 'rxjs';
6
- import * as i1 from '@myrmidon/cadmus-api';
7
- import { CadmusApiModule } from '@myrmidon/cadmus-api';
8
- import * as i4 from '@myrmidon/cadmus-core';
9
- import { TokenLocation, CadmusCoreModule } from '@myrmidon/cadmus-core';
10
- import * as i1$1 from '@angular/router';
11
- import * as i2 from '@angular/material/snack-bar';
12
- import { deepCopy, NgToolsModule } from '@myrmidon/ng-tools';
13
- import { CommonModule } from '@angular/common';
14
-
15
- /**
16
- * App state ELF repository.
17
- */
18
- class AppRepository {
19
- constructor(_facetService, _flagService, _thesaurusService, _previewService) {
20
- this._facetService = _facetService;
21
- this._flagService = _flagService;
22
- this._thesaurusService = _thesaurusService;
23
- this._previewService = _previewService;
24
- this._store = createStore({ name: 'app' }, withProps({
25
- facets: [],
26
- flags: [],
27
- previewJKeys: [],
28
- previewFKeys: [],
29
- previewCKeys: [],
30
- }), withRequestsStatus());
31
- this.facets$ = this._store.pipe(select((state) => state.facets));
32
- this.flags$ = this._store.pipe(select((state) => state.flags));
33
- this.typeThesaurus$ = this._store.pipe(select((state) => state.typeThesaurus));
34
- this.itemBrowserThesaurus$ = this._store.pipe(select((state) => state.itemBrowserThesaurus));
35
- this.previewJKeys$ = this._store.pipe(select((state) => state.previewJKeys));
36
- this.previewFKeys$ = this._store.pipe(select((state) => state.previewFKeys));
37
- this.previewCKeys$ = this._store.pipe(select((state) => state.previewCKeys));
38
- }
39
- getValue() {
40
- return this._store.getValue();
41
- }
42
- load() {
43
- this._store.update(updateRequestStatus('app', 'pending'));
44
- const facets$ = this._facetService.getFacets();
45
- const flags$ = this._flagService.getFlags();
46
- const thesauri$ = this._thesaurusService.getThesauriSet([
47
- 'model-types@en',
48
- 'item-browsers@en',
49
- ]);
50
- const jKeys$ = this._previewService.getKeys('J');
51
- const fKeys$ = this._previewService.getKeys('F');
52
- const cKeys$ = this._previewService.getKeys('C');
53
- forkJoin({
54
- facets: facets$,
55
- flags: flags$,
56
- thesauri: thesauri$,
57
- jKeys: jKeys$,
58
- fKeys: fKeys$,
59
- cKeys: cKeys$,
60
- }).subscribe({
61
- next: (result) => {
62
- this._store.update(updateRequestStatus('app', 'success'));
63
- this._store.update((state) => (Object.assign(Object.assign({}, state), { facets: result.facets, flags: result.flags, typeThesaurus: result.thesauri['model-types'], itemBrowserThesaurus: result.thesauri['item-browsers'], previewJKeys: result.jKeys, previewFKeys: result.fKeys, previewCKeys: result.cKeys })));
64
- },
65
- });
66
- }
67
- }
68
- AppRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: AppRepository, deps: [{ token: i1.FacetService }, { token: i1.FlagService }, { token: i1.ThesaurusService }, { token: i1.PreviewService }], target: i0.ɵɵFactoryTarget.Injectable });
69
- AppRepository.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: AppRepository, providedIn: 'root' });
70
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: AppRepository, decorators: [{
71
- type: Injectable,
72
- args: [{ providedIn: 'root' }]
73
- }], ctorParameters: function () { return [{ type: i1.FacetService }, { type: i1.FlagService }, { type: i1.ThesaurusService }, { type: i1.PreviewService }]; } });
74
-
75
- /**
76
- * Service used by layer part editor wrappers to load part and thesauri
77
- * or save parts.
78
- */
79
- class FragmentEditorService {
80
- constructor(_itemService, _thesaurusService) {
81
- this._itemService = _itemService;
82
- this._thesaurusService = _thesaurusService;
83
- this._loading$ = new BehaviorSubject(false);
84
- this.loading$ = this._loading$.asObservable();
85
- this._saving$ = new BehaviorSubject(false);
86
- this.saving$ = this._saving$.asObservable();
87
- }
88
- getFragmentFromPart(part, loc) {
89
- const fr = part.fragments.find((f) => f.location === loc);
90
- return (fr !== null && fr !== void 0 ? fr : {
91
- location: loc,
92
- });
93
- }
94
- loadWithThesauri(identity, thesauriIds) {
95
- // remove trailing ! from IDs if any
96
- const unscopedIds = thesauriIds.map((id) => {
97
- return this._thesaurusService.getScopedId(id);
98
- });
99
- return new Promise((resolve, reject) => {
100
- // load part and thesauri
101
- forkJoin({
102
- part: this._itemService.getPart(identity.partId || ''),
103
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
104
- t: this._itemService.getBaseTextPart(identity.itemId),
105
- }).subscribe({
106
- next: (result) => {
107
- const layerPart = result.part;
108
- const thesauri = result.thesauri;
109
- const fr = this.getFragmentFromPart(layerPart, identity.loc);
110
- // if the loaded part has a thesaurus scope, reload the thesauri
111
- if (layerPart.thesaurusScope) {
112
- const scopedIds = thesauriIds.map((id) => {
113
- return this._thesaurusService.getScopedId(id, layerPart.thesaurusScope);
114
- });
115
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
116
- next: (scopedThesauri) => {
117
- this._loading$.next(false);
118
- resolve({
119
- value: fr,
120
- layerPart: layerPart,
121
- baseText: result.t.text,
122
- thesauri: scopedThesauri,
123
- });
124
- },
125
- error: (error) => {
126
- this._loading$.next(false);
127
- console.error(error);
128
- reject({
129
- message: 'Error loading thesauri ' + scopedIds.join(', '),
130
- error: error,
131
- });
132
- },
133
- });
134
- }
135
- else {
136
- // else just use the loaded thesauri
137
- this._loading$.next(false);
138
- resolve({
139
- value: fr,
140
- layerPart: layerPart,
141
- baseText: result.t.text,
142
- thesauri: thesauri,
143
- });
144
- }
145
- },
146
- error: (error) => {
147
- this._loading$.next(false);
148
- console.error(error);
149
- reject({
150
- message: 'Error loading thesauri ' + unscopedIds.join(', '),
151
- error: error,
152
- });
153
- },
154
- });
155
- });
156
- }
157
- loadWithoutThesauri(identity) {
158
- this._loading$.next(true);
159
- return new Promise((resolve, reject) => {
160
- // load layer part and base text
161
- forkJoin({
162
- t: this._itemService.getBaseTextPart(identity.itemId),
163
- p: this._itemService.getPart(identity.partId),
164
- }).subscribe({
165
- next: (result) => {
166
- this._loading$.next(false);
167
- const layerPart = result.p;
168
- resolve({
169
- value: this.getFragmentFromPart(layerPart, identity.loc),
170
- layerPart: layerPart,
171
- baseText: result.t.text,
172
- thesauri: {},
173
- });
174
- },
175
- error: (error) => {
176
- this._loading$.next(false);
177
- console.error(error);
178
- reject({
179
- message: 'Error loading layer part ' + identity.partId,
180
- error: error,
181
- });
182
- },
183
- });
184
- });
185
- }
186
- /**
187
- * Load into the state the part with the specified ID
188
- * and its thesauri.
189
- * If the ID is not specified, it's a new part; in this case,
190
- * it will get a null ID, but use the specified itemId and roleId.
191
- *
192
- * @param identity The part identity.
193
- * @param thesauriIds The thesauri IDs array.
194
- */
195
- load(identity, thesauriIds) {
196
- if (thesauriIds === null || thesauriIds === void 0 ? void 0 : thesauriIds.length) {
197
- return this.loadWithThesauri(identity, thesauriIds);
198
- }
199
- else {
200
- return this.loadWithoutThesauri(identity);
201
- }
202
- }
203
- /**
204
- * Save the fragments in their layer part.
205
- *
206
- * @param part The part.
207
- * @returns Promise which when successful returns the saved part.
208
- */
209
- save(part) {
210
- this._saving$.next(true);
211
- return new Promise((resolve, reject) => {
212
- this._itemService.addPart(part).subscribe({
213
- next: (saved) => {
214
- this._saving$.next(false);
215
- resolve(saved);
216
- },
217
- error: (error) => {
218
- this._saving$.next(false);
219
- console.error(error);
220
- reject({
221
- message: 'Error saving part ' + part.id,
222
- error: error,
223
- });
224
- },
225
- });
226
- });
227
- }
228
- }
229
- FragmentEditorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: FragmentEditorService, deps: [{ token: i1.ItemService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
230
- FragmentEditorService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: FragmentEditorService, providedIn: 'root' });
231
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: FragmentEditorService, decorators: [{
232
- type: Injectable,
233
- args: [{
234
- providedIn: 'root',
235
- }]
236
- }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.ThesaurusService }]; } });
237
-
238
- /**
239
- * Base class for fragment feature editors.
240
- * This is similar to the EditPartFeatureBase class.
241
- */
242
- class EditFragmentFeatureBase {
243
- constructor(_router, route, snackbar, editorService, _libraryRouteService) {
244
- var _a;
245
- this._router = _router;
246
- this.snackbar = snackbar;
247
- this.editorService = editorService;
248
- this._libraryRouteService = _libraryRouteService;
249
- // fragment route:
250
- // /items/<iid>/@partGroup/fragment/<pid>/@frTypeId/<loc>?frrid
251
- const itemId = route.snapshot.params['iid'];
252
- const partId = route.snapshot.params['pid'];
253
- const frTypeId = (_a = route.snapshot.url[2]) === null || _a === void 0 ? void 0 : _a.path;
254
- const loc = route.snapshot.params['loc'];
255
- const frRoleId = route.snapshot.queryParams['frrid'];
256
- this.identity = {
257
- itemId: itemId,
258
- typeId: '',
259
- partId: partId,
260
- roleId: frTypeId,
261
- frTypeId: frTypeId,
262
- frRoleId: frRoleId,
263
- loc: loc,
264
- };
265
- // subscriptions
266
- this._loadingSub = this.editorService.loading$.subscribe((loading) => (this.loading = loading));
267
- this._savingSub = this.editorService.saving$.subscribe((saving) => (this.saving = saving));
268
- }
269
- /**
270
- * Override in derived classes to return the IDs of the thesauri requested
271
- * for this editor.
272
- *
273
- * @return Array with thesauri IDs, eventually empty.
274
- */
275
- getReqThesauriIds() {
276
- return [];
277
- }
278
- /**
279
- * Called once data has been loaded.
280
- */
281
- onDataLoaded() { }
282
- ngOnInit() {
283
- var _a;
284
- this.loading = true;
285
- this.frLoc = (_a = TokenLocation.parse(this.identity.loc)) !== null && _a !== void 0 ? _a : undefined;
286
- const thesIds = this.getReqThesauriIds();
287
- this.editorService
288
- .load(this.identity, thesIds)
289
- .then((data) => {
290
- if (data) {
291
- this.data = data;
292
- this.onDataLoaded();
293
- }
294
- })
295
- .catch((reason) => {
296
- this.snackbar.open(reason.message, 'OK');
297
- });
298
- }
299
- ngOnDestroy() {
300
- var _a, _b;
301
- (_a = this._loadingSub) === null || _a === void 0 ? void 0 : _a.unsubscribe();
302
- (_b = this._savingSub) === null || _b === void 0 ? void 0 : _b.unsubscribe();
303
- }
304
- canDeactivate() {
305
- return !this.dirty;
306
- }
307
- /**
308
- * Called when the wrapped editor dirty state changes.
309
- * In the HTML template, you MUST bind this handler to the dirtyChange event
310
- * emitted by your wrapped editor (i.e. (dirtyChange)="onDirtyChange($event)").
311
- *
312
- * @param value The value of the dirty state.
313
- */
314
- onDirtyChange(value) {
315
- console.log('part dirty change (from editor): ' + value);
316
- this.dirty = value;
317
- }
318
- /**
319
- * Save the fragment.
320
- *
321
- * @param fragment The fragment to be saved.
322
- */
323
- save(fragment) {
324
- // update a copy of the edited part with new fragment
325
- const part = Object.assign({}, this.data.layerPart);
326
- const frIndex = part.fragments.findIndex((f) => f.location === this.identity.loc);
327
- if (frIndex > -1) {
328
- part.fragments.splice(frIndex, 1, fragment);
329
- }
330
- else {
331
- part.fragments.push(fragment);
332
- }
333
- // save the new layer part with the replaced fragment
334
- this.editorService.save(part).then((p) => {
335
- console.log(p.id);
336
- this.snackbar.open('Fragment saved', 'OK', {
337
- duration: 3000,
338
- });
339
- }, (error) => {
340
- console.error(error);
341
- this.snackbar.open('Error saving fragment', 'OK');
342
- });
343
- }
344
- close() {
345
- // /items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>
346
- const part = this.data.layerPart;
347
- const editorKey = this._libraryRouteService.getEditorKeyFromPartType(part.typeId, part.roleId);
348
- const url = `/items/${this.identity.itemId}/${editorKey.partKey}/${part.typeId}/${this.identity.partId}`;
349
- this._router.navigate([url], {
350
- queryParams: {
351
- rid: this.identity.frTypeId,
352
- },
353
- });
354
- }
355
- }
356
- EditFragmentFeatureBase.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", 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 });
357
- EditFragmentFeatureBase.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.0.2", type: EditFragmentFeatureBase, selector: "ng-component", ngImport: i0, template: '', isInline: true });
358
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditFragmentFeatureBase, decorators: [{
359
- type: Component,
360
- args: [{
361
- template: '',
362
- }]
363
- }], ctorParameters: function () { return [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: FragmentEditorService }, { type: i4.LibraryRouteService }]; } });
364
-
365
- /**
366
- * Service used by part editor wrappers to load part and thesauri
367
- * or save parts.
368
- */
369
- class PartEditorService {
370
- constructor(_itemService, _thesaurusService) {
371
- this._itemService = _itemService;
372
- this._thesaurusService = _thesaurusService;
373
- this._loading$ = new BehaviorSubject(false);
374
- this.loading$ = this._loading$.asObservable();
375
- this._saving$ = new BehaviorSubject(false);
376
- this.saving$ = this._saving$.asObservable();
377
- }
378
- loadWithThesauri(identity, thesauriIds) {
379
- // remove trailing ! from IDs if any
380
- const unscopedIds = thesauriIds.map((id) => {
381
- return this._thesaurusService.getScopedId(id);
382
- });
383
- // return a promise
384
- this._loading$.next(true);
385
- return new Promise((resolve, reject) => {
386
- // load part and thesauri
387
- forkJoin({
388
- part: this._itemService.getPart(identity.partId || ''),
389
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
390
- }).subscribe({
391
- next: (result) => {
392
- const part = result.part;
393
- const thesauri = result.thesauri;
394
- // if the loaded part has a thesaurus scope, reload the thesauri
395
- if (part === null || part === void 0 ? void 0 : part.thesaurusScope) {
396
- const scopedIds = thesauriIds.map((id) => {
397
- return this._thesaurusService.getScopedId(id, part.thesaurusScope);
398
- });
399
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
400
- next: (scopedThesauri) => {
401
- this._loading$.next(false);
402
- resolve({
403
- value: part,
404
- thesauri: scopedThesauri,
405
- });
406
- },
407
- error: (error) => {
408
- this._loading$.next(false);
409
- console.error(error);
410
- reject({
411
- message: 'Error loading thesauri ' + scopedIds.join(', '),
412
- error: error,
413
- });
414
- },
415
- });
416
- }
417
- else {
418
- // else just use the loaded thesauri
419
- this._loading$.next(false);
420
- resolve({
421
- value: part,
422
- thesauri: thesauri,
423
- });
424
- }
425
- },
426
- error: (error) => {
427
- this._loading$.next(false);
428
- console.error(error);
429
- reject({
430
- message: 'Error loading thesauri ' + unscopedIds.join(', '),
431
- error: error,
432
- });
433
- },
434
- });
435
- });
436
- }
437
- loadWithoutThesauri(partId) {
438
- return new Promise((resolve, reject) => {
439
- this._loading$.next(true);
440
- this._itemService
441
- .getPart(partId)
442
- .pipe(take(1), map((part) => {
443
- return part
444
- ? {
445
- value: part,
446
- thesauri: {},
447
- }
448
- : null;
449
- }))
450
- .subscribe({
451
- next: (result) => {
452
- this._loading$.next(false);
453
- resolve(result);
454
- },
455
- error: (error) => {
456
- this._loading$.next(false);
457
- console.error(error);
458
- reject({
459
- message: 'Error loading part ' + partId,
460
- error: error,
461
- });
462
- },
463
- });
464
- });
465
- }
466
- /**
467
- * Load into the state the part with the specified ID
468
- * and its thesauri.
469
- * If the ID is not specified, it's a new part; in this case,
470
- * it will get a null ID, but use the specified itemId and roleId.
471
- *
472
- * @param identity The part identity.
473
- * @param thesauriIds The thesauri IDs array.
474
- */
475
- load(identity, thesauriIds) {
476
- if (thesauriIds === null || thesauriIds === void 0 ? void 0 : thesauriIds.length) {
477
- return this.loadWithThesauri(identity, thesauriIds);
478
- }
479
- else {
480
- return this.loadWithoutThesauri(identity.partId);
481
- }
482
- }
483
- /**
484
- * Save the part.
485
- *
486
- * @param part The part.
487
- * @returns Promise which when successful returns the saved part.
488
- */
489
- save(part) {
490
- this._saving$.next(true);
491
- return new Promise((resolve, reject) => {
492
- this._itemService.addPart(part).subscribe({
493
- next: (saved) => {
494
- this._saving$.next(false);
495
- resolve(saved);
496
- },
497
- error: (error) => {
498
- this._saving$.next(false);
499
- console.error(error);
500
- reject({
501
- message: 'Error saving part ' + part.id,
502
- error: error,
503
- });
504
- },
505
- });
506
- });
507
- }
508
- }
509
- PartEditorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: PartEditorService, deps: [{ token: i1.ItemService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
510
- PartEditorService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: PartEditorService, providedIn: 'root' });
511
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: PartEditorService, decorators: [{
512
- type: Injectable,
513
- args: [{
514
- providedIn: 'root',
515
- }]
516
- }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.ThesaurusService }]; } });
517
-
518
- /**
519
- * Base class for part feature editors.
520
- * A part feature editor is a wrapper component around a model editor component,
521
- * used to separate the editor's logic from its host infrastructure.
522
- */
523
- class EditPartFeatureBase {
524
- constructor(router, route, snackbar, itemService, thesaurusService, editorService) {
525
- this.router = router;
526
- this.snackbar = snackbar;
527
- this.itemService = itemService;
528
- this.thesaurusService = thesaurusService;
529
- this.editorService = editorService;
530
- // part route:
531
- // /items/<iid>/@partGroup/@partTypeId/<pid>?rid
532
- // item ID
533
- const itemId = route.snapshot.params['iid'];
534
- // type ID (from "@partTypeId/:pid")
535
- const typeId = route.snapshot.routeConfig.path.substring(0, route.snapshot.routeConfig.path.indexOf('/'));
536
- // part ID or null (if "new")
537
- let partId = route.snapshot.params['pid'];
538
- if (partId === 'new') {
539
- partId = null;
540
- }
541
- // role ID or null (if "default")
542
- let roleId = route.snapshot.queryParams['rid'];
543
- if (roleId === 'default') {
544
- roleId = null;
545
- }
546
- this.identity = {
547
- itemId: itemId,
548
- typeId: typeId,
549
- partId: partId,
550
- roleId: roleId,
551
- };
552
- // subscriptions
553
- this._loadingSub = this.editorService.loading$.subscribe((loading) => (this.loading = loading));
554
- this._savingSub = this.editorService.saving$.subscribe((saving) => (this.saving = saving));
555
- }
556
- /**
557
- * Override in derived classes to return the IDs of the thesauri requested
558
- * for this editor.
559
- *
560
- * @return Array with thesauri IDs, eventually empty.
561
- */
562
- getReqThesauriIds() {
563
- return [];
564
- }
565
- /**
566
- * Called once data has been loaded.
567
- */
568
- onDataLoaded() { }
569
- ngOnInit() {
570
- // load data
571
- this.loading = true;
572
- const thesIds = this.getReqThesauriIds();
573
- this.editorService
574
- .load(this.identity, thesIds)
575
- .then((data) => {
576
- if (data) {
577
- this.data = data;
578
- this.onDataLoaded();
579
- }
580
- })
581
- .catch((reason) => {
582
- this.snackbar.open(reason.message, 'OK');
583
- });
584
- }
585
- ngOnDestroy() {
586
- var _a, _b;
587
- (_a = this._loadingSub) === null || _a === void 0 ? void 0 : _a.unsubscribe();
588
- (_b = this._savingSub) === null || _b === void 0 ? void 0 : _b.unsubscribe();
589
- }
590
- canDeactivate() {
591
- return !this.dirty;
592
- }
593
- /**
594
- * Called when the wrapped editor dirty state changes.
595
- * In the HTML template, you MUST bind this handler to the dirtyChange event
596
- * emitted by your wrapped editor (i.e. (dirtyChange)="onDirtyChange($event)").
597
- *
598
- * @param value The value of the dirty state.
599
- */
600
- onDirtyChange(value) {
601
- console.log('part dirty change (from editor): ' + value);
602
- this.dirty = value;
603
- }
604
- /**
605
- * Save the part.
606
- *
607
- * @param part The part to be saved.
608
- */
609
- save(part) {
610
- console.log('Saving part ID: ' + part.id);
611
- this.editorService.save(part).then((saved) => {
612
- // update the route-defined part ID if it was null (new part)
613
- console.log('Saved part ID: ' + saved.id);
614
- if (!this.identity.partId) {
615
- this.identity = Object.assign(Object.assign({}, this.identity), { partId: saved.id });
616
- console.log('Updated identity: ' + JSON.stringify(this.identity));
617
- }
618
- console.log('Part saved: ' + saved.id);
619
- this.snackbar.open('Part saved', 'OK', {
620
- duration: 3000,
621
- });
622
- }, (reason) => {
623
- this.snackbar.open(reason.message, 'OK');
624
- });
625
- }
626
- /**
627
- * Close the editor by navigating back to the part's item.
628
- */
629
- close() {
630
- this.router.navigate(['items', this.identity.itemId]);
631
- }
632
- }
633
- EditPartFeatureBase.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", 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 });
634
- EditPartFeatureBase.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.0.2", type: EditPartFeatureBase, selector: "ng-component", ngImport: i0, template: '', isInline: true });
635
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditPartFeatureBase, decorators: [{
636
- type: Component,
637
- args: [{
638
- template: '',
639
- }]
640
- }], ctorParameters: function () { return [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: i1.ItemService }, { type: i1.ThesaurusService }, { type: PartEditorService }]; } });
641
-
642
- class EditedThesaurusRepository {
643
- constructor(_thesaurusService) {
644
- this._thesaurusService = _thesaurusService;
645
- this._store = createStore({ name: 'edited-thesaurus' }, withProps({}), withRequestsStatus());
646
- this._loading$ = new BehaviorSubject(false);
647
- this.loading$ = this._loading$.asObservable();
648
- this._saving$ = new BehaviorSubject(false);
649
- this.saving$ = this._saving$.asObservable();
650
- this.thesaurus$ = this._store.pipe(select((state) => state.thesaurus));
651
- }
652
- getValue() {
653
- return this._store.getValue();
654
- }
655
- getThesaurus() {
656
- return this._store.query((state) => state.thesaurus);
657
- }
658
- /**
659
- * Load the specified thesaurus.
660
- *
661
- * @param id The thesaurus ID or undefined to load a new thesaurus.
662
- */
663
- load(id) {
664
- this._loading$.next(true);
665
- if (id) {
666
- this._thesaurusService.getThesaurus(id, true).subscribe({
667
- next: (thesaurus) => {
668
- this._loading$.next(false);
669
- this._store.update(setProp('thesaurus', thesaurus));
670
- },
671
- error: (error) => {
672
- this._loading$.next(false);
673
- console.error('Error loding thesaurus: ' + JSON.stringify(error || {}));
674
- },
675
- });
676
- }
677
- else {
678
- this._store.update(setProp('thesaurus', {
679
- id: '',
680
- language: 'en',
681
- entries: [],
682
- }));
683
- }
684
- }
685
- /**
686
- * Save the specified thesaurus.
687
- *
688
- * @param thesaurus The thesaurus to save.
689
- * @returns Promise with saved thesaurus.
690
- */
691
- save(thesaurus) {
692
- this._saving$.next(true);
693
- return new Promise((resolve, reject) => {
694
- this._thesaurusService.addThesaurus(thesaurus).subscribe({
695
- next: (saved) => {
696
- this._saving$.next(false);
697
- this.load(saved.id);
698
- resolve(saved);
699
- },
700
- error: (error) => {
701
- this._saving$.next(false);
702
- console.error('Error loding thesaurus: ' + JSON.stringify(error || {}));
703
- reject(error);
704
- },
705
- });
706
- });
707
- }
708
- }
709
- EditedThesaurusRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedThesaurusRepository, deps: [{ token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
710
- EditedThesaurusRepository.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedThesaurusRepository, providedIn: 'root' });
711
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedThesaurusRepository, decorators: [{
712
- type: Injectable,
713
- args: [{ providedIn: 'root' }]
714
- }], ctorParameters: function () { return [{ type: i1.ThesaurusService }]; } });
715
-
716
- /**
717
- * Edited layer part ELF repository.
718
- */
719
- class EditedLayerRepository {
720
- constructor(_itemService, _facetService, _thesaurusService) {
721
- this._itemService = _itemService;
722
- this._facetService = _facetService;
723
- this._thesaurusService = _thesaurusService;
724
- this._store = createStore({ name: 'edited-layer' }, withProps({
725
- locations: [],
726
- breakChance: 0,
727
- layerHints: [],
728
- }), withRequestsStatus());
729
- this._loading$ = new BehaviorSubject(false);
730
- this.loading$ = this._loading$.asObservable();
731
- this._saving$ = new BehaviorSubject(false);
732
- this.saving$ = this._saving$.asObservable();
733
- this.part$ = this._store.pipe(select((state) => state.part));
734
- this.baseText$ = this._store.pipe(select((state) => state.baseText));
735
- this.baseTextPart$ = this._store.pipe(select((state) => state.baseTextPart));
736
- this.locations$ = this._store.pipe(select((state) => state.locations));
737
- this.breakChance$ = this._store.pipe(select((state) => state.breakChance));
738
- this.layerHints$ = this._store.pipe(select((state) => state.layerHints));
739
- }
740
- reset() {
741
- this._store.reset();
742
- }
743
- getValue() {
744
- return this._store.getValue();
745
- }
746
- getPart() {
747
- return this._store.query((state) => state.part);
748
- }
749
- getBaseText() {
750
- return this._store.query((state => state.baseText));
751
- }
752
- getLocations() {
753
- return this._store.query((state) => state.locations);
754
- }
755
- getPartLocations(part) {
756
- const locations = [];
757
- if (part && part.fragments) {
758
- part.fragments.forEach((p) => {
759
- locations.push(TokenLocation.parse(p.location));
760
- });
761
- }
762
- return locations;
763
- }
764
- loadWithThesauri(itemId, partId, thesauriIds) {
765
- // remove trailing ! from IDs if any
766
- const unscopedIds = thesauriIds.map((id) => {
767
- return this._thesaurusService.getScopedId(id);
768
- });
769
- this._loading$.next(true);
770
- forkJoin({
771
- // TODO: eventually optimize by adding method param to load only fragments locations
772
- layerPart: this._itemService.getPart(partId),
773
- baseText: this._itemService.getBaseTextPart(itemId),
774
- layers: this._facetService.getFacetParts(itemId, true),
775
- breakChance: this._itemService.getLayerPartBreakChance(partId),
776
- layerHints: this._itemService.getLayerPartHints(partId),
777
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
778
- }).subscribe({
779
- next: (result) => {
780
- var _a;
781
- // if the loaded part has a thesaurus scope, reload the thesauri
782
- if ((_a = result.layerPart) === null || _a === void 0 ? void 0 : _a.thesaurusScope) {
783
- const scopedIds = thesauriIds.map((id) => {
784
- return this._thesaurusService.getScopedId(id, result.layerPart.thesaurusScope);
785
- });
786
- this._thesaurusService.getThesauriSet(scopedIds).subscribe({
787
- next: (thesauri) => {
788
- this._loading$.next(false);
789
- this._store.update((state) => (Object.assign(Object.assign({}, state), { part: result.layerPart, baseText: result.baseText.text, baseTextPart: result.baseText.part, locations: this.getPartLocations(result.layerPart), breakChance: result.breakChance.chance, layerHints: result.layerHints, thesauri: thesauri })));
790
- },
791
- error: (error) => {
792
- this._loading$.next(false);
793
- console.error('Error loading thesauri: ' + JSON.stringify(error || {}));
794
- },
795
- });
796
- }
797
- else {
798
- this._loading$.next(false);
799
- this._store.update((state) => (Object.assign(Object.assign({}, state), { part: result.layerPart, baseText: result.baseText.text, baseTextPart: result.baseText.part, locations: this.getPartLocations(result.layerPart), breakChance: result.breakChance.chance, layerHints: result.layerHints, thesauri: result.thesauri })));
800
- }
801
- },
802
- error: (error) => {
803
- this._loading$.next(false);
804
- console.error('Error loading text layer part: ' + JSON.stringify(error || {}));
805
- },
806
- });
807
- }
808
- loadWithoutThesauri(itemId, partId) {
809
- this._loading$.next(true);
810
- forkJoin({
811
- // TODO: eventually optimize by adding method param to load only fragments locations
812
- layerPart: this._itemService.getPart(partId),
813
- baseText: this._itemService.getBaseTextPart(itemId),
814
- layers: this._facetService.getFacetParts(itemId, true),
815
- breakChance: this._itemService.getLayerPartBreakChance(partId),
816
- layerHints: this._itemService.getLayerPartHints(partId),
817
- }).subscribe({
818
- next: (result) => {
819
- this._loading$.next(false);
820
- this._store.update((state) => (Object.assign(Object.assign({}, state), { part: result.layerPart, baseText: result.baseText.text, baseTextPart: result.baseText.part, locations: this.getPartLocations(result.layerPart), breakChance: result.breakChance.chance, layerHints: result.layerHints })));
821
- },
822
- error: (error) => {
823
- this._loading$.next(false);
824
- console.error('Error loading text layer part: ' + JSON.stringify(error || {}));
825
- },
826
- });
827
- }
828
- /**
829
- * Load the state for editing layer part(s).
830
- *
831
- * @param itemId The item ID the layer part belongs to.
832
- * @param partId The layer part ID.
833
- * @param thesauriIds The optional thesauri IDs to load.
834
- */
835
- load(itemId, partId, thesauriIds) {
836
- if (thesauriIds === null || thesauriIds === void 0 ? void 0 : thesauriIds.length) {
837
- this.loadWithThesauri(itemId, partId, thesauriIds);
838
- }
839
- else {
840
- this.loadWithoutThesauri(itemId, partId);
841
- }
842
- }
843
- /**
844
- * Refresh the layer part break chance.
845
- */
846
- refreshBreakChance() {
847
- const store = this._store.getValue();
848
- const part = store.part;
849
- if (!part) {
850
- return;
851
- }
852
- this._itemService.getLayerPartBreakChance(part.id).subscribe({
853
- next: (result) => {
854
- this._store.update(setProp('breakChance', result.chance));
855
- },
856
- error: (error) => {
857
- console.error('Error calculating break chance: ' + JSON.stringify(error || {}));
858
- this._store.update(setProp('breakChance', -1));
859
- },
860
- });
861
- }
862
- applyLayerPatches(partId, patches) {
863
- this._saving$.next(true);
864
- this._itemService.applyLayerPatches(partId, patches).subscribe({
865
- next: (part) => {
866
- this._saving$.next(false);
867
- this.load(part.itemId, partId);
868
- },
869
- error: (error) => {
870
- this._saving$.next(false);
871
- console.error('Error patching text layer part: ' + JSON.stringify(error || {}));
872
- },
873
- });
874
- }
875
- /**
876
- * Delete the fragment at the specified location.
877
- *
878
- * @param loc The fragment's location.
879
- */
880
- deleteFragment(loc) {
881
- // find the fragment
882
- let part = this._store.getValue().part;
883
- if (!part) {
884
- return;
885
- }
886
- const i = part.fragments.findIndex((p) => {
887
- var _a;
888
- return (_a = TokenLocation.parse(p.location)) === null || _a === void 0 ? void 0 : _a.overlaps(loc);
889
- });
890
- if (i === -1) {
891
- return;
892
- }
893
- this._saving$.next(true);
894
- // remove it from the part
895
- // work on a copy, as store objects are immutable
896
- part = deepCopy(part);
897
- part.fragments.splice(i, 1);
898
- // update the part and reload state once done
899
- this._itemService.addPart(part).subscribe({
900
- next: (_) => {
901
- this._saving$.next(false);
902
- this.load(part.itemId, part.id);
903
- },
904
- error: (error) => {
905
- this._saving$.next(false);
906
- console.error(error);
907
- console.error(`Error deleting fragment at ${loc} in part ${part.id}: ` +
908
- JSON.stringify(error || {}));
909
- },
910
- });
911
- }
912
- /**
913
- * Save the specified fragment.
914
- *
915
- * @param fragment The fragment.
916
- */
917
- saveFragment(fragment) {
918
- // find the fragment
919
- let part = this._store.getValue().part;
920
- if (!part) {
921
- return;
922
- }
923
- // add or replace it
924
- // work on a copy, as store objects are immutable
925
- this._saving$.next(true);
926
- part = deepCopy(part);
927
- // replace all the overlapping fragments with the new one
928
- const newLoc = TokenLocation.parse(fragment.location);
929
- let insertAt = 0;
930
- for (let i = part.fragments.length - 1; i > -1; i--) {
931
- const frLoc = TokenLocation.parse(part.fragments[i].location);
932
- if (newLoc.compareTo(frLoc) >= 0) {
933
- insertAt = i + 1;
934
- }
935
- if (newLoc.overlaps(frLoc)) {
936
- part.fragments.splice(i, 1);
937
- if (insertAt > i && insertAt > 0) {
938
- insertAt--;
939
- }
940
- }
941
- }
942
- // add the new fragment
943
- part.fragments.splice(insertAt, 0, fragment);
944
- // update the part and reload once done
945
- this._itemService.addPart(part).subscribe({
946
- next: (_) => {
947
- this._saving$.next(false);
948
- this.load(part.itemId, part.id);
949
- },
950
- error: (error) => {
951
- this._saving$.next(false);
952
- console.error(`Error saving fragment at ${fragment.location} in part ${part.id}: ` + JSON.stringify(error || {}));
953
- },
954
- });
955
- }
956
- }
957
- EditedLayerRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedLayerRepository, deps: [{ token: i1.ItemService }, { token: i1.FacetService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
958
- EditedLayerRepository.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedLayerRepository, providedIn: 'root' });
959
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: EditedLayerRepository, decorators: [{
960
- type: Injectable,
961
- args: [{ providedIn: 'root' }]
962
- }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.FacetService }, { type: i1.ThesaurusService }]; } });
963
-
964
- class CadmusStateModule {
965
- }
966
- CadmusStateModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: CadmusStateModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
967
- CadmusStateModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.2", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
968
- // Cadmus
969
- CadmusCoreModule,
970
- CadmusApiModule,
971
- NgToolsModule] });
972
- CadmusStateModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
973
- // Cadmus
974
- CadmusCoreModule,
975
- CadmusApiModule,
976
- NgToolsModule] });
977
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: CadmusStateModule, decorators: [{
978
- type: NgModule,
979
- args: [{
980
- imports: [
981
- CommonModule,
982
- // Cadmus
983
- CadmusCoreModule,
984
- CadmusApiModule,
985
- NgToolsModule,
986
- ],
987
- }]
988
- }] });
989
-
990
- /*
991
- * Public API Surface of cadmus-state
992
- */
993
-
994
- /**
995
- * Generated bundle index. Do not edit.
996
- */
997
-
998
- export { AppRepository, CadmusStateModule, EditFragmentFeatureBase, EditPartFeatureBase, EditedLayerRepository, EditedThesaurusRepository, FragmentEditorService, PartEditorService };
999
- //# sourceMappingURL=myrmidon-cadmus-state.mjs.map
1000
- //# sourceMappingURL=myrmidon-cadmus-state.mjs.map