|
| 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 | + |
1 | 192 | /**
|
2 | 193 | * Represents a frame for programmatic access to object frame, anywhere within a document
|
3 | 194 | * Recursive data structure where this.children contains an indexed array of object frames
|
|
11 | 202 | * @param parent parent object
|
12 | 203 | * @returns
|
13 | 204 | */
|
14 |
| -const FrameHelper = require('./frameHelper'); |
15 |
| -const FramePattern = require('./framePattern'); |
16 | 205 |
|
17 | 206 | function ObjectFrame(cls, dataframes, classframes, parent, parentframe) {
|
18 | 207 | // the class of the frame - mandatory
|
@@ -168,7 +357,7 @@ ObjectFrame.prototype.clear = function () {
|
168 | 357 | */
|
169 | 358 | ObjectFrame.prototype.filter = function (rules, onmatch, options) {
|
170 | 359 | 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); |
172 | 361 | //if(hits.length || cascading){
|
173 | 362 | for (const prop of Object.keys(this.properties)) {
|
174 | 363 | this.properties[prop].filter(rules, onmatch, options);
|
@@ -650,7 +839,7 @@ PropertyFrame.prototype.createEmpty = function(){
|
650 | 839 |
|
651 | 840 | PropertyFrame.prototype.filter = function (rules, onmatch, options) {
|
652 | 841 | 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); |
654 | 843 | //if(hits.length || cascading){
|
655 | 844 | for (var i = 0; i<this.values.length; i++){
|
656 | 845 | this.values[i].filter(rules, onmatch, options);
|
@@ -760,7 +949,7 @@ DataFrame.prototype.load = function (frame) {
|
760 | 949 | };
|
761 | 950 |
|
762 | 951 | DataFrame.prototype.filter = function (rules, onmatch) {
|
763 |
| - FramePattern.FramePatternMatcher.testRules(rules, this, onmatch); |
| 952 | + //FramePattern.FramePatternMatcher.testRules(rules, this, onmatch); |
764 | 953 | return this;
|
765 | 954 | };
|
766 | 955 |
|
@@ -1166,4 +1355,4 @@ Restriction.prototype.loadRestriction = function (restriction) {
|
1166 | 1355 | }
|
1167 | 1356 | };
|
1168 | 1357 |
|
1169 |
| -module.exports = ObjectFrame; |
| 1358 | +module.exports = TerminusDocument; |
0 commit comments