@alizarin/clm 0.2.1-alpha.83

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.
package/dist/clm.js ADDED
@@ -0,0 +1,286 @@
1
+ import { RDM, nodeConfig, registerExtensionHandler, registerResolvableDatatype, utils, viewModels, wasmReady } from "alizarin";
2
+ function getReferenceValueFromCollection(e, c) {
3
+ let l = e.__allConcepts?.[c];
4
+ if (!l) return null;
5
+ let u = [];
6
+ for (let [e, c] of Object.entries(l.prefLabels || {})) {
7
+ let d = c;
8
+ u.push({
9
+ id: d.id,
10
+ language_id: e,
11
+ list_item_id: l.id,
12
+ value: d.value,
13
+ valuetype_id: "prefLabel"
14
+ });
15
+ }
16
+ return {
17
+ labels: u,
18
+ list_id: e.id,
19
+ uri: l.source || `http://localhost:8000/plugins/controlled-list-manager/item/${l.id}`,
20
+ id: l.id
21
+ };
22
+ }
23
+ function getReferenceValueByLabelFromCollection(e, c) {
24
+ let l = c.trim().toLowerCase(), u = e.__values || {}, d = Object.values(u).find((e) => e.value?.trim().toLowerCase() === l);
25
+ return !d || !d.__concept ? null : getReferenceValueFromCollection(e, d.__concept.id);
26
+ }
27
+ function flattenToReferences(e) {
28
+ let c = [], l = (e) => {
29
+ if (e) {
30
+ if (e._ref) {
31
+ let l = e._ref, u = typeof l.toJSON == "function" ? l.toJSON() : l;
32
+ u && (u.labels || u.__needs_rdm_label_lookup) && c.push(u);
33
+ return;
34
+ }
35
+ if (Array.isArray(e)) {
36
+ e.forEach(l);
37
+ return;
38
+ }
39
+ if (e.__needs_rdm_label_lookup && e.label) {
40
+ c.push(e);
41
+ return;
42
+ }
43
+ if (e.labels) {
44
+ c.push(e);
45
+ return;
46
+ }
47
+ if (typeof e == "string") try {
48
+ l(JSON.parse(e));
49
+ } catch {}
50
+ }
51
+ };
52
+ return l(e), c;
53
+ }
54
+ function renderReferenceDisplay(e, c) {
55
+ if (!e) return null;
56
+ if (e.__needs_rdm_label_lookup && e.label) return e.label;
57
+ if (e.labels && e.labels.length > 0) {
58
+ let l = e.labels.find((e) => e.language_id === c && e.valuetype_id === "prefLabel");
59
+ if (l) return l.value;
60
+ let u = e.labels.find((e) => e.language_id === c);
61
+ if (u) return u.value;
62
+ let d = e.labels.find((e) => e.valuetype_id === "prefLabel");
63
+ return d ? d.value : e.labels[0].value;
64
+ }
65
+ return null;
66
+ }
67
+ wasmReady.then(() => {
68
+ registerExtensionHandler("reference", { renderDisplay: (e, c) => {
69
+ if (!e) return null;
70
+ let l = flattenToReferences(e);
71
+ return l.length === 0 ? null : l.map((e) => renderReferenceDisplay(e, c)).filter((e) => e !== null).join(", ");
72
+ } });
73
+ });
74
+ var StaticReferenceLabel = class {
75
+ id;
76
+ language_id;
77
+ list_item_id;
78
+ value;
79
+ valuetype_id;
80
+ constructor(e) {
81
+ this.id = e.id, this.language_id = e.language_id, this.list_item_id = e.list_item_id, this.value = e.value, this.valuetype_id = e.valuetype_id;
82
+ }
83
+ toJSON() {
84
+ return {
85
+ id: this.id,
86
+ language_id: this.language_id,
87
+ list_item_id: this.list_item_id,
88
+ value: this.value,
89
+ valuetype_id: this.valuetype_id
90
+ };
91
+ }
92
+ }, StaticReference = class {
93
+ labels;
94
+ list_id;
95
+ uri;
96
+ constructor(e) {
97
+ this.list_id = e.list_id, this.uri = e.uri, this.labels = [];
98
+ for (let c of e.labels) c instanceof StaticReferenceLabel ? this.labels.push(c) : this.labels.push(new StaticReferenceLabel(c));
99
+ }
100
+ toJSON() {
101
+ return {
102
+ labels: this.labels.map((e) => e.toJSON()),
103
+ list_id: this.list_id,
104
+ uri: this.uri
105
+ };
106
+ }
107
+ };
108
+ function referenceToString(e) {
109
+ if (e.labels.length == 1) return e.labels[0].value;
110
+ let c, l = utils.getCurrentLanguage();
111
+ for (let u of e.labels) if (u.valuetype_id === "prefLabel" && (c = u.value, u.language_id === l)) return c;
112
+ return c || "(undefined)";
113
+ }
114
+ var ReferenceValueViewModel = class c extends String {
115
+ _ = void 0;
116
+ __parentPseudo;
117
+ describeField = () => this.__parentPseudo ? this.__parentPseudo.describeField() : null;
118
+ describeFieldGroup = () => this.__parentPseudo ? this.__parentPseudo.describeFieldGroup() : null;
119
+ _ref;
120
+ _pendingLookup = null;
121
+ _resolvedRef = null;
122
+ _resolutionPromise = null;
123
+ _tile = null;
124
+ _nodeid = null;
125
+ _collectionId = null;
126
+ get [Symbol.toStringTag]() {
127
+ return "ReferenceValue";
128
+ }
129
+ constructor(e, c, l, u, d) {
130
+ super(e ? referenceToString(e) : c?.type === "label" ? c.label : "(pending)"), this._ref = e, this._pendingLookup = c || null, this._tile = l || null, this._nodeid = u || null, this._collectionId = d ?? c?.collectionId ?? null;
131
+ }
132
+ async getParent() {
133
+ let l = await this._resolvePending();
134
+ if (!l || !this._collectionId) return null;
135
+ let u = l.labels[0]?.list_item_id;
136
+ if (!u) return null;
137
+ let d = await RDM.retrieveCollection(this._collectionId);
138
+ if (!d.getParentId) throw Error(`Collection ${this._collectionId} does not support hierarchy lookups. Ensure WASM is initialized and the collection is a StaticCollection.`);
139
+ let f = d.getParentId(u);
140
+ if (!f) return null;
141
+ let p = getReferenceValueFromCollection(d, f);
142
+ return p ? new c(new StaticReference(p), void 0, void 0, void 0, this._collectionId) : null;
143
+ }
144
+ get parent() {
145
+ return this.getParent();
146
+ }
147
+ async getAncestors() {
148
+ let e = [], c = this;
149
+ for (; (c = await c.parent) !== null;) e.push(c);
150
+ return e;
151
+ }
152
+ get ancestors() {
153
+ return this.getAncestors();
154
+ }
155
+ async _resolvePending() {
156
+ return this._resolvedRef ? this._resolvedRef : this._ref ? (this._resolvedRef = this._ref, this._ref) : this._pendingLookup ? (this._resolutionPromise ||= (async () => {
157
+ let c = this._pendingLookup, l = await RDM.retrieveCollection(c.collectionId), u = null;
158
+ if (c.type === "uuid" ? (u = getReferenceValueFromCollection(l, c.uuid), u || console.error("Could not find reference for UUID", c.uuid, "in collection", c.collectionId)) : c.type === "label" && (u = getReferenceValueByLabelFromCollection(l, c.label), u || console.error("Could not find reference for label", c.label, "in collection", c.collectionId)), u && (this._resolvedRef = new StaticReference(u), this._tile && this._nodeid)) {
159
+ let e = this._tile.data.get(this._nodeid);
160
+ Array.isArray(e) || this._tile.data.set(this._nodeid, this._resolvedRef.toJSON());
161
+ }
162
+ return this._resolvedRef;
163
+ })(), this._resolutionPromise) : null;
164
+ }
165
+ async forJson() {
166
+ let e = await this._resolvePending();
167
+ return e && typeof e.toJSON == "function" ? e.toJSON() : e;
168
+ }
169
+ async getValue() {
170
+ return this._resolvePending();
171
+ }
172
+ async getDisplay() {
173
+ let e = await this._resolvePending();
174
+ return e ? referenceToString(e) : "(unresolved)";
175
+ }
176
+ toJSON() {
177
+ return this._resolvedRef ? this._resolvedRef.toJSON() : this._ref ? this._ref.toJSON() : this._pendingLookup ? this._pendingLookup.type === "uuid" ? {
178
+ __needs_rdm_lookup: !0,
179
+ uuid: this._pendingLookup.uuid
180
+ } : {
181
+ __needs_rdm_label_lookup: !0,
182
+ label: this._pendingLookup.label,
183
+ controlledList: this._pendingLookup.collectionId
184
+ } : null;
185
+ }
186
+ async __asTileData() {
187
+ let e = await this._resolvePending();
188
+ return e && typeof e.toJSON == "function" ? e.toJSON() : e;
189
+ }
190
+ static async __create(e, l, u, d) {
191
+ let f = l.nodeid, p = l.config?.controlledList || l.config?.rdmCollection;
192
+ if (!p) throw Error(`Node ${l.alias} (${l.nodeid}) missing controlledList or rdmCollection in config`);
193
+ if (e && (e.data.has(f) || e.data.set(f, null), u !== null)) {
194
+ if (u instanceof Promise) return u.then((u) => c.__create(e, l, u, d));
195
+ if (typeof u == "string") if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.exec(u)) {
196
+ let l = {
197
+ type: "uuid",
198
+ uuid: u,
199
+ collectionId: p
200
+ };
201
+ return e.data.set(f, {
202
+ __needs_rdm_lookup: !0,
203
+ uuid: u
204
+ }), new c(null, l, e, f, p);
205
+ } else throw Error(`Set references using values from collections, not strings: ${u}`);
206
+ else if (typeof u == "object" && u && u.__needs_rdm_lookup && u.uuid) {
207
+ let l = {
208
+ type: "uuid",
209
+ uuid: u.uuid,
210
+ collectionId: p
211
+ };
212
+ return e.data.set(f, u), new c(null, l, e, f, p);
213
+ } else if (typeof u == "object" && u && u.__needs_rdm_label_lookup && u.label) {
214
+ let l = u.controlledList || p, d = {
215
+ type: "label",
216
+ label: u.label,
217
+ collectionId: l
218
+ };
219
+ return e.data.set(f, u), new c(null, d, e, f, l);
220
+ } else if (Array.isArray(u) && u.length > 0 && "labels" in u[0]) {
221
+ let l = new StaticReference(u[0]);
222
+ return e.data.set(f, l.toJSON()), new c(l, void 0, void 0, void 0, p);
223
+ } else if (typeof u == "object" && u && "labels" in u) {
224
+ let l = new StaticReference(u);
225
+ return e.data.set(f, l.toJSON()), new c(l, void 0, void 0, void 0, p);
226
+ } else throw Error("Could not set reference from this data: " + JSON.stringify(u));
227
+ }
228
+ return !e || !u ? null : new c(u, void 0, void 0, void 0, p);
229
+ }
230
+ }, ReferenceListViewModel = class e extends Array {
231
+ _ = void 0;
232
+ __parentPseudo;
233
+ describeField = () => this.__parentPseudo ? this.__parentPseudo.describeField() : null;
234
+ describeFieldGroup = () => this.__parentPseudo ? this.__parentPseudo.describeFieldGroup() : null;
235
+ _value = null;
236
+ get [Symbol.toStringTag]() {
237
+ return "ReferenceList";
238
+ }
239
+ toString() {
240
+ return this.map((e) => e?.toString() ?? "").filter((e) => e).join(", ");
241
+ }
242
+ includes(e) {
243
+ return this.some((c) => c == e);
244
+ }
245
+ toJSON() {
246
+ let e = [];
247
+ for (let c = 0; c < this.length; c++) {
248
+ let l = this[c];
249
+ if (l && l._ref) {
250
+ let c = l._ref;
251
+ typeof c.toJSON == "function" ? e.push(c.toJSON()) : e.push(c);
252
+ } else l && e.push(null);
253
+ }
254
+ return e;
255
+ }
256
+ async forJson() {
257
+ let e = await this._value;
258
+ return e ? (await Promise.all(e.map((e) => e ? e.forJson() : null))).filter((e) => e !== null) : null;
259
+ }
260
+ static async __create(c, l, u, d = null) {
261
+ let f = l.nodeid;
262
+ if (c.data.has(f) || c.data.set(f, null), u === null) {
263
+ let c = new e();
264
+ return c._value = Promise.resolve([]), c;
265
+ }
266
+ if (!Array.isArray(u)) throw Error(`Cannot set an (entire) reference list value on node ${f} except via an array: ${JSON.stringify(u)}`);
267
+ let p = u.map((e, u) => e instanceof ReferenceValueViewModel ? Promise.resolve(e) : ReferenceValueViewModel.__create(c, l, e, {})), m = (await Promise.all(p)).filter((e) => e !== null), h = await Promise.all(m.map(async (e) => {
268
+ let c = await e.getValue();
269
+ return c && typeof c.toJSON == "function" ? c.toJSON() : c;
270
+ }));
271
+ c.data.set(f, h);
272
+ let g = new e();
273
+ return g.push(...m), g._value = Promise.resolve(m), g;
274
+ }
275
+ async __asTileData() {
276
+ if (!this._value) return null;
277
+ let e = await this._value;
278
+ return Promise.all(e.map(async (e) => e ? e.__asTileData ? await e.__asTileData() : e : null));
279
+ }
280
+ }, ReferenceMergedDataType = class {
281
+ static async __create(e, l, u, d) {
282
+ let f = nodeConfig.nodeConfigManager.retrieve(l);
283
+ return f && f.multiValue ? ReferenceListViewModel.__create(e, l, u, d) : ReferenceValueViewModel.__create(e, l, u, d);
284
+ }
285
+ };
286
+ viewModels.CUSTOM_DATATYPES.set("reference", ReferenceMergedDataType), registerResolvableDatatype("reference");
package/dist/main.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@alizarin/clm",
3
+ "version": "0.2.1-alpha.83",
4
+ "description": "Alizarin display serializers for CLM reference datatypes",
5
+ "license": "AGPL-3.0",
6
+ "author": "Phil Weir <phil.weir@flaxandteal.co.uk>",
7
+ "type": "module",
8
+ "main": "./dist/clm.js",
9
+ "types": "./dist/main.d.ts",
10
+ "files": [
11
+ "dist/clm.js",
12
+ "dist/main.d.ts"
13
+ ],
14
+ "scripts": {
15
+ "dev": "vite",
16
+ "build": "vite build && tsc -p tsconfig.build.json",
17
+ "preview": "vite preview",
18
+ "test": "cd ../../../.. && npm test -- ext/js/@alizarin/clm/tests/clm.test.ts"
19
+ },
20
+ "peerDependencies": {
21
+ "alizarin": "0.2.1-alpha.83"
22
+ },
23
+ "devDependencies": {
24
+ "alizarin": "file:../../../..",
25
+ "typescript": "~5.9.3",
26
+ "vite": "npm:rolldown-vite@7.2.5"
27
+ },
28
+ "overrides": {
29
+ "vite": "npm:rolldown-vite@7.2.5"
30
+ }
31
+ }