-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM52.js
219 lines (186 loc) · 7.75 KB
/
M52.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
(function (){
var myConnector = tableau.makeConnector();
var cols;
setupConnector = function (){
var server = $('#serverInput').val().trim();
var token = $('#tokenInput').val().trim();
var selectedCols = [];
// http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
// todo how do we handle "java.util.List"?
$("#keySelection input[type=checkbox]:checked").each(function(idx, el) {
var index = $(el).closest("tr").data("index");
selectedCols.push(cols[index]);
});
var tableInfo = {
id: "M52Feed",
alias: "table",
description: "Data from the M52 API",
columns: selectedCols
};
if (server && token){
tableau.connectionData = JSON.stringify({
server: server,
token: token,
tableInfo : tableInfo
});
tableau.connectionName = "Method52";
}
};
var attachMouseDrag = function() {
var down = false;
var current = [false];
var firstDrag = false;
$("#keySelection tr")
.mousedown(function(ev) {
down = true;
firstDrag = true;
current = $(ev.target).closest("tr");
// console.log("down");
})
.mousemove(function(ev) {
var el = $(ev.target).closest("tr");
if(down && el[0] != current[0]) {
current = el;
var cb = current.find("input");
cb.prop("checked", !cb.prop("checked"));
// console.log(ev);
}
if(firstDrag) {
var cb = el.find("input");
cb.prop("checked", !cb.prop("checked"));
firstDrag = false;
}
})
.mouseup(function(ev) {
down = false;
var el = $(ev.target).closest("tr");
if(el[0] = current[0] && ev.target.type != "checkbox" && firstDrag) {
var cb = current.find("input");
cb.prop("checked", !cb.prop("checked"));
}
firstDrag = false;
// console.log("up");
});
};
var getSchema = function (){
var token = $("#tokenInput").val().trim();
var server = $('#serverInput').val().trim();
// http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
var apiURL = server + "/supergui/access-tokens/handle?target=schema&token=" + token;
var types = {
"java.lang.Long": tableau.dataTypeEnum.int,
"java.lang.Integer": tableau.dataTypeEnum.int,
"java.lang.Float": tableau.dataTypeEnum.float,
"java.lang.Double": tableau.dataTypeEnum.float,
"java.lang.String": tableau.dataTypeEnum.string,
"java.lang.Boolean": tableau.dataTypeEnum.bool,
"org.joda.time.Instant": tableau.dataTypeEnum.datetime,
"java.util.List": tableau.dataTypeEnum.string
}
$.getJSON(apiURL, function (resp){
cols = [];
$.each(resp.schema, function (keyName, value){
//http://tableau.github.io/webdataconnector/ref/api_ref.html#webdataconnectorapi.columninfo
const type = types[value.type.class];
cols.push({
id: keyName.replace(/\W+/g, "_"),
alias: keyName,
description: value.type.class,
dataType: type
});
});
cols.sort(function(a,b){
var textA = a.alias.toUpperCase();
var textB = b.alias.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
$.each(cols, function(index, value){
$('#keySelection > tbody:last-child').append('<tr data-index="'+index+'"><td>'+value.alias+'</td><td><input type="checkbox"></td></tr>');
});
attachMouseDrag();
$("#submitButton").prop('disabled', false);
}).error(function (jqXHR, textStatus, errorThrown){
var msg;
if(jqXHR.responseJSON) {
msg = jqXHR.responseJSON.error;
}
alert(textStatus + ": " + errorThrown + ( msg ? " - " + msg : ""));
});
};
myConnector.getSchema = function (schemaCallback){
var params = JSON.parse(tableau.connectionData);
schemaCallback([params.tableInfo]);
};
myConnector.getData = function (table, doneCallback){
var params = JSON.parse(tableau.connectionData);
var apiURL = params.server + "/supergui/access-tokens/handle?token=" + params.token;
var originalURL = apiURL;
var columnTypes = {};
$.each(table.tableInfo.columns, function(index, value){
columnTypes[value.id] = value.description;
});
var getMoreData = function (){
$.getJSON(apiURL, function (resp){
if (resp.data == "EOF"){
console.info("End of data stream");
doneCallback();
} else {
if (resp.data.length==0){
console.info("No new data has been fetched.");
} else {
console.info("Got more data: " + resp.data.length);
}
var parsedResponse = [];
$.each(resp.data, function (index, rawObj){
var cleanedObj = new Object();
$.each(rawObj, function (keyName, value){
if (value != null && value.iMillis !== undefined){
// this is a serialised jodatime Instant
value = new Date(value.iMillis).toISOString().replace("T", " ");
value = value.substr(0, value.indexOf("."));
}
var cleanKey = keyName.replace(/\W+/g, "_");
if(value != null && cleanKey in columnTypes) {
if(columnTypes[cleanKey] == "java.util.List") {
value = JSON.stringify(value);
}
cleanedObj[cleanKey] = value;
}
});
parsedResponse.push(cleanedObj);
});
table.appendRows(parsedResponse);
if (resp.next) {
apiURL = originalURL + "&next=" + resp.next;
}
getMoreData();
}
}).error(function (jqXHR, textStatus, errorThrown){
tableau.abortWithError(errorThrown + "; " + textStatus);
})
;
}
getMoreData();
};
tableau.registerConnector(myConnector);
$(document).ready(function (){
$("#toggleSelected").click(function (){
$("#keySelection input[type=checkbox]").each(function(i, cb){
$(cb).prop("checked", !$(cb).prop("checked"));
});
});
$("#allSelected").click(function (){
$("#keySelection input[type=checkbox]").each(function(i, cb){
$(cb).prop("checked", true);
});
});
$("#checkToken").click(function (){
getSchema();
});
$("#submitButton").click(function (){
console.log("clicked");
setupConnector();
tableau.submit();
});
});
})();