Skip to content

Commit 37c8029

Browse files
preparing and cleaning libraries for release
1 parent be0edb5 commit 37c8029

18 files changed

+1199
-832
lines changed

dist/terminus-client.min.js

Lines changed: 81 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/terminus-client.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
'use strict';
21
module.exports = {
32
WOQLClient:require('./lib/woqlClient'),
43
ConnectionCapabilities:require('./lib/connectionCapabilities'),
54
ConnectionConfig:require('./lib/connectionConfig'),
65
IDParser:require('./lib/terminusIDParser'),
76
ErrorMessage:require('./lib/errorMessage'),
8-
FrameHelper:require('./lib/frameHelper'),
9-
ObjectFrame:require('./lib/objectFrame'),
7+
UTILS:require('./lib/utils'),
8+
TerminusDocument:require('./lib/document'),
109
WOQLResult:require('./lib/woqlResult'),
10+
WOQLRule:require('./lib/woqlRule'),
11+
FrameRule:require('./lib/frameRule'),
1112
WOQL:require('./lib/woql')};

lib/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

lib/dispatchRequest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @license Apache Version 2
44
* @summary Functions for dispatching API requests via the axios library.
55
*/
6+
67
const axios = require('axios');
78
const UTILS = require('./utils.js');
89
const CONST = require('./const.js');

lib/objectFrame.js renamed to lib/document.js

Lines changed: 195 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,194 @@
1+
/**
2+
* @file Javascript Terminus Document Classes
3+
* @license Apache Version 2
4+
* @summary Helper classes for accessing documents returned by the Terminus DB API programmatically
5+
*
6+
* Usage: let doc = new TerminusDocument(client);
7+
*
8+
* These set the objects document property and return promises:
9+
*
10+
* doc.loadDocument(URL) //.then(() => console.log(this.document));
11+
* doc.loadComplete(URL, CLS) // .then(() => console.log(this.document))
12+
* doc.loadSchema(cls) //.then(() => console.log(this.document))
13+
*
14+
* These just set the object's document property
15+
* doc.loadJSON(json_frames, cls) //console.log(this.document)
16+
* doc.loadDataFrames(json_frames, cls)
17+
* doc.loadClassFrames(json_frames, cls)
18+
*
19+
*/
20+
const WOQLClient = require('./woqlClient');
21+
const FrameHelper = require('./utils');
22+
23+
24+
/**
25+
*
26+
* @param {WOQLClient} [client] - woql client object to reuse for loading the doucument
27+
* @summary creates a Terminus Document Object for loading documents from the Terminus Document API
28+
*/
29+
function TerminusDocument(client){
30+
this.client = client;
31+
if(!this.client){
32+
this.client = new WOQLClient();
33+
}
34+
}
35+
36+
TerminusDocument.prototype.db = function(dburl){
37+
this.client.connectionConfig.setDB(dburl);
38+
return this;
39+
}
40+
41+
42+
43+
/**
44+
* @param {String} url - loads the document frame from the document API in frame form
45+
* @summary - loads the frame encapsulates more meta-data in the json and reduces the number of api calls we need to make
46+
* @returns {Promise}
47+
*/
48+
TerminusDocument.prototype.loadDocument = function(url, encoding){
49+
encoding = encoding || "terminus:frame";
50+
return this.client.getDocument(url, {"terminus:encoding": encoding})
51+
.then((response) => (encoding == "terminus:frame" ? this.loadDataFrames(response) : this.loadJSON(response)));
52+
}
53+
54+
TerminusDocument.prototype.loadSchema = function(cls, dbURL){
55+
var ncls = FrameHelper.unshorten(cls);
56+
return this.client.getClassFrame(dbURL, ncls)
57+
.then((response) => this.loadSchemaFrames(response, ncls));
58+
}
59+
60+
/**
61+
* @param {String} url - loads the document frame along with its class frame
62+
* @param {String} [cls] - optional class id of the document - if absent class frames will be loaded from the document class once it is loaded
63+
* @returns {Promise}
64+
* @summary - loads a document frame and it's class frame in unison
65+
*/
66+
67+
TerminusDocument.prototype.loadComplete = function(url, cls){
68+
if(cls){
69+
return Promise.all([this.loadDocument(url), this.loadDocumentSchema(cls)]);
70+
}
71+
else {
72+
return this.client.getDocument(url, {"terminus:encoding": "terminus:frame"})
73+
.then((response) => this.loadDocumentSchema(this.document.cls));
74+
}
75+
}
76+
77+
TerminusDocument.prototype.loadJSON = function(json, type){
78+
if(this.docid){
79+
return this.loadDocument(this.docid);
80+
}
81+
else if(this.clsid){
82+
return this.loadDocumentSchema(this.clsid);
83+
}
84+
console.error("Either docid or clid must be set before load is called")
85+
}
86+
87+
TerminusDocument.prototype.loadDataFrames = function(dataframes, cls, classframes){
88+
if(!cls){
89+
if(this.document) cls = this.document.cls;
90+
else {
91+
if(dataframes && dataframes.length && dataframes[0] && dataframes[0].domain){
92+
cls = dataframes[0].domain;
93+
}
94+
}
95+
}
96+
if(cls){
97+
if(!this.document){
98+
this.document = new TerminusClient.ObjectFrame(cls, dataframes, classframes);
99+
}
100+
else {
101+
this.document.loadDataFrames(dataframes);
102+
}
103+
}
104+
else {
105+
console.log("Missing Class" + " " + "Failed to add dataframes due to missing class");
106+
}
107+
}
108+
109+
TerminusDocument.prototype.loadSchemaFrames = function(classframes, cls){
110+
if(!cls){
111+
if(classframes && classframes.length && classframes[0] && classframes[0].domain){
112+
cls = classframes[0].domain;
113+
}
114+
}
115+
if(cls){
116+
if(!this.document){
117+
this.document = new TerminusClient.ObjectFrame(cls);
118+
}
119+
if(classframes){
120+
this.document.loadClassFrames(classframes);
121+
if(!this.document.subjid){
122+
this.document.newDoc = true;
123+
this.document.fillFromSchema("_:");
124+
}
125+
}
126+
}
127+
else {
128+
console.log("Missing Class", "Failed to add class frames due to missing both class and classframes");
129+
}
130+
}
131+
132+
TerminusDocument.prototype.render = function(){
133+
if(!this.renderer && this.document){
134+
if(this.config.renderer()){
135+
this.renderer = this.config.renderer();
136+
}
137+
else {
138+
this.applyRulesToDocument(this.document, this.config);
139+
this.renderer = this.document;
140+
}
141+
}
142+
if(this.renderer && this.renderer.render){
143+
return this.renderer.render(this.document);
144+
}
145+
else if(typeof this.renderer == "function"){
146+
var x = this.renderer(this.document);
147+
return x;
148+
}
149+
else {
150+
console.log("didna make it - no render function for document");
151+
}
152+
}
153+
154+
/*
155+
* adds render and compare functions to object frames
156+
*/
157+
TerminusDocument.prototype.applyRulesToDocument = function(doc, config){
158+
var self = this;
159+
function onmatch(frame, rule){
160+
if(typeof rule.render() != "undefined"){
161+
frame.render = rule.render();
162+
}
163+
else {
164+
if(rule.renderer()){
165+
var renderer = self.loadRenderer(rule.renderer(), frame, rule.args);
166+
}
167+
if(renderer && renderer.render){
168+
frame.render = function(frame){
169+
return renderer.render(frame);
170+
}
171+
}
172+
}
173+
if(rule.compare()){
174+
frame.compare = rule.compare();
175+
}
176+
config.setFrameDisplayOptions(frame, rule);
177+
}
178+
if(doc.render){
179+
function clearDisplay(frame, rule){
180+
delete(frame['render']);
181+
delete(frame['display_options']);
182+
frame.compare = TerminusClient.ObjectFrame.prototype.compare;
183+
}
184+
var nconfig = TerminusClient.WOQL.document();
185+
nconfig.all();
186+
doc.filter(nconfig.rules, clearDisplay);
187+
}
188+
doc.filter(config.rules, onmatch);
189+
}
190+
191+
1192
/**
2193
* Represents a frame for programmatic access to object frame, anywhere within a document
3194
* Recursive data structure where this.children contains an indexed array of object frames
@@ -11,8 +202,6 @@
11202
* @param parent parent object
12203
* @returns
13204
*/
14-
const FrameHelper = require('./frameHelper');
15-
const FramePattern = require('./framePattern');
16205

17206
function ObjectFrame(cls, dataframes, classframes, parent, parentframe) {
18207
// the class of the frame - mandatory
@@ -168,7 +357,7 @@ ObjectFrame.prototype.clear = function () {
168357
*/
169358
ObjectFrame.prototype.filter = function (rules, onmatch, options) {
170359
const cascading = (options && options.ignore_failure ? true : false);
171-
var hits = FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
360+
//var hits = FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
172361
//if(hits.length || cascading){
173362
for (const prop of Object.keys(this.properties)) {
174363
this.properties[prop].filter(rules, onmatch, options);
@@ -650,7 +839,7 @@ PropertyFrame.prototype.createEmpty = function(){
650839

651840
PropertyFrame.prototype.filter = function (rules, onmatch, options) {
652841
const cascading = (options && options.ignore_failure ? true : false);
653-
var hits = FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
842+
//var hits = FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
654843
//if(hits.length || cascading){
655844
for (var i = 0; i<this.values.length; i++){
656845
this.values[i].filter(rules, onmatch, options);
@@ -760,7 +949,7 @@ DataFrame.prototype.load = function (frame) {
760949
};
761950

762951
DataFrame.prototype.filter = function (rules, onmatch) {
763-
FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
952+
//FramePattern.FramePatternMatcher.testRules(rules, this, onmatch);
764953
return this;
765954
};
766955

@@ -1166,4 +1355,4 @@ Restriction.prototype.loadRestriction = function (restriction) {
11661355
}
11671356
};
11681357

1169-
module.exports = ObjectFrame;
1358+
module.exports = TerminusDocument;

0 commit comments

Comments
 (0)