-
Notifications
You must be signed in to change notification settings - Fork 5
/
curious.js
255 lines (215 loc) · 7.35 KB
/
curious.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Curious client-side JSON parsing
//
(function(){
var CuriousObjects = (function() {
function CuriousObject(hash_data) {
this.id = hash_data.id;
for (var k in hash_data) {
this[k] = hash_data[k];
}
this.__url = null;
this.__model = null;
this.__dirty = false;
}
function parse_objects(data, model, klass) {
if (data.objects === undefined) { return []; }
var objects = [];
for (var i=0; i<data.objects.length; i++) {
var obj = data.objects[i];
var url = data.urls[i];
var obj_data = {};
for (var j=0; j<data.fields.length; j++) {
obj_data[data.fields[j]] = obj[j];
}
var obj;
if (!klass)
obj = new CuriousObject(obj_data);
else {
obj = new klass();
for (var k in obj_data) { obj[k] = obj_data[k]; }
}
obj.id = obj_data.id;
obj.__url = url;
obj.__model = model;
objects.push(obj);
}
return objects;
}
function parse_results_with_trees(relationships, classes, results, existing_object_dicts) {
// get objects associated with each subquery. for each subquery, build a
// hash of ID to object. existing_object_dicts should be an array of dicts,
// each dict is a mapping of ID to existing objects. if existing objects
// are specified, will build relationships using existing objects.
var objects = [];
var trees = [];
for (var i=0; i<results.data.length; i++) {
var klass = null;
if (classes)
klass = classes[i];
var model = results.results[i].model;
var result_objects = parse_objects(results.data[i], model, klass);
var d = {};
for (var j=0; j<result_objects.length; j++) {
if (existing_object_dicts !== undefined && existing_object_dicts !== null &&
existing_object_dicts[i] !== undefined && existing_object_dicts[i] !== null &&
existing_object_dicts[i][result_objects[j].id] !== undefined) {
d[result_objects[j].id] = existing_object_dicts[i][result_objects[j].id];
}
else {
d[result_objects[j].id] = result_objects[j];
}
}
objects.push(d);
trees.push(null);
}
// for each subquery, add a relationship to results of next subquery, and
// then a reverse relationship
for (var i=1; i<results.results.length; i++) {
var rel = relationships[i];
var res_tups = results.results[i].objects;
var join_idx = results.results[i].join_index;
var join_src = objects[join_idx];
var join_obj = objects[i];
var rev = relationships[join_idx];
trees[i] = results.results[i].tree;
// add empty replationship
for (var k in join_src) { join_src[k][rel] = []; }
for (var k in join_obj) { join_obj[k][rev] = []; }
for (var j=0; j<res_tups.length; j++) {
var obj_src = res_tups[j];
var src = join_src[obj_src[1]];
if (obj_src[0]) {
var obj = join_obj[obj_src[0]];
// forward relationship from query to next query
src[rel].push(obj);
// reverse relationship
obj[rev].push(src);
}
}
}
return {objects: objects, trees: trees};
}
function parse_results(relationships, classes, results, existing_object_dicts) {
return parse_results_with_trees(relationships, classes, results, existing_object_dicts).objects;
}
function dict_to_array(d) {
var r = [];
for (var k in d) { r.push(d[k]); }
return r;
}
function array_to_dict(a) {
var d = {};
for (var i=0; i<a.length; i++) {
d[a[i].id] = a[i];
}
return d;
}
function id_list(objects) {
var ids = [];
for (var i=0; i<objects.length; i++) {
if (ids.indexOf(objects[i].id) < 0) {
ids.push(objects[i].id);
}
}
return ids;
}
function id_str(objects) {
var ids = id_list(objects);
if (ids.length === 0) { return null; }
return ids.join(',');
}
return {
parse: parse_results,
parse_with_trees: parse_results_with_trees,
d2a: dict_to_array,
a2d: array_to_dict,
id_list: id_list,
id_str: id_str
}
}());
// Helper for making a Curious query and getting back parsed objects. Use with
// angular $http compatible HTTP request facilities (e.g. jQuery?)
var CuriousQ = function(curious_url, http, app_default_params, quiet) {
function __get(q, params, relationships, classes, existing_object_arrays, cb, trees_cb) {
var i, k;
var args, immutable_args;
var data_array;
var existing_object_dicts;
var post_cb;
if (quiet === undefined || quiet !== true) {
console.warn(q);
}
if (existing_object_arrays) {
existing_object_dicts = [];
for (i = 0; i < existing_object_arrays.length; i++) {
data_array = existing_object_arrays[i];
if (data_array) {
existing_object_dicts.push(CuriousObjects.a2d(data_array));
}
else {
existing_object_dicts.push(null);
}
}
}
// Default args
args = {x: 0, fk: 0};
// App-level arg settings
if (app_default_params) {
for (k in app_default_params) {
if (app_default_params.hasOwnProperty(k)) {
args[k] = app_default_params[k];
}
}
}
// Query-level arg settings
if (params) {
for (k in params) {
if (params.hasOwnProperty(k)) {
args[k] = params[k];
}
}
}
// Values for immutable args: these are always set, no matter what
immutable_args = {d: 1, q: q};
for (k in immutable_args) {
args[k] = immutable_args[k];
}
post_cb = function(resp) {
var i;
var objects;
var res;
res = CuriousObjects.parse_with_trees(relationships, classes, resp.result, existing_object_dicts);
objects = res.objects;
for (i = 0; i < objects.length; i++) { objects[i] = CuriousObjects.d2a(objects[i]); }
cb(objects);
if (trees_cb) { trees_cb(res.trees); }
};
return http.post(curious_url, args).success(post_cb);
}
function get(q, relationships, cb, params, tree_cb) {
return __get(q, params, relationships, null, null, cb, tree_cb);
}
function get_with_objs(q, relationships, existing_object_arrays, cb, params, tree_cb) {
return __get(q, params, relationships, null, existing_object_arrays, cb, tree_cb);
}
function get_with_start(q, relationships, starting_objects, cb, params, tree_cb) {
var i;
var existing_object_arrays = [];
for (i = 0; i < relationships.length; i++) {
existing_object_arrays.push(null);
}
existing_object_arrays[0] = starting_objects;
return __get(q, params, relationships, null, existing_object_arrays, cb, tree_cb);
}
return {
get: get,
get_with_objs: get_with_objs,
get_with_start: get_with_start
}
};
var ex = undefined;
if (typeof window !== 'undefined') { ex = window; }
else if (typeof exports !== 'undefined' && exports) { ex = exports; }
ex.CuriousQ = CuriousQ;
ex.CuriousObjects = CuriousObjects;
})();