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