Repository navigation
Expand file tree
/
Copy pathtask-xd-pr.js
More file actions
360 lines (329 loc) · 11.5 KB
/
Copy pathtask-xd-pr.js
File metadata and controls
360 lines (329 loc) · 11.5 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Cross-domain task proxy implementation for Bebras task API - v1.0 - 08/2014
*
* This file implements a "TaskProxyManager" object in the global scope so that
* platforms using this file can just call
* "myTask = TaskProxyManager.getTaskProxy(task_id)" to get the task object
* associated to a task loaded in an iframe. Then they can just call
* "myTask.myFunction()", with function of the API, the task object will take
* care of message sending and receiving with the actual task.
* 'task_id' here refers to the "id" attribute of the iframe in which a task is
* loaded.
*
* It depends on jQuery (though this could be removed) and jschannel.
*
*/
/**
* Global objects
*/
window.TaskProxyManager = {
tasks: {},
platforms: {},
getRandomID: function() {
var low = Math.floor(Math.random() * 922337203).toString();
var high = Math.floor(Math.random() * 2000000000).toString();
return high + low;
},
getTaskProxy: function(idFrame, success, force, error) {
var errorFun = error ? error : function() {console.error(arguments);}
if (TaskProxyManager.tasks[idFrame] && !force) {
success(TaskProxyManager.tasks[idFrame]);
} else {
if (force) {
TaskProxyManager.deleteTaskProxy(idFrame);
}
$('#'+idFrame).each(function() {
TaskProxyManager.tasks[idFrame] = new Task($(this), function() {
if (idFrame in TaskProxyManager.platforms) {
TaskProxyManager.tasks[idFrame].setPlatform(TaskProxyManager.platforms[idFrame]);
}
success(TaskProxyManager.tasks[idFrame]);
}, errorFun);
});
}
},
setPlatform: function(task, platform) {
TaskProxyManager.platforms[task.Id] = platform;
TaskProxyManager.tasks[task.Id].setPlatform(platform);
},
deleteTaskProxy: function(idFrame) {
var task = TaskProxyManager.tasks[idFrame];
if (task && task.chan) {
task.chan.destroy();
}
delete(TaskProxyManager.tasks[idFrame]);
delete(TaskProxyManager.platforms[idFrame]);
},
getUrl: function(taskUrl, sToken, sPlatform, prefix, sLocale) {
var channelId = (prefix ? prefix : '')+this.getRandomID();
if (taskUrl.indexOf('?') == -1) {
// the idea is not to change the base url even if we change token, so we put token after #
taskUrl = taskUrl + '?';
} else {
taskUrl = taskUrl + '&';
}
var url = taskUrl+'sToken='+encodeURIComponent(sToken)+'&sPlatform='+encodeURIComponent(sPlatform)+'&channelId='+encodeURIComponent(channelId);
if(sLocale) { url += '&sLocale='+encodeURIComponent(sLocale); }
return url;
}
};
TaskProxyManager.getGraderProxy = TaskProxyManager.getTaskProxy;
/**
* Enable / Disable messages debug
*/
var platformDebug = false;
/*
* Task object, created from an iframe DOM element
*/
function Task(iframe, success, error) {
this.iframe = iframe;
this.Id = iframe.attr('id');
this.platformSet = false;
function getUrlParameterByName(name, url) {
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var nbSecs = 0;
var checkInterval = setInterval(function() {
if (nbSecs > 300) {
error();
clearInterval(checkInterval);
checkInterval = null;
}
nbSecs = nbSecs + 1;
}, 1000);
this.chan = Channel.build({
window: iframe[0].contentWindow,
origin: "*",
scope: getUrlParameterByName('channelId', iframe[0].src),
onReady: function() {
if (checkInterval) {
clearInterval(checkInterval);
success();
}
}
});
this.setPlatform = function(platform) {
this.platform = platform;
var self = this;
if (this.platformSet) {
// in this case, the bound functions will call the new platform
return;
}
this.chan.bind('platform.validate', function (trans, mode) {
self.platform.validate(mode, trans.complete, trans.error);
trans.delayReturn(true);
console.error('platform.validate with mode '+mode);
});
this.chan.bind('platform.getTaskParams', function (trans, keyDefault) {
var key = keyDefault ? keyDefault[0] : undefined;
var defaultValue = keyDefault ? keyDefault[1] : undefined;
self.platform.getTaskParams(key, defaultValue, trans.complete, trans.error);
trans.delayReturn(true);
});
this.chan.bind('platform.showView', function (trans, view) {
self.platform.showView(view, trans.complete, trans.error);
trans.delayReturn(true);
});
this.chan.bind('platform.askHint', function (trans, hintToken) {
self.platform.askHint(hintToken, trans.complete, trans.error);
trans.delayReturn(true);
});
this.chan.bind('platform.updateDisplay', function (trans, data) {
self.platform.updateDisplay(data, trans.complete, trans.error);
trans.delayReturn(true);
});
this.chan.bind('platform.openUrl', function (trans, url) {
self.platform.openUrl(url, trans.complete, trans.error);
trans.delayReturn(true);
});
this.chan.bind('platform.log', function (trans, data) {
platform.log(data, trans.complete, trans.error);
trans.delayReturn(true);
});
self.platformSet = true;
// Legacy calls
this.chan.bind('platform.updateHeight', function (trans, height) {
self.platform.updateDisplay({ height: height }, trans.complete, trans.error);
trans.delayReturn(true);
});
};
}
Task.prototype.getSourceId = function() {
return this.Id;
};
Task.prototype.getTargetUrl = function() {
return this.iframe.attr('src');
};
Task.prototype.getTarget = function() {
return this.iframe[0].contentWindow;
};
Task.prototype.getDomain = function() {
var url = this.getTargetUrl();
return url.substr(0, url.indexOf('/', 7));
};
/**
* Task API functions
*/
Task.prototype.load = function(views, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.load",
params: views,
success: success,
error: error
});
};
Task.prototype.unload = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.unload",
timeout: 2000,
error: error,
success: success
});
};
Task.prototype.getHeight = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.getHeight",
timeout: 500,
error: error,
success: success
});
};
Task.prototype.updateToken = function(token, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.updateToken",
params: token,
timeout: 10000,
error: error,
success: success
});
};
Task.prototype.getMetaData = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.getMetaData",
timeout: 2000,
error: error,
success: success
});
};
Task.prototype.getAnswer = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.getAnswer",
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.reloadAnswer = function(answer, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.reloadAnswer",
params: answer,
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.getState = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.getState",
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.reloadState = function(state, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.reloadState",
params: state,
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.getViews = function(success, error) {
if (!error) error = function() {console.error(arguments)};
this.chan.call({method: "task.getViews",
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.showViews = function(views, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.showViews",
params: views,
error: error,
success: success,
timeout: 2000
});
};
Task.prototype.gradeAnswer = function(answer, answerToken, success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
var newSuccess = function(successMonoArg) {
if (successMonoArg instanceof Array) {
var argzero = successMonoArg[0];
var argone = (successMonoArg.length > 1) ? successMonoArg[1] : null;
var argtwo = (successMonoArg.length > 2) ? successMonoArg[2] : null;
var argthree = (successMonoArg.length > 3) ? successMonoArg[3] : null;
success(argzero, argone, argtwo, argthree);
} else {
success(successMonoArg);
}
};
this.chan.call({method: "task.gradeAnswer",
params: [answer, answerToken],
error: error,
success: newSuccess,
timeout: window.gradeAnswerTimeoutVal ? window.gradeAnswerTimeoutVal : 40000
});
};
Task.prototype.getResources = function(success, error) {
if (!error) error = function(errMsg) {console.error(errMsg)};
this.chan.call({method: "task.getResources",
params: [],
error: error,
success: success,
timeout: 2000
});
};
// for grader.gradeTask
Task.prototype.gradeTask = Task.prototype.gradeAnswer;
/*
* Platform object definition, created from a Task object (see below)
*/
function Platform(task) {
this.task = task;
}
Platform.prototype.getTask = function() {
return this.task;
};
/*
* Simple prototypes for platform API functions, to be overriden by your
* platform's specific functions (for each platform object)
*/
Platform.prototype.validate = function(mode, success, error) {error('platform.validate is not defined');};
Platform.prototype.showView = function(views, success, error) {error('platform.validate is not defined');};
Platform.prototype.askHint = function(platformToken, success, error) {error('platform.validate is not defined');};
Platform.prototype.updateHeight = function(height, success, error) { this.updateDisplay({height: height}); };
Platform.prototype.updateDisplay = function(data, success, error) {
if(data.height) {
this.task.iframe.height(parseInt(data.height)+40);success();
}
};
Platform.prototype.openUrl = function(url, success, error) {error('platform.openUrl is not defined!');};
Platform.prototype.log = function(data) {error('platform.log is not defined!');};
Platform.prototype.getTaskParams = function(key, defaultValue, success, error) {
var res = {minScore: -3, maxScore: 10, randomSeed: 0, noScore: 0, readOnly: false, options: {}};
if (key) {
if (key !== 'options' && key in res) {
res = res[key];
} else if (res.options && key in res.options) {
res = res.options[key];
} else {
res = (typeof defaultValue !== 'undefined') ? defaultValue : null;
}
}
success(res);
};
window.Platform = Platform;