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