|
| 1 | +// data source based on JSON files |
| 2 | +var EVE-Oj = EVE-Oj || {}; |
| 3 | +EVE-Oj.data.src_json = EVE-Oj.data.src_json || Object.create(EVE-Oj.data.src_base); |
| 4 | +(function ($) { |
| 5 | + var ME = EVE-Oj.data.src_json, |
| 6 | + E = EVE-Oj, |
| 7 | + D = EVE-Oj.data, |
| 8 | + _LoadFileDone, |
| 9 | + _LoadFileFail |
| 10 | + ; |
| 11 | + |
| 12 | + $.extend(true, ME.c, { |
| 13 | + // extend default config |
| 14 | + 'cache': true, |
| 15 | + 'datatype': 'json' |
| 16 | + }); |
| 17 | + ME.metaInf = null; |
| 18 | + ME.jsonfiles = {}; |
| 19 | + |
| 20 | + ME.SetConfig = function(p, config, ctx) { |
| 21 | + var that = this; |
| 22 | + if (!config.hasOwnProperty('path') || typeof config['path'] != 'string') { |
| 23 | + p.rejectWith(ctx, [this, 'error', 'path is required']); |
| 24 | + return; |
| 25 | + } |
| 26 | + $.extend(true, this.c, config); |
| 27 | + if (!this.c['datatype'] != 'json' && this.c['datatype'] != 'jsonp') { |
| 28 | + p.rejectWith(ctx, [this, 'error', 'invalid datatype: ' + this.c['datatype']]); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + $.ajax({ |
| 33 | + 'dataType': this.c['datatype'], |
| 34 | + 'cache': this.c['cache'], |
| 35 | + 'jsonp': false, |
| 36 | + 'jsonpCallback': 'EVE-Oj_metainf_callback', |
| 37 | + 'url': this.c['path'] + '/metainf.' + this.c['datatype'] |
| 38 | + }).done(function (data, status, jqxhr) { |
| 39 | + that.metainfDone(data, status, jqxhr, p, ctx); |
| 40 | + }).fail(function (jqxhr, status, error) { |
| 41 | + that.metainfFail(jqxhr, status, error, p, ctx); |
| 42 | + }); |
| 43 | + }; |
| 44 | + |
| 45 | + ME.metainfDone = function (data, status, jqxhr, p, ctx) { |
| 46 | + var tbl, |
| 47 | + newt, |
| 48 | + i; |
| 49 | + |
| 50 | + if (!data) return p.rejectWith(ctx, [this, 'error', 'invalid data object']); |
| 51 | + if (!data.hasOwnProperty('formatID') || data['formatID'] != '1') return p.rejectWith(ctx, [this, 'error', 'unknown data format']); |
| 52 | + if (!data.hasOwnProperty('schema') || !data.hasOwnProperty('version')) return p.rejectWith(ctx, [this, 'error', 'data has no version information']); |
| 53 | + if (!data.hasOwnProperty('tables') || !data.hasOwnProperty('tables')) return p.rejectWith(ctx, [this, 'error', 'data has no table information']); |
| 54 | + |
| 55 | + // reset stuff |
| 56 | + // this.metaInf = data; |
| 57 | + this.tables = {}; |
| 58 | + this.jsonfiles = {}; |
| 59 | + |
| 60 | + for (tbl in data['tables']) { |
| 61 | + if (!data['tables'].hasOwnProperty(tbl)) continue; |
| 62 | + |
| 63 | + // create a new table from our metadata |
| 64 | + newt = D.Table.Create(tbl, this, data['tables'][tbl]); |
| 65 | + this.tables[newt.name] = newt; |
| 66 | + |
| 67 | + // collect a list of json sources |
| 68 | + for (i = 0; i < newt.segments.length; i++) { |
| 69 | + if (this.jsonfiles.hasOwnProperty(newt.segments[i].tag)) continue; |
| 70 | + this.jsonfiles[newt.segments[i].tag] = {'loaded': false, 'p': null}; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + p.resolveWith(ctx, [this]); |
| 75 | + }; |
| 76 | + |
| 77 | + ME.metainfFail = function (jqxhr, status, error, p, ctx) { |
| 78 | + p.rejectWith(ctx, [this, status, error]); |
| 79 | + }; |
| 80 | + |
| 81 | + ME.GetMetainf = function (prop) { |
| 82 | + if (!this.metaInf) return null; |
| 83 | + if (!this.metaInf.hasOwnProperty(prop)) return null; |
| 84 | + return this.metaInf[prop]; |
| 85 | + }; |
| 86 | + |
| 87 | + ME.GetVersion = function () { |
| 88 | + return this.GetMetainf('version'); |
| 89 | + }; |
| 90 | + |
| 91 | + ME.GetVerdesc = function () { |
| 92 | + return this.GetMetainf('verdesc'); |
| 93 | + }; |
| 94 | + |
| 95 | + ME.GetSchema = function () { |
| 96 | + return this.GetMetainf('schema'); |
| 97 | + }; |
| 98 | + |
| 99 | + _LoadFileDone = function(jsf, data) { |
| 100 | + if (!data || !data.hasOwnProperty('tables')) { |
| 101 | + this.jsonfiles[jsf].p.reject(jsf, 'error', 'invalid data object'); |
| 102 | + } |
| 103 | + else if (!data.hasOwnProperty('formatID') || data['formatID'] != '1') { |
| 104 | + this.jsonfiles[jsf].p.reject(jsf, 'error', 'unknown data format'); |
| 105 | + } |
| 106 | + else { |
| 107 | + this.jsonfiles[jsf].loaded = true; |
| 108 | + this.jsonfiles[jsf].data = data; |
| 109 | + this.jsonfiles[jsf].p.resolve(jsf, data); |
| 110 | + } |
| 111 | + }; |
| 112 | + _LoadFileFail = function(jsf, status, error) { |
| 113 | + this.jsonfiles[jsf].p.reject(jsf, status, error); |
| 114 | + }; |
| 115 | + ME.LoadFile = function(jsf) { |
| 116 | + var p, |
| 117 | + self = this; |
| 118 | + if (this.jsonfiles[jsf].loaded) { |
| 119 | + return $.Deferred().resolveWith(null, [jsf, this.jsonfiles[jsf].data]).promise(); |
| 120 | + } |
| 121 | + else if (this.jsonfiles[jsf].p != null) { |
| 122 | + return this.jsonfiles[jsf].p.promise(); |
| 123 | + } |
| 124 | + else { |
| 125 | + this.jsonfiles[jsf].p = $.Deferred(); |
| 126 | + $.ajax({ |
| 127 | + 'dataType': this.c['datatype'], |
| 128 | + 'cache': this.c['cache'], |
| 129 | + 'jsonp': false, |
| 130 | + 'jsonpCallback': 'EVE-Oj_' + jsf + '_callback', |
| 131 | + 'url': this.c['path'] + '/' + jsf + '.' + this.c['datatype'] |
| 132 | + }) |
| 133 | + .done(function (data, status, jqxhr) { _LoadFileDone.apply(self, [jsf, data]) }) |
| 134 | + .fail(function (jqxhr, status, error) { _LoadFileFail.apply(self, [jsf, status, error]) }); |
| 135 | + return this.jsonfiles[jsf].p.promise(); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | +})(jQuery); |
0 commit comments