@myrmidon/cadmus-state 1.1.1 → 2.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 (58) hide show
  1. package/esm2020/lib/app.repository.mjs +75 -0
  2. package/esm2020/lib/cadmus-state.module.mjs +4 -4
  3. package/esm2020/lib/edit-fragment-feature-base.mjs +76 -63
  4. package/esm2020/lib/edit-part-feature-base.mjs +78 -68
  5. package/esm2020/lib/edited-layer.repository.mjs +277 -0
  6. package/esm2020/lib/edited-thesaurus.repository.mjs +80 -0
  7. package/esm2020/lib/fragment-editor.service.mjs +167 -0
  8. package/esm2020/lib/part-editor.service.mjs +157 -0
  9. package/esm2020/public-api.mjs +6 -19
  10. package/fesm2015/myrmidon-cadmus-state.mjs +644 -1092
  11. package/fesm2015/myrmidon-cadmus-state.mjs.map +1 -1
  12. package/fesm2020/myrmidon-cadmus-state.mjs +671 -1079
  13. package/fesm2020/myrmidon-cadmus-state.mjs.map +1 -1
  14. package/lib/app.repository.d.ts +63 -0
  15. package/lib/edit-fragment-feature-base.d.ts +35 -53
  16. package/lib/edit-part-feature-base.d.ts +37 -51
  17. package/lib/edited-layer.repository.d.ts +90 -0
  18. package/lib/edited-thesaurus.repository.d.ts +34 -0
  19. package/lib/fragment-editor.service.d.ts +40 -0
  20. package/lib/part-editor.service.d.ts +39 -0
  21. package/package.json +3 -3
  22. package/public-api.d.ts +5 -18
  23. package/esm2020/lib/app.query.mjs +0 -35
  24. package/esm2020/lib/app.service.mjs +0 -57
  25. package/esm2020/lib/app.store.mjs +0 -26
  26. package/esm2020/lib/edit-fragment-query-base.mjs +0 -20
  27. package/esm2020/lib/edit-fragment-service-base.mjs +0 -140
  28. package/esm2020/lib/edit-fragment.store.mjs +0 -36
  29. package/esm2020/lib/edit-item.query.mjs +0 -52
  30. package/esm2020/lib/edit-item.service.mjs +0 -212
  31. package/esm2020/lib/edit-item.store.mjs +0 -40
  32. package/esm2020/lib/edit-layer-part.query.mjs +0 -35
  33. package/esm2020/lib/edit-layer-part.service.mjs +0 -230
  34. package/esm2020/lib/edit-layer-part.store.mjs +0 -49
  35. package/esm2020/lib/edit-part-query-base.mjs +0 -25
  36. package/esm2020/lib/edit-part-service-base.mjs +0 -120
  37. package/esm2020/lib/edit-part.store.mjs +0 -31
  38. package/esm2020/lib/edit-thesaurus.query.mjs +0 -26
  39. package/esm2020/lib/edit-thesaurus.service.mjs +0 -59
  40. package/esm2020/lib/edit-thesaurus.store.mjs +0 -30
  41. package/lib/app.query.d.ts +0 -17
  42. package/lib/app.service.d.ts +0 -14
  43. package/lib/app.store.d.ts +0 -43
  44. package/lib/edit-fragment-query-base.d.ts +0 -12
  45. package/lib/edit-fragment-service-base.d.ts +0 -39
  46. package/lib/edit-fragment.store.d.ts +0 -24
  47. package/lib/edit-item.query.d.ts +0 -22
  48. package/lib/edit-item.service.d.ts +0 -38
  49. package/lib/edit-item.store.d.ts +0 -49
  50. package/lib/edit-layer-part.query.d.ts +0 -17
  51. package/lib/edit-layer-part.service.d.ts +0 -39
  52. package/lib/edit-layer-part.store.d.ts +0 -57
  53. package/lib/edit-part-query-base.d.ts +0 -17
  54. package/lib/edit-part-service-base.d.ts +0 -42
  55. package/lib/edit-part.store.d.ts +0 -32
  56. package/lib/edit-thesaurus.query.d.ts +0 -13
  57. package/lib/edit-thesaurus.service.d.ts +0 -14
  58. package/lib/edit-thesaurus.store.d.ts +0 -17
@@ -1,483 +1,325 @@
1
1
  import * as i0 from '@angular/core';
2
- import { Injectable, NgModule } from '@angular/core';
3
- import { Store, StoreConfig, Query } from '@datorama/akita';
4
- import { __decorate } from 'tslib';
5
- import { forkJoin, take } from 'rxjs';
6
- import * as i2 from '@myrmidon/cadmus-api';
2
+ import { Injectable, Component, NgModule } from '@angular/core';
3
+ import { createStore, withProps, select, setProp } from '@ngneat/elf';
4
+ import { withRequestsStatus, updateRequestStatus } from '@ngneat/elf-requests';
5
+ import { forkJoin, BehaviorSubject, take, map } from 'rxjs';
6
+ import * as i1 from '@myrmidon/cadmus-api';
7
7
  import { CadmusApiModule } from '@myrmidon/cadmus-api';
8
- import * as i2$1 from '@myrmidon/cadmus-core';
8
+ import * as i4 from '@myrmidon/cadmus-core';
9
9
  import { TokenLocation, CadmusCoreModule } from '@myrmidon/cadmus-core';
10
+ import * as i1$1 from '@angular/router';
11
+ import * as i2 from '@angular/material/snack-bar';
10
12
  import { deepCopy, NgToolsModule } from '@myrmidon/ng-tools';
11
13
  import { CommonModule } from '@angular/common';
12
14
 
13
- const initialState$2 = {
14
- facets: [],
15
- flags: [],
16
- previewRKeys: [],
17
- previewFKeys: [],
18
- };
19
- let AppStore = class AppStore extends Store {
20
- constructor() {
21
- super(initialState$2);
22
- }
23
- };
24
- AppStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
25
- AppStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppStore, providedIn: 'root' });
26
- AppStore = __decorate([
27
- StoreConfig({ name: 'app', resettable: false })
28
- ], AppStore);
29
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppStore, decorators: [{
30
- type: Injectable,
31
- args: [{ providedIn: 'root' }]
32
- }], ctorParameters: function () { return []; } });
33
-
34
- class AppQuery extends Query {
35
- constructor(store) {
36
- super(store);
37
- this.store = store;
38
- }
39
- selectFacets() {
40
- return this.select((state) => state.facets);
41
- }
42
- selectFlags() {
43
- return this.select((state) => state.flags);
44
- }
45
- selectTypeThesaurus() {
46
- return this.select((state) => state.typeThesaurus);
47
- }
48
- selectItemBrowserThesaurus() {
49
- return this.select((state) => state.itemBrowserThesaurus);
50
- }
51
- selectPreviewRKeys() {
52
- return this.select((state) => state.previewRKeys);
53
- }
54
- selectPreviewFKeys() {
55
- return this.select((state) => state.previewFKeys);
56
- }
57
- }
58
- AppQuery.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppQuery, deps: [{ token: AppStore }], target: i0.ɵɵFactoryTarget.Injectable });
59
- AppQuery.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppQuery, providedIn: 'root' });
60
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppQuery, decorators: [{
61
- type: Injectable,
62
- args: [{ providedIn: 'root' }]
63
- }], ctorParameters: function () { return [{ type: AppStore }]; } });
64
-
65
- class AppService {
66
- constructor(_store, _facetService, _flagService, _thesaurusService, _previewService) {
67
- this._store = _store;
15
+ /**
16
+ * App state ELF repository.
17
+ */
18
+ class AppRepository {
19
+ constructor(_facetService, _flagService, _thesaurusService, _previewService) {
68
20
  this._facetService = _facetService;
69
21
  this._flagService = _flagService;
70
22
  this._thesaurusService = _thesaurusService;
71
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();
72
41
  }
73
42
  load() {
74
- this._store.setLoading(true);
43
+ this._store.update(updateRequestStatus('app', 'pending'));
75
44
  const facets$ = this._facetService.getFacets();
76
45
  const flags$ = this._flagService.getFlags();
77
46
  const thesauri$ = this._thesaurusService.getThesauriSet([
78
47
  'model-types@en',
79
48
  'item-browsers@en',
80
49
  ]);
81
- const rKeys$ = this._previewService.getKeys(false);
82
- const fKeys$ = this._previewService.getKeys(true);
50
+ const jKeys$ = this._previewService.getKeys('J');
51
+ const fKeys$ = this._previewService.getKeys('F');
52
+ const cKeys$ = this._previewService.getKeys('C');
83
53
  forkJoin({
84
54
  facets: facets$,
85
55
  flags: flags$,
86
56
  thesauri: thesauri$,
87
- rKeys: rKeys$,
57
+ jKeys: jKeys$,
88
58
  fKeys: fKeys$,
59
+ cKeys: cKeys$,
89
60
  }).subscribe({
90
61
  next: (result) => {
91
- this._store.setLoading(false);
92
- this._store.setError(null);
93
- this._store.update({
62
+ this._store.update(updateRequestStatus('app', 'success'));
63
+ this._store.update((state) => ({
64
+ ...state,
94
65
  facets: result.facets,
95
66
  flags: result.flags,
96
67
  typeThesaurus: result.thesauri['model-types'],
97
68
  itemBrowserThesaurus: result.thesauri['item-browsers'],
98
- previewRKeys: result.rKeys,
99
- previewFKeys: result.fKeys
100
- });
69
+ previewJKeys: result.jKeys,
70
+ previewFKeys: result.fKeys,
71
+ previewCKeys: result.cKeys,
72
+ }));
101
73
  },
102
- error: (error) => {
103
- console.error(error);
104
- this._store.setLoading(false);
105
- this._store.setError('Error loading app state');
106
- },
107
- });
108
- }
109
- }
110
- AppService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppService, deps: [{ token: AppStore }, { token: i2.FacetService }, { token: i2.FlagService }, { token: i2.ThesaurusService }, { token: i2.PreviewService }], target: i0.ɵɵFactoryTarget.Injectable });
111
- AppService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppService, providedIn: 'root' });
112
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: AppService, decorators: [{
113
- type: Injectable,
114
- args: [{ providedIn: 'root' }]
115
- }], ctorParameters: function () { return [{ type: AppStore }, { type: i2.FacetService }, { type: i2.FlagService }, { type: i2.ThesaurusService }, { type: i2.PreviewService }]; } });
116
-
117
- const initialState$1 = {
118
- parts: [],
119
- partGroups: [],
120
- layerPartInfos: [],
121
- };
122
- let EditItemStore = class EditItemStore extends Store {
123
- constructor() {
124
- super(initialState$1);
125
- }
126
- setDirty(value = true) {
127
- this.update({
128
- dirty: value,
129
- });
130
- }
131
- setSaving(value = true) {
132
- this.update({
133
- saving: value,
134
- });
135
- }
136
- setDeletingPart(value = true) {
137
- this.update({
138
- deletingPart: value,
139
74
  });
140
75
  }
141
- };
142
- EditItemStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
143
- EditItemStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemStore, providedIn: 'root' });
144
- EditItemStore = __decorate([
145
- StoreConfig({ name: 'edit-item', resettable: true })
146
- ], EditItemStore);
147
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemStore, decorators: [{
148
- type: Injectable,
149
- args: [{ providedIn: 'root' }]
150
- }], ctorParameters: function () { return []; } });
151
-
152
- /**
153
- * The query facade to the edit item store.
154
- */
155
- class EditItemQuery extends Query {
156
- constructor(store, _libraryRouteService) {
157
- super(store);
158
- this.store = store;
159
- this._libraryRouteService = _libraryRouteService;
160
- }
161
- selectItem() {
162
- return this.select((state) => state.item);
163
- }
164
- selectParts() {
165
- return this.select((state) => state.parts);
166
- }
167
- selectFacet() {
168
- return this.select((state) => state.facet);
169
- }
170
- selectSaving() {
171
- return this.select((state) => (state.saving ? true : false));
172
- }
173
- selectDeletingPart() {
174
- return this.select((state) => (state.deletingPart ? true : false));
175
- }
176
- hasItem(id) {
177
- const item = this.getValue().item;
178
- if (!item || (id && id !== item.id)) {
179
- return false;
180
- }
181
- return true;
182
- }
183
- getEditorKeyFromPartTypeId(typeId, roleId) {
184
- const facet = this.getValue().facet;
185
- if (!facet) {
186
- return 'default';
187
- }
188
- return this._libraryRouteService.getEditorKeyFromPartType(typeId, roleId)
189
- .partKey;
190
- }
191
76
  }
192
- EditItemQuery.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemQuery, deps: [{ token: EditItemStore }, { token: i2$1.LibraryRouteService }], target: i0.ɵɵFactoryTarget.Injectable });
193
- EditItemQuery.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemQuery, providedIn: 'root' });
194
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemQuery, decorators: [{
77
+ AppRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.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.0.0", ngImport: i0, type: AppRepository, providedIn: 'root' });
79
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: AppRepository, decorators: [{
195
80
  type: Injectable,
196
81
  args: [{ providedIn: 'root' }]
197
- }], ctorParameters: function () { return [{ type: EditItemStore }, { type: i2$1.LibraryRouteService }]; } });
82
+ }], ctorParameters: function () { return [{ type: i1.FacetService }, { type: i1.FlagService }, { type: i1.ThesaurusService }, { type: i1.PreviewService }]; } });
198
83
 
199
84
  /**
200
- * The service which handles the edit item store.
85
+ * Service used by layer part editor wrappers to load part and thesauri
86
+ * or save parts.
201
87
  */
202
- class EditItemService {
203
- constructor(_store, _appQuery, _itemService) {
204
- this._store = _store;
205
- this._appQuery = _appQuery;
88
+ class FragmentEditorService {
89
+ constructor(_itemService, _thesaurusService) {
206
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
+ });
207
102
  }
208
- pickDefaultFacet(facets) {
209
- if (!facets.length) {
210
- return undefined;
211
- }
212
- // if there is a facet with id="default", pick it
213
- const defaultFacet = facets.find((f) => f.id === 'default');
214
- if (defaultFacet) {
215
- return defaultFacet;
216
- }
217
- // else just pick the first in the list
218
- return facets[0];
219
- }
220
- /**
221
- * Load the item with the specified ID (if any; the ID can be null for
222
- * a new item), and all the required lookup data into the item store.
223
- */
224
- load(itemId) {
225
- this._store.setLoading(true);
226
- const appState = this._appQuery.getValue();
227
- // if not a new item, include it in load
228
- const layers$ = this._itemService.getItemLayerInfo(itemId || 'new', true);
229
- if (itemId) {
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
230
110
  forkJoin({
231
- item: this._itemService.getItem(itemId, true),
232
- layers: layers$,
233
- }).pipe(take(1)).subscribe({
111
+ part: this._itemService.getPart(identity.partId || ''),
112
+ thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
113
+ t: this._itemService.getBaseTextPart(identity.itemId),
114
+ }).subscribe({
234
115
  next: (result) => {
235
- this._store.setLoading(false);
236
- this._store.setError(null);
237
- const itemFacet = appState.facets.find((f) => {
238
- return f.id === result.item.facetId;
239
- });
240
- const facetParts = itemFacet ? itemFacet.partDefinitions : [];
241
- this._store.update({
242
- item: result.item,
243
- parts: result.item.parts,
244
- partGroups: this._itemService.groupParts(result.item.parts || [], facetParts),
245
- layerPartInfos: result.layers,
246
- facet: appState.facets.find((f) => f.id === result.item.facetId),
247
- });
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
+ }
248
154
  },
249
155
  error: (error) => {
156
+ this._loading$.next(false);
250
157
  console.error(error);
251
- this._store.setLoading(false);
252
- this._store.setError('Error loading item ' + itemId);
158
+ reject({
159
+ message: 'Error loading thesauri ' + unscopedIds.join(', '),
160
+ error: error,
161
+ });
253
162
  },
254
163
  });
255
- }
256
- else {
257
- // if new, just set an empty item
164
+ });
165
+ }
166
+ loadWithoutThesauri(identity) {
167
+ this._loading$.next(true);
168
+ return new Promise((resolve, reject) => {
169
+ // load layer part and base text
258
170
  forkJoin({
259
- layers: layers$,
260
- }).pipe(take(1)).subscribe({
171
+ t: this._itemService.getBaseTextPart(identity.itemId),
172
+ p: this._itemService.getPart(identity.partId),
173
+ }).subscribe({
261
174
  next: (result) => {
262
- this._store.setLoading(false);
263
- this._store.setError(null);
264
- this._store.update({
265
- item: {
266
- id: '',
267
- title: '',
268
- description: '',
269
- facetId: '',
270
- groupId: '',
271
- sortKey: '',
272
- flags: 0,
273
- timeCreated: new Date(),
274
- creatorId: '',
275
- timeModified: new Date(),
276
- userId: '',
277
- },
278
- parts: [],
279
- partGroups: [],
280
- layerPartInfos: result.layers,
281
- facet: this.pickDefaultFacet(appState.facets),
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: {},
282
182
  });
283
183
  },
284
184
  error: (error) => {
185
+ this._loading$.next(false);
285
186
  console.error(error);
286
- this._store.setLoading(false);
287
- this._store.setError('Error loading item ' + itemId);
187
+ reject({
188
+ message: 'Error loading layer part ' + identity.partId,
189
+ error: error,
190
+ });
288
191
  },
289
192
  });
290
- }
193
+ });
291
194
  }
292
195
  /**
293
- * Reset the item.
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.
294
203
  */
295
- reset() {
296
- this._store.reset();
204
+ load(identity, thesauriIds) {
205
+ if (thesauriIds?.length) {
206
+ return this.loadWithThesauri(identity, thesauriIds);
207
+ }
208
+ else {
209
+ return this.loadWithoutThesauri(identity);
210
+ }
297
211
  }
298
212
  /**
299
- * Save the item and update the store.
300
- * @param item The item to be saved.
213
+ * Save the fragments in their layer part.
214
+ *
215
+ * @param part The part.
216
+ * @returns Promise which when successful returns the saved part.
301
217
  */
302
- save(item) {
303
- this._store.setSaving();
218
+ save(part) {
219
+ this._saving$.next(true);
304
220
  return new Promise((resolve, reject) => {
305
- this._itemService
306
- .addItem(item)
307
- .pipe(take(1))
308
- .subscribe({
221
+ this._itemService.addPart(part).subscribe({
309
222
  next: (saved) => {
310
- this._store.setSaving(false);
311
- // reload the store
312
- this.load(saved.id);
223
+ this._saving$.next(false);
313
224
  resolve(saved);
314
225
  },
315
226
  error: (error) => {
227
+ this._saving$.next(false);
316
228
  console.error(error);
317
- this._store.setSaving(false);
318
- this._store.setError('Error saving item');
319
- reject(error);
320
- },
321
- });
322
- });
323
- }
324
- /**
325
- * Delete the part with the specified ID from the edited item.
326
- * @param id The ID of the part to be deleted.
327
- */
328
- deletePart(id) {
329
- this._store.setDeletingPart();
330
- return new Promise((resolve, reject) => {
331
- // delete from server
332
- this._itemService
333
- .deletePart(id)
334
- .pipe(take(1))
335
- .subscribe({
336
- next: (_) => {
337
- this._store.setDeletingPart(false);
338
- // reload the store
339
- this.load(this._store.getValue().item?.id);
340
- resolve(id);
341
- },
342
- error: (error) => {
343
- console.log(error);
344
- this._store.setDeletingPart(false);
345
- this._store.setError('Error deleting part ' + id);
346
- reject(error);
229
+ reject({
230
+ message: 'Error saving part ' + part.id,
231
+ error: error,
232
+ });
347
233
  },
348
234
  });
349
235
  });
350
236
  }
351
- addNewLayerPart(itemId, typeId, roleId) {
352
- const part = {
353
- itemId,
354
- typeId,
355
- roleId,
356
- id: '',
357
- creatorId: '',
358
- userId: '',
359
- timeCreated: new Date(),
360
- timeModified: new Date(),
361
- };
362
- this._store.setSaving();
363
- this._itemService
364
- .addPart(part)
365
- .pipe(take(1))
366
- .subscribe({
367
- next: (_) => {
368
- this._store.setSaving(false);
369
- // reload the store
370
- this.load(this._store.getValue().item?.id);
371
- },
372
- error: (error) => {
373
- console.log(error);
374
- this._store.setSaving(false);
375
- this._store.setError('Error adding new layer part for item ' + itemId);
376
- },
377
- });
378
- }
379
- setPartThesaurusScope(ids, scope) {
380
- this._store.setSaving();
381
- this._itemService
382
- .setPartThesaurusScope(ids, scope)
383
- .pipe(take(1))
384
- .subscribe({
385
- next: (_) => {
386
- this._store.setSaving(false);
387
- // reload the store
388
- this.load(this._store.getValue().item?.id);
389
- },
390
- error: (error) => {
391
- console.log(error);
392
- this._store.setSaving(false);
393
- this._store.setError("Error setting item's parts scope");
394
- },
395
- });
396
- }
397
237
  }
398
- EditItemService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemService, deps: [{ token: EditItemStore }, { token: AppQuery }, { token: i2.ItemService }], target: i0.ɵɵFactoryTarget.Injectable });
399
- EditItemService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemService, providedIn: 'root' });
400
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditItemService, decorators: [{
238
+ FragmentEditorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.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.0.0", ngImport: i0, type: FragmentEditorService, providedIn: 'root' });
240
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: FragmentEditorService, decorators: [{
401
241
  type: Injectable,
402
- args: [{ providedIn: 'root' }]
403
- }], ctorParameters: function () { return [{ type: EditItemStore }, { type: AppQuery }, { type: i2.ItemService }]; } });
242
+ args: [{
243
+ providedIn: 'root',
244
+ }]
245
+ }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.ThesaurusService }]; } });
404
246
 
405
247
  /**
406
248
  * Base class for fragment feature editors.
407
249
  * This is similar to the EditPartFeatureBase class.
408
250
  */
409
251
  class EditFragmentFeatureBase {
410
- constructor(_router, route, snackbar, _editFrQuery, _editFrService, _editItemQuery, _editItemService, _editLayersQuery, _editLayersService, _libraryRouteService) {
252
+ constructor(_router, route, snackbar, editorService, _libraryRouteService) {
411
253
  this._router = _router;
412
254
  this.snackbar = snackbar;
413
- this._editFrQuery = _editFrQuery;
414
- this._editFrService = _editFrService;
415
- this._editItemQuery = _editItemQuery;
416
- this._editItemService = _editItemService;
417
- this._editLayersQuery = _editLayersQuery;
418
- this._editLayersService = _editLayersService;
255
+ this.editorService = editorService;
419
256
  this._libraryRouteService = _libraryRouteService;
420
- this.fragment$ = this._editFrQuery.selectFragment();
421
- this.thesauri$ = this._editFrQuery.selectThesauri();
422
- this.baseText$ = this._editLayersQuery.select((state) => state.baseText);
423
- this.itemId = route.snapshot.params['iid'];
424
- this.partId = route.snapshot.params['pid'];
425
- this.frTypeId = route.snapshot.url[2]?.path;
426
- this.loc = route.snapshot.params['loc'];
427
- this.frRoleId = route.snapshot.queryParams['frrid'];
428
- // connect _stateDirty to the value of the edit state
429
- this._editFrQuery.selectDirty().subscribe((d) => {
430
- console.log('fr dirty change (from state): ' + d);
431
- this._stateDirty = d ? true : false;
432
- });
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));
433
276
  }
434
277
  /**
435
- * Implementation of ComponentCanDeactivate. The editor can deactivate
436
- * when both its "root" form in the wrapped UI editor and the edit state
437
- * are not dirty. The form gets dirty when edited by the user; the edit
438
- * state gets dirty after a failed save attempt.
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.
439
282
  */
440
- canDeactivate() {
441
- // can-deactivate from dirty
442
- return !this._formDirty && !this._stateDirty;
443
- }
444
- ensureItemLoaded(id) {
445
- if (!this._editItemQuery.hasItem(id)) {
446
- this._editItemService.load(id);
447
- }
448
- }
449
- ensureLayersLoaded(thesauriIds) {
450
- if (!this._editLayersQuery.getValue().part) {
451
- this._editLayersService.load(this.itemId, this.partId, thesauriIds);
452
- }
283
+ getReqThesauriIds() {
284
+ return [];
453
285
  }
454
286
  /**
455
- * Initialize the editor. You MUST call this in your OnInit.
456
- *
457
- * @param thesauriIds The optional ID(s) of the thesauri sets you want
458
- * to use in your editor.
287
+ * Called once data has been loaded.
459
288
  */
460
- initEditor(thesauriIds) {
461
- this.fragment$ = this._editFrQuery.selectFragment();
462
- this.thesauri$ = this._editFrQuery.selectThesauri();
463
- this.baseText$ = this._editLayersQuery.select((state) => state.baseText);
464
- this.frLoc = TokenLocation.parse(this.loc) ?? undefined;
465
- // load item if required
466
- this.ensureItemLoaded(this.itemId);
467
- // load layers if required
468
- this.ensureLayersLoaded(thesauriIds);
469
- // load fragment
470
- this._editFrService.load(this.partId, this.loc, thesauriIds);
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;
471
312
  }
472
313
  /**
473
314
  * Called when the wrapped editor dirty state changes.
474
315
  * In the HTML template, you MUST bind this handler to the dirtyChange event
475
316
  * emitted by your wrapped editor (i.e. (dirtyChange)="onDirtyChange($event)").
317
+ *
476
318
  * @param value The value of the dirty state.
477
319
  */
478
320
  onDirtyChange(value) {
479
- console.log('fr dirty change (from editor): ' + value);
480
- this._formDirty = value;
321
+ console.log('part dirty change (from editor): ' + value);
322
+ this.dirty = value;
481
323
  }
482
324
  /**
483
325
  * Save the fragment.
@@ -485,10 +327,9 @@ class EditFragmentFeatureBase {
485
327
  * @param fragment The fragment to be saved.
486
328
  */
487
329
  save(fragment) {
488
- // get a copy of the container part
489
- const part = JSON.parse(JSON.stringify(this._editLayersQuery.getValue().part));
490
- // replace the fragment edited in it
491
- const frIndex = part.fragments.findIndex((f) => f.location === this.loc);
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);
492
333
  if (frIndex > -1) {
493
334
  part.fragments.splice(frIndex, 1, fragment);
494
335
  }
@@ -496,7 +337,7 @@ class EditFragmentFeatureBase {
496
337
  part.fragments.push(fragment);
497
338
  }
498
339
  // save the new layer part with the replaced fragment
499
- this._editFrService.save(part).then((p) => {
340
+ this.editorService.save(part).then((p) => {
500
341
  console.log(p.id);
501
342
  this.snackbar.open('Fragment saved', 'OK', {
502
343
  duration: 3000,
@@ -508,280 +349,251 @@ class EditFragmentFeatureBase {
508
349
  }
509
350
  close() {
510
351
  // /items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>
511
- const part = this._editLayersQuery.getValue().part;
352
+ const part = this.data.layerPart;
512
353
  const editorKey = this._libraryRouteService.getEditorKeyFromPartType(part.typeId, part.roleId);
513
- const url = `/items/${this.itemId}/${editorKey.partKey}/${part.typeId}/${this.partId}`;
354
+ const url = `/items/${this.identity.itemId}/${editorKey.partKey}/${part.typeId}/${this.identity.partId}`;
514
355
  this._router.navigate([url], {
515
356
  queryParams: {
516
- rid: this.frTypeId,
357
+ rid: this.identity.frTypeId,
517
358
  },
518
359
  });
519
360
  }
520
- }
521
-
522
- class EditFragmentQueryBase extends Query {
523
- constructor(store) {
524
- super(store);
525
- this.store = store;
526
- }
527
- selectDirty() {
528
- return this.select((state) => state.dirty);
529
- }
530
- selectSaving() {
531
- return this.select((state) => state.saving);
532
- }
533
- selectFragment() {
534
- return this.select((state) => state.fragment);
535
- }
536
- selectThesauri() {
537
- return this.select((state) => state.thesauri);
538
- }
539
- }
540
-
541
- const editFragmentInitialState = {
542
- dirty: false,
543
- saving: false,
544
- loading: false,
545
- };
546
- /**
547
- * General-purpose edit-fragment store. This can be used with any fragment,
548
- * so that you should only provide your fragment store, query, and service
549
- * (usually extending EditFragmentServiceBase).
550
- */
551
- let EditFragmentStore = class EditFragmentStore extends Store {
552
- constructor() {
553
- super(editFragmentInitialState);
554
- }
555
- setDirty(value) {
556
- this.update({ dirty: value });
557
- }
558
- setSaving(value) {
559
- this.update({ saving: value });
560
- }
561
- };
562
- EditFragmentStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditFragmentStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
563
- EditFragmentStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditFragmentStore, providedIn: 'root' });
564
- EditFragmentStore = __decorate([
565
- StoreConfig({ name: 'edit-fragment' })
566
- ], EditFragmentStore);
567
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditFragmentStore, decorators: [{
568
- type: Injectable,
569
- args: [{ providedIn: 'root' }]
570
- }], ctorParameters: function () { return []; } });
361
+ }
362
+ EditFragmentFeatureBase.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.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.0.0", type: EditFragmentFeatureBase, selector: "ng-component", ngImport: i0, template: '', isInline: true });
364
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditFragmentFeatureBase, decorators: [{
365
+ type: Component,
366
+ args: [{
367
+ template: '',
368
+ }]
369
+ }], ctorParameters: function () { return [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: FragmentEditorService }, { type: i4.LibraryRouteService }]; } });
571
370
 
572
371
  /**
573
- * Base class for fragment editor services.
372
+ * Service used by part editor wrappers to load part and thesauri
373
+ * or save parts.
574
374
  */
575
- class EditFragmentServiceBase {
375
+ class PartEditorService {
576
376
  constructor(_itemService, _thesaurusService) {
577
377
  this._itemService = _itemService;
578
378
  this._thesaurusService = _thesaurusService;
579
- }
580
- /**
581
- * Load into the state the fragment with the specified ID
582
- * and its thesauri.
583
- *
584
- * @param partId The ID of the part the fragment belongs to.
585
- * @param loc The fragment's location.
586
- * @param thesauriIds The thesauri IDs array, or null.
587
- */
588
- load(partId, loc, thesauriIds = null) {
589
- if (!this.store) {
590
- return;
591
- }
592
- // signal that we're loading
593
- this.store.setLoading(true);
594
- // if thesauri are required:
595
- if (thesauriIds) {
596
- // remove trailing ! from IDs if any
597
- const unscopedIds = thesauriIds.map((id) => {
598
- return this._thesaurusService.getScopedId(id);
599
- });
600
- // fetch fragment and thesauri
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
601
393
  forkJoin({
602
- part: this._itemService.getPart(partId),
394
+ part: this._itemService.getPart(identity.partId || ''),
603
395
  thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
604
- }).subscribe((result) => {
605
- // loading has ended
606
- this.store.setLoading(false);
607
- const layerPart = result.part;
608
- const fr = layerPart.fragments.find((f) => f.location === loc);
609
- if (!fr) {
610
- // not found: new fragment
611
- this.store.update({
612
- fragment: {
613
- location: loc,
614
- },
615
- thesauri: result.thesauri,
616
- loading: false,
617
- error: null,
618
- });
619
- }
620
- else {
621
- // found: existing fragment
622
- this.store.update({
623
- fragment: fr,
624
- thesauri: result.thesauri,
625
- loading: false,
626
- error: null,
627
- });
628
- }
629
- // if the loaded layer part has a thesaurus scope, reload the thesauri
630
- if (result.part?.thesaurusScope) {
631
- const scopedIds = thesauriIds.map((id) => {
632
- return this._thesaurusService.getScopedId(id, result.part?.thesaurusScope);
633
- });
634
- // loading again
635
- this.store.setLoading(true);
636
- this._thesaurusService.getThesauriSet(scopedIds).subscribe((thesauri) => {
637
- // completed, replace the thesauri
638
- this.store.update({
639
- thesauri,
640
- loading: false
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,
641
429
  });
642
- }, (error) => {
643
- console.error(error);
644
- this.store.setLoading(false);
645
- this.store.setError('Error loading thesauri ' + scopedIds.join(', '));
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,
646
438
  });
647
- } // scoped
439
+ },
648
440
  });
649
- }
650
- else {
651
- // without thesauri to be fetched, just fetch the part
652
- this._itemService.getPart(partId).subscribe((part) => {
653
- const layerPart = part;
654
- const fr = layerPart.fragments.find((f) => f.location === loc);
655
- if (!fr) {
656
- // not found: new fragment
657
- this.store.update({
658
- fragment: {
659
- location: loc,
660
- },
661
- loading: false,
662
- error: null,
663
- });
664
- }
665
- else {
666
- this.store.update({
667
- fragment: fr,
668
- loading: false,
669
- error: null,
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,
670
467
  });
671
- }
672
- }, (error) => {
673
- console.error(error);
674
- this.store.setLoading(false);
675
- this.store.setError('Error loading fragment\'s part ' + partId);
468
+ },
676
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);
677
487
  }
678
488
  }
679
489
  /**
680
- * Save the fragments in their layer part.
490
+ * Save the part.
681
491
  *
682
492
  * @param part The part.
683
- * @returns Promise which when successful returns the
684
- * saved part (which contains the fragment being saved).
493
+ * @returns Promise which when successful returns the saved part.
685
494
  */
686
495
  save(part) {
687
- this.store.setSaving(true);
688
- this.store.setDirty(true);
496
+ this._saving$.next(true);
689
497
  return new Promise((resolve, reject) => {
690
- this._itemService.addPart(part).subscribe((saved) => {
691
- this.store.update({
692
- part: saved,
693
- saving: false,
694
- dirty: false,
695
- error: null,
696
- });
697
- resolve(part);
698
- }, (error) => {
699
- console.error(error);
700
- this.store.setSaving(false);
701
- this.store.setError('Error saving fragment\'s part');
702
- reject(error);
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
+ },
703
511
  });
704
512
  });
705
513
  }
706
- setDirty(value = true) {
707
- this.store.setDirty(value);
708
- }
709
- }
514
+ }
515
+ PartEditorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.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.0.0", ngImport: i0, type: PartEditorService, providedIn: 'root' });
517
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: PartEditorService, decorators: [{
518
+ type: Injectable,
519
+ args: [{
520
+ providedIn: 'root',
521
+ }]
522
+ }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.ThesaurusService }]; } });
710
523
 
711
524
  /**
712
525
  * Base class for part feature editors.
713
- * A part feature editor is a wrapper component, linking a part edit
714
- * state (accessed from a part query and loaded with a part service)
715
- * to a corresponding wrapped UI editor.
716
- * It implements ComponentCanDeactivate to allow using the
717
- * PendingChangesGuard. To this end, it tracks the dirty state of
718
- * the edited data coming from 2 different sources: the wrapped editor
719
- * state (from its "root" form), and the store edit state. This is
720
- * set to dirty when a save attempt fails.
721
- * Thus, at the end the user will be prompted when closing an editor
722
- * either because he has changed data in it, or because he attempted
723
- * a save without success.
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.
724
528
  */
725
529
  class EditPartFeatureBase {
726
- constructor(router, route, snackbar, _editPartQuery, _editPartService, _editItemQuery, _editItemService) {
530
+ constructor(router, route, snackbar, itemService, thesaurusService, editorService) {
727
531
  this.router = router;
728
532
  this.snackbar = snackbar;
729
- this._editPartQuery = _editPartQuery;
730
- this._editPartService = _editPartService;
731
- this._editItemQuery = _editItemQuery;
732
- this._editItemService = _editItemService;
533
+ this.itemService = itemService;
534
+ this.thesaurusService = thesaurusService;
535
+ this.editorService = editorService;
536
+ // part route:
537
+ // /items/<iid>/@partGroup/@partTypeId/<pid>?rid
733
538
  // item ID
734
- this.itemId = route.snapshot.params['iid'];
735
- // type ID (from "TYPEID/:pid")
736
- this.typeId = route.snapshot.routeConfig.path.substring(0, route.snapshot.routeConfig.path.indexOf('/'));
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('/'));
737
542
  // part ID or null (if "new")
738
- this.partId = route.snapshot.params['pid'];
739
- if (this.partId === 'new') {
740
- this.partId = undefined;
543
+ let partId = route.snapshot.params['pid'];
544
+ if (partId === 'new') {
545
+ partId = null;
741
546
  }
742
547
  // role ID or null (if "default")
743
- this.roleId = route.snapshot.queryParams['rid'];
744
- if (this.roleId === 'default') {
745
- this.roleId = undefined;
548
+ let roleId = route.snapshot.queryParams['rid'];
549
+ if (roleId === 'default') {
550
+ roleId = null;
746
551
  }
747
- // connect _stateDirty to the value observed in the part state
748
- this._editPartQuery.selectDirty().subscribe((d) => {
749
- console.log('part dirty change (from state): ' + d);
750
- this._stateDirty = d;
751
- });
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));
752
561
  }
753
562
  /**
754
- * Implementation of ComponentCanDeactivate. The editor can deactivate
755
- * when both its "root" form in the wrapped UI editor and the edit state
756
- * are not dirty. The form gets dirty when edited by the user; the edit
757
- * state gets dirty after a failed save attempt.
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.
758
567
  */
759
- canDeactivate() {
760
- // can-deactivate from dirty
761
- return !this._formDirty && !this._stateDirty;
762
- }
763
- ensureItemLoaded(id) {
764
- if (!this._editItemQuery.hasItem(id)) {
765
- this._editItemService.load(id);
766
- }
568
+ getReqThesauriIds() {
569
+ return [];
767
570
  }
768
571
  /**
769
- * Initialize the editor. You MUST call this in your OnInit.
770
- *
771
- * @param thesauriIds The optional ID(s) of the thesauri sets you want
772
- * to use in your editor.
572
+ * Called once data has been loaded.
773
573
  */
774
- initEditor(thesauriIds) {
775
- // connect part and thesauri to state via query
776
- this.part$ = this._editPartQuery.selectPart();
777
- this.thesauri$ = this._editPartQuery.selectThesauri();
778
- // load item if required
779
- this.ensureItemLoaded(this.itemId);
780
- // load part (none if new) and thesauri
781
- this._editPartService.load(this.partId, // a new part has null here
782
- thesauriIds,
783
- // for a new part, we must supply item and role
784
- this.itemId, this.typeId, this.roleId);
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;
785
597
  }
786
598
  /**
787
599
  * Called when the wrapped editor dirty state changes.
@@ -792,7 +604,7 @@ class EditPartFeatureBase {
792
604
  */
793
605
  onDirtyChange(value) {
794
606
  console.log('part dirty change (from editor): ' + value);
795
- this._formDirty = value;
607
+ this.dirty = value;
796
608
  }
797
609
  /**
798
610
  * Save the part.
@@ -800,282 +612,146 @@ class EditPartFeatureBase {
800
612
  * @param part The part to be saved.
801
613
  */
802
614
  save(part) {
803
- // save via part service, which invokes item service
804
- // for the backend, and then updates the part's store
805
- this._editPartService.save(part).then((saved) => {
615
+ this.editorService.save(part).then((saved) => {
806
616
  // update the route-defined part ID if it was null (new part)
807
- if (!this.partId) {
808
- this.partId = saved.id;
617
+ if (!this.identity.partId) {
618
+ this.identity = { ...this.identity, partId: saved.id };
809
619
  }
810
620
  console.log('Part saved: ' + saved.id);
811
621
  this.snackbar.open('Part saved', 'OK', {
812
622
  duration: 3000,
813
623
  });
814
- }, (error) => {
815
- console.error(error);
816
- this.snackbar.open('Error saving part', 'OK');
624
+ }, (reason) => {
625
+ this.snackbar.open(reason.message, 'OK');
817
626
  });
818
627
  }
819
628
  /**
820
629
  * Close the editor by navigating back to the part's item.
821
630
  */
822
631
  close() {
823
- this.router.navigate(['items', this.itemId]);
632
+ this.router.navigate(['items', this.identity.itemId]);
824
633
  }
825
- }
634
+ }
635
+ EditPartFeatureBase.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.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 });
636
+ EditPartFeatureBase.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.0.0", type: EditPartFeatureBase, selector: "ng-component", ngImport: i0, template: '', isInline: true });
637
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditPartFeatureBase, decorators: [{
638
+ type: Component,
639
+ args: [{
640
+ template: '',
641
+ }]
642
+ }], ctorParameters: function () { return [{ type: i1$1.Router }, { type: i1$1.ActivatedRoute }, { type: i2.MatSnackBar }, { type: i1.ItemService }, { type: i1.ThesaurusService }, { type: PartEditorService }]; } });
826
643
 
827
- /**
828
- * Base class for implementing editor part queries.
829
- * To implement an editor part query, just extend this passing the specialized
830
- * part's store to the constructor.
831
- */
832
- class EditPartQueryBase extends Query {
833
- constructor(store) {
834
- super(store);
835
- this.store = store;
836
- }
837
- selectDirty() {
838
- return this.select((state) => state.dirty);
839
- }
840
- selectSaving() {
841
- return this.select((state) => state.saving);
842
- }
843
- selectPart() {
844
- return this.select((state) => state.part || undefined);
644
+ class EditedThesaurusRepository {
645
+ constructor(_thesaurusService) {
646
+ this._thesaurusService = _thesaurusService;
647
+ this._store = createStore({ name: 'edited-thesaurus' }, withProps({}), withRequestsStatus());
648
+ this._loading$ = new BehaviorSubject(false);
649
+ this.loading$ = this._loading$.asObservable();
650
+ this._saving$ = new BehaviorSubject(false);
651
+ this.saving$ = this._saving$.asObservable();
652
+ this.thesaurus$ = this._store.pipe(select((state) => state.thesaurus));
845
653
  }
846
- selectThesauri() {
847
- return this.select((state) => state.thesauri || undefined);
654
+ getValue() {
655
+ return this._store.getValue();
848
656
  }
849
- }
850
-
851
- /**
852
- * Base class for part editor services.
853
- */
854
- class EditPartServiceBase {
855
- constructor(itemService, thesaurusService) {
856
- this.itemService = itemService;
857
- this.thesaurusService = thesaurusService;
657
+ getThesaurus() {
658
+ return this._store.query((state) => state.thesaurus);
858
659
  }
859
660
  /**
860
- * Load into the state the part with the specified ID
861
- * and its thesauri.
862
- * If the ID is not specified, it's a new part; in this case,
863
- * it will get a null ID, but use the specified itemId and roleId.
661
+ * Load the specified thesaurus.
864
662
  *
865
- * @param partId The part ID, unless it's a new part.
866
- * @param thesauriIds The thesauri IDs array.
867
- * @param itemId The item ID for the new part.
868
- * @param typeId The part type ID for the new part.
869
- * @param roleId The role ID for the new part.
663
+ * @param id The thesaurus ID or undefined to load a new thesaurus.
870
664
  */
871
- load(partId, thesauriIds, itemId, typeId, roleId) {
872
- // signal that we're loading
873
- this.store.setLoading(true);
874
- // if thesauri are required:
875
- if (thesauriIds) {
876
- // remove trailing ! from IDs if any
877
- const unscopedIds = thesauriIds.map((id) => {
878
- return this.thesaurusService.getScopedId(id);
879
- });
880
- // fetch part and thesauri
881
- forkJoin({
882
- part: this.itemService.getPart(partId || ''),
883
- thesauri: this.thesaurusService.getThesauriSet(unscopedIds),
884
- }).subscribe((result) => {
885
- // loading has ended
886
- this.store.setLoading(false);
887
- // update the store with a new or existing part,
888
- // and its requested thesauri
889
- this.store.update({
890
- part: result.part || {
891
- id: null,
892
- itemId,
893
- typeId,
894
- roleId,
895
- },
896
- thesauri: result.thesauri,
897
- });
898
- // if the loaded part has a thesaurus scope, reload the thesauri
899
- if (result.part?.thesaurusScope) {
900
- const scopedIds = thesauriIds.map((id) => {
901
- return this.thesaurusService.getScopedId(id, result.part?.thesaurusScope);
902
- });
903
- // loading again
904
- this.store.setLoading(true);
905
- this.thesaurusService.getThesauriSet(scopedIds).subscribe((thesauri) => {
906
- // completed, replace the thesauri
907
- this.store.update({
908
- thesauri,
909
- loading: false,
910
- });
911
- }, (error) => {
912
- console.error(error);
913
- this.store.setLoading(false);
914
- this.store.setError('Error loading thesauri ' + scopedIds.join(', '));
915
- });
916
- } // scoped
665
+ load(id) {
666
+ this._loading$.next(true);
667
+ if (id) {
668
+ this._thesaurusService.getThesaurus(id, true).subscribe({
669
+ next: (thesaurus) => {
670
+ this._loading$.next(false);
671
+ this._store.update(setProp('thesaurus', thesaurus));
672
+ },
673
+ error: (error) => {
674
+ this._loading$.next(false);
675
+ console.error('Error loding thesaurus: ' + JSON.stringify(error || {}));
676
+ },
917
677
  });
918
678
  }
919
679
  else {
920
- // without thesauri to be fetched, just fetch the part
921
- this.itemService.getPart(partId || '').subscribe((part) => {
922
- this.store.update({
923
- part: part || {
924
- id: null,
925
- itemId,
926
- typeId,
927
- roleId,
928
- },
929
- loading: false,
930
- error: null,
931
- });
932
- }, (error) => {
933
- console.error(error);
934
- this.store.setLoading(false);
935
- this.store.setError('Error loading part ' + partId);
936
- });
680
+ this._store.update(setProp('thesaurus', {
681
+ id: '',
682
+ language: 'en',
683
+ entries: [],
684
+ }));
937
685
  }
938
686
  }
939
687
  /**
940
- * Save the part.
688
+ * Save the specified thesaurus.
941
689
  *
942
- * @param part The part.
943
- * @returns Promise which when successful returns the saved part.
690
+ * @param thesaurus The thesaurus to save.
691
+ * @returns Promise with saved thesaurus.
944
692
  */
945
- save(part) {
946
- this.store.setSaving(true);
947
- this.store.setDirty(true);
693
+ save(thesaurus) {
694
+ this._saving$.next(true);
948
695
  return new Promise((resolve, reject) => {
949
- this.itemService.addPart(part).subscribe((saved) => {
950
- this.store.update({
951
- part: saved,
952
- saving: false,
953
- dirty: false,
954
- error: null,
955
- });
956
- resolve(part);
957
- }, (error) => {
958
- console.error(error);
959
- this.store.setSaving(false);
960
- this.store.setError('Error saving part');
961
- reject(error);
696
+ this._thesaurusService.addThesaurus(thesaurus).subscribe({
697
+ next: (saved) => {
698
+ this._saving$.next(false);
699
+ this.load(saved.id);
700
+ resolve(saved);
701
+ },
702
+ error: (error) => {
703
+ this._saving$.next(false);
704
+ console.error('Error loding thesaurus: ' + JSON.stringify(error || {}));
705
+ reject(error);
706
+ },
962
707
  });
963
708
  });
964
709
  }
965
- setDirty(value = true) {
966
- this.store.setDirty(value);
967
- }
968
- }
969
-
970
- /**
971
- * General-purpose edit-part store. This can be used with any part, so that
972
- * you should only provide your part store, query, and service (usually
973
- * extending EditPartServiceBase).
974
- */
975
- let EditPartStore = class EditPartStore extends Store {
976
- constructor() {
977
- super({});
978
- }
979
- setDirty(value) {
980
- this.update({ dirty: value });
981
- }
982
- setSaving(value) {
983
- this.update({ saving: value });
984
- }
985
- };
986
- EditPartStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditPartStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
987
- EditPartStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditPartStore, providedIn: 'root' });
988
- EditPartStore = __decorate([
989
- StoreConfig({ name: 'edit-part' })
990
- ], EditPartStore);
991
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditPartStore, decorators: [{
992
- type: Injectable,
993
- args: [{ providedIn: 'root' }]
994
- }], ctorParameters: function () { return []; } });
995
-
996
- const initialState = {
997
- locations: [],
998
- breakChance: -1,
999
- layerHints: [],
1000
- thesauri: {},
1001
- loading: false,
1002
- deletingFragment: false,
1003
- savingFragment: false,
1004
- refreshingBreakChance: false,
1005
- patchingLayer: false,
1006
- };
1007
- let EditLayerPartStore = class EditLayerPartStore extends Store {
1008
- constructor() {
1009
- super(initialState);
1010
- }
1011
- setDeletingFragment(value = true) {
1012
- this.update({ deletingFragment: value });
1013
- }
1014
- setSavingFragment(value = true) {
1015
- this.update({ savingFragment: value });
1016
- }
1017
- setRefreshingBreakChance(value = true) {
1018
- this.update({ refreshingBreakChance: value });
1019
- }
1020
- setBreakChance(value) {
1021
- this.update({ breakChance: value });
1022
- }
1023
- setPatchingLayer(value = true) {
1024
- this.update({ patchingLayer: value });
1025
- }
1026
- setLocations(locations) {
1027
- this.update({ locations });
1028
- }
1029
- };
1030
- EditLayerPartStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
1031
- EditLayerPartStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartStore, providedIn: 'root' });
1032
- EditLayerPartStore = __decorate([
1033
- StoreConfig({ name: 'edit-layer-part' })
1034
- ], EditLayerPartStore);
1035
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartStore, decorators: [{
1036
- type: Injectable,
1037
- args: [{ providedIn: 'root' }]
1038
- }], ctorParameters: function () { return []; } });
1039
-
1040
- class EditLayerPartQuery extends Query {
1041
- constructor(store) {
1042
- super(store);
1043
- this.store = store;
1044
- }
1045
- selectDeletingFragment() {
1046
- return this.select((state) => state.deletingFragment);
1047
- }
1048
- selectSavingFragment() {
1049
- return this.select((state) => state.savingFragment);
1050
- }
1051
- selectRefreshingBreakChance() {
1052
- return this.select((state) => state.refreshingBreakChance);
1053
- }
1054
- selectBreakChance() {
1055
- return this.select((state) => state.breakChance);
1056
- }
1057
- selectLayerHints() {
1058
- return this.select((state) => state.layerHints);
1059
- }
1060
- selectPatchingLayers() {
1061
- return this.select((state) => state.patchingLayer);
1062
- }
1063
710
  }
1064
- EditLayerPartQuery.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartQuery, deps: [{ token: EditLayerPartStore }], target: i0.ɵɵFactoryTarget.Injectable });
1065
- EditLayerPartQuery.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartQuery, providedIn: 'root' });
1066
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartQuery, decorators: [{
711
+ EditedThesaurusRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedThesaurusRepository, deps: [{ token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
712
+ EditedThesaurusRepository.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedThesaurusRepository, providedIn: 'root' });
713
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedThesaurusRepository, decorators: [{
1067
714
  type: Injectable,
1068
715
  args: [{ providedIn: 'root' }]
1069
- }], ctorParameters: function () { return [{ type: EditLayerPartStore }]; } });
716
+ }], ctorParameters: function () { return [{ type: i1.ThesaurusService }]; } });
1070
717
 
1071
- class EditLayerPartService {
1072
- constructor(_store, _itemService, _facetService, _thesaurusService) {
1073
- this._store = _store;
718
+ /**
719
+ * Edited layer part ELF repository.
720
+ */
721
+ class EditedLayerRepository {
722
+ constructor(_itemService, _facetService, _thesaurusService) {
1074
723
  this._itemService = _itemService;
1075
724
  this._facetService = _facetService;
1076
725
  this._thesaurusService = _thesaurusService;
1077
- }
1078
- getLocations(part) {
726
+ this._store = createStore({ name: 'edited-layer' }, withProps({
727
+ locations: [],
728
+ breakChance: 0,
729
+ layerHints: [],
730
+ }), withRequestsStatus());
731
+ this._loading$ = new BehaviorSubject(false);
732
+ this.loading$ = this._loading$.asObservable();
733
+ this._saving$ = new BehaviorSubject(false);
734
+ this.saving$ = this._saving$.asObservable();
735
+ this.part$ = this._store.pipe(select((state) => state.part));
736
+ this.baseText$ = this._store.pipe(select((state) => state.baseText));
737
+ this.baseTextPart$ = this._store.pipe(select((state) => state.baseTextPart));
738
+ this.locations$ = this._store.pipe(select((state) => state.locations));
739
+ this.breakChance$ = this._store.pipe(select((state) => state.breakChance));
740
+ this.layerHints$ = this._store.pipe(select((state) => state.layerHints));
741
+ }
742
+ getValue() {
743
+ return this._store.getValue();
744
+ }
745
+ getPart() {
746
+ return this._store.query((state) => state.part);
747
+ }
748
+ getBaseText() {
749
+ return this._store.query((state => state.baseText));
750
+ }
751
+ getLocations() {
752
+ return this._store.query((state) => state.locations);
753
+ }
754
+ getPartLocations(part) {
1079
755
  const locations = [];
1080
756
  if (part && part.fragments) {
1081
757
  part.fragments.forEach((p) => {
@@ -1084,89 +760,108 @@ class EditLayerPartService {
1084
760
  }
1085
761
  return locations;
1086
762
  }
1087
- /**
1088
- * Load the state for editing layer part(s).
1089
- *
1090
- * @param itemId The item ID the layer part belongs to.
1091
- * @param partId The layer part ID.
1092
- * @param thesauriIds The optional thesauri IDs to load.
1093
- */
1094
- load(itemId, partId, thesauriIds) {
1095
- if (this._store.getValue().loading) {
1096
- return;
1097
- }
1098
- this._store.setLoading(true);
1099
- if (thesauriIds) {
1100
- // remove trailing ! from IDs if any
1101
- const unscopedIds = thesauriIds.map((id) => {
1102
- return this._thesaurusService.getScopedId(id);
1103
- });
1104
- forkJoin({
1105
- // TODO: eventually optimize by adding method param to load only fragments locations
1106
- layerPart: this._itemService.getPart(partId),
1107
- baseText: this._itemService.getBaseTextPart(itemId),
1108
- layers: this._facetService.getFacetParts(itemId, true),
1109
- breakChance: this._itemService.getLayerPartBreakChance(partId),
1110
- layerHints: this._itemService.getLayerPartHints(partId),
1111
- thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
1112
- }).subscribe((result) => {
1113
- this._store.update({
1114
- part: result.layerPart,
1115
- baseText: result.baseText.text,
1116
- baseTextPart: result.baseText.part,
1117
- locations: this.getLocations(result.layerPart),
1118
- breakChance: result.breakChance.chance,
1119
- layerHints: result.layerHints,
1120
- thesauri: result.thesauri,
1121
- loading: false,
1122
- error: undefined,
1123
- });
763
+ loadWithThesauri(itemId, partId, thesauriIds) {
764
+ // remove trailing ! from IDs if any
765
+ const unscopedIds = thesauriIds.map((id) => {
766
+ return this._thesaurusService.getScopedId(id);
767
+ });
768
+ this._loading$.next(true);
769
+ forkJoin({
770
+ // TODO: eventually optimize by adding method param to load only fragments locations
771
+ layerPart: this._itemService.getPart(partId),
772
+ baseText: this._itemService.getBaseTextPart(itemId),
773
+ layers: this._facetService.getFacetParts(itemId, true),
774
+ breakChance: this._itemService.getLayerPartBreakChance(partId),
775
+ layerHints: this._itemService.getLayerPartHints(partId),
776
+ thesauri: this._thesaurusService.getThesauriSet(unscopedIds),
777
+ }).subscribe({
778
+ next: (result) => {
1124
779
  // if the loaded part has a thesaurus scope, reload the thesauri
1125
780
  if (result.layerPart?.thesaurusScope) {
1126
781
  const scopedIds = thesauriIds.map((id) => {
1127
782
  return this._thesaurusService.getScopedId(id, result.layerPart.thesaurusScope);
1128
783
  });
1129
- this._store.setLoading(true);
1130
- this._thesaurusService.getThesauriSet(scopedIds).subscribe((thesauri) => {
1131
- this._store.update({
1132
- thesauri,
1133
- });
1134
- }, (error) => {
1135
- console.error(error);
1136
- this._store.setLoading(false);
1137
- this._store.setError('Error loading thesauri ' + scopedIds.join(', '));
784
+ this._thesaurusService.getThesauriSet(scopedIds).subscribe({
785
+ next: (thesauri) => {
786
+ this._loading$.next(false);
787
+ this._store.update((state) => ({
788
+ ...state,
789
+ part: result.layerPart,
790
+ baseText: result.baseText.text,
791
+ baseTextPart: result.baseText.part,
792
+ locations: this.getPartLocations(result.layerPart),
793
+ breakChance: result.breakChance.chance,
794
+ layerHints: result.layerHints,
795
+ thesauri: thesauri,
796
+ }));
797
+ },
798
+ error: (error) => {
799
+ this._loading$.next(false);
800
+ console.error('Error loading thesauri: ' + JSON.stringify(error || {}));
801
+ },
1138
802
  });
1139
- } // scoped
1140
- }, (error) => {
1141
- console.error(error);
1142
- this._store.setLoading(false);
1143
- this._store.setError('Error loading text layer part ' + partId);
1144
- });
1145
- }
1146
- else {
1147
- forkJoin({
1148
- // TODO: eventually optimize by adding method param to load only fragments locations
1149
- layerPart: this._itemService.getPart(partId),
1150
- baseText: this._itemService.getBaseTextPart(itemId),
1151
- layers: this._facetService.getFacetParts(itemId, true),
1152
- breakChance: this._itemService.getLayerPartBreakChance(partId),
1153
- layerHints: this._itemService.getLayerPartHints(partId),
1154
- }).subscribe((result) => {
1155
- this._store.update({
803
+ }
804
+ else {
805
+ this._loading$.next(false);
806
+ this._store.update((state) => ({
807
+ ...state,
808
+ part: result.layerPart,
809
+ baseText: result.baseText.text,
810
+ baseTextPart: result.baseText.part,
811
+ locations: this.getPartLocations(result.layerPart),
812
+ breakChance: result.breakChance.chance,
813
+ layerHints: result.layerHints,
814
+ thesauri: result.thesauri,
815
+ }));
816
+ }
817
+ },
818
+ error: (error) => {
819
+ this._loading$.next(false);
820
+ console.error('Error loading text layer part: ' + JSON.stringify(error || {}));
821
+ },
822
+ });
823
+ }
824
+ loadWithoutThesauri(itemId, partId) {
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
+ }).subscribe({
834
+ next: (result) => {
835
+ this._loading$.next(false);
836
+ this._store.update((state) => ({
837
+ ...state,
1156
838
  part: result.layerPart,
1157
839
  baseText: result.baseText.text,
1158
840
  baseTextPart: result.baseText.part,
1159
- locations: this.getLocations(result.layerPart),
841
+ locations: this.getPartLocations(result.layerPart),
1160
842
  breakChance: result.breakChance.chance,
1161
843
  layerHints: result.layerHints,
1162
- loading: false,
1163
- error: undefined,
1164
- });
1165
- }, (error) => {
1166
- console.error(error);
1167
- this._store.setLoading(false);
1168
- this._store.setError('Error loading text layer part ' + partId);
1169
- });
844
+ }));
845
+ },
846
+ error: (error) => {
847
+ this._loading$.next(false);
848
+ console.error('Error loading text layer part: ' + JSON.stringify(error || {}));
849
+ },
850
+ });
851
+ }
852
+ /**
853
+ * Load the state for editing layer part(s).
854
+ *
855
+ * @param itemId The item ID the layer part belongs to.
856
+ * @param partId The layer part ID.
857
+ * @param thesauriIds The optional thesauri IDs to load.
858
+ */
859
+ load(itemId, partId, thesauriIds) {
860
+ if (thesauriIds?.length) {
861
+ this.loadWithThesauri(itemId, partId, thesauriIds);
862
+ }
863
+ else {
864
+ this.loadWithoutThesauri(itemId, partId);
1170
865
  }
1171
866
  }
1172
867
  /**
@@ -1175,29 +870,30 @@ class EditLayerPartService {
1175
870
  refreshBreakChance() {
1176
871
  const store = this._store.getValue();
1177
872
  const part = store.part;
1178
- if (!part || store.refreshingBreakChance) {
873
+ if (!part) {
1179
874
  return;
1180
875
  }
1181
- this._store.setRefreshingBreakChance(true);
1182
- this._itemService.getLayerPartBreakChance(part.id).subscribe((result) => {
1183
- this._store.setRefreshingBreakChance(false);
1184
- this._store.setBreakChance(result.chance);
1185
- }, (error) => {
1186
- console.error(error);
1187
- this._store.setRefreshingBreakChance(false);
1188
- this._store.setBreakChance(-1);
1189
- this._store.setError('Error refreshing break chance for text layer part ' + part.id);
876
+ this._itemService.getLayerPartBreakChance(part.id).subscribe({
877
+ next: (result) => {
878
+ this._store.update(setProp('breakChance', result.chance));
879
+ },
880
+ error: (error) => {
881
+ console.error('Error calculating break chance: ' + JSON.stringify(error || {}));
882
+ this._store.update(setProp('breakChance', -1));
883
+ },
1190
884
  });
1191
885
  }
1192
886
  applyLayerPatches(partId, patches) {
1193
- this._store.setPatchingLayer(true);
1194
- this._itemService.applyLayerPatches(partId, patches).subscribe((part) => {
1195
- this._store.setPatchingLayer(false);
1196
- this.load(part.itemId, partId);
1197
- }, (error) => {
1198
- console.error(error);
1199
- this._store.setPatchingLayer(false);
1200
- this._store.setError('Error patching text layer part ' + partId);
887
+ this._saving$.next(true);
888
+ this._itemService.applyLayerPatches(partId, patches).subscribe({
889
+ next: (part) => {
890
+ this._saving$.next(false);
891
+ this.load(part.itemId, partId);
892
+ },
893
+ error: (error) => {
894
+ this._saving$.next(false);
895
+ console.error('Error patching text layer part: ' + JSON.stringify(error || {}));
896
+ },
1201
897
  });
1202
898
  }
1203
899
  /**
@@ -1206,7 +902,6 @@ class EditLayerPartService {
1206
902
  * @param loc The fragment's location.
1207
903
  */
1208
904
  deleteFragment(loc) {
1209
- this._store.setDeletingFragment(true);
1210
905
  // find the fragment
1211
906
  let part = this._store.getValue().part;
1212
907
  if (!part) {
@@ -1218,23 +913,23 @@ class EditLayerPartService {
1218
913
  if (i === -1) {
1219
914
  return;
1220
915
  }
916
+ this._saving$.next(true);
1221
917
  // remove it from the part
1222
918
  // work on a copy, as store objects are immutable
1223
919
  part = deepCopy(part);
1224
920
  part.fragments.splice(i, 1);
1225
921
  // update the part and reload state once done
1226
- this._itemService.addPart(part).subscribe((_) => {
1227
- this._store.setDeletingFragment(false);
1228
- this.load(part.itemId, part.id);
1229
- // this._store.update({
1230
- // part: part,
1231
- // deletingFragment: false,
1232
- // error: null
1233
- // });
1234
- }, (error) => {
1235
- console.error(error);
1236
- this._store.setDeletingFragment(false);
1237
- this._store.setError(`Error deleting fragment at ${loc} in part ${part.id}`);
922
+ this._itemService.addPart(part).subscribe({
923
+ next: (_) => {
924
+ this._saving$.next(false);
925
+ this.load(part.itemId, part.id);
926
+ },
927
+ error: (error) => {
928
+ this._saving$.next(false);
929
+ console.error(error);
930
+ console.error(`Error deleting fragment at ${loc} in part ${part.id}: ` +
931
+ JSON.stringify(error || {}));
932
+ },
1238
933
  });
1239
934
  }
1240
935
  /**
@@ -1243,7 +938,6 @@ class EditLayerPartService {
1243
938
  * @param fragment The fragment.
1244
939
  */
1245
940
  saveFragment(fragment) {
1246
- this._store.setSavingFragment();
1247
941
  // find the fragment
1248
942
  let part = this._store.getValue().part;
1249
943
  if (!part) {
@@ -1251,6 +945,7 @@ class EditLayerPartService {
1251
945
  }
1252
946
  // add or replace it
1253
947
  // work on a copy, as store objects are immutable
948
+ this._saving$.next(true);
1254
949
  part = deepCopy(part);
1255
950
  // replace all the overlapping fragments with the new one
1256
951
  const newLoc = TokenLocation.parse(fragment.location);
@@ -1270,143 +965,39 @@ class EditLayerPartService {
1270
965
  // add the new fragment
1271
966
  part.fragments.splice(insertAt, 0, fragment);
1272
967
  // update the part and reload once done
1273
- this._itemService.addPart(part).subscribe((_) => {
1274
- this.load(part.itemId, part.id);
1275
- // this._store.update({
1276
- // part: part,
1277
- // deletingFragment: false,
1278
- // error: null
1279
- // });
1280
- }, (error) => {
1281
- console.error(error);
1282
- this._store.setSavingFragment(false);
1283
- this._store.setError(`Error saving fragment at ${fragment.location} in part ${part.id}`);
1284
- });
1285
- }
1286
- }
1287
- EditLayerPartService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartService, deps: [{ token: EditLayerPartStore }, { token: i2.ItemService }, { token: i2.FacetService }, { token: i2.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
1288
- EditLayerPartService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartService, providedIn: 'root' });
1289
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditLayerPartService, decorators: [{
1290
- type: Injectable,
1291
- args: [{ providedIn: 'root' }]
1292
- }], ctorParameters: function () { return [{ type: EditLayerPartStore }, { type: i2.ItemService }, { type: i2.FacetService }, { type: i2.ThesaurusService }]; } });
1293
-
1294
- let EditThesaurusStore = class EditThesaurusStore extends Store {
1295
- constructor() {
1296
- super({});
1297
- }
1298
- setDirty(value = true) {
1299
- this.update({
1300
- dirty: value,
1301
- });
1302
- }
1303
- setSaving(value = true) {
1304
- this.update({
1305
- saving: value,
1306
- });
1307
- }
1308
- };
1309
- EditThesaurusStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
1310
- EditThesaurusStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusStore, providedIn: 'root' });
1311
- EditThesaurusStore = __decorate([
1312
- StoreConfig({ name: 'edit-thesaurus', resettable: true })
1313
- ], EditThesaurusStore);
1314
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusStore, decorators: [{
1315
- type: Injectable,
1316
- args: [{ providedIn: 'root' }]
1317
- }], ctorParameters: function () { return []; } });
1318
-
1319
- class EditThesaurusQuery extends Query {
1320
- constructor(store) {
1321
- super(store);
1322
- this.store = store;
1323
- }
1324
- selectThesaurus() {
1325
- return this.select(state => state.thesaurus);
1326
- }
1327
- // public selectLoading(): Observable<boolean | undefined> {
1328
- // return this.select(state => state.loading);
1329
- // }
1330
- selectSaving() {
1331
- return this.select(state => state.saving);
1332
- }
1333
- }
1334
- EditThesaurusQuery.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusQuery, deps: [{ token: EditThesaurusStore }], target: i0.ɵɵFactoryTarget.Injectable });
1335
- EditThesaurusQuery.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusQuery, providedIn: 'root' });
1336
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusQuery, decorators: [{
1337
- type: Injectable,
1338
- args: [{ providedIn: 'root' }]
1339
- }], ctorParameters: function () { return [{ type: EditThesaurusStore }]; } });
1340
-
1341
- class EditThesaurusService {
1342
- constructor(_store, _thesaurusService) {
1343
- this._store = _store;
1344
- this._thesaurusService = _thesaurusService;
1345
- }
1346
- load(id) {
1347
- this._store.setLoading(true);
1348
- if (id) {
1349
- this._thesaurusService.getThesaurus(id, true).subscribe((thesaurus) => {
1350
- this._store.setLoading(false);
1351
- this._store.update({
1352
- thesaurus,
1353
- });
1354
- }, (error) => {
1355
- console.error(error);
1356
- this._store.setLoading(false);
1357
- this._store.setError('Error loading thesaurus ' + id);
1358
- });
1359
- }
1360
- else {
1361
- this._store.update({
1362
- thesaurus: {
1363
- id: '',
1364
- language: 'en',
1365
- entries: [],
1366
- },
1367
- });
1368
- }
1369
- }
1370
- reset() {
1371
- this._store.reset();
1372
- }
1373
- save(thesaurus) {
1374
- this._store.setSaving(true);
1375
- return new Promise((resolve, reject) => {
1376
- this._thesaurusService.addThesaurus(thesaurus).subscribe((saved) => {
1377
- this._store.setSaving(false);
1378
- this.load(this._store.getValue().thesaurus.id);
1379
- resolve(saved);
1380
- }, (error) => {
1381
- console.error(error);
1382
- this._store.setSaving(false);
1383
- this._store.setError('Error saving thesaurus');
1384
- reject(error);
1385
- });
968
+ this._itemService.addPart(part).subscribe({
969
+ next: (_) => {
970
+ this._saving$.next(false);
971
+ this.load(part.itemId, part.id);
972
+ },
973
+ error: (error) => {
974
+ this._saving$.next(false);
975
+ console.error(`Error saving fragment at ${fragment.location} in part ${part.id}: ` + JSON.stringify(error || {}));
976
+ },
1386
977
  });
1387
978
  }
1388
979
  }
1389
- EditThesaurusService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusService, deps: [{ token: EditThesaurusStore }, { token: i2.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
1390
- EditThesaurusService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusService, providedIn: 'root' });
1391
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: EditThesaurusService, decorators: [{
980
+ EditedLayerRepository.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedLayerRepository, deps: [{ token: i1.ItemService }, { token: i1.FacetService }, { token: i1.ThesaurusService }], target: i0.ɵɵFactoryTarget.Injectable });
981
+ EditedLayerRepository.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedLayerRepository, providedIn: 'root' });
982
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: EditedLayerRepository, decorators: [{
1392
983
  type: Injectable,
1393
984
  args: [{ providedIn: 'root' }]
1394
- }], ctorParameters: function () { return [{ type: EditThesaurusStore }, { type: i2.ThesaurusService }]; } });
985
+ }], ctorParameters: function () { return [{ type: i1.ItemService }, { type: i1.FacetService }, { type: i1.ThesaurusService }]; } });
1395
986
 
1396
987
  class CadmusStateModule {
1397
988
  }
1398
- CadmusStateModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: CadmusStateModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
1399
- CadmusStateModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.1", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
989
+ CadmusStateModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: CadmusStateModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
990
+ CadmusStateModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.0", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
1400
991
  // Cadmus
1401
992
  CadmusCoreModule,
1402
993
  CadmusApiModule,
1403
994
  NgToolsModule] });
1404
- CadmusStateModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
995
+ CadmusStateModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: CadmusStateModule, imports: [CommonModule,
1405
996
  // Cadmus
1406
997
  CadmusCoreModule,
1407
998
  CadmusApiModule,
1408
999
  NgToolsModule] });
1409
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: CadmusStateModule, decorators: [{
1000
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: CadmusStateModule, decorators: [{
1410
1001
  type: NgModule,
1411
1002
  args: [{
1412
1003
  imports: [
@@ -1427,5 +1018,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImpor
1427
1018
  * Generated bundle index. Do not edit.
1428
1019
  */
1429
1020
 
1430
- export { AppQuery, AppService, AppStore, CadmusStateModule, EditFragmentFeatureBase, EditFragmentQueryBase, EditFragmentServiceBase, EditFragmentStore, EditItemQuery, EditItemService, EditItemStore, EditLayerPartQuery, EditLayerPartService, EditLayerPartStore, EditPartFeatureBase, EditPartQueryBase, EditPartServiceBase, EditPartStore, EditThesaurusQuery, EditThesaurusService, EditThesaurusStore, editFragmentInitialState };
1021
+ export { AppRepository, CadmusStateModule, EditFragmentFeatureBase, EditPartFeatureBase, EditedLayerRepository, EditedThesaurusRepository, FragmentEditorService, PartEditorService };
1022
+ //# sourceMappingURL=myrmidon-cadmus-state.mjs.map
1431
1023
  //# sourceMappingURL=myrmidon-cadmus-state.mjs.map