forked from aguestuser/show-me-the-money
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLsDataSource.js
117 lines (98 loc) · 2.7 KB
/
LsDataSource.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
"use strict";
(function() {
var root = this;
var previous_LsDataSource = root.LsDataSource;
var toQueryString = function(obj) {
var parts = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
if (Array.isArray(obj[i])) {
obj[i].forEach(function(val) {
parts.push(encodeURIComponent(i+"[]") + "=" + encodeURIComponent(val));
});
} else {
parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
}
}
}
return parts.join("&");
}
var get = function(url, data, onSucess, onFail) {
var httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
onSucess(JSON.parse(httpRequest.responseText));
} else {
if (onFail) {
onFail({ status: httpRequest.status, error: httpRequest.responseText });
}
}
}
};
var fullUrl = url + (data ? "?" + toQueryString(data) : "")
httpRequest.open('GET', fullUrl);
httpRequest.send();
};
var LsDataSource = {
name: 'LittleSis',
baseUrl: 'http://littlesis.org',
findNodes: function(text, callback) {
get(
this.baseUrl + '/maps/find_nodes',
{ num: 12, desc: true, with_ids: true, q: text },
callback
);
},
getNodeWithEdges: function(nodeId, nodeIds, callback) {
get(
this.baseUrl + '/maps/node_with_edges',
{ node_id: nodeId, node_ids: nodeIds },
callback
);
},
getConnectedNodesOptions: {
category_id: {
1: "Position",
2: "Education",
3: "Membership",
4: "Family",
5: "Donation",
6: "Transaction",
7: "Lobbying",
8: "Social",
9: "Professional",
10: "Ownership",
11: "Hierarchy",
12: "Generic"
}
},
getConnectedNodes: function(nodeId, nodeIds, options, callback) {
options = options || {};
options.node_id = nodeId;
options.node_ids = nodeIds;
get(
this.baseUrl + '/maps/edges_with_nodes',
options,
callback
);
}
};
LsDataSource.noConflict = function() {
root.LsDataSource = previous_LsDataSource;
return LsDataSource;
}
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = LsDataSource;
}
exports.LsDataSource = LsDataSource;
}
else {
root.LsDataSource = LsDataSource;
}
}).call(this);