terminusdb 12.0.2

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 (86) hide show
  1. package/Contributing.md +36 -0
  2. package/LICENSE +201 -0
  3. package/README.md +175 -0
  4. package/RELEASE_NOTES.md +462 -0
  5. package/dist/index.html +22 -0
  6. package/dist/terminusdb-client.min.js +3 -0
  7. package/dist/terminusdb-client.min.js.LICENSE.txt +188 -0
  8. package/dist/terminusdb-client.min.js.map +1 -0
  9. package/dist/typescript/index.d.ts +14 -0
  10. package/dist/typescript/lib/accessControl.d.ts +554 -0
  11. package/dist/typescript/lib/axiosInstance.d.ts +2 -0
  12. package/dist/typescript/lib/connectionConfig.d.ts +381 -0
  13. package/dist/typescript/lib/const.d.ts +54 -0
  14. package/dist/typescript/lib/dispatchRequest.d.ts +17 -0
  15. package/dist/typescript/lib/errorMessage.d.ts +25 -0
  16. package/dist/typescript/lib/query/woqlBuilder.d.ts +75 -0
  17. package/dist/typescript/lib/query/woqlCore.d.ts +341 -0
  18. package/dist/typescript/lib/query/woqlDoc.d.ts +63 -0
  19. package/dist/typescript/lib/query/woqlLibrary.d.ts +718 -0
  20. package/dist/typescript/lib/query/woqlPrinter.d.ts +71 -0
  21. package/dist/typescript/lib/query/woqlQuery.d.ts +833 -0
  22. package/dist/typescript/lib/typedef.d.ts +624 -0
  23. package/dist/typescript/lib/utils.d.ts +199 -0
  24. package/dist/typescript/lib/valueHash.d.ts +146 -0
  25. package/dist/typescript/lib/viewer/chartConfig.d.ts +62 -0
  26. package/dist/typescript/lib/viewer/chooserConfig.d.ts +38 -0
  27. package/dist/typescript/lib/viewer/documentFrame.d.ts +44 -0
  28. package/dist/typescript/lib/viewer/frameConfig.d.ts +74 -0
  29. package/dist/typescript/lib/viewer/frameRule.d.ts +145 -0
  30. package/dist/typescript/lib/viewer/graphConfig.d.ts +73 -0
  31. package/dist/typescript/lib/viewer/objectFrame.d.ts +212 -0
  32. package/dist/typescript/lib/viewer/streamConfig.d.ts +23 -0
  33. package/dist/typescript/lib/viewer/tableConfig.d.ts +66 -0
  34. package/dist/typescript/lib/viewer/terminusRule.d.ts +75 -0
  35. package/dist/typescript/lib/viewer/viewConfig.d.ts +47 -0
  36. package/dist/typescript/lib/viewer/woqlChart.d.ts +1 -0
  37. package/dist/typescript/lib/viewer/woqlChooser.d.ts +56 -0
  38. package/dist/typescript/lib/viewer/woqlGraph.d.ts +26 -0
  39. package/dist/typescript/lib/viewer/woqlPaging.d.ts +1 -0
  40. package/dist/typescript/lib/viewer/woqlResult.d.ts +128 -0
  41. package/dist/typescript/lib/viewer/woqlRule.d.ts +96 -0
  42. package/dist/typescript/lib/viewer/woqlStream.d.ts +31 -0
  43. package/dist/typescript/lib/viewer/woqlTable.d.ts +102 -0
  44. package/dist/typescript/lib/viewer/woqlView.d.ts +49 -0
  45. package/dist/typescript/lib/woql.d.ts +1267 -0
  46. package/dist/typescript/lib/woqlClient.d.ts +1216 -0
  47. package/index.js +28 -0
  48. package/lib/.eslintrc +1 -0
  49. package/lib/accessControl.js +988 -0
  50. package/lib/axiosInstance.js +5 -0
  51. package/lib/connectionConfig.js +765 -0
  52. package/lib/const.js +59 -0
  53. package/lib/dispatchRequest.js +236 -0
  54. package/lib/errorMessage.js +110 -0
  55. package/lib/query/woqlBuilder.js +234 -0
  56. package/lib/query/woqlCore.js +934 -0
  57. package/lib/query/woqlDoc.js +177 -0
  58. package/lib/query/woqlLibrary.js +1015 -0
  59. package/lib/query/woqlPrinter.js +476 -0
  60. package/lib/query/woqlQuery.js +1865 -0
  61. package/lib/typedef.js +248 -0
  62. package/lib/utils.js +817 -0
  63. package/lib/valueHash.js_old +581 -0
  64. package/lib/viewer/chartConfig.js +411 -0
  65. package/lib/viewer/chooserConfig.js +234 -0
  66. package/lib/viewer/documentFrame.js +206 -0
  67. package/lib/viewer/frameConfig.js +469 -0
  68. package/lib/viewer/frameRule.js +519 -0
  69. package/lib/viewer/graphConfig.js +345 -0
  70. package/lib/viewer/objectFrame.js +1550 -0
  71. package/lib/viewer/streamConfig.js +82 -0
  72. package/lib/viewer/tableConfig.js +310 -0
  73. package/lib/viewer/terminusRule.js +196 -0
  74. package/lib/viewer/viewConfig.js +219 -0
  75. package/lib/viewer/woqlChart.js +17 -0
  76. package/lib/viewer/woqlChooser.js +171 -0
  77. package/lib/viewer/woqlGraph.js +295 -0
  78. package/lib/viewer/woqlPaging.js +148 -0
  79. package/lib/viewer/woqlResult.js +258 -0
  80. package/lib/viewer/woqlRule.js +312 -0
  81. package/lib/viewer/woqlStream.js +27 -0
  82. package/lib/viewer/woqlTable.js +332 -0
  83. package/lib/viewer/woqlView.js +107 -0
  84. package/lib/woql.js +1693 -0
  85. package/lib/woqlClient.js +2091 -0
  86. package/package.json +110 -0
@@ -0,0 +1,1550 @@
1
+ /* eslint-disable consistent-return */
2
+ /* eslint-disable prefer-destructuring */
3
+ /* eslint-disable guard-for-in */
4
+ /* eslint-disable max-len */
5
+ /* eslint-disable no-redeclare */
6
+ /* eslint-disable block-scoped-var */
7
+ /* eslint-disable no-var */
8
+ /* eslint-disable vars-on-top */
9
+ /* eslint-disable no-plusplus */
10
+ /* eslint-disable no-unused-vars */
11
+ /* eslint-disable no-shadow */
12
+ /* eslint-disable no-param-reassign */
13
+ /* eslint-disable no-continue */
14
+ /* eslint-disable no-console */
15
+ /* eslint-disable no-restricted-syntax */
16
+ /* eslint-disable no-use-before-define */
17
+ const FrameHelper = require('../utils');
18
+ const { FrameRule } = require('./frameRule');
19
+ const WOQL = require('../woql');
20
+
21
+ /**
22
+ * @file Javascript Terminus Document Classes
23
+ * @license Apache Version 2
24
+ * Helper classes for accessing documents returned by the Terminus DB API programmatically
25
+ *
26
+ * @example
27
+ * let doc = new TerminusDocument(client);
28
+ *
29
+ * //These set the objects document property and return promises:
30
+ *
31
+ * doc.loadDocument(URL).then(() => console.log(this.document));
32
+ * doc.loadComplete(URL, CLS).then(() => console.log(this.document))
33
+ * doc.loadSchema(cls).then(() => console.log(this.document))
34
+ *
35
+ * //These just set the object's document property
36
+ * doc.loadJSON(json_frames, cls) //console.log(this.document)
37
+ * doc.loadDataFrames(json_frames, cls)
38
+ * doc.loadClassFrames(json_frames, cls)
39
+ * @description Represents a frame for programmatic access to object frame,
40
+ * anywhere within a document
41
+ * Recursive data structure where this.children contains an indexed array of object frames
42
+ * and this.dataframes contains a property indexed array of data frames
43
+ * Every object frame carries a reference to its classframe
44
+ * This gives us instructions as to how to create new frames according to the schema
45
+ * After that it's turtles all the way down.
46
+ * @param cls - ID of the class (URL)
47
+ * @param classframe - an array of frames representing a class
48
+ * @param archetypes list of class frames
49
+ * @param parent parent object
50
+ * @returns {ObjectFrame}
51
+ */
52
+
53
+ function ObjectFrame(cls, jsonld, classframes, parent) {
54
+ // the class of the frame - mandatory
55
+ this.empty();
56
+ this.cls = FrameHelper.unshorten(cls);
57
+
58
+ // array of frames representing the full class frame
59
+ // (optional - required to be able to safely add missing properties)
60
+ if (classframes && typeof classframes === 'object') {
61
+ this.loadClassFrames(classframes);
62
+ }
63
+ // console.log("this", this)
64
+ if (jsonld && typeof jsonld === 'object' && Object.keys(jsonld).length) {
65
+ this.originalDocument = jsonld;
66
+ this.loadJSONLDDocument(jsonld);
67
+ // console.log("this after jsonld doc call", this)
68
+ } else {
69
+ this.originalDocument = false;
70
+ }
71
+ // parent object frame
72
+ this.parent = parent;
73
+ // set to true if this is a newly created document
74
+ this.newDoc = false;
75
+ }
76
+
77
+ /**
78
+ * Loads class frames for the object's class - the instructions about how to put an object together
79
+ */
80
+ ObjectFrame.prototype.loadClassFrames = function (classframes) {
81
+ for (let j = 0; j < classframes.length; j += 1) {
82
+ if (classframes[j]['@context']) this.jsonld_context = classframes[j]['@context'];
83
+
84
+ const cf = new ClassFrame(classframes[j], this);
85
+ if (cf.isValid()) {
86
+ if (!this.classframes) this.classframes = {};
87
+ this.classframes[classframes[j].property] = cf;
88
+ if (cf.isObject() && this.properties[classframes[j].property]) {
89
+ for (let i = 0; i < this.properties[classframes[j].property].values.length; i += 1) {
90
+ this.properties[classframes[j].property].values[i].loadClassFrames(classframes[j].frame);
91
+ }
92
+ }
93
+ } else {
94
+ // eslint-disable-next-line no-console
95
+ console.log('Invalid classframe', cf);
96
+ }
97
+ }
98
+ return this;
99
+ };
100
+
101
+ /**
102
+ * Does this object have a schema loaded?
103
+ */
104
+ ObjectFrame.prototype.hasSchema = function () {
105
+ return !FrameHelper.empty(this.classframes);
106
+ };
107
+
108
+ /*
109
+ * Loads an array of data frames into the object's internal index
110
+ * {property: [frames]}
111
+ */
112
+ /* ObjectFrame.prototype.loadDataFrames = function (frames) {
113
+ if (typeof frames !== 'object' || !frames.length) return undefined;
114
+ if (!this.originalFrames) this.originalFrames = frames;
115
+ if (!this.subjid && frames[0].domainValue) this.subjid = frames[0].domainValue;
116
+ for (let i = 0; i < frames.length; i += 1) {
117
+ if (frames[i]['@context']) this.jsonld_context = frames[i]['@context'];
118
+ let cframe = this.getPropertyClassFrame(frames[i].property, frames[i]);
119
+ if (cframe && cframe.isClassChoice()) {
120
+ cframe = cframe.getChosenClassFrame(frames[i].range);
121
+ if (!cframe) {
122
+ console.log(`no choice frame ${frames[i].range}`);
123
+ }
124
+ } else if (cframe && cframe.isLogic()) {
125
+ cframe = cframe.getChosenFrame(frames[i]);
126
+ }
127
+ if (cframe) {
128
+ if (typeof this.properties[frames[i].property] === 'undefined') {
129
+ this.properties[frames[i].property] = new PropertyFrame(frames[i].property, cframe, this);
130
+ }
131
+ this.properties[frames[i].property].addFrame(frames[i]);
132
+ }
133
+ }
134
+ return this;
135
+ }; */
136
+
137
+ ObjectFrame.prototype.loadJSONLDDocument = function (rdoc) { // KItty check herer
138
+ if (typeof rdoc !== 'object') return undefined;
139
+ const doc = FrameHelper.json_unshorten(rdoc);
140
+ if (!this.originalDocument) this.originalDocument = doc;
141
+ if (!this.subjid && doc['@id']) {
142
+ this.subjid = FrameHelper.unshorten(doc['@id']);
143
+ }
144
+ if (doc['@context']) this.jsonld_context = doc['@context'];
145
+ for (const prop in doc) {
146
+ if (prop[0] === '@' || (typeof doc[prop] === 'object' && Object.keys(doc[prop]).length === 0)) continue;
147
+ let cframe = this.getPropertyClassFrame(prop, doc);
148
+ if (cframe && cframe.isClassChoice()) {
149
+ // cframe = cframe.getChosenClassFrame(FrameHelper.unshorten(doc[prop]["@type"]),cframe.parent)
150
+ if (!cframe) {
151
+ console.log(`no choice frame ${doc[prop]['@type']}`);
152
+ }
153
+ } else if (cframe && cframe.isLogic()) {
154
+ cframe = cframe.getChosenFrame(doc[prop]);
155
+ }
156
+ if (cframe) {
157
+ if (typeof this.properties[prop] === 'undefined') {
158
+ this.properties[prop] = new PropertyFrame(prop, cframe, this);
159
+ }
160
+ this.properties[prop].addJSONLDDocument(doc[prop]);
161
+ }
162
+ }
163
+ return this;
164
+ };
165
+
166
+ /*
167
+ * Serialises the javascript object as an array of frames
168
+ */
169
+ ObjectFrame.prototype.getAsFrame = function (prop, parent) {
170
+ prop = FrameHelper.unshorten(prop);
171
+ if (this.parentframe) return this.parentframe;
172
+ const ff = { type: 'objectProperty', property: prop };
173
+ ff.range = this.cls;
174
+ ff.domain = parent.cls;
175
+ ff.domainValue = parent.subjid;
176
+ ff.frame = [];
177
+ for (const prop of Object.keys(this.properties)) {
178
+ ff.frame = ff.frame.concat(ff.frame, this.properties[prop].getAsFrames());
179
+ }
180
+ return ff;
181
+ };
182
+
183
+ ObjectFrame.prototype.getAsFrames = function (prop, parent) {
184
+ let frames = [];
185
+ for (const prop of Object.keys(this.properties)) {
186
+ frames = frames.concat(frames, this.properties[prop].getAsFrames());
187
+ }
188
+ return frames;
189
+ };
190
+
191
+ /**
192
+ * Cleans out object and sets everything empty
193
+ */
194
+ ObjectFrame.prototype.empty = function () {
195
+ // all indexed by property
196
+ this.properties = {};
197
+ this.restrictions = {};
198
+ this.subjid = false;
199
+ this.cls = false;
200
+ };
201
+
202
+ /*
203
+ * Resets object state to original state
204
+ */
205
+ ObjectFrame.prototype.reset = function (prop) {
206
+ if (prop) {
207
+ prop = FrameHelper.unshorten(prop);
208
+ const props = [];
209
+ for (let i = 0; i < this.originalFrames.length; i += 1) {
210
+ if (this.originalFrames[i].property === prop) {
211
+ props.push(this.originalFrames[i]);
212
+ }
213
+ }
214
+ if (this.properties[prop]) this.properties[prop] = [];
215
+ this.loadDataFrames(props);
216
+ } else {
217
+ this.restrictions = {};
218
+ this.properties = {};
219
+ this.loadDataFrames(this.originalFrames);
220
+ }
221
+ };
222
+
223
+ /*
224
+ * Clears out any specific information from a tree
225
+ */
226
+ ObjectFrame.prototype.clear = function () {
227
+ for (const prop of Object.keys(this.properties)) {
228
+ this.properties[prop].clear();
229
+ }
230
+ return this;
231
+ };
232
+
233
+ /*
234
+ * Filters the frame and its children
235
+ */
236
+ ObjectFrame.prototype.mfilter = function (rules, onmatch) {
237
+ const hits = new FrameRule().testRules(rules, this, onmatch);
238
+ for (const prop of Object.keys(this.properties)) {
239
+ if (!this.properties[prop].mfilter) {
240
+ console.log(prop, this.properties[prop]);
241
+ } else {
242
+ this.properties[prop].mfilter(rules, onmatch);
243
+ }
244
+ }
245
+ return this;
246
+ };
247
+
248
+ /**
249
+ * If a class frame is present, it is returned for the given property
250
+ * If no class frame is present and an instance frame is passed in the
251
+ * this enables make it up as you go along editing
252
+ * second argument a class frame will be created from the instance frame.
253
+ */
254
+ ObjectFrame.prototype.getPropertyClassFrame = function (prop, jsonlddoc) {
255
+ if (typeof prop === 'object') {
256
+ return new ClassFrame(prop);
257
+ }
258
+
259
+ prop = FrameHelper.unshorten(prop);
260
+
261
+ if (this.classframes && typeof this.classframes === 'object' && typeof this.classframes[prop] === 'object') {
262
+ // console.log("returning " + prop, this.classframes[prop])
263
+ return this.classframes[prop];
264
+ }
265
+ if (jsonlddoc) {
266
+ const cf = new ClassFrame();
267
+ cf.loadFromJSONLD(jsonlddoc, prop);
268
+ return cf;
269
+ }
270
+ if (this.properties[prop]) {
271
+ return new ClassFrame(this.properties[prop].values[0]);
272
+ }
273
+ return false;
274
+ };
275
+
276
+ /*
277
+ * Returns a list of properties - type can be filled|missing|all
278
+ */
279
+ ObjectFrame.prototype.getProperties = function (type) {
280
+ if (type === 'filled' || !this.classframes) {
281
+ return Object.keys(this.properties);
282
+ }
283
+ if (type === 'missing') {
284
+ const filled = Object.keys(this.properties).map((item) => FrameHelper.unshorten(item));
285
+ const all = Object.keys(this.classframes).map((item) => FrameHelper.unshorten(item));
286
+ const missing = [];
287
+ for (let i = 0; i < all.length; i++) {
288
+ if (filled.indexOf(all[i]) === -1 && missing.indexOf(all[i]) === -1) {
289
+ missing.push(all[i]);
290
+ }
291
+ }
292
+ return missing;
293
+ }
294
+ return Object.keys(this.classframes);
295
+ };
296
+
297
+ /**
298
+ * Missing properties are those that are present in the classframe,
299
+ * but not instantiated in the current object frame
300
+ * for this to work we need to load the frame with the associated classframe
301
+ */
302
+ ObjectFrame.prototype.getMissingPropertyList = function () {
303
+ const missing = this.getProperties('missing');
304
+ const nmissing = [];
305
+ for (let i = 0; i < missing.length; i++) {
306
+ const cframe = this.getPropertyClassFrame(missing[i]);
307
+ if (cframe) {
308
+ var newb = { label: cframe.getLabel(), value: missing[i] };
309
+ } else {
310
+ var newb = { label: missing[i], value: missing[i] };
311
+ }
312
+ nmissing.push(newb);
313
+ }
314
+ return nmissing;
315
+ };
316
+
317
+ /**
318
+ * Returns a list of all the classes that can show up in the frame
319
+ */
320
+ ObjectFrame.prototype.getPossibleContainedClasses = function () {
321
+ const cls = [];
322
+ function efcf(frames) {
323
+ if (frames.type && frames.type === 'class_choice') {
324
+ efcf(frames.operands);
325
+ }
326
+ if (!Array.isArray(frames)) frames = [frames];
327
+ for (let i = 0; i < frames.length; i++) {
328
+ if (frames[i].domain && cls.indexOf(frames[i].domain) === -1) cls.push(frames[i].domain);
329
+ if (frames[i].frame) {
330
+ efcf(frames[i].frame);
331
+ }
332
+ }
333
+ }
334
+ efcf(Object.values(this.classframes));
335
+ return cls;
336
+ };
337
+
338
+ ObjectFrame.prototype.getDocumentLinks = function () {
339
+ const vals = [];
340
+ const props = this.getProperties('filled');
341
+ for (let i = 0; i < props.length; i++) {
342
+ const mprop = this.properties[props[i]];
343
+ for (let k = 0; k < mprop.values.length; k++) {
344
+ const dval = mprop.values[k];
345
+ if (dval.isObject() && dval.getDocumentLinks) {
346
+ const nvals = dval.getDocumentLinks();
347
+ for (let l = 0; l < nvals.length; l++) {
348
+ if (vals.indexOf(nvals[l]) === -1) {
349
+ vals.push(nvals[l]);
350
+ }
351
+ }
352
+ } else if (dval.isDocument()) {
353
+ const nv = dval.get();
354
+ if (vals.indexOf(nv) === -1) {
355
+ vals.push(nv);
356
+ }
357
+ }
358
+ }
359
+ }
360
+ return vals;
361
+ };
362
+
363
+ /**
364
+ * List of properties that are filled in the object
365
+ */
366
+ ObjectFrame.prototype.getFilledPropertyList = function () {
367
+ const props = this.getProperties('filled');
368
+ const filled = [];
369
+ for (let i = 0; i < props.length; i++) {
370
+ const cframe = this.getPropertyClassFrame(props[i]);
371
+ if (cframe) {
372
+ var newb = { label: cframe.getLabel(), value: props[i] };
373
+ } else {
374
+ var newb = { label: props[i], value: props[i] };
375
+ }
376
+ filled.push(newb);
377
+ }
378
+ return filled;
379
+ };
380
+
381
+ /*
382
+ * Fills the object frame from the schema - adding all the necessary property
383
+ * frames, etc to make it complete
384
+ */
385
+ ObjectFrame.prototype.fillFromSchema = function (newid) {
386
+ if (newid) this.subjid = newid;
387
+ newid = (newid || FrameHelper.genBNID(`${FrameHelper.urlFragment(this.cls)}_`));
388
+ const properties = {};
389
+ if (this.classframes) {
390
+ for (const prop of Object.keys(this.classframes)) {
391
+ const pf = this.getPropertyClassFrame(prop);
392
+ properties[prop] = new PropertyFrame(prop, pf, this);
393
+ properties[prop].fillFromSchema(newid);
394
+ }
395
+ }
396
+ this.properties = properties;
397
+ this.originalFrames = [];
398
+ for (const prop of Object.keys(this.properties)) {
399
+ this.originalFrames.push(this.properties[prop].getAsFrames());
400
+ }
401
+ return this;
402
+ };
403
+
404
+ /*
405
+ * Clones an object frame to be a copy of the current frame
406
+ */
407
+ ObjectFrame.prototype.clone = function (newid) {
408
+ const properties = {};
409
+ const cloned = new ObjectFrame(this.cls, false, false, this.parent);
410
+ cloned.classframes = this.classframes;
411
+ cloned.subjid = newid;
412
+ for (const prop of Object.keys(this.properties)) {
413
+ properties[prop] = this.properties[prop].clone();
414
+ }
415
+ cloned.properties = properties;
416
+ return cloned;
417
+ };
418
+
419
+ /**
420
+ * Returns a child frame with a particular id
421
+ * If the second parameter is included, it will only look in that specific property
422
+ * Otherwise searches all properties for the child
423
+ */
424
+ ObjectFrame.prototype.getChild = function (childid, prop) {
425
+ let pframe = this.getProperty(prop);
426
+ for (let i = 0; i < pframe.values.length; pframe++) {
427
+ if (pframe.values[i].isObject() && pframe.values[i].subject === childid) return pframe.values[i];
428
+ }
429
+ if (!prop) {
430
+ for (const key of Object.keys(this.properties)) {
431
+ for (let i = 0; i < this.properties[key].values.length; i += 1) {
432
+ if (this.properties[key].values[i].subject() === childid) return this.properties[key].values[i];
433
+ }
434
+ }
435
+ }
436
+ return false;
437
+ };
438
+
439
+ ObjectFrame.prototype.addProperty = function (prop, cls) {
440
+ if (typeof prop !== 'object') prop = FrameHelper.unshorten(prop);
441
+ const cframe = this.getPropertyClassFrame(prop);
442
+ let ndata = false;
443
+ if (cframe) {
444
+ const nprop = new PropertyFrame(prop, cframe, this);
445
+ if (cframe.isObject()) {
446
+ if (!cframe.isClassChoice()) {
447
+ ndata = cframe.createEmpty(FrameHelper.genBNID(`${FrameHelper.urlFragment(cframe.range)}_`));
448
+ }
449
+ if (cls) {
450
+ ndata = cframe.createEmptyChoice(cls, FrameHelper.genBNID(`${FrameHelper.urlFragment(cls)}_`));
451
+ }
452
+ const clss = cframe.getClassChoices();
453
+ if (clss && clss.length) {
454
+ ndata = cframe.createEmptyChoice(clss[0], FrameHelper.genBNID(`${FrameHelper.urlFragment(clss[0])}_`));
455
+ }
456
+ } else {
457
+ ndata = cframe.createEmpty();
458
+ }
459
+ if (ndata) {
460
+ nprop.addValueFrame(ndata);
461
+ }
462
+ if (typeof this.properties[prop] === 'undefined') {
463
+ if (typeof prop === 'object') var p = prop.property;
464
+ else var p = prop;
465
+ this.properties[p] = nprop;
466
+ // this.properties[prop] = nprop;
467
+ }
468
+ // else {
469
+ // this.properties[prop].push(nprop);
470
+ // }
471
+ nprop.status = 'new';
472
+ return nprop;
473
+ }
474
+ return false;
475
+ };
476
+
477
+ ObjectFrame.prototype.addPropertyValue = function (prop, value) {
478
+ prop = FrameHelper.unshorten(prop);
479
+ if (this.properties[prop]) return this.properties[prop].addValue(value);
480
+ return null;
481
+ };
482
+
483
+ ObjectFrame.prototype.removeProperty = function (prop) {
484
+ prop = FrameHelper.unshorten(prop);
485
+ if (typeof this.properties[prop] !== 'undefined') {
486
+ delete (this.properties[prop]);
487
+ }
488
+ };
489
+
490
+ ObjectFrame.prototype.removePropertyValue = function (prop, value, index) {
491
+ prop = FrameHelper.unshorten(prop);
492
+ const pframe = this.properties[prop];
493
+ pframe.removeValue(value, index);
494
+ if (pframe.values.length === 0) {
495
+ this.removeProperty(prop);
496
+ }
497
+ };
498
+
499
+ ObjectFrame.prototype.error = function (msg) {
500
+ if (!this.errors) this.errors = [];
501
+ this.errors.push({ type: 'Internal Object Frame Error', msg });
502
+ };
503
+
504
+ ObjectFrame.prototype.extract = function () {
505
+ const extracts = {};
506
+ for (const prop in this.properties) {
507
+ const extracted = this.properties[prop].extract();
508
+ if (!FrameHelper.empty(extracted)) {
509
+ if (typeof extracts[prop] === 'undefined') extracts[prop] = [];
510
+ extracts[prop] = extracts[prop].concat(extracted);
511
+ }
512
+ if (extracts[prop] && extracts[prop].length === 1) extracts[prop] = extracts[prop][0];
513
+ }
514
+ if (FrameHelper.empty(extracts) && this.parent) {
515
+ return false;
516
+ }
517
+
518
+ const ext = this.extractJSONLD(extracts);
519
+ return ext;
520
+ };
521
+
522
+ ObjectFrame.prototype.extractJSONLD = function (extracts) {
523
+ extracts['@type'] = this.cls;
524
+ if (this.subject() !== '_:') extracts['@id'] = this.subject();
525
+ if (this.jsonld_context) extracts['@context'] = this.jsonld_context;
526
+ return extracts;
527
+ };
528
+
529
+ ObjectFrame.prototype.subject = function () {
530
+ return this.subjid || '';
531
+ };
532
+
533
+ ObjectFrame.prototype.get = ObjectFrame.prototype.subject;
534
+ ObjectFrame.prototype.set = function (val) {
535
+ this.subjid = val;
536
+ };
537
+
538
+ ObjectFrame.prototype.isObject = function () { return true; };
539
+ ObjectFrame.prototype.isProperty = function () { return false; };
540
+ ObjectFrame.prototype.isData = function () { return false; };
541
+ ObjectFrame.prototype.isClassChoice = function () {
542
+ return (this.frame && this.frame.type === 'class_choice');
543
+ };
544
+
545
+ ObjectFrame.prototype.subjectClass = function () {
546
+ return this.cls;
547
+ };
548
+
549
+ ObjectFrame.prototype.depth = function () {
550
+ if (this.parent) return (this.parent.depth() + 1);
551
+ return 0;
552
+ };
553
+
554
+ ObjectFrame.prototype.getProperty = function (prop) {
555
+ return this.properties[prop];
556
+ };
557
+
558
+ ObjectFrame.prototype.first = function (prop) {
559
+ if (this.properties && this.properties[prop]) {
560
+ return this.properties[prop].first();
561
+ }
562
+ };
563
+
564
+ ObjectFrame.prototype.property = function (prop) {
565
+ if (this.parent) return this.parent.property();
566
+ return false;
567
+ };
568
+
569
+ ObjectFrame.prototype.parentObject = function () {
570
+ if (this.parent && this.parent.parent) {
571
+ return this.parent.parent;
572
+ }
573
+ return false;
574
+ };
575
+
576
+ ObjectFrame.prototype.root = function () {
577
+ if (this.parent) return false;
578
+ return true;
579
+ };
580
+
581
+ ObjectFrame.prototype.renderProperties = function () {
582
+ const props = this.sortProperties();
583
+ const nprops = [];
584
+ for (let i = 0; i < props.length; i++) {
585
+ if (this.properties[props[i]].render) {
586
+ const rend = this.properties[props[i]].render(this.properties[props[i]]);
587
+ if (rend) nprops.push(rend);
588
+ }
589
+ }
590
+ return nprops;
591
+ };
592
+
593
+ ObjectFrame.prototype.sortProperties = function () {
594
+ const unsorted = Object.keys(this.properties);
595
+ if (this.compare) {
596
+ return unsorted.sort((a, b) => this.compare(a, b, this));
597
+ }
598
+ return unsorted.sort((a, b) => this.standardCompare(a, b, this));
599
+ };
600
+
601
+ /*
602
+ * Label first, then datatype properties, then object properties
603
+ */
604
+ ObjectFrame.prototype.standardCompare = function (a, b, doc) {
605
+ if (FrameHelper.shorten(a) === 'rdfs:label') return -1;
606
+ if (FrameHelper.shorten(b) === 'rdfs:label') return 1;
607
+ if (FrameHelper.shorten(a) === 'rdfs:comment') return -1;
608
+ if (FrameHelper.shorten(b) === 'rdfs:comment') return 1;
609
+ if (doc.properties[a].isData() && doc.properties[b].isObject()) return -1;
610
+ if (doc.properties[b].isData() && doc.properties[a].isObject()) return 1;
611
+ return 0;
612
+ };
613
+
614
+ ObjectFrame.prototype.cardControlAllows = function (action) {
615
+ if (!this.parent) return true;
616
+ if (this.parent.cframe.hasRestriction()) {
617
+ const rest = this.parent.cframe.restriction;
618
+ const currentnum = this.parent.values.length;
619
+ if (action === 'add' || action === 'clone') {
620
+ if (rest.max && currentnum >= rest.max) {
621
+ return false;
622
+ }
623
+ }
624
+ if (action === 'delete' && (rest.min && currentnum <= rest.min)) {
625
+ return false;
626
+ }
627
+ }
628
+ return true;
629
+ };
630
+
631
+ ObjectFrame.prototype.isUpdated = function () {
632
+ let i = 0;
633
+ for (const prop in this.properties) {
634
+ if (this.originalFrames[i] !== prop) return true;
635
+ if (this.properties[prop].isUpdated()) return true;
636
+ i++;
637
+ }
638
+ if (i !== this.originalFrames.length) return true;
639
+ return false;
640
+ };
641
+
642
+ ObjectFrame.prototype.isNew = function () {
643
+ return (this.subject().substring(0, 2) === '_:');
644
+ };
645
+
646
+ ObjectFrame.prototype.getSummary = function () {
647
+ const ret = { status: 'ok' };
648
+ if (this.isUpdated()) ret.status = 'updated';
649
+ if (this.isNew()) ret.status = 'new';
650
+ ret.propcount = 0;
651
+ for (const prop in this.properties) {
652
+ ret.propcount++;
653
+ }
654
+ ret.long = `${ret.propcount} properties`;
655
+ return ret;
656
+ };
657
+
658
+ ObjectFrame.prototype.saveQuery = function () {
659
+ const q = WOQL.update_object(this.extract());
660
+ this.pathToDoc(q);
661
+ return q;
662
+ };
663
+
664
+ ObjectFrame.prototype.pathToDoc = function (q) {
665
+ q.add_triple(this.subjid, 'type', this.cls);
666
+ if (this.parent) {
667
+ q.add_triple(this.parent.subject(), 'type', this.parent.subjectClass());
668
+ q.add_triple(this.parent.subject(), this.parent.predicate, this.subjid);
669
+ if (this.parent.parent) {
670
+ this.parent.parent.pathToDoc(q);
671
+ }
672
+ }
673
+ };
674
+
675
+ ObjectFrame.prototype.deleteQuery = function () {
676
+ const q = WOQL.delete_object(this.subjid);
677
+ if (this.parent) {
678
+ q.delete_triple(this.parent.subject(), this.parent.predicate, this.subjid);
679
+ }
680
+ return q;
681
+ };
682
+
683
+ function PropertyFrame(property, cframe, parent) {
684
+ this.predicate = property;
685
+ this.cframe = cframe;
686
+ this.parent = parent;
687
+ this.values = [];
688
+ }
689
+
690
+ PropertyFrame.prototype.addJSONLDDocument = function (jsonld) {
691
+ if (this.cframe.isData()) {
692
+ if (Array.isArray(jsonld)) {
693
+ for (var i = 0; i < jsonld.length; i++) {
694
+ var df = new DataFrame(jsonld[i], this, this.values.length);
695
+ this.values.push(df);
696
+ }
697
+ } else {
698
+ var df = new DataFrame(jsonld, this, this.values.length);
699
+ this.values.push(df);
700
+ }
701
+ } else if (Array.isArray(jsonld)) {
702
+ for (var i = 0; i < jsonld.length; i++) {
703
+ const kid = new ObjectFrame(FrameHelper.unshorten(jsonld[i]['@type']), jsonld[i], this.cframe.frame, this);
704
+ this.values.push(kid);
705
+ }
706
+ } else {
707
+ const kid = new ObjectFrame(jsonld['@type'], jsonld, this.cframe.frame, this);
708
+ this.values.push(kid);
709
+ }
710
+ };
711
+
712
+ PropertyFrame.prototype.addFrame = function (frame) {
713
+ if (this.cframe.isData()) {
714
+ const df = new DataFrame(frame, this, this.values.length);
715
+ this.values.push(df);
716
+ } else {
717
+ const kid = new ObjectFrame(this.range(), this.cframe.frame, frame.frame, this, frame);
718
+ this.values.push(kid);
719
+ }
720
+ };
721
+
722
+ PropertyFrame.prototype.addValueFrame = function (oframe) {
723
+ if (oframe) {
724
+ oframe.parent = this;
725
+ oframe.index = this.values.length;
726
+ this.values.push(oframe);
727
+ }
728
+ };
729
+
730
+ PropertyFrame.prototype.addValue = function (val) {
731
+ const nu = this.createEmpty();
732
+ if (val) nu.set(val);
733
+ this.addValueFrame(nu);
734
+ return nu;
735
+ };
736
+
737
+ PropertyFrame.prototype.fillFromSchema = function (newid) {
738
+ if (this.isData() || (this.isObject() && !this.isClassChoice())) {
739
+ const values = [];
740
+ if (this.cframe.hasRestriction() && this.cframe.restriction.min) {
741
+ for (let i = 0; i < this.cframe.restriction.min; i += 1) {
742
+ var nframe = this.createEmpty(newid);
743
+ nframe.parent = this;
744
+ values.push(nframe);
745
+ }
746
+ } else {
747
+ var nframe = this.createEmpty(newid);
748
+ nframe.parent = this;
749
+ values.push(nframe);
750
+ }
751
+ this.values = values;
752
+ } else if (this.isClassChoice()) {
753
+ const clss = this.cframe.getClassChoices();
754
+ if (clss && clss.length) {
755
+ const empty = this.cframe.createEmptyChoice(clss[0], FrameHelper.genBNID(`${FrameHelper.urlFragment(clss[0])}_`));
756
+ empty.parent = this;
757
+ this.values = [empty];
758
+ }
759
+ }
760
+ };
761
+
762
+ PropertyFrame.prototype.isData = function () {
763
+ return this.cframe.isData();
764
+ };
765
+
766
+ PropertyFrame.prototype.isObject = function () {
767
+ return this.cframe.isObject();
768
+ };
769
+
770
+ PropertyFrame.prototype.isProperty = function () {
771
+ return true;
772
+ };
773
+
774
+ PropertyFrame.prototype.property = function () {
775
+ return this.predicate;
776
+ };
777
+
778
+ PropertyFrame.prototype.extract = function () {
779
+ const extracts = [];
780
+ const hasVal = (val) => {
781
+ if (val['@value']) {
782
+ for (var i = 0; i < extracts.length; i++) {
783
+ if (extracts[i]['@value'] && extracts[i]['@value'] === val['@value']
784
+ && extracts[i]['@type'] && extracts[i]['@type'] === val['@type']) return true;
785
+ }
786
+ return false;
787
+ }
788
+ if (val['@id']) {
789
+ for (var i = 0; i < extracts.length; i++) {
790
+ if (extracts[i]['@id'] && extracts[i]['@id'] === val['@id']) return true;
791
+ }
792
+ return false;
793
+ }
794
+ };
795
+
796
+ for (let i = 0; i < this.values.length; i++) {
797
+ const val = this.values[i].extract();
798
+ if (val !== '' && val !== false && typeof val !== 'undefined' && !hasVal(val)) extracts.push(val);
799
+ }
800
+ return extracts;
801
+ };
802
+
803
+ PropertyFrame.prototype.subject = function () {
804
+ return (this.parent ? this.parent.subject() : false);
805
+ };
806
+ PropertyFrame.prototype.subjectClass = function () {
807
+ return (this.parent ? this.parent.subjectClass() : false);
808
+ };
809
+ PropertyFrame.prototype.depth = function () {
810
+ return (this.parent ? this.parent.depth() : false);
811
+ };
812
+ PropertyFrame.prototype.updated = function () {
813
+ return (this.parent ? this.parent.childUpdated() : false);
814
+ };
815
+ PropertyFrame.prototype.range = function () {
816
+ return (this.cframe ? this.cframe.range : '');
817
+ };
818
+ PropertyFrame.prototype.getLabel = function () {
819
+ return (
820
+ // this.cframe ? this.cframe.getLabel() : '');
821
+ this.cframe ? this.cframe.getLabel() : this.predicate.getLabel());
822
+ };
823
+ PropertyFrame.prototype.getComment = function () {
824
+ return (this.cframe ? this.cframe.getComment() : false);
825
+ };
826
+ PropertyFrame.prototype.hasCardinalityRestriction = function () {
827
+ return (this.cframe ? this.cframe.hasRestriction() : false);
828
+ };
829
+ PropertyFrame.prototype.getRestriction = function () {
830
+ return (this.cframe ? this.cframe.restriction : false);
831
+ };
832
+ PropertyFrame.prototype.isClassChoice = function () {
833
+ return (this.cframe ? this.cframe.isClassChoice() : false);
834
+ };
835
+
836
+ PropertyFrame.prototype.deletePropertyValue = function (value, index) {
837
+ this.parent.removePropertyValue(this.property(), value, index);
838
+ };
839
+
840
+ PropertyFrame.prototype.removeValue = function (value, index) {
841
+ const nvals = [];
842
+ for (let i = 0; i < this.values.length; i++) {
843
+ if (this.values[i].index !== value.index) {
844
+ nvals.push(this.values[i]);
845
+ }
846
+ }
847
+ this.values = nvals;
848
+ /* let nvals = [] //trial
849
+ for(var i = 0; i<this.values.length; i++){
850
+ if(this.values[i].get() != value){
851
+ nvals.push(this.values[i])
852
+ }
853
+ }
854
+ this.values = nvals */
855
+ };
856
+
857
+ PropertyFrame.prototype.get = function () {
858
+ const gets = [];
859
+ for (let i = 0; i < this.values.length; i++) {
860
+ if (this.values[i]) {
861
+ const x = this.values[i].get();
862
+ if (x) gets.push(x);
863
+ }
864
+ }
865
+ return gets;
866
+ };
867
+
868
+ PropertyFrame.prototype.set = function (val) {
869
+ for (let i = 0; i < this.values.length; i++) {
870
+ if (this.values[i]) {
871
+ this.values[i].set(val);
872
+ }
873
+ }
874
+ };
875
+
876
+ PropertyFrame.prototype.clear = function () {
877
+ for (let i = 0; i < this.values.length; i++) {
878
+ this.values[i].clear();
879
+ }
880
+ };
881
+
882
+ PropertyFrame.prototype.clone = function () {
883
+ const cvalues = [];
884
+ const cloned = new PropertyFrame(this.predicate, this.cframe, this.parent);
885
+ for (let i = 0; i < this.values.length; i++) {
886
+ cvalues.push(this.values[i].clone());
887
+ }
888
+ cloned.values = cvalues;
889
+ return cloned;
890
+ //
891
+ };
892
+
893
+ PropertyFrame.prototype.getAsFrames = function () {
894
+ let fs = [];
895
+ for (let i = 0; i < this.values.length; i++) {
896
+ if (this.values[i]) {
897
+ if (this.isData()) {
898
+ fs.push(this.values[i].getAsFrame());
899
+ } else {
900
+ fs = fs.concat(this.values[i].getAsFrames());
901
+ }
902
+ }
903
+ }
904
+ return fs;
905
+ };
906
+
907
+ PropertyFrame.prototype.createEmpty = function () {
908
+ if (this.cframe.isData()) {
909
+ const df = this.cframe.copy(this.subject());
910
+ df.set('');
911
+ df.status = 'new';
912
+ return df;
913
+ }
914
+ if (this.cframe.isObject()) {
915
+ if (!this.cframe.isClassChoice()) {
916
+ const df = this.cframe.createEmpty(FrameHelper.genBNID(`${FrameHelper.urlFragment(this.cframe.range)}_`));
917
+ df.status = 'new';
918
+ return df;
919
+ }
920
+
921
+ const df = new ClassFrame(this.cframe);
922
+ df.status = 'new';
923
+ return df;
924
+ }
925
+ };
926
+
927
+ PropertyFrame.prototype.mfilter = function (rules, onmatch) {
928
+ const hits = new FrameRule().testRules(rules, this, onmatch);
929
+ for (let i = 0; i < this.values.length; i++) {
930
+ this.values[i].mfilter(rules, onmatch);
931
+ }
932
+ return this;
933
+ };
934
+
935
+ /*
936
+ * Shorthand functions to make it easier to access the underlying data
937
+ */
938
+
939
+ PropertyFrame.prototype.first = function () {
940
+ if (this.values && this.values[0]) {
941
+ return this.values[0].get();
942
+ }
943
+ };
944
+
945
+ PropertyFrame.prototype.renderValues = function () {
946
+ const sortedVals = this.sortValues();
947
+ const vals = [];
948
+ for (let i = 0; i < sortedVals.length; i++) {
949
+ if (sortedVals[i] && sortedVals[i].render) {
950
+ const rend = sortedVals[i].render(sortedVals[i]);
951
+ if (rend) vals.push(rend);
952
+ }
953
+ }
954
+ return vals;
955
+ };
956
+
957
+ PropertyFrame.prototype.sortValues = function () {
958
+ if (this.compare) {
959
+ return this.values.sort((a, b) => this.compare(a, b, this));
960
+ }
961
+ return this.values;
962
+ };
963
+
964
+ PropertyFrame.prototype.cardControlAllows = function (action) {
965
+ if (this.cframe.hasRestriction()) {
966
+ const rest = this.cframe.restriction;
967
+ const currentnum = this.values.length;
968
+ if (action === 'add' || action === 'clone') {
969
+ if (rest.max && currentnum >= rest.max) {
970
+ return false;
971
+ }
972
+ }
973
+ if (action === 'delete' && (rest.min)) {
974
+ return false;
975
+ }
976
+ }
977
+ return true;
978
+ };
979
+
980
+ PropertyFrame.prototype.isUpdated = function () {
981
+ return true;
982
+ // eslint-disable-next-line no-unreachable
983
+ if (this.values.length !== this.originalValues.length) return true;
984
+ for (let i = 0; i < this.values.length; i++) {
985
+ if (this.cframe && this.cframe.isData()) {
986
+ if (this.values[i].value() !== this.originalValues[i]) {
987
+ return true;
988
+ }
989
+ } else {
990
+ if (this.values[i].subject() !== this.originalValues[i]) {
991
+ return true;
992
+ }
993
+ if (this.values[i].isUpdated()) {
994
+ return true;
995
+ }
996
+ }
997
+ }
998
+ return false;
999
+ };
1000
+
1001
+ PropertyFrame.prototype.saveQuery = function () {
1002
+ const q = WOQL.query();
1003
+ for (let i = 0; i < this.values.length; i++) {
1004
+ q.and(this.values[i].saveQuery());
1005
+ }
1006
+ this.parent.pathToDoc(q);
1007
+ return q;
1008
+ };
1009
+
1010
+ PropertyFrame.prototype.deleteQuery = function () {
1011
+ const q = WOQL.query();
1012
+ for (let i = 0; i < this.values.length; i++) {
1013
+ q.and(this.values[i].deleteQuery());
1014
+ }
1015
+ return q;
1016
+ };
1017
+
1018
+ function DataFrame(jsonld, parent, index) {
1019
+ this.err = false;
1020
+ this.index = index;
1021
+ if (parent) {
1022
+ this.loadParent(parent);
1023
+ }
1024
+ if (jsonld) {
1025
+ this.rangeValue = jsonld;
1026
+ if (jsonld['@type']) this.range = jsonld['@type'];
1027
+ if (jsonld['@language']) this.language = jsonld['@language'];
1028
+ if (!this.type) this.type = (jsonld['@value'] ? 'datatypeProperty' : 'objectProperty');
1029
+ }
1030
+ return this;
1031
+ }
1032
+
1033
+ DataFrame.prototype.loadParent = function (parent, index) {
1034
+ this.parent = parent;
1035
+ this.type = parent.type;
1036
+ // the id of the object that owns this dataframe
1037
+ this.domainValue = parent.subject();
1038
+ this.subjid = (parent.subject() || false);
1039
+ this.domain = parent.cframe ? parent.cframe.domain : parent.subjectClass();
1040
+ this.predicate = parent.cframe ? parent.cframe.predicate : parent.property();
1041
+ this.frame = parent.cframe ? parent.cframe.frame : false;
1042
+ this.label = parent.getLabel();
1043
+ this.comment = parent.getComment();
1044
+ this.range = ((parent && parent.cframe) ? parent.cframe.range : false);
1045
+ const restriction = (parent.cframe ? parent.cframe.restriction : false);
1046
+ this.restriction = false;
1047
+ if (restriction && typeof restriction === 'object') {
1048
+ this.restriction = new Restriction(restriction);
1049
+ }
1050
+ };
1051
+
1052
+ DataFrame.prototype.copy = function (newid) {
1053
+ const copy = new DataFrame();
1054
+ copy.parent = this.parent;
1055
+ copy.range = this.range;
1056
+ copy.rangeValue = this.rangeValue;
1057
+ copy.index = this.index;
1058
+ copy.type = this.type;
1059
+ copy.domain = this.domain;
1060
+ copy.domainValue = newid || this.domainValue;
1061
+ copy.predicate = this.predicate;
1062
+ copy.frame = this.frame;
1063
+ copy.label = this.label;
1064
+ copy.comment = this.comment;
1065
+ if (this.restriction) copy.restriction = this.restriction;
1066
+ if (this.language) copy.language = this.language;
1067
+ return copy;
1068
+ };
1069
+
1070
+ DataFrame.prototype.mfilter = function (rules, onmatch) {
1071
+ const hits = new FrameRule().testRules(rules, this, onmatch);
1072
+ return this;
1073
+ };
1074
+
1075
+ DataFrame.prototype.reset = function () {
1076
+ this.set(this.originalValue);
1077
+ // this.redraw();
1078
+ };
1079
+
1080
+ DataFrame.prototype.clone = function () {
1081
+ const newb = this.parent.addPropertyValue(this.get());
1082
+ };
1083
+
1084
+ DataFrame.prototype.depth = function () { return (this.parent ? this.parent.depth() : false); };
1085
+ DataFrame.prototype.property = function () { return (this.parent ? this.parent.property() : false); };
1086
+ DataFrame.prototype.subject = function () { return (this.parent ? this.parent.subject() : false); };
1087
+ DataFrame.prototype.subjectClass = function () { return (this.parent ? this.parent.subjectClass() : false); };
1088
+ DataFrame.prototype.type = function () { return (this.range ? this.range : false); };
1089
+
1090
+ DataFrame.prototype.isValidType = function (dt) {
1091
+ const vtypes = ['datatypeProperty', 'objectProperty', 'restriction '];
1092
+ if (vtypes.indexOf(dt) === -1) return false;
1093
+ return true;
1094
+ };
1095
+
1096
+ DataFrame.prototype.getAsFrame = function () {
1097
+ const ff = { type: this.type, property: this.property() };
1098
+ if (this.range) ff.range = this.range;
1099
+ if (this.rangeValue) ff.rangeValue = this.rangeValue;
1100
+ if (this.domain) ff.domain = this.domain;
1101
+ if (this.domainValue) ff.domainValue = this.domainValue;
1102
+ if (this.frame) ff.frame = this.frame;
1103
+ if (this.label) ff.label = this.label;
1104
+ if (this.comment) ff.comment = this.comment;
1105
+ return ff;
1106
+ };
1107
+
1108
+ DataFrame.prototype.hasRestriction = function () {
1109
+ if (this.restriction) {
1110
+ return this.restriction.hasCardRestriction();
1111
+ }
1112
+ return false;
1113
+ };
1114
+
1115
+ DataFrame.prototype.getLabel = function () {
1116
+ let lab = '';
1117
+ if (FrameHelper.shorten(this.predicate) === 'rdfs:label') return 'Name';
1118
+ if (FrameHelper.shorten(this.predicate) === 'rdfs:comment') return 'Description';
1119
+ if (this.label && typeof this.label === 'object') lab = this.label['@value'];
1120
+ if (this.label && typeof this.label === 'string') lab = this.label;
1121
+ // always return something
1122
+ if (!lab && this.predicate) {
1123
+ lab = FrameHelper.labelFromURL(this.predicate);
1124
+ }
1125
+ if (!lab) lab = FrameHelper.labelFromURL(this.cls);
1126
+ return lab;
1127
+ };
1128
+
1129
+ DataFrame.prototype.getType = function () {
1130
+ if (this.range) return this.range;
1131
+ if (this.rangeValue && this.rangeValue['@type']) return this.rangeValue['@type'];
1132
+ return false;
1133
+ };
1134
+
1135
+ ObjectFrame.prototype.getLabel = DataFrame.prototype.getLabel;
1136
+
1137
+ DataFrame.prototype.getComment = function () {
1138
+ let comment = '';
1139
+ if (this.comment && typeof this.comment === 'object') comment = this.comment['@value'];
1140
+ if (this.comment && typeof this.comment === 'string') comment = this.comment;
1141
+ return comment;
1142
+ };
1143
+
1144
+ ObjectFrame.prototype.getComment = DataFrame.prototype.getComment;
1145
+
1146
+ DataFrame.prototype.error = function (msg) {
1147
+ if (msg) this.err = msg;
1148
+ if (!this.errors) this.errors = [];
1149
+ this.errors.push({ type: 'Internal Data Frame Error', msg });
1150
+ return this.err;
1151
+ };
1152
+
1153
+ DataFrame.prototype.isValid = function () {
1154
+ if (!(this.type && this.isValidType(this.type))) {
1155
+ this.error(`Missing or Illegal Frame Type ${this.type}`);
1156
+ return false;
1157
+ }
1158
+ if (!(this.predicate)) {
1159
+ this.error('Missing Frame Property');
1160
+ return false;
1161
+ }
1162
+ if (!(this.domain)) {
1163
+ this.error('Missing Frame Domain');
1164
+ return false;
1165
+ }
1166
+ if (!(this.range)) {
1167
+ this.error('Missing Frame Range');
1168
+ return false;
1169
+ }
1170
+ if (this.isObjectProperty() && !(this.frame && typeof this.frame === 'object')) {
1171
+ this.error('Missing Object Frame');
1172
+ return false;
1173
+ }
1174
+ return true;
1175
+ };
1176
+
1177
+ DataFrame.prototype.isObjectProperty = function () {
1178
+ return this.type === 'objectProperty';
1179
+ };
1180
+
1181
+ DataFrame.prototype.isData = function () {
1182
+ return true;
1183
+ };
1184
+
1185
+ DataFrame.prototype.isDatatypeProperty = function () {
1186
+ return this.type === 'datatypeProperty';
1187
+ };
1188
+
1189
+ DataFrame.prototype.isLogic = function () {
1190
+ if (this.type === 'and' || this.type === 'or' || this.type === 'xor') {
1191
+ return true;
1192
+ }
1193
+ return false;
1194
+ };
1195
+
1196
+ DataFrame.prototype.isRestriction = function () {
1197
+ return this.type === 'restriction';
1198
+ };
1199
+
1200
+ DataFrame.prototype.ftype = function () {
1201
+ if (this.isDocument()) return 'document';
1202
+ if (this.isDatatypeProperty()) return 'data';
1203
+ if (this.isChoice()) return 'oneOf';
1204
+ if (this.isObject()) return 'object';
1205
+ if (this.isLogic()) return 'logic';
1206
+ if (this.isClassChoice()) return 'class_choice';
1207
+ return undefined;
1208
+ };
1209
+
1210
+ DataFrame.prototype.isClassChoice = function () {
1211
+ return (this.frame && this.frame.type === 'class_choice');
1212
+ };
1213
+
1214
+ DataFrame.prototype.isString = function () {
1215
+ if (this.range === FrameHelper.getStdURL('xsd', 'string')) {
1216
+ return true;
1217
+ }
1218
+ return false;
1219
+ };
1220
+
1221
+ DataFrame.prototype.getChoiceOptions = function () {
1222
+ const opts = [];
1223
+ for (let i = 0; i < this.frame.elements.length; i += 1) {
1224
+ const option = {};
1225
+ if (this.frame.elements[i].label) {
1226
+ option.label = this.frame.elements[i].label['@value'];
1227
+ } else {
1228
+ option.label = FrameHelper.labelFromURL(this.frame.elements[i].class);
1229
+ }
1230
+ option.value = this.frame.elements[i].class;
1231
+ opts.push(option);
1232
+ }
1233
+ return opts;
1234
+ };
1235
+
1236
+ DataFrame.prototype.lang = function () {
1237
+ return this.language || 'en';
1238
+ };
1239
+
1240
+ DataFrame.prototype.isChoice = function () {
1241
+ return (this.frame && this.frame.type === 'oneOf');
1242
+ };
1243
+
1244
+ DataFrame.prototype.isDocument = function () {
1245
+ return (this.frame && this.frame.type === 'document');
1246
+ };
1247
+
1248
+ DataFrame.prototype.isObject = function () {
1249
+ return (this.isObjectProperty() && this.frame && !(this.isChoice() || this.isDocument()));
1250
+ };
1251
+
1252
+ DataFrame.prototype.isProperty = function () {
1253
+ return false;
1254
+ };
1255
+
1256
+ DataFrame.prototype.getTypeShorthand = function () {
1257
+ if (this.isDocument()) return 'document';
1258
+ if (this.isChoice()) return 'choice';
1259
+ const sh = FrameHelper.getShorthand(this.getType());
1260
+ return (sh || this.getType());
1261
+ };
1262
+
1263
+ DataFrame.prototype.get = function () {
1264
+ if (this.contents) {
1265
+ return this.contents;
1266
+ }
1267
+ if (this.isDatatypeProperty() && this.rangeValue && typeof this.rangeValue['@value'] !== 'undefined') {
1268
+ return this.rangeValue['@value'];
1269
+ }
1270
+ if (this.isChoice() || this.isDocument()) {
1271
+ return (this.rangeValue && this.rangeValue['@id'] ? this.rangeValue['@id'] : '');
1272
+ }
1273
+ return '';
1274
+ };
1275
+
1276
+ DataFrame.prototype.set = function (value, normalizer) {
1277
+ if (normalizer) value = normalizer(value, this);
1278
+ this.contents = value;
1279
+ if (this.isChoice() || this.isDocument()) {
1280
+ this.frame.domainValue = value;
1281
+ }
1282
+ if (this.isDatatypeProperty()) {
1283
+ if (!this.rangeValue) this.rangeValue = { '@type': this.range };
1284
+ this.rangeValue['@value'] = value;
1285
+ }
1286
+ };
1287
+
1288
+ DataFrame.prototype.clear = function () {
1289
+ if (this.isDocument() || this.isChoice() || this.isDatatypeProperty()) {
1290
+ this.set('');
1291
+ }
1292
+ };
1293
+
1294
+ DataFrame.prototype.cardControlAllows = function (action) {
1295
+ if (this.parent.cframe.hasRestriction()) {
1296
+ const rest = this.parent.cframe.restriction;
1297
+ const currentnum = this.parent.values.length;
1298
+ if (action === 'add' || action === 'clone') {
1299
+ if (rest.max && currentnum >= rest.max) {
1300
+ return false;
1301
+ }
1302
+ }
1303
+ if (action === 'delete' && (rest.min && currentnum <= rest.min)) {
1304
+ return false;
1305
+ }
1306
+ }
1307
+ return true;
1308
+ };
1309
+
1310
+ DataFrame.prototype.extract = function () {
1311
+ const val = this.get();
1312
+ if (val !== '' && val !== false) {
1313
+ const objlit = {};
1314
+ if (!this.isDocument()) {
1315
+ objlit['@type'] = this.getType();
1316
+ }
1317
+ if (this.isChoice() || this.isDocument()) {
1318
+ objlit['@id'] = val;
1319
+ } else {
1320
+ objlit['@value'] = val;
1321
+ }
1322
+ if (this.language) {
1323
+ objlit['@language'] = this.language;
1324
+ }
1325
+ return objlit;
1326
+ }
1327
+ return val;
1328
+ };
1329
+
1330
+ DataFrame.prototype.saveQuery = function (newval, ntype, nlang) {
1331
+ let upd;
1332
+ if (newval === '') return false;
1333
+ if (this.isDocument() || this.isChoice()) {
1334
+ upd = WOQL.iri(newval);
1335
+ } else {
1336
+ upd = { '@value': newval };
1337
+ upd['@type'] = ntype || this.range;
1338
+ if (nlang || this.language) upd['@language'] = nlang || this.language;
1339
+ }
1340
+ let q;
1341
+ if (this.get() !== '') {
1342
+ q = WOQL.update_triple(this.parent.subject(), this.predicate, upd, this.extract());
1343
+ } else {
1344
+ q = WOQL.add_triple(this.parent.subject(), this.predicate, upd);
1345
+ }
1346
+
1347
+ if (this.parent.parent) {
1348
+ this.parent.parent.pathToDoc(q);
1349
+ }
1350
+ return q;
1351
+ };
1352
+
1353
+ DataFrame.prototype.deleteQuery = function () {
1354
+ const q = WOQL.delete_triple(this.subject(), this.predicate, this.extract());
1355
+ return q;
1356
+ };
1357
+
1358
+ /*
1359
+ * Class frames represent the archetypal version of a property frame as returned by the class frame api
1360
+ */
1361
+ function ClassFrame(frame, parent, label) {
1362
+ this.err = false;
1363
+ this.parent = parent;
1364
+ // the id of the object that owns this dataframe
1365
+ this.subjid = (parent ? parent.subjid : false);
1366
+ if (frame) {
1367
+ if (frame.label) {
1368
+ const dl = label || '';
1369
+ if (frame.label['@value']) this.label = frame.label['@value'];
1370
+ else if (frame.label) this.label = frame.label;
1371
+ else this.label = dl;
1372
+ }
1373
+ this.load(frame);
1374
+ }
1375
+ }
1376
+
1377
+ ClassFrame.prototype = DataFrame.prototype;
1378
+
1379
+ ClassFrame.prototype.load = function (frame) {
1380
+ if (typeof frame !== 'object') {
1381
+ this.error('No frame passed to load');
1382
+ return;
1383
+ }
1384
+ this.domainValue = frame.domainValue;
1385
+ this.subjid = (frame.domainValue ? frame.domainValue : false);
1386
+ this.type = frame.type;
1387
+ this.domain = frame.domain;
1388
+ this.predicate = frame.property;
1389
+ this.frame = frame.frame;
1390
+ this.comment = frame.comment ? frame.comment['@value'] : '';
1391
+ this.range = frame.range;
1392
+
1393
+ // all the meta-data carried in frames:
1394
+ this.rangeValue = frame.rangeValue;
1395
+ };
1396
+
1397
+ ClassFrame.prototype.loadFromJSONLD = function (jsonld, prop) {
1398
+ if (jsonld[prop]) {
1399
+ this.predicate = FrameHelper.unshorten(prop);
1400
+ this.type = (jsonld[prop]['@value'] ? 'datatypeProperty' : 'objectProperty');
1401
+ this.range = FrameHelper.unshorten(jsonld[prop]['@type']);
1402
+ this.rangeValue = jsonld[prop]['@id'] ? jsonld[prop]['@id'] : jsonld[prop];
1403
+ }
1404
+ this.domain = FrameHelper.unshorten(jsonld['@type']);
1405
+ this.domainValue = FrameHelper.unshorten(jsonld['@id']);
1406
+ this.label = FrameHelper.labelFromURL(this.predicate);
1407
+ this.comment = '';// this.getComment()
1408
+ };
1409
+
1410
+ ClassFrame.prototype.isClassChoice = function () {
1411
+ return (this.frame && this.frame.type === 'class_choice');
1412
+ };
1413
+
1414
+ ClassFrame.prototype.loadFromObjectFrame = function (par, child) {
1415
+ // all the meta-data carried in frames:
1416
+ this.type = 'objectProperty';
1417
+ this.predicate = par.property;
1418
+ this.range = par.range;
1419
+ this.rangeValue = child.subjid;
1420
+ this.domain = par.cls;
1421
+ this.domainValue = par.subjid;
1422
+ this.label = par.getLabel();
1423
+ this.comment = par.getComment();
1424
+ this.restriction = par.restriction;
1425
+ };
1426
+
1427
+ ClassFrame.prototype.createEmptyChoice = function (cls, newid) {
1428
+ const cf = this.getChosenClassFrames(cls);
1429
+ const objframe = new ObjectFrame(cls);
1430
+ if (newid) objframe.subjid = newid;
1431
+ objframe.loadClassFrames(cf);
1432
+ const fframe = objframe.fillFromSchema();
1433
+ fframe.status = 'new';
1434
+ return fframe;
1435
+ };
1436
+
1437
+ ClassFrame.prototype.getChosenClassFrames = function (chosen) {
1438
+ let nc = [];
1439
+ for (let i = 0; i < this.frame.operands.length; i += 1) {
1440
+ for (let j = 0; j < this.frame.operands[i].length; j += 1) {
1441
+ if (chosen === this.frame.operands[i][j].domain) {
1442
+ nc = nc.concat(this.frame.operands[i][j]);
1443
+ }
1444
+ }
1445
+ }
1446
+ return nc;
1447
+ };
1448
+
1449
+ ClassFrame.prototype.createEmpty = function (newid) {
1450
+ let objframe;
1451
+ if (this.isObject()) {
1452
+ if (this.isClassChoice()) {
1453
+ // objframe=new ObjectFrame(this.predicate, {}, this)
1454
+ objframe = new ObjectFrame(this.predicate);
1455
+ } else {
1456
+ objframe = new ObjectFrame(this.range);
1457
+ }
1458
+ if (newid) objframe.subjid = newid;
1459
+ objframe.loadClassFrames(this.frame);
1460
+ const fframe = objframe.fillFromSchema();
1461
+ fframe.status = 'new';
1462
+ return fframe;
1463
+ }
1464
+ if (this.isData()) {
1465
+ const dataframe = this.copy(newid);
1466
+ dataframe.set('');
1467
+ dataframe.status = 'new';
1468
+ return dataframe;
1469
+ }
1470
+ return undefined;
1471
+ };
1472
+
1473
+ ClassFrame.prototype.cloneDataFrame = function (newid, other) {
1474
+ return other;
1475
+ };
1476
+
1477
+ ClassFrame.prototype.clone = function (newid, other) {
1478
+ if (this.isObject()) {
1479
+ console.log('cannot clone class frame');
1480
+ } else if (this.isData()) {
1481
+ return this.cloneDataFrame(newid, other);
1482
+ }
1483
+ return undefined;
1484
+ };
1485
+
1486
+ ClassFrame.prototype.getClassChoices = function () {
1487
+ const choices = [];
1488
+ // if (this.frame.operands) {
1489
+ /* for (let i = 0; i < this.frame.operands.length; i += 1) {
1490
+ for (let j = 0; j < this.frame.operands[i].length; j += 1) {
1491
+ const domcls = this.frame.operands[i][j].domain;
1492
+ if (domcls && choices.indexOf(domcls) === -1) {
1493
+ choices.push(domcls);
1494
+ }
1495
+ }
1496
+ } */
1497
+ // }
1498
+ return choices;
1499
+ };
1500
+
1501
+ ClassFrame.prototype.getChosenClassFrame = function (chosen, parent) {
1502
+ for (let i = 0; i < this.frame.operands.length; i += 1) {
1503
+ const operand = this.frame.operands[i];
1504
+ if (operand.class && chosen === operand.class) {
1505
+ const cf = new ClassFrame(operand, parent, this.label);
1506
+ return cf;
1507
+ }
1508
+ }
1509
+ return false;
1510
+ };
1511
+
1512
+ ClassFrame.prototype.getChosenFrame = function (dataframe) {
1513
+ return dataframe;
1514
+ };
1515
+
1516
+ ClassFrame.prototype.isData = function () {
1517
+ return (this.isDocument() || this.isChoice() || this.isDatatypeProperty());
1518
+ };
1519
+
1520
+ function Restriction(restriction) {
1521
+ this.min = 0;
1522
+ this.max = 0;
1523
+ if (restriction) this.loadRestriction(restriction);
1524
+ }
1525
+
1526
+ Restriction.prototype.hasCardRestriction = function () {
1527
+ if (this.min === 0 && this.max === 0) return false;
1528
+ return true;
1529
+ };
1530
+
1531
+ Restriction.prototype.loadRestriction = function (restriction) {
1532
+ if (typeof restriction.cardinality !== 'undefined') {
1533
+ this.max = restriction.cardinality;
1534
+ this.min = restriction.cardinality;
1535
+ } else if (typeof restriction.maxCardinality !== 'undefined') {
1536
+ this.max = restriction.maxCardinality;
1537
+ } else if (typeof restriction.minCardinality !== 'undefined') {
1538
+ this.min = restriction.minCardinality;
1539
+ } else if (typeof restriction.type !== 'undefined' && restriction.type === 'and' && typeof restriction.operands === 'object') {
1540
+ for (let i = 0; i < restriction.operands.length; i += 1) {
1541
+ const nrest = new Restriction(restriction.operands[i]);
1542
+ if (this.max === 0 && nrest.max > 0) this.max = nrest.max;
1543
+ else if (this.max > 0 && nrest.max > 0 && nrest.max < this.max) this.max = nrest.max;
1544
+ if (this.min === 0 && nrest.min > 0) this.min = nrest.min;
1545
+ else if (this.min > 0 && nrest.min > 0 && nrest.min > this.min) this.min = nrest.min;
1546
+ }
1547
+ }
1548
+ };
1549
+
1550
+ module.exports = ObjectFrame;