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