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