|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -var extend = require('node.extend'); |
4 | | -var Source = require('./SDD.Source'); |
5 | | -var Table = require('./SDD.Table'); |
6 | 3 | var Utils = require('./Utils'); |
| 4 | +var req_browser_ignore = require; |
7 | 5 |
|
8 | | -var P = exports.P = Utils.create(Source.P); // public methods, inherit from base Source class |
9 | | - |
10 | | -exports.D = extend(true, {}, Source.D, { |
11 | | - // default object properties |
12 | | - 'cfg': { |
13 | | - 'cache': true, |
14 | | - 'datatype': 'json', |
15 | | - 'timeout': 0 |
16 | | - }, |
17 | | - 'jsonfiles': {} |
18 | | -}); |
19 | | - |
20 | | -exports.Create = function(config) { |
21 | | - var obj = Utils.create(P); |
22 | | - extend(true, obj, exports.D); |
23 | | - obj.Config(config); |
24 | | - return obj; |
25 | | -}; |
26 | | - |
27 | | -P.Config = function(config) { |
28 | | - extend(this.cfg, config); |
29 | | -}; |
30 | | - |
31 | | -function MetainfDone(data, status, jqxhr, p, ctx) { |
32 | | - var tbl, |
33 | | - newt, |
34 | | - i; |
35 | | - |
36 | | - if (!data) return p.rejectWith(ctx, [this, 'error', 'invalid data object']); |
37 | | - if (!data.hasOwnProperty('formatID') || data['formatID'] != '1') return p.rejectWith(ctx, [this, 'error', 'unknown data format']); |
38 | | - if (!data.hasOwnProperty('schema') || !data.hasOwnProperty('version')) return p.rejectWith(ctx, [this, 'error', 'data has no version information']); |
39 | | - if (!data.hasOwnProperty('tables') || !data.hasOwnProperty('tables')) return p.rejectWith(ctx, [this, 'error', 'data has no table information']); |
40 | | - this.version = data['version']; |
41 | | - this.schema = data['schema']; |
42 | | - if (data.hasOwnProperty('verdesc')) this.verdesc = data['verdesc']; |
43 | | - |
44 | | - // reset stuff |
45 | | - this.tables = {}; |
46 | | - this.jsonfiles = {}; |
47 | | - |
48 | | - for (tbl in data['tables']) { |
49 | | - if (!data['tables'].hasOwnProperty(tbl)) continue; |
50 | | - |
51 | | - // create a new table from our metadata |
52 | | - newt = Table.Create(tbl, this, data['tables'][tbl]); |
53 | | - this.tables[newt.name] = newt; |
54 | | - |
55 | | - // collect a list of json sources |
56 | | - for (i = 0; i < newt.segments.length; i++) { |
57 | | - if (this.jsonfiles.hasOwnProperty(newt.segments[i].tag)) continue; |
58 | | - this.jsonfiles[newt.segments[i].tag] = {'loaded': false, 'p': null}; |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - p.resolveWith(ctx, [this]); |
63 | | -} |
64 | | - |
65 | | -function MetainfFail(jqxhr, status, error, p, ctx) { |
66 | | - p.rejectWith(ctx, [this, status, error]); |
67 | | -} |
68 | | - |
69 | | -P.LoadMeta = function(ctx) { |
70 | | - var self = this, |
71 | | - p = Utils.deferred() |
72 | | - ; |
73 | | - |
74 | | - if (!this.cfg.hasOwnProperty('path') || typeof this.cfg['path'] != 'string') { |
75 | | - return p.rejectWith(ctx, [this, 'error', 'path is required']).promise(); |
76 | | - } |
77 | | - if (this.cfg['datatype'] != 'json' && this.cfg['datatype'] != 'jsonp') { |
78 | | - return p.rejectWith(ctx, [this, 'error', 'invalid datatype: ' + this.cfg['datatype']]).promise(); |
79 | | - } |
80 | | - |
81 | | - Utils.ajax({ |
82 | | - 'dataType': this.cfg['datatype'], |
83 | | - 'cache': this.cfg['cache'], |
84 | | - 'jsonp': false, |
85 | | - 'timeout': this.cfg['timeout'], |
86 | | - 'jsonpCallback': 'EVEoj_metainf_callback', |
87 | | - 'url': this.cfg['path'] + '/metainf.' + this.cfg['datatype'] |
88 | | - }).done(function (data, status, jqxhr) { |
89 | | - MetainfDone.apply(self, [data, status, jqxhr, p, ctx]); |
90 | | - }).fail(function (jqxhr, status, error) { |
91 | | - MetainfFail.apply(self, [jqxhr, status, error, p, ctx]); |
92 | | - }); |
93 | | - |
94 | | - return p.promise(); |
95 | | -}; |
96 | | - |
97 | | -function LoadFileDone(ctx, jsf, data) { |
98 | | - if (!data || !data.hasOwnProperty('tables')) { |
99 | | - this.jsonfiles[jsf].p.rejectWith(ctx, [jsf, 'error', 'invalid data object']); |
100 | | - } |
101 | | - else if (!data.hasOwnProperty('formatID') || data['formatID'] != '1') { |
102 | | - this.jsonfiles[jsf].p.rejectWith(ctx, [jsf, 'error', 'unknown data format']); |
103 | | - } |
104 | | - else { |
105 | | - this.jsonfiles[jsf].loaded = true; |
106 | | - this.jsonfiles[jsf].data = data; |
107 | | - this.jsonfiles[jsf].p.resolveWith(ctx, [jsf, data]); |
108 | | - } |
| 6 | +if (Utils.isBrowser) { |
| 7 | + module.exports = require('./SDD.Source.json_browser.js'); |
109 | 8 | } |
110 | | -function LoadFileFail(ctx, jsf, status, error) { |
111 | | - this.jsonfiles[jsf].p.rejectWith(ctx, [jsf, status, error]); |
| 9 | +else { |
| 10 | + module.exports = req_browser_ignore('./SDD.Source.json_node.js'); |
112 | 11 | } |
113 | | -P.LoadTag = function(jsf, ctx) { |
114 | | - var self = this; |
115 | | - if (this.jsonfiles[jsf].loaded) { |
116 | | - return Utils.deferred().resolveWith(null, [jsf, this.jsonfiles[jsf].data]).promise(); |
117 | | - } |
118 | | - else if (this.jsonfiles[jsf].p != null) { |
119 | | - return this.jsonfiles[jsf].p.promise(); |
120 | | - } |
121 | | - else { |
122 | | - this.jsonfiles[jsf].p = Utils.deferred(); |
123 | | - Utils.ajax({ |
124 | | - 'dataType': this.cfg['datatype'], |
125 | | - 'cache': this.cfg['cache'], |
126 | | - 'jsonp': false, |
127 | | - 'timeout': this.cfg['timeout'], |
128 | | - 'jsonpCallback': 'EVEoj_' + jsf + '_callback', |
129 | | - 'url': this.cfg['path'] + '/' + jsf + '.' + this.cfg['datatype'] |
130 | | - }) |
131 | | - .done(function (data, status, jqxhr) { LoadFileDone.apply(self, [ctx, jsf, data]) }) |
132 | | - .fail(function (jqxhr, status, error) { LoadFileFail.apply(self, [ctx, jsf, status, error]) }); |
133 | | - return this.jsonfiles[jsf].p.promise(); |
134 | | - } |
135 | | -}; |
0 commit comments