Skip to content

Commit 4bdfc89

Browse files
committed
frame viewer class choice code
1 parent 074e063 commit 4bdfc89

File tree

3 files changed

+98
-60
lines changed

3 files changed

+98
-60
lines changed

lib/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Utils.compareIDs = function(ida, idb) {
138138
*/
139139
Utils.shorten = function(url, prefixes) {
140140
if (!url) return undefined
141+
if(typeof url == "function") return undefined
141142
prefixes = prefixes || Utils.standard_urls
142143
for (const pref in prefixes) {
143144
if (prefixes[pref] == url.substring(0, prefixes[pref].length)) {

lib/viewer/objectFrame.js

Lines changed: 96 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ function ObjectFrame(cls, jsonld, classframes, parent) {
4646
if (classframes && typeof classframes === 'object') {
4747
this.loadClassFrames(classframes);
4848
}
49+
//console.log("this", this)
4950
if (jsonld && typeof jsonld == 'object' && Object.keys(jsonld).length) {
5051
this.originalDocument = jsonld;
5152
this.loadJSONLDDocument(jsonld);
53+
//console.log("this after jsonld doc call", this)
5254
} else {
5355
this.originalDocument = false;
5456
}
@@ -58,6 +60,7 @@ function ObjectFrame(cls, jsonld, classframes, parent) {
5860
this.newDoc = false;
5961
}
6062

63+
6164
/**
6265
* Loads class frames for the object's class - the instructions about how to put an object together
6366
*/
@@ -118,7 +121,7 @@ ObjectFrame.prototype.hasSchema = function () {
118121
return this;
119122
};*/
120123

121-
ObjectFrame.prototype.loadJSONLDDocument = function (rdoc) {
124+
ObjectFrame.prototype.loadJSONLDDocument = function (rdoc) { //KItty check herer
122125
if (typeof rdoc !== 'object') return undefined;
123126
let doc = FrameHelper.json_unshorten(rdoc)
124127
if (!this.originalDocument) this.originalDocument = doc;
@@ -130,8 +133,8 @@ ObjectFrame.prototype.loadJSONLDDocument = function (rdoc) {
130133
if(prop[0] == "@" || (typeof doc[prop] == "object" && Object.keys(doc[prop]).length == 0)) continue
131134
let cframe = this.getPropertyClassFrame(prop, doc)
132135
if (cframe && cframe.isClassChoice()) {
133-
cframe = cframe.getChosenClassFrame(FrameHelper.unshorten(doc[prop]["@type"]))
134-
if (!cframe) {
136+
//cframe = cframe.getChosenClassFrame(FrameHelper.unshorten(doc[prop]["@type"]), cframe.parent)
137+
if (!cframe) {
135138
console.log(`no choice frame ${doc[prop]["@type"]}`);
136139
}
137140
} else if (cframe && cframe.isLogic()) {
@@ -239,7 +242,13 @@ ObjectFrame.prototype.mfilter = function (rules, onmatch) {
239242
* second argument a class frame will be created from the instance frame.
240243
*/
241244
ObjectFrame.prototype.getPropertyClassFrame = function (prop, jsonlddoc) {
245+
246+
if (typeof prop == 'object') {
247+
return new ClassFrame(prop);
248+
}
249+
242250
prop = FrameHelper.unshorten(prop)
251+
243252
if (this.classframes && typeof this.classframes === 'object' && typeof this.classframes[prop] === 'object') {
244253
//console.log("returning " + prop, this.classframes[prop])
245254
return this.classframes[prop];
@@ -311,7 +320,7 @@ ObjectFrame.prototype.getPossibleContainedClasses = function () {
311320
if(frames[i].domain && cls.indexOf(frames[i].domain) == -1) cls.push(frames[i].domain)
312321
if(frames[i].frame){
313322
efcf(frames[i].frame)
314-
}
323+
}
315324
}
316325
}
317326
efcf(Object.values(this.classframes))
@@ -330,7 +339,7 @@ ObjectFrame.prototype.getDocumentLinks = function(){
330339
for(var l = 0; l<nvals.length; l++){
331340
if(vals.indexOf(nvals[l]) == -1){
332341
vals.push(nvals[l])
333-
}
342+
}
334343
}
335344
}
336345
else if(dval.isDocument()){
@@ -422,7 +431,7 @@ ObjectFrame.prototype.getChild = function (childid, prop) {
422431
};
423432

424433
ObjectFrame.prototype.addProperty = function (prop, cls) {
425-
prop = FrameHelper.unshorten(prop)
434+
if (typeof prop !== 'object') prop = FrameHelper.unshorten(prop)
426435
const cframe = this.getPropertyClassFrame(prop);
427436
var ndata = false;
428437
if(cframe){
@@ -446,11 +455,14 @@ ObjectFrame.prototype.addProperty = function (prop, cls) {
446455
nprop.addValueFrame(ndata);
447456
}
448457
if (typeof this.properties[prop] === 'undefined') {
449-
this.properties[prop] = nprop;
458+
if (typeof prop == 'object') var p = prop.property
459+
else var p = prop
460+
this.properties[p] = nprop;
461+
//this.properties[prop] = nprop;
450462
}
451-
else {
463+
//else {
452464
//this.properties[prop].push(nprop);
453-
}
465+
//}
454466
nprop.status = "new"
455467
return nprop;
456468
}
@@ -487,15 +499,12 @@ ObjectFrame.prototype.error = function (msg) {
487499
ObjectFrame.prototype.extract = function(){
488500
var extracts = {};
489501
for(var prop in this.properties){
490-
//if(!Array.isArray(this.properties[prop])) this.properties[prop] = [this.properties[prop]]
491-
//for(var i = 0; i<this.properties[prop].length; i++){
492-
var extracted = this.properties[prop].extract();
493-
if(!FrameHelper.empty(extracted)){
494-
if(typeof extracts[prop] == "undefined") extracts[prop] = [];
495-
extracts[prop] = extracts[prop].concat(extracted);
496-
}
497-
//}
498-
if(extracts[prop] && extracts[prop].length == 1) extracts[prop] = extracts[prop][0]
502+
var extracted = this.properties[prop].extract();
503+
if(!FrameHelper.empty(extracted)){
504+
if(typeof extracts[prop] == "undefined") extracts[prop] = [];
505+
extracts[prop] = extracts[prop].concat(extracted);
506+
}
507+
if(extracts[prop] && extracts[prop].length == 1) extracts[prop] = extracts[prop][0]
499508
}
500509
if(FrameHelper.empty(extracts) && this.parent){
501510
return false;
@@ -526,6 +535,9 @@ ObjectFrame.prototype.set = function(val){
526535
ObjectFrame.prototype.isObject = function () { return true; };
527536
ObjectFrame.prototype.isProperty = function () { return false; };
528537
ObjectFrame.prototype.isData = function () { return false; };
538+
ObjectFrame.prototype.isClassChoice = function () {
539+
return (this.frame && this.frame.type === 'class_choice');
540+
};
529541

530542

531543
ObjectFrame.prototype.subjectClass = function () {
@@ -655,7 +667,7 @@ ObjectFrame.prototype.pathToDoc = function (q){
655667
if(this.parent.parent){
656668
this.parent.parent.pathToDoc(q)
657669
}
658-
}
670+
}
659671
}
660672

661673
ObjectFrame.prototype.deleteQuery = function (){
@@ -712,9 +724,12 @@ PropertyFrame.prototype.addFrame = function (frame) {
712724
};
713725

714726
PropertyFrame.prototype.addValueFrame = function (oframe) {
715-
oframe.parent = this;
716-
oframe.index = this.values.length;
717-
this.values.push(oframe);
727+
if(oframe){
728+
oframe.parent = this;
729+
oframe.index = this.values.length;
730+
this.values.push(oframe);
731+
732+
}
718733
};
719734

720735
PropertyFrame.prototype.addValue = function (val) {
@@ -773,7 +788,7 @@ PropertyFrame.prototype.extract = function () {
773788
const hasVal = (val) => {
774789
if(val['@value']){
775790
for(var i = 0; i<extracts.length; i++){
776-
if(extracts[i]["@value"] && extracts[i]["@value"] == val["@value"]
791+
if(extracts[i]["@value"] && extracts[i]["@value"] == val["@value"]
777792
&& extracts[i]["@type"] && extracts[i]["@type"] == val["@type"]) return true
778793
}
779794
return false
@@ -786,6 +801,7 @@ PropertyFrame.prototype.extract = function () {
786801
return false
787802
}
788803
}
804+
789805
for (let i = 0; i < this.values.length; i++) {
790806
const val = this.values[i].extract();
791807
if (val !== '' && val !== false && typeof val !== 'undefined' && !hasVal(val)) extracts.push(val);
@@ -810,7 +826,8 @@ PropertyFrame.prototype.range = function () {
810826
};
811827
PropertyFrame.prototype.getLabel = function () {
812828
return (
813-
this.cframe ? this.cframe.getLabel() : '');
829+
//this.cframe ? this.cframe.getLabel() : '');
830+
this.cframe ? this.cframe.getLabel() : this.predicate.getLabel());
814831
};
815832
PropertyFrame.prototype.getComment = function () {
816833
return (this.cframe ? this.cframe.getComment() : false);
@@ -832,11 +849,18 @@ PropertyFrame.prototype.deletePropertyValue = function (value, index) {
832849
PropertyFrame.prototype.removeValue = function (value, index) {
833850
let nvals = []
834851
for(var i = 0; i<this.values.length; i++){
835-
if(this.values[i].get() != value){
852+
if(this.values[i].index != value.index){
836853
nvals.push(this.values[i])
837854
}
838855
}
839856
this.values = nvals
857+
/*let nvals = [] //trial
858+
for(var i = 0; i<this.values.length; i++){
859+
if(this.values[i].get() != value){
860+
nvals.push(this.values[i])
861+
}
862+
}
863+
this.values = nvals*/
840864
}
841865

842866

@@ -894,24 +918,22 @@ PropertyFrame.prototype.getAsFrames = function(){
894918

895919
PropertyFrame.prototype.createEmpty = function(){
896920
if (this.cframe.isData()) {
897-
const df = this.cframe.copy(this.subject());
921+
const df = this.cframe.copy(this.subject());
898922
df.set('');
899923
df.status = "new"
900-
console.log(df)
901924
return df;
902925
}
903926
else if (this.cframe.isObject()) {
904927
if (!this.cframe.isClassChoice()) {
905928
const df = this.cframe.createEmpty(FrameHelper.genBNID(FrameHelper.urlFragment(this.cframe.range)+ "_"));
906929
df.status = "new"
907930
return df;
908-
}
909-
const clss = this.cframe.getClassChoices();
910-
if (clss && clss.length) {
911-
const df = this.cframe.createEmptyChoice(clss[0], FrameHelper.genBNID(FrameHelper.urlFragment(clss[0])+ "_"));
912-
df.status = "new"
913-
return df;
914-
}
931+
}
932+
933+
const df =new ClassFrame(this.cframe)
934+
df.status = "new"
935+
return df;
936+
915937
}
916938
}
917939

@@ -1015,8 +1037,8 @@ function DataFrame(jsonld, parent, index) {
10151037
if(jsonld){
10161038
this.rangeValue = jsonld
10171039
if(jsonld['@type']) this.range = jsonld['@type']
1018-
if(jsonld['@language']) this.language = jsonld['@language']
1019-
if(!this.type) this.type = (jsonld['@value'] ? "datatypeProperty" : "objectProperty")
1040+
if(jsonld['@language']) this.language = jsonld['@language']
1041+
if(!this.type) this.type = (jsonld['@value'] ? "datatypeProperty" : "objectProperty")
10201042
}
10211043
return this
10221044
}
@@ -1140,7 +1162,7 @@ ObjectFrame.prototype.getComment = DataFrame.prototype.getComment;
11401162
DataFrame.prototype.error = function (msg) {
11411163
if (msg) this.err = msg;
11421164
if(!this.errors) this.errors = []
1143-
this.errors.push({"type": "Internal Data Frame Error", msg})
1165+
this.errors.push({"type": "Internal Data Frame Error", msg})
11441166
return this.err;
11451167
};
11461168

@@ -1333,7 +1355,7 @@ DataFrame.prototype.saveQuery = function (newval, ntype, nlang){
13331355
else {
13341356
upd = {"@value": newval}
13351357
upd["@type"] = ntype || this.range
1336-
if(nlang || this.language) upd["@language"] = nlang || this.language
1358+
if(nlang || this.language) upd["@language"] = nlang || this.language
13371359
}
13381360
let q
13391361
if(this.get() !== ""){
@@ -1358,15 +1380,25 @@ DataFrame.prototype.deleteQuery = function (){
13581380
/*
13591381
* Class frames represent the archetypal version of a property frame as returned by the class frame api
13601382
*/
1361-
function ClassFrame(frame, parent) {
1383+
function ClassFrame(frame, parent, label) {
13621384
this.err = false;
13631385
this.parent = parent;
13641386
// the id of the object that owns this dataframe
13651387
this.subjid = (parent ? parent.subjid : false);
13661388
if (frame) {
1389+
if(frame.label){
1390+
let dl = label ? label : ""
1391+
if(frame.label["@value"])
1392+
this.label=frame.label["@value"]
1393+
else if (frame.label)
1394+
this.label=frame.label
1395+
else this.label=dl
1396+
}
13671397
this.load(frame);
13681398
}
13691399
}
1400+
1401+
13701402
ClassFrame.prototype = DataFrame.prototype;
13711403

13721404
ClassFrame.prototype.load = function (frame) {
@@ -1380,7 +1412,6 @@ ClassFrame.prototype.load = function (frame) {
13801412
this.domain = frame.domain;
13811413
this.predicate = frame.property;
13821414
this.frame = frame.frame;
1383-
this.label = frame.label ? frame.label["@value"] : ""
13841415
this.comment = frame.comment ? frame.comment['@value'] : ""
13851416
this.range = frame.range;
13861417

@@ -1390,9 +1421,8 @@ ClassFrame.prototype.load = function (frame) {
13901421

13911422

13921423

1393-
//cf.loadFromJSONLD(jsonlddoc)
1424+
13941425
ClassFrame.prototype.loadFromJSONLD = function (jsonld, prop) {
1395-
prop = FrameHelper.shorten(prop)
13961426
if(jsonld[prop]){
13971427
this.predicate = FrameHelper.unshorten(prop)
13981428
this.type = (jsonld[prop]["@value"] ? 'datatypeProperty' : 'objectProperty')
@@ -1405,6 +1435,10 @@ ClassFrame.prototype.loadFromJSONLD = function (jsonld, prop) {
14051435
this.comment = ""//this.getComment()
14061436
}
14071437

1438+
ClassFrame.prototype.isClassChoice = function () {
1439+
return (this.frame && this.frame.type === 'class_choice')
1440+
};
1441+
14081442

14091443
ClassFrame.prototype.loadFromObjectFrame = function (par, child) {
14101444
// all the meta-data carried in frames:
@@ -1442,8 +1476,15 @@ ClassFrame.prototype.getChosenClassFrames = function (chosen) {
14421476
};
14431477

14441478
ClassFrame.prototype.createEmpty = function (newid) {
1479+
var objframe
14451480
if (this.isObject()) {
1446-
const objframe = new ObjectFrame(this.range);
1481+
if(this.isClassChoice()){
1482+
//objframe=new ObjectFrame(this.predicate, {}, this)
1483+
objframe=new ObjectFrame(this.predicate)
1484+
}
1485+
else {
1486+
objframe = new ObjectFrame(this.range);
1487+
}
14471488
if (newid) objframe.subjid = newid;
14481489
objframe.loadClassFrames(this.frame);
14491490
const fframe = objframe.fillFromSchema();
@@ -1472,35 +1513,30 @@ ClassFrame.prototype.clone = function (newid, other) {
14721513
return undefined;
14731514
};
14741515

1516+
14751517
ClassFrame.prototype.getClassChoices = function () {
14761518
const choices = [];
1477-
if (this.frame.operands) {
1478-
for (let i = 0; i < this.frame.operands.length; i += 1) {
1519+
//if (this.frame.operands) {
1520+
/*for (let i = 0; i < this.frame.operands.length; i += 1) {
14791521
for (let j = 0; j < this.frame.operands[i].length; j += 1) {
14801522
const domcls = this.frame.operands[i][j].domain;
14811523
if (domcls && choices.indexOf(domcls) === -1) {
14821524
choices.push(domcls);
14831525
}
14841526
}
1485-
}
1486-
}
1527+
}*/
1528+
//}
14871529
return choices;
14881530
};
14891531

1490-
ClassFrame.prototype.getChosenClassFrame = function (chosen) {
1491-
for (let i = 0; i < this.frame.operands.length; i += 1) {
1532+
1533+
ClassFrame.prototype.getChosenClassFrame = function (chosen, parent) {
1534+
for (let i = 0; i < this.frame.operands.length; i += 1) {
14921535
const operand = this.frame.operands[i];
1493-
if (operand.length) {
1494-
for (let j = 0; j < operand.length; j += 1) {
1495-
if (operand[j] && (chosen === operand[j].domain)) {
1496-
const cf = new ClassFrame(operand);
1497-
return cf;
1498-
}
1499-
}
1500-
} else if (operand.range && chosen === operand.range) {
1501-
const cf = new ClassFrame(operand);
1502-
return cf;
1503-
}
1536+
if(operand.class && chosen === operand.class){
1537+
const cf = new ClassFrame(operand, parent, this.label);
1538+
return cf;
1539+
}
15041540
}
15051541
return false;
15061542
};

0 commit comments

Comments
 (0)