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