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