-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdocumentFrame.js
206 lines (187 loc) · 6.32 KB
/
documentFrame.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
/* eslint-disable no-unused-vars */
/* eslint-disable consistent-return */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-param-reassign */
const ObjectFrame = require('./objectFrame');
const FrameConfig = require('./frameConfig');
const FrameHelper = require('../utils');
/**
* @file Document Frame
* @license Apache Version 2
*/
function DocumentFrame(client, config) {
this.client = client;
this.config = (config || new FrameConfig());
this.load_schema = false;
}
DocumentFrame.prototype.options = function (opts) {
this.config = opts;
return this;
};
DocumentFrame.prototype.db = function (dburl) {
this.client.db(dburl);
return this;
};
/**
* @param {String} url - loads the document frame from the document API in frame form
* - loads the frame encapsulates more meta-data in the json and reduces the
* number of api calls we need to make
* @returns {Promise}
*/
DocumentFrame.prototype.loadDocument = function (url, encoding) {
encoding = encoding || 'system:frame';
return this.client.getDocument(url, { 'system:encoding': encoding })
.then((response) => {
(encoding === 'system:frame' ? this.loadDataFrames(response) : this.loadJSON(response));
});
};
DocumentFrame.prototype.loadSchema = function (cls, dbURL) {
const ncls = FrameHelper.unshorten(cls);
return this.client.getClassFrame(dbURL, ncls)
.then((response) => this.loadSchemaFrames(response, ncls));
};
/**
* @param {String} url - loads the document frame along with its class frame
* @param {String} [cls] - optional class id of the document - if absent class frames
* will be loaded from the document class once it is loaded
* @returns {Promise}
* - loads a document frame and it's class frame in unison
*/
DocumentFrame.prototype.loadComplete = function (url, cls) {
if (cls) {
return Promise.all([this.loadDocument(url), this.loadDocumentSchema(cls)]);
}
return this.loadDocument(url)
.then(() => {
this.loadSchema(this.document.cls);
});
};
DocumentFrame.prototype.loadJSON = function (json, type) {
if (this.docid) {
return this.loadDocument(this.docid);
}
if (this.clsid) {
return this.loadDocumentSchema(this.clsid);
}
// eslint-disable-next-line no-console
console.error('Either docid or clid must be set before load is called');
};
DocumentFrame.prototype.loadData = function (jsonld, cls, classframes) {
if (!cls) {
if (this.document) cls = this.document.cls;
else if (jsonld && jsonld['@type']) {
cls = jsonld['@type'];
}
}
if (cls) {
if (!this.document) {
this.document = new ObjectFrame(cls, jsonld, classframes);
} else {
this.document.loadJSONLDDocument(jsonld);
}
} else {
// eslint-disable-next-line no-console
console.log('Missing Class Failed to add dataframes due to missing class');
}
};
DocumentFrame.prototype.load = function (classframes, doc) {
this.document = new ObjectFrame(doc['@type'], doc, classframes);
};
DocumentFrame.prototype.loadSchemaFrames = function (classframes, cls) {
if (!cls) {
if (classframes && classframes.length && classframes[0] && classframes[0].domain) {
cls = classframes[0].domain;
}
}
if (cls) {
if (!this.document) {
this.document = new ObjectFrame(cls);
}
if (classframes) {
this.document.loadClassFrames(classframes);
if (!this.document.subjid) {
this.document.newDoc = true;
this.document.fillFromSchema(FrameHelper.genBNID(`${FrameHelper.urlFragment(cls)}_`));
}
}
} else {
// eslint-disable-next-line no-console
console.log('Missing Class', 'Failed to add class frames due to missing both class and classframes');
}
};
DocumentFrame.prototype.filterFrame = function (loadRenderer) {
const myfilt = function (frame, rule) {
if (typeof rule.render() !== 'undefined') {
frame.render = rule.render();
}
/* else {
if(rule.renderer()){
var renderer = loadRenderer(rule.renderer(), frame, rule.args);
}
if(renderer){
frame.render = function(fframe){
return renderer(fframe);
}
}
} */
if (rule.compare()) {
frame.compare = rule.compare();
}
if (rule.errors()) {
frame.errors = frame.errors ? frame.errors.concat(rule.errors()) : rule.errors();
} else if (rule.errors() === false) delete frame.errors;
};
this.applyRules(false, false, myfilt);
};
DocumentFrame.prototype.setErrors = function (errors, frameconf) {
this.clearErrors(frameconf);
// eslint-disable-next-line no-plusplus
for (let i = 0; i < errors.length; i++) {
// eslint-disable-next-line no-use-before-define
addRuleForVio(frameconf, errors[i]);
}
const myfilt = function (frame, rule) {
if (rule.errors()) {
frame.errors = frame.errors ? frame.errors.concat(rule.errors()) : rule.errors();
}
};
this.applyRules(false, frameconf, myfilt);
};
DocumentFrame.prototype.clearErrors = function (frameconf) {
frameconf.all();
const myfilt = function (frame, rule) {
if (frame.errors) delete (frame.errors);
};
this.applyRules(false, frameconf, myfilt);
frameconf.rules = [];
};
function addRuleForVio(docview, error) {
const prop = (error['vio:property'] ? error['vio:property']['@value'] : false);
const subj = (error['vio:subject'] ? error['vio:subject']['@value'] : false);
const msg = (error['vio:message'] ? error['vio:message']['@value'] : false);
let val = (error['api:value'] ? error['api:value'] : false);
if (val && val[0] === '"' && val[val.length - 1] === '"') val = val.substring(1, val.length - 1);
const type = (error['api:type'] ? error['api:type'] : false);
if (type && val) { // api:BadCast
docview.data().value(val).type(type).errors([error]);
}
if (prop && subj) { // untypedInstanceViolation
const shrt = FrameHelper.shorten(subj);
if (shrt.substring(0, 5) === 'woql:') shrt === shrt.substring(5);
docview.data().property(prop).value(shrt, subj).errors([error]);
}
}
/* "
* adds render and compare functions to object frames
*/
DocumentFrame.prototype.applyRules = function (doc, config, mymatch) {
doc = doc || this.document;
if (!doc) return;
config = (config || this.config);
const onmatch = function (frame, rule) {
config.setFrameDisplayOptions(frame, rule);
if (mymatch) mymatch(frame, rule);
};
doc.mfilter(config.rules, onmatch);
};
module.exports = DocumentFrame;