forked from nezroy/EVEoj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDD.Source.json_browser.js
More file actions
128 lines (112 loc) · 3.38 KB
/
Copy pathSDD.Source.json_browser.js
File metadata and controls
128 lines (112 loc) · 3.38 KB
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
var extend = require("node.extend"),
Source = require("./SDD.Source"),
Table = require("./SDD.Table"),
Utils = require("./Utils"),
Promise = require("./Promise");
var P = exports.P = Utils.create(Source.P); // public methods, inherit from base Source class
exports.D = extend(true, {}, Source.D, {
// default object properties
cfg: {
cache: true,
datatype: "json",
timeout: 0
},
jsonfiles: {}
});
exports.Create = function(config) {
var obj = Utils.create(P);
extend(true, obj, exports.D);
obj.Config(config);
return obj;
};
P.Config = function(config) {
extend(this.cfg, config);
};
function MetainfDone(data, ctx) {
var tbl,
newt,
i;
if (!data) return Promise.reject(new Error("invalid data object"));
if (!data.hasOwnProperty("formatID") || data.formatID != "1") return Promise.reject(new Error("unknown data format"));
if (!data.hasOwnProperty("schema") || !data.hasOwnProperty("version")) return Promise.reject(new Error("data has no version information"));
if (!data.hasOwnProperty("tables") || !data.hasOwnProperty("tables")) return Promise.reject(new Error("data has no table information"));
this.version = data.version;
this.schema = data.schema;
if (data.hasOwnProperty("verdesc")) this.verdesc = data.verdesc;
// reset stuff
this.tables = {};
this.jsonfiles = {};
for (tbl in data.tables) {
if (!data.tables.hasOwnProperty(tbl)) continue;
// create a new table from our metadata
newt = Table.Create(tbl, this, data.tables[tbl]);
this.tables[newt.name] = newt;
// collect a list of json sources
for (i = 0; i < newt.segments.length; i++) {
if (this.jsonfiles.hasOwnProperty(newt.segments[i].tag)) continue;
this.jsonfiles[newt.segments[i].tag] = {
loaded: false,
p: null
};
}
}
return Promise.resolve({
context: ctx,
source: this
});
}
P.LoadMeta = function(ctx) {
var self = this;
if (!this.cfg.hasOwnProperty("path") || typeof this.cfg.path != "string") {
return Promise.reject(new Error("path is required"));
}
if (this.cfg.datatype != "json" && this.cfg.datatype != "jsonp") {
return Promise.reject(new Error("invalid datatype: " + this.cfg.datatype));
}
return Utils.ajaxP(this.cfg.path + "/metainf." + this.cfg.datatype, {
dataType: this.cfg.datatype,
cache: this.cfg.cache,
jsonp: false,
timeout: this.cfg.timeout
}).then(function(data) {
return MetainfDone.apply(self, [data, ctx]);
});
};
function LoadFileDone(res, rej, ctx, jsf, data) {
if (!data || !data.hasOwnProperty("tables")) {
return rej(new Error("invalid data object"));
} else if (!data.hasOwnProperty("formatID") || data.formatID != "1") {
return rej(new Error("unknown data format"));
} else {
this.jsonfiles[jsf].loaded = true;
this.jsonfiles[jsf].data = data;
return res({
context: ctx,
tag: jsf,
data: data
});
}
}
P.LoadTag = function(jsf, ctx) {
var self = this;
if (this.jsonfiles[jsf].loaded) {
return Promise.resolve({
tag: jsf,
data: this.jsonfiles[jsf].data
});
} else if (this.jsonfiles[jsf].p !== null) {
return this.jsonfiles[jsf].p;
} else {
this.jsonfiles[jsf].p = new Promise(function(res, rej) {
Utils.ajaxP(self.cfg.path + "/" + jsf + "." + self.cfg.datatype, {
dataType: self.cfg.datatype,
cache: self.cfg.cache,
jsonp: false,
timeout: self.cfg.timeout
}).then(function(data) {
LoadFileDone.apply(self, [res, rej, ctx, jsf, data]);
});
});
return this.jsonfiles[jsf].p;
}
};