-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathobjectFrame.js
1552 lines (1408 loc) · 46.2 KB
/
objectFrame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable consistent-return */
/* eslint-disable prefer-destructuring */
/* eslint-disable guard-for-in */
/* eslint-disable max-len */
/* eslint-disable no-redeclare */
/* eslint-disable block-scoped-var */
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
/* eslint-disable no-plusplus */
/* eslint-disable no-unused-vars */
/* eslint-disable no-shadow */
/* eslint-disable no-param-reassign */
/* eslint-disable no-continue */
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-use-before-define */
const FrameHelper = require('../utils');
const { FrameRule } = require('./frameRule');
const WOQL = require('../woql');
/**
* @file Javascript Terminus Document Classes
* @license Apache Version 2
* Helper classes for accessing documents returned by the Terminus DB API programmatically
*
* @example
* let doc = new TerminusDocument(client);
*
* //These set the objects document property and return promises:
*
* doc.loadDocument(URL).then(() => console.log(this.document));
* doc.loadComplete(URL, CLS).then(() => console.log(this.document))
* doc.loadSchema(cls).then(() => console.log(this.document))
*
* //These just set the object's document property
* doc.loadJSON(json_frames, cls) //console.log(this.document)
* doc.loadDataFrames(json_frames, cls)
* doc.loadClassFrames(json_frames, cls)
* @example
*
* @description Represents a frame for programmatic access to object frame,
* anywhere within a document
* Recursive data structure where this.children contains an indexed array of object frames
* and this.dataframes contains a property indexed array of data frames
* Every object frame carries a reference to its classframe
* This gives us instructions as to how to create new frames according to the schema
* After that it's turtles all the way down.
* @param cls - ID of the class (URL)
* @param classframe - an array of frames representing a class
* @param archetypes list of class frames
* @param parent parent object
* @returns
*/
function ObjectFrame(cls, jsonld, classframes, parent) {
// the class of the frame - mandatory
this.empty();
this.cls = FrameHelper.unshorten(cls);
// array of frames representing the full class frame
// (optional - required to be able to safely add missing properties)
if (classframes && typeof classframes === 'object') {
this.loadClassFrames(classframes);
}
// console.log("this", this)
if (jsonld && typeof jsonld === 'object' && Object.keys(jsonld).length) {
this.originalDocument = jsonld;
this.loadJSONLDDocument(jsonld);
// console.log("this after jsonld doc call", this)
} else {
this.originalDocument = false;
}
// parent object frame
this.parent = parent;
// set to true if this is a newly created document
this.newDoc = false;
}
/**
* Loads class frames for the object's class - the instructions about how to put an object together
*/
ObjectFrame.prototype.loadClassFrames = function (classframes) {
for (let j = 0; j < classframes.length; j += 1) {
if (classframes[j]['@context']) this.jsonld_context = classframes[j]['@context'];
const cf = new ClassFrame(classframes[j], this);
if (cf.isValid()) {
if (!this.classframes) this.classframes = {};
this.classframes[classframes[j].property] = cf;
if (cf.isObject() && this.properties[classframes[j].property]) {
for (let i = 0; i < this.properties[classframes[j].property].values.length; i += 1) {
this.properties[classframes[j].property].values[i].loadClassFrames(classframes[j].frame);
}
}
} else {
// eslint-disable-next-line no-console
console.log('Invalid classframe', cf);
}
}
return this;
};
/**
* Does this object have a schema loaded?
*/
ObjectFrame.prototype.hasSchema = function () {
return !FrameHelper.empty(this.classframes);
};
/*
* Loads an array of data frames into the object's internal index
* {property: [frames]}
*/
/* ObjectFrame.prototype.loadDataFrames = function (frames) {
if (typeof frames !== 'object' || !frames.length) return undefined;
if (!this.originalFrames) this.originalFrames = frames;
if (!this.subjid && frames[0].domainValue) this.subjid = frames[0].domainValue;
for (let i = 0; i < frames.length; i += 1) {
if (frames[i]['@context']) this.jsonld_context = frames[i]['@context'];
let cframe = this.getPropertyClassFrame(frames[i].property, frames[i]);
if (cframe && cframe.isClassChoice()) {
cframe = cframe.getChosenClassFrame(frames[i].range);
if (!cframe) {
console.log(`no choice frame ${frames[i].range}`);
}
} else if (cframe && cframe.isLogic()) {
cframe = cframe.getChosenFrame(frames[i]);
}
if (cframe) {
if (typeof this.properties[frames[i].property] === 'undefined') {
this.properties[frames[i].property] = new PropertyFrame(frames[i].property, cframe, this);
}
this.properties[frames[i].property].addFrame(frames[i]);
}
}
return this;
}; */
ObjectFrame.prototype.loadJSONLDDocument = function (rdoc) { // KItty check herer
if (typeof rdoc !== 'object') return undefined;
const doc = FrameHelper.json_unshorten(rdoc);
if (!this.originalDocument) this.originalDocument = doc;
if (!this.subjid && doc['@id']) {
this.subjid = FrameHelper.unshorten(doc['@id']);
}
if (doc['@context']) this.jsonld_context = doc['@context'];
for (const prop in doc) {
if (prop[0] === '@' || (typeof doc[prop] === 'object' && Object.keys(doc[prop]).length === 0)) continue;
let cframe = this.getPropertyClassFrame(prop, doc);
if (cframe && cframe.isClassChoice()) {
// cframe = cframe.getChosenClassFrame(FrameHelper.unshorten(doc[prop]["@type"]),cframe.parent)
if (!cframe) {
console.log(`no choice frame ${doc[prop]['@type']}`);
}
} else if (cframe && cframe.isLogic()) {
cframe = cframe.getChosenFrame(doc[prop]);
}
if (cframe) {
if (typeof this.properties[prop] === 'undefined') {
this.properties[prop] = new PropertyFrame(prop, cframe, this);
}
this.properties[prop].addJSONLDDocument(doc[prop]);
}
}
return this;
};
/*
* Serialises the javascript object as an array of frames
*/
ObjectFrame.prototype.getAsFrame = function (prop, parent) {
prop = FrameHelper.unshorten(prop);
if (this.parentframe) return this.parentframe;
const ff = { type: 'objectProperty', property: prop };
ff.range = this.cls;
ff.domain = parent.cls;
ff.domainValue = parent.subjid;
ff.frame = [];
for (const prop of Object.keys(this.properties)) {
ff.frame = ff.frame.concat(ff.frame, this.properties[prop].getAsFrames());
}
return ff;
};
ObjectFrame.prototype.getAsFrames = function (prop, parent) {
let frames = [];
for (const prop of Object.keys(this.properties)) {
frames = frames.concat(frames, this.properties[prop].getAsFrames());
}
return frames;
};
/**
* Cleans out object and sets everything empty
*/
ObjectFrame.prototype.empty = function () {
// all indexed by property
this.properties = {};
this.restrictions = {};
this.subjid = false;
this.cls = false;
};
/*
* Resets object state to original state
*/
ObjectFrame.prototype.reset = function (prop) {
if (prop) {
prop = FrameHelper.unshorten(prop);
const props = [];
for (let i = 0; i < this.originalFrames.length; i += 1) {
if (this.originalFrames[i].property === prop) {
props.push(this.originalFrames[i]);
}
}
if (this.properties[prop]) this.properties[prop] = [];
this.loadDataFrames(props);
} else {
this.restrictions = {};
this.properties = {};
this.loadDataFrames(this.originalFrames);
}
};
/*
* Clears out any specific information from a tree
*/
ObjectFrame.prototype.clear = function () {
for (const prop of Object.keys(this.properties)) {
this.properties[prop].clear();
}
return this;
};
/*
* Filters the frame and its children
*/
ObjectFrame.prototype.mfilter = function (rules, onmatch) {
const hits = new FrameRule().testRules(rules, this, onmatch);
for (const prop of Object.keys(this.properties)) {
if (!this.properties[prop].mfilter) {
console.log(prop, this.properties[prop]);
} else {
this.properties[prop].mfilter(rules, onmatch);
}
}
return this;
};
/**
* If a class frame is present, it is returned for the given property
* If no class frame is present and an instance frame is passed in the
* this enables make it up as you go along editing
* second argument a class frame will be created from the instance frame.
*/
ObjectFrame.prototype.getPropertyClassFrame = function (prop, jsonlddoc) {
if (typeof prop === 'object') {
return new ClassFrame(prop);
}
prop = FrameHelper.unshorten(prop);
if (this.classframes && typeof this.classframes === 'object' && typeof this.classframes[prop] === 'object') {
// console.log("returning " + prop, this.classframes[prop])
return this.classframes[prop];
}
if (jsonlddoc) {
const cf = new ClassFrame();
cf.loadFromJSONLD(jsonlddoc, prop);
return cf;
}
if (this.properties[prop]) {
return new ClassFrame(this.properties[prop].values[0]);
}
return false;
};
/*
* Returns a list of properties - type can be filled|missing|all
*/
ObjectFrame.prototype.getProperties = function (type) {
if (type === 'filled' || !this.classframes) {
return Object.keys(this.properties);
}
if (type === 'missing') {
const filled = Object.keys(this.properties).map((item) => FrameHelper.unshorten(item));
const all = Object.keys(this.classframes).map((item) => FrameHelper.unshorten(item));
const missing = [];
for (let i = 0; i < all.length; i++) {
if (filled.indexOf(all[i]) === -1 && missing.indexOf(all[i]) === -1) {
missing.push(all[i]);
}
}
return missing;
}
return Object.keys(this.classframes);
};
/**
* Missing properties are those that are present in the classframe,
* but not instantiated in the current object frame
* for this to work we need to load the frame with the associated classframe
*/
ObjectFrame.prototype.getMissingPropertyList = function () {
const missing = this.getProperties('missing');
const nmissing = [];
for (let i = 0; i < missing.length; i++) {
const cframe = this.getPropertyClassFrame(missing[i]);
if (cframe) {
var newb = { label: cframe.getLabel(), value: missing[i] };
} else {
var newb = { label: missing[i], value: missing[i] };
}
nmissing.push(newb);
}
return nmissing;
};
/**
* Returns a list of all the classes that can show up in the frame
*/
ObjectFrame.prototype.getPossibleContainedClasses = function () {
const cls = [];
function efcf(frames) {
if (frames.type && frames.type === 'class_choice') {
efcf(frames.operands);
}
if (!Array.isArray(frames)) frames = [frames];
for (let i = 0; i < frames.length; i++) {
if (frames[i].domain && cls.indexOf(frames[i].domain) === -1) cls.push(frames[i].domain);
if (frames[i].frame) {
efcf(frames[i].frame);
}
}
}
efcf(Object.values(this.classframes));
return cls;
};
ObjectFrame.prototype.getDocumentLinks = function () {
const vals = [];
const props = this.getProperties('filled');
for (let i = 0; i < props.length; i++) {
const mprop = this.properties[props[i]];
for (let k = 0; k < mprop.values.length; k++) {
const dval = mprop.values[k];
if (dval.isObject() && dval.getDocumentLinks) {
const nvals = dval.getDocumentLinks();
for (let l = 0; l < nvals.length; l++) {
if (vals.indexOf(nvals[l]) === -1) {
vals.push(nvals[l]);
}
}
} else if (dval.isDocument()) {
const nv = dval.get();
if (vals.indexOf(nv) === -1) {
vals.push(nv);
}
}
}
}
return vals;
};
/**
* List of properties that are filled in the object
*/
ObjectFrame.prototype.getFilledPropertyList = function () {
const props = this.getProperties('filled');
const filled = [];
for (let i = 0; i < props.length; i++) {
const cframe = this.getPropertyClassFrame(props[i]);
if (cframe) {
var newb = { label: cframe.getLabel(), value: props[i] };
} else {
var newb = { label: props[i], value: props[i] };
}
filled.push(newb);
}
return filled;
};
/*
* Fills the object frame from the schema - adding all the necessary property
* frames, etc to make it complete
*/
ObjectFrame.prototype.fillFromSchema = function (newid) {
if (newid) this.subjid = newid;
newid = (newid || FrameHelper.genBNID(`${FrameHelper.urlFragment(this.cls)}_`));
const properties = {};
if (this.classframes) {
for (const prop of Object.keys(this.classframes)) {
const pf = this.getPropertyClassFrame(prop);
properties[prop] = new PropertyFrame(prop, pf, this);
properties[prop].fillFromSchema(newid);
}
}
this.properties = properties;
this.originalFrames = [];
for (const prop of Object.keys(this.properties)) {
this.originalFrames.push(this.properties[prop].getAsFrames());
}
return this;
};
/*
* Clones an object frame to be a copy of the current frame
*/
ObjectFrame.prototype.clone = function (newid) {
const properties = {};
const cloned = new ObjectFrame(this.cls, false, false, this.parent);
cloned.classframes = this.classframes;
cloned.subjid = newid;
for (const prop of Object.keys(this.properties)) {
properties[prop] = this.properties[prop].clone();
}
cloned.properties = properties;
return cloned;
};
/**
* Returns a child frame with a particular id
* If the second parameter is included, it will only look in that specific property
* Otherwise searches all properties for the child
*/
ObjectFrame.prototype.getChild = function (childid, prop) {
let pframe = this.getProperty(prop);
for (let i = 0; i < pframe.values.length; pframe++) {
if (pframe.values[i].isObject() && pframe.values[i].subject === childid) return pframe.values[i];
}
if (!prop) {
for (const key of Object.keys(this.properties)) {
for (let i = 0; i < this.properties[key].values.length; i += 1) {
if (this.properties[key].values[i].subject() === childid) return this.properties[key].values[i];
}
}
}
return false;
};
ObjectFrame.prototype.addProperty = function (prop, cls) {
if (typeof prop !== 'object') prop = FrameHelper.unshorten(prop);
const cframe = this.getPropertyClassFrame(prop);
let ndata = false;
if (cframe) {
const nprop = new PropertyFrame(prop, cframe, this);
if (cframe.isObject()) {
if (!cframe.isClassChoice()) {
ndata = cframe.createEmpty(FrameHelper.genBNID(`${FrameHelper.urlFragment(cframe.range)}_`));
}
if (cls) {
ndata = cframe.createEmptyChoice(cls, FrameHelper.genBNID(`${FrameHelper.urlFragment(cls)}_`));
}
const clss = cframe.getClassChoices();
if (clss && clss.length) {
ndata = cframe.createEmptyChoice(clss[0], FrameHelper.genBNID(`${FrameHelper.urlFragment(clss[0])}_`));
}
} else {
ndata = cframe.createEmpty();
}
if (ndata) {
nprop.addValueFrame(ndata);
}
if (typeof this.properties[prop] === 'undefined') {
if (typeof prop === 'object') var p = prop.property;
else var p = prop;
this.properties[p] = nprop;
// this.properties[prop] = nprop;
}
// else {
// this.properties[prop].push(nprop);
// }
nprop.status = 'new';
return nprop;
}
return false;
};
ObjectFrame.prototype.addPropertyValue = function (prop, value) {
prop = FrameHelper.unshorten(prop);
if (this.properties[prop]) return this.properties[prop].addValue(value);
return null;
};
ObjectFrame.prototype.removeProperty = function (prop) {
prop = FrameHelper.unshorten(prop);
if (typeof this.properties[prop] !== 'undefined') {
delete (this.properties[prop]);
}
};
ObjectFrame.prototype.removePropertyValue = function (prop, value, index) {
prop = FrameHelper.unshorten(prop);
const pframe = this.properties[prop];
pframe.removeValue(value, index);
if (pframe.values.length === 0) {
this.removeProperty(prop);
}
};
ObjectFrame.prototype.error = function (msg) {
if (!this.errors) this.errors = [];
this.errors.push({ type: 'Internal Object Frame Error', msg });
};
ObjectFrame.prototype.extract = function () {
const extracts = {};
for (const prop in this.properties) {
const extracted = this.properties[prop].extract();
if (!FrameHelper.empty(extracted)) {
if (typeof extracts[prop] === 'undefined') extracts[prop] = [];
extracts[prop] = extracts[prop].concat(extracted);
}
if (extracts[prop] && extracts[prop].length === 1) extracts[prop] = extracts[prop][0];
}
if (FrameHelper.empty(extracts) && this.parent) {
return false;
}
const ext = this.extractJSONLD(extracts);
return ext;
};
ObjectFrame.prototype.extractJSONLD = function (extracts) {
extracts['@type'] = this.cls;
if (this.subject() !== '_:') extracts['@id'] = this.subject();
if (this.jsonld_context) extracts['@context'] = this.jsonld_context;
return extracts;
};
ObjectFrame.prototype.subject = function () {
return this.subjid || '';
};
ObjectFrame.prototype.get = ObjectFrame.prototype.subject;
ObjectFrame.prototype.set = function (val) {
this.subjid = val;
};
ObjectFrame.prototype.isObject = function () { return true; };
ObjectFrame.prototype.isProperty = function () { return false; };
ObjectFrame.prototype.isData = function () { return false; };
ObjectFrame.prototype.isClassChoice = function () {
return (this.frame && this.frame.type === 'class_choice');
};
ObjectFrame.prototype.subjectClass = function () {
return this.cls;
};
ObjectFrame.prototype.depth = function () {
if (this.parent) return (this.parent.depth() + 1);
return 0;
};
ObjectFrame.prototype.getProperty = function (prop) {
return this.properties[prop];
};
ObjectFrame.prototype.first = function (prop) {
if (this.properties && this.properties[prop]) {
return this.properties[prop].first();
}
};
ObjectFrame.prototype.property = function (prop) {
if (this.parent) return this.parent.property();
return false;
};
ObjectFrame.prototype.parentObject = function () {
if (this.parent && this.parent.parent) {
return this.parent.parent;
}
return false;
};
ObjectFrame.prototype.root = function () {
if (this.parent) return false;
return true;
};
ObjectFrame.prototype.renderProperties = function () {
const props = this.sortProperties();
const nprops = [];
for (let i = 0; i < props.length; i++) {
if (this.properties[props[i]].render) {
const rend = this.properties[props[i]].render(this.properties[props[i]]);
if (rend) nprops.push(rend);
}
}
return nprops;
};
ObjectFrame.prototype.sortProperties = function () {
const unsorted = Object.keys(this.properties);
if (this.compare) {
return unsorted.sort((a, b) => this.compare(a, b, this));
}
return unsorted.sort((a, b) => this.standardCompare(a, b, this));
};
/*
* Label first, then datatype properties, then object properties
*/
ObjectFrame.prototype.standardCompare = function (a, b, doc) {
if (FrameHelper.shorten(a) === 'rdfs:label') return -1;
if (FrameHelper.shorten(b) === 'rdfs:label') return 1;
if (FrameHelper.shorten(a) === 'rdfs:comment') return -1;
if (FrameHelper.shorten(b) === 'rdfs:comment') return 1;
if (doc.properties[a].isData() && doc.properties[b].isObject()) return -1;
if (doc.properties[b].isData() && doc.properties[a].isObject()) return 1;
return 0;
};
ObjectFrame.prototype.cardControlAllows = function (action) {
if (!this.parent) return true;
if (this.parent.cframe.hasRestriction()) {
const rest = this.parent.cframe.restriction;
const currentnum = this.parent.values.length;
if (action === 'add' || action === 'clone') {
if (rest.max && currentnum >= rest.max) {
return false;
}
}
if (action === 'delete' && (rest.min && currentnum <= rest.min)) {
return false;
}
}
return true;
};
ObjectFrame.prototype.isUpdated = function () {
let i = 0;
for (const prop in this.properties) {
if (this.originalFrames[i] !== prop) return true;
if (this.properties[prop].isUpdated()) return true;
i++;
}
if (i !== this.originalFrames.length) return true;
return false;
};
ObjectFrame.prototype.isNew = function () {
return (this.subject().substring(0, 2) === '_:');
};
ObjectFrame.prototype.getSummary = function () {
const ret = { status: 'ok' };
if (this.isUpdated()) ret.status = 'updated';
if (this.isNew()) ret.status = 'new';
ret.propcount = 0;
for (const prop in this.properties) {
ret.propcount++;
}
ret.long = `${ret.propcount} properties`;
return ret;
};
ObjectFrame.prototype.saveQuery = function () {
const q = WOQL.update_object(this.extract());
this.pathToDoc(q);
return q;
};
ObjectFrame.prototype.pathToDoc = function (q) {
q.add_triple(this.subjid, 'type', this.cls);
if (this.parent) {
q.add_triple(this.parent.subject(), 'type', this.parent.subjectClass());
q.add_triple(this.parent.subject(), this.parent.predicate, this.subjid);
if (this.parent.parent) {
this.parent.parent.pathToDoc(q);
}
}
};
ObjectFrame.prototype.deleteQuery = function () {
const q = WOQL.delete_object(this.subjid);
if (this.parent) {
q.delete_triple(this.parent.subject(), this.parent.predicate, this.subjid);
}
return q;
};
function PropertyFrame(property, cframe, parent) {
this.predicate = property;
this.cframe = cframe;
this.parent = parent;
this.values = [];
}
PropertyFrame.prototype.addJSONLDDocument = function (jsonld) {
if (this.cframe.isData()) {
if (Array.isArray(jsonld)) {
for (var i = 0; i < jsonld.length; i++) {
var df = new DataFrame(jsonld[i], this, this.values.length);
this.values.push(df);
}
} else {
var df = new DataFrame(jsonld, this, this.values.length);
this.values.push(df);
}
} else if (Array.isArray(jsonld)) {
for (var i = 0; i < jsonld.length; i++) {
const kid = new ObjectFrame(FrameHelper.unshorten(jsonld[i]['@type']), jsonld[i], this.cframe.frame, this);
this.values.push(kid);
}
} else {
const kid = new ObjectFrame(jsonld['@type'], jsonld, this.cframe.frame, this);
this.values.push(kid);
}
};
PropertyFrame.prototype.addFrame = function (frame) {
if (this.cframe.isData()) {
const df = new DataFrame(frame, this, this.values.length);
this.values.push(df);
} else {
const kid = new ObjectFrame(this.range(), this.cframe.frame, frame.frame, this, frame);
this.values.push(kid);
}
};
PropertyFrame.prototype.addValueFrame = function (oframe) {
if (oframe) {
oframe.parent = this;
oframe.index = this.values.length;
this.values.push(oframe);
}
};
PropertyFrame.prototype.addValue = function (val) {
const nu = this.createEmpty();
if (val) nu.set(val);
this.addValueFrame(nu);
return nu;
};
PropertyFrame.prototype.fillFromSchema = function (newid) {
if (this.isData() || (this.isObject() && !this.isClassChoice())) {
const values = [];
if (this.cframe.hasRestriction() && this.cframe.restriction.min) {
for (let i = 0; i < this.cframe.restriction.min; i += 1) {
var nframe = this.createEmpty(newid);
nframe.parent = this;
values.push(nframe);
}
} else {
var nframe = this.createEmpty(newid);
nframe.parent = this;
values.push(nframe);
}
this.values = values;
} else if (this.isClassChoice()) {
const clss = this.cframe.getClassChoices();
if (clss && clss.length) {
const empty = this.cframe.createEmptyChoice(clss[0], FrameHelper.genBNID(`${FrameHelper.urlFragment(clss[0])}_`));
empty.parent = this;
this.values = [empty];
}
}
};
PropertyFrame.prototype.isData = function () {
return this.cframe.isData();
};
PropertyFrame.prototype.isObject = function () {
return this.cframe.isObject();
};
PropertyFrame.prototype.isProperty = function () {
return true;
};
PropertyFrame.prototype.property = function () {
return this.predicate;
};
PropertyFrame.prototype.extract = function () {
const extracts = [];
const hasVal = (val) => {
if (val['@value']) {
for (var i = 0; i < extracts.length; i++) {
if (extracts[i]['@value'] && extracts[i]['@value'] === val['@value']
&& extracts[i]['@type'] && extracts[i]['@type'] === val['@type']) return true;
}
return false;
}
if (val['@id']) {
for (var i = 0; i < extracts.length; i++) {
if (extracts[i]['@id'] && extracts[i]['@id'] === val['@id']) return true;
}
return false;
}
};
for (let i = 0; i < this.values.length; i++) {
const val = this.values[i].extract();
if (val !== '' && val !== false && typeof val !== 'undefined' && !hasVal(val)) extracts.push(val);
}
return extracts;
};
PropertyFrame.prototype.subject = function () {
return (this.parent ? this.parent.subject() : false);
};
PropertyFrame.prototype.subjectClass = function () {
return (this.parent ? this.parent.subjectClass() : false);
};
PropertyFrame.prototype.depth = function () {
return (this.parent ? this.parent.depth() : false);
};
PropertyFrame.prototype.updated = function () {
return (this.parent ? this.parent.childUpdated() : false);
};
PropertyFrame.prototype.range = function () {
return (this.cframe ? this.cframe.range : '');
};
PropertyFrame.prototype.getLabel = function () {
return (
// this.cframe ? this.cframe.getLabel() : '');
this.cframe ? this.cframe.getLabel() : this.predicate.getLabel());
};
PropertyFrame.prototype.getComment = function () {
return (this.cframe ? this.cframe.getComment() : false);
};
PropertyFrame.prototype.hasCardinalityRestriction = function () {
return (this.cframe ? this.cframe.hasRestriction() : false);
};
PropertyFrame.prototype.getRestriction = function () {
return (this.cframe ? this.cframe.restriction : false);
};
PropertyFrame.prototype.isClassChoice = function () {
return (this.cframe ? this.cframe.isClassChoice() : false);
};
PropertyFrame.prototype.deletePropertyValue = function (value, index) {
this.parent.removePropertyValue(this.property(), value, index);
};
PropertyFrame.prototype.removeValue = function (value, index) {
const nvals = [];
for (let i = 0; i < this.values.length; i++) {
if (this.values[i].index !== value.index) {
nvals.push(this.values[i]);
}
}
this.values = nvals;
/* let nvals = [] //trial
for(var i = 0; i<this.values.length; i++){
if(this.values[i].get() != value){
nvals.push(this.values[i])
}
}
this.values = nvals */
};
PropertyFrame.prototype.get = function () {
const gets = [];
for (let i = 0; i < this.values.length; i++) {
if (this.values[i]) {
const x = this.values[i].get();
if (x) gets.push(x);
}
}
return gets;
};
PropertyFrame.prototype.set = function (val) {
for (let i = 0; i < this.values.length; i++) {
if (this.values[i]) {
this.values[i].set(val);
}
}
};
PropertyFrame.prototype.clear = function () {
for (let i = 0; i < this.values.length; i++) {
this.values[i].clear();
}
};
PropertyFrame.prototype.clone = function () {
const cvalues = [];
const cloned = new PropertyFrame(this.predicate, this.cframe, this.parent);
for (let i = 0; i < this.values.length; i++) {
cvalues.push(this.values[i].clone());
}
cloned.values = cvalues;
return cloned;
//
};
PropertyFrame.prototype.getAsFrames = function () {
let fs = [];
for (let i = 0; i < this.values.length; i++) {
if (this.values[i]) {
if (this.isData()) {
fs.push(this.values[i].getAsFrame());
} else {
fs = fs.concat(this.values[i].getAsFrames());
}
}
}
return fs;
};
PropertyFrame.prototype.createEmpty = function () {
if (this.cframe.isData()) {
const df = this.cframe.copy(this.subject());
df.set('');
df.status = 'new';
return df;
}
if (this.cframe.isObject()) {
if (!this.cframe.isClassChoice()) {
const df = this.cframe.createEmpty(FrameHelper.genBNID(`${FrameHelper.urlFragment(this.cframe.range)}_`));
df.status = 'new';
return df;
}
const df = new ClassFrame(this.cframe);
df.status = 'new';
return df;
}
};
PropertyFrame.prototype.mfilter = function (rules, onmatch) {
const hits = new FrameRule().testRules(rules, this, onmatch);
for (let i = 0; i < this.values.length; i++) {
this.values[i].mfilter(rules, onmatch);
}
return this;
};
/*
* Shorthand functions to make it easier to access the underlying data
*/
PropertyFrame.prototype.first = function () {
if (this.values && this.values[0]) {
return this.values[0].get();
}
};
PropertyFrame.prototype.renderValues = function () {
const sortedVals = this.sortValues();
const vals = [];
for (let i = 0; i < sortedVals.length; i++) {
if (sortedVals[i] && sortedVals[i].render) {
const rend = sortedVals[i].render(sortedVals[i]);
if (rend) vals.push(rend);
}
}
return vals;
};
PropertyFrame.prototype.sortValues = function () {
if (this.compare) {
return this.values.sort((a, b) => this.compare(a, b, this));
}
return this.values;
};
PropertyFrame.prototype.cardControlAllows = function (action) {
if (this.cframe.hasRestriction()) {
const rest = this.cframe.restriction;
const currentnum = this.values.length;
if (action === 'add' || action === 'clone') {
if (rest.max && currentnum >= rest.max) {
return false;
}
}
if (action === 'delete' && (rest.min)) {
return false;
}
}
return true;
};
PropertyFrame.prototype.isUpdated = function () {
return true;
// eslint-disable-next-line no-unreachable
if (this.values.length !== this.originalValues.length) return true;
for (let i = 0; i < this.values.length; i++) {
if (this.cframe && this.cframe.isData()) {
if (this.values[i].value() !== this.originalValues[i]) {
return true;
}
} else {
if (this.values[i].subject() !== this.originalValues[i]) {
return true;
}
if (this.values[i].isUpdated()) {
return true;
}
}
}
return false;