-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
29477 lines (24714 loc) · 833 KB
/
bundle.js
File metadata and controls
29477 lines (24714 loc) · 833 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){
// file: main.js
// author: Linhai Yin
// desc: root file for bundling the Form Interface
var CrowdCurioClient = require('crowdcurio-client');
require('./odh-test');
global.csrftoken = $("[name='csrfmiddlewaretoken']").val();
// set UI vars
var DEV = window.DEV;
var task = window.task || -1;
var user = window.user || -1;
var experiment = window.experiment || -1;
var condition = window.condition|| -1;
var containerId = window.container || 'task-container';
containerElement = $('#' + containerId);
console.log(DEV);
console.log(task);
console.log(user);
console.log(experiment);
console.log(condition);
console.log(containerId);
var apiClient = new CrowdCurioClient();
var config = {
apiClient: apiClient,
};
containerElement.ODHInterface(config);
Interface = containerElement.data('crowdcurio-ODHInterface');
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./odh-test":2,"crowdcurio-client":3}],2:[function(require,module,exports){
var jQuery = require('jquery');
var $ = jQuery;
require('jquery-ui-browserify');
$.widget('crowdcurio.ODHInterface', {
options: {
apiClient: undefined,
config:{}
},
_create: function() {
var that = this;
// 1. initialize the config
this.options.config = window.config;
// 1.5. make sure we have a mode set
this.options.config.mode = this.options.config.mode || 'static'; // default to static
// 3. init the api client
var apiClient = that._getApiClient();
console.log(window);
apiClient.init({
user: window.user,
task: window.task,
experiment: window.experiment,
condition: window.condition,
configuration: window.config
});
var ele = $("#main");
apiClient.help_request.build(ele);
},
_getApiClient: function() {
var that = this;
return that.options.apiClient;
},
_createStaticHTMLContainers: function() {
var that = this;
var content= ' \
<div id="d4o-container" > \
<div id="header"> \
</div> \
<div id="nav-bar" class="nav-bar"> \
</div> \
<div id="submit-a-dive" class="submit-a-dive"> \
<form id="submit-a-dive-form"> \
</form> \
<button id="submit-btn" class="btn submit-btn waves-effect waves-light" name="action" style="margin-left: auto; margin-right: auto; display: block;">Submit\
<i class="material-icons right">send</i>\
</button>\
</div> \
<div id="your-dives" style="display: none;"> \
</div> \
</div> \
';
$(that.element).html(content);
},
_renderNavBar: function() {
var that = this;
/*
*
* Nav Bar Section
*
*/
var sub_a_dive = $('<a>').attr('class', "nav-bar").text("Submit a Dive").appendTo('#nav-bar');
sub_a_dive.attr('style', "color: #FFFFFF;");
sub_a_dive.attr('id', "submit-a-dive-nav");
sub_a_dive.click(function(e) {
$('#submit-a-dive-nav').attr('style', "font-size: 25pt; color: #FFFFFF;");
$('#your-dives-nav').attr('style', "font-size:20pt; color: #039be5;");
$('#submit-a-dive').attr('style', that.sub_a_dive_active_style);
$('#your-dives').attr('style', that.your_dives_inactive_style);
});
var your_dives = $('<a>').attr('class', "nav-bar").text("Your Dives").appendTo('#nav-bar');
your_dives.attr('style', "font-size: 20pt;");
your_dives.attr('id', 'your-dives-nav');
your_dives.click(function(e) {
$('#your-dives-nav').attr('style', "font-size: 25pt; color: #FFFFFF;");
$('#submit-a-dive-nav').attr('style', "font-size:20pt; color: #039be5;");
$('#submit-a-dive').attr('style', that.sub_a_dive_inactive_style);
$('#your-dives').attr('style', that.your_dives_active_style);
});
},
_renderInputForm: function(data) {
that = this;
// create the dive which holds everything
var div = $("<div>").appendTo("#submit-a-dive-form");
div.addClass("d40");
// for each element we want to put into the div
$.each(data.content, function(k, element) {
// Generate the question before the DOM element
if(typeof element.question !== 'undfined' && element.question) {
div.append(element.question);
}
// Generate the DOM element
if(typeof element.html === 'undefined' || !element.html) {
console.log("ERROR: " + data.slug + " did not have an html tag");
}
var domObj = $("<" + element.html + ">").appendTo(div);
domObj.attr('id', data.slug);
// Set up the attributes
$.each(element.headers, function(i, header) {
domObj.attr(i, header);
});
domObj.append(element.content);
that._submit_forms.push(domObj);
});
},
_renderYourDives : function() {
var that = this;
var headers = [];
var temp = that._submit_forms;
var apiClient = that._getApiClient();
// create table to hold the data
// for/.each
var table = $("<table>").appendTo("#your-dives");
table.attr('id', 'your-dives-table');
var thead = $("<thead>").appendTo(table);
var headerList = $("<tr>").appendTo(thead);
// Get the data items
$.each(that._submit_forms, function(i, form) {
headers.push(form[0].id);
headerList.append("<th>" + form[0].id + "</th>");
});
// load the data into the table
apiClient.listAll('response', {},function(data) {
that._fillPrevDiveTable(data, headers);
});
// graph it somehow?
// find a library?
},
_fillPrevDiveTable : function(data, headers) {
var tbody = $("<tbody>").appendTo("#your-dives-table");
data.forEach(function(ele) {
var tableRow = $("<tr>").appendTo(tbody);
headers.forEach(function(header) {
tableRow.append("<td>" + ele.content[header] + "</td>");
});
});
},
_submitResponse: function(e) {
var that = this;
var headers = []
var content = {}
that._submit_forms.forEach(function(form) {
console.log(form);
content[form[0].id] = form.val();
headers.push(form[0].id);
});
console.log(headers);
// save a response through the api client
var apiClient = that._getApiClient();
apiClient.create('response', {
content: content
}, function(result){
that._responseSubmitted(headers);
});
// for testing the UI only
//that._responseSubmitted(headers);
},
_responseSubmitted : function(headers) {
that = this;
$('#your-dives-nav').attr('style', "font-size: 25pt; color: #FFFFFF; display: none;");
$('#submit-a-dive-nav').attr('style', "font-size:20pt; color: #039be5; display: none;");
$('#submit-a-dive').attr('style', that.sub_a_dive_inactive_style);
$('#your-dives').attr('style', that.your_dives_active_style);
$("#your-dives").html("");
that._renderYourDives();
}
});
},{"jquery":12,"jquery-ui-browserify":11}],3:[function(require,module,exports){
var TaskRoutingManager = require('./task-router');
var TaskSession = require('./task-session');
var HelpRequest = require('./help-request');
function CrowdCurioClient(){
// core api vars
this.auth = null;
this.client = null;
this.task_session = null;
this.help_request = null;
// routing manager
this.router = null;
// routing vars
this.task = null;
this.data = null;
this.user = null;
}
CrowdCurioClient.prototype.init = function(params, delay_connect){
var that = this;
return new Promise(function(resolve, reject) {
// authenticate & instantiate the client's connection
that.auth = new coreapi.auth.SessionAuthentication({
csrfCookieName: 'csrftoken',
csrfHeaderName: 'X-CSRFToken'
})
that.client = new coreapi.Client({auth: that.auth})
// create the task routing manager
that.router = new TaskRoutingManager();
// set (1) task and (2) experiment vars for routing
that.user = {id: params['user'], type: 'User'};
that.task = {id: params['task'], type: 'Task'};
if(params['experiment'] !== undefined && params['experiment'] > 0){
that.experiment = {id: params['experiment'], type: 'Experiment'}
that.condition = {id: params['condition'], type: 'Condition'}
that.router.init(that.client, {
'page_size': 3,
'task': params['task'],
'experiment': params['experiment'],
'condition': params['condition']
});
} else {
that.experiment = null;
that.router.init(that.client, {
'page_size': 3,
'task': params['task']
});
}
// if collaboration is active, create and initialize a task session and wait to resolve the promise until the task session has been initialized
// Note: if collaboration is "delayed", then the interface needs to call
// client.task_session.connect() before using TaskSession features
if(params['configuration']['collaboration']){
if(params['configuration']['collaboration']['active']){
if(params['configuration']['collaboration']['automatic']) {
that.task_session = new TaskSession();
that.task_session.init(jQuery.extend({'client': that.client}, params)).then(function(){
console.log(that.task_session.task_session);
that.task_session.connect(that.task_session.task_session);
resolve();
});
} else {
that.task_session = new TaskSession();
that.task_session.init(jQuery.extend({'client': that.client}, params)).then(function(){
console.log(that.task_session.task_session);
that.task_session.connect(that.task_session.task_session);
resolve();
});
that.help_request = new HelpRequest();
that.help_request.init(jQuery.extend({"client":that, 'apiClient': that.client, 'task_session': that.task_session}, params));
console.log("FML");
}
}
} else {
resolve();
}
}.bind(this));
}
CrowdCurioClient.prototype.setData = function(id){
this.data = {id: id, type: 'Data'};
}
CrowdCurioClient.prototype.getNextTask = function(queue_type, callback){
var that = this;
// call the router's get next task function.
this.router.getNextTask(queue_type, function(task){
// if no task was returned from the API, return an empty object
if(task === undefined){
callback({});
} else {
// return the task
callback(task);
}
});
}
// General-Purpose CRUD Operations for Models
// note: create is supported for two models (event/response)
// update and partialUpdate are supported for all models
// list and listAll are supported for two models (response/data)
// delete is supported for all models
CrowdCurioClient.prototype.create = function(model, params, callback){
var that = this;
// extend params by adding relations
if(model === 'response'){
params = jQuery.extend({
owner: that.user,
data: that.data,
task: that.task,
experiment: that.experiment,
condition: that.condition
}, params);
} else if (model === 'event'){
params = jQuery.extend({
user: that.user,
experiment: that.experiment,
condition: that.condition
}, params);
} else if (model == 'annotation'){
params = jQuery.extend({
owner: that.user,
data: that.data,
task: that.task,
experiment: that.experiment,
condition: that.condition,
task_session: {id: that.task_session.task_session, type: 'TaskSession'},
updated_by: that.user
}, params);
}
let action = [model, "create"];
this.client.action(schema, action, params).then(function(result) {
callback(result);
});
}
CrowdCurioClient.prototype.update = function(model, params, callback){
var that = this;
if (model == 'annotation'){
params = jQuery.extend({
updated_by: that.user
}, params);
}
let action = [model, "update"];
this.client.action(schema, action, params).then(function(result) {
callback(result);
});
}
CrowdCurioClient.prototype.partialUpdate = function(model, params, callback){
let action = [model, "partial_update"];
this.client.action(schema, action, params).then(function(result) {
callback(result);
});
}
CrowdCurioClient.prototype.list = function(model, params, callback){
var that = this;
// extend params by adding relations
if(model === 'response'){
if (that.user) {
params.owner = that.user.id;
}
if (that.data) {
params.data = that.data.id;
}
if (that.task) {
params.task = that.task.id;
}
if (that.experiment) {
params.experiment = that.experiment.id;
}
if (that.condition) {
params.condition = that.condition.id;
}
} else if (model === 'data'){
if (that.task) {
params.task = that.task.id;
}
if (that.experiment) {
params.experiment = that.experiment.id;
}
if (that.condition) {
params.condition = that.condition.id;
}
}
let action = [model, "list"];
this.client.action(schema, action, params).then(function(result) {
callback(result);
});
}
CrowdCurioClient.prototype.listAll = function(model, params, callback){
let that = this;
let results = [];
params.page = 1;
listNextPage();
function listNextPage() {
that.list(model, params, function(response) {
Array.prototype.push.apply(results, response.results);
if (!response.links.next) {
callback(results);
return;
}
params.page += 1;
listNextPage();
});
};
}
CrowdCurioClient.prototype.delete = function(model, params, callback){
let action = [model, "delete"];
this.client.action(schema, action, params).then(function(result) {
callback(result);
});
}
module.exports = CrowdCurioClient;
},{"./help-request":4,"./task-router":5,"./task-session":6}],4:[function(require,module,exports){
var jQuery = require('jquery');
var $ = jQuery;
require('jquery-ui-browserify');
var ReconnectingWebSocket = require('reconnectingwebsocket');
var TaskSession = require('./task-session');
function getCookie(cname) {
var name = cname;
var decodedCookie = decodeURIComponent(document.cookie);
console.log(decodedCookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var cookie = ca[i].split("=")
console.log(cookie);
if(cookie[0].replace(/^\s+|\s+$/g, '') === cname){
return cookie[1];
}
}
return "";
}
Array.prototype.diff = function (a) {
return this.filter(function (i) {
return a.indexOf(i) === -1;
});
};
function print(message){
var currentDate = '[' + new Date().toUTCString() + '] ';
console.log(currentDate+message)
}
function xhttpRequest(path, message) {
var xhttp;
xhttp=new XMLHttpRequest();
//console.log(xhttp);
//console.log(getCookie(csrftoken));
xhttp.open("POST", path, false);
xhttp.setRequestHeader('X-CSRFToken', getCookie("csrftoken"), false);
// TODO: setup proper csrf
xhttp.send(message);
console.log(xhttp);
return xhttp;
}
function tab(id) {
document.getElementById("help-request-container").style.display = "none";
document.getElementById("help-request-button-container").style.display = "none";
document.getElementById("help-request-create-container").style.display = "none";
document.getElementById("help-request-resolve-container").style.display = "none";
if(id === "help-request-container") {
document.getElementById("help-request-container").style.display = "block";
document.getElementById("help-request-button-container").style.display = "block";
} else {
document.getElementById(id).style.display = "block";
}
}
HelpRequest.prototype.getFocus = function() {
return this.focus
}
//TODO: have this broadcast the change?
/**
*
* Does not broadcast the focus change
*/
HelpRequest.prototype.setFocus = function(element) {
this.focus = element;
}
HelpRequest.prototype.setFocusOnReply = function(helpRequest) {
return function(message) {
var data = message.payload.message;
helpRequest.focus = data;
}
}
// Create, CoreAPI?
HelpRequest.prototype.createHelpRequest = function(params) {
var that = this;
var random = Math.floor(Math.random()*1000000);
// make the task session
var channel_name = "t" + that.task.id + "e" + that.experiment.id + "u" + that.user.id + "r" + random;
that.task_session.create(channel_name).then((message) => {
// make the help request
console.log("creating help_request");
console.log({
"command": "odh_create",
"channel_name": channel_name,
"task_session": message,
"task": that.task.id,
"experiment": that.experiment.id,
"random": random,
"name": params["name"],
"summary": params["summary"],
"message": $("#chat-input-box").val().trim()
});
that.task_session.socket.send(JSON.stringify({
"command": "odh_create",
"channel_name": channel_name,
"task_session": message,
"task": that.task.id,
"experiment": that.experiment.id,
"random": random,
"name": params["name"],
"summary": params["summary"],
"message": $("#chat-input-box").val().trim()
}));
console.log(message);
});
}
/**
"text": json.dumps({
"msg_type": 30,
"payload": {
'task_session': str(ts.id),
'message': {
"type": "HelpRequest",
"help_request": {
"id": hr.id,
"task_session": {
"id": ts.id,
"channel_name": ts.channel_name
}
},
},
'username': message.user.username,
}
}),
*/
HelpRequest.prototype.CreateHelpRequestOnReply = function(helpRequest) {
return function(message) {
var data = message.payload.message;
console.log(message);
alert("ODH CREATED");
console.log("Finished Creating Help Request");
// Delete/leave old task request
console.log("Leaving old help Request");
helpRequest.task_session.socket.send(JSON.stringify({
"command": "leave", // determines which handler will be used (see chat/routing.py)
"task_session": helpRequest.task_session.id,
"user": helpRequest.user.id,
"task": helpRequest.task.id,
'experiment': helpRequest.experiment.id,
'condition': helpRequest.condition.id,
"channel_name": helpRequest.task_session.channel_name
}));
helpRequest.task_session.socket.close(code=1000, reason="Changing session",{keepClosed: true});
var element = document.getElementById("chats");
element.outerHTML = "";
delete element;
// Join the Help Request by creating a new task session object
// create new task session
helpRequest.client.task_session = new TaskSession();
var params = {"user": helpRequest.user.id, "task": helpRequest.task.id};
if(helpRequest.experiment){
params = jQuery.extend({'experiment': helpRequest.experiment.id}, params);
}
if(helpRequest.condition){
params = jQuery.extend({'condition': helpRequest.condition.id}, params);
}
params = jQuery.extend({'client': helpRequest.apiClient}, params);
helpRequest.client.task_session.init(params).then(function(){
// update task session info
helpRequest.client.task_session.task_session = {id: data.help_request.task_session.id, type: "TaskSession"};
helpRequest.client.task_session.channel_name = data.help_request.task_session.channel_name;
helpRequest.client.task_session.id = data.help_request.task_session.id;
// update help request info
helpRequest.help_request = data.help_request;
// handle assigning handlers
handlers = {
"odhCreated": helpRequest.CreateHelpRequestOnReply(helpRequest),
"odhResolved": helpRequest.resolveOnReply(helpRequest),
"odhSetFocus": helpRequest.setFocusOnReply(helpRequest),
}
helpRequest.client.task_session.setListeners(handlers);
// connect
helpRequest.task_session = helpRequest.client.task_session;
console.log("Joining new Help Request")
helpRequest.task_session.connect(data.help_request.task_session.id);
});
}
}
// list, CoreAPI?
/**
* Lists the HelpRequests related to the task/experiment
*
* @param: task
* @param: experiment
* @param: condition
* @param: focus
*/
HelpRequest.prototype.list = function(params, callback) {
var that = this;
params = jQuery.extend({
task: that.task.id,
experiment: that.experiment.id,
resolved: false,
}, params);
let action = ['helprequest', "list"];
this.apiClient.action(schema, action, params).then(function(result) {
callback(result);
});
}
// Join, Core API?
/**
* Connects to the task session stored in the help
*
* NOTE: make sure to set tasK_channel.channel_name
*
* @param: help_request
*
*/
HelpRequest.prototype.joinHelpRequest = function(help_request, task_session, channel_name) {
var that = this;
// Leave old task Session
var element = document.getElementById("chats");
element.outerHTML = "";
delete element;
that.task_session.socket.close(code=1000, reason="Changing session",{keepClosed: true});
// Join the Help Request by creating a new task session object
// create new task session
that.client.task_session = new TaskSession();
var params = {"user": that.user.id, "task": that.task.id};
if(that.experiment){
params = jQuery.extend({'experiment': that.experiment.id}, params);
}
if(that.condition){
params = jQuery.extend({'condition': that.condition.id}, params);
}
params = jQuery.extend({'client': that.apiClient}, params);
that.client.task_session.init(params).then(function(){
// update task session info
that.client.task_session.task_session = {id: task_session, type: "TaskSession"};
that.client.task_session.channel_name = channel_name;
that.client.task_session.id = task_session;
// update help request info
that.help_request = {id: help_request};
// handle assigning handlers
handlers = {
"odhCreated": that.CreateHelpRequestOnReply(that),
"odhResolved": that.resolveOnReply(that),
"odhSetFocus": that.setFocusOnReply(that),
}
that.client.task_session.setListeners(handlers);
// connect
that.task_session = that.client.task_session;
console.log("Joining new Help Request")
that.task_session.connect(task_session);
});
}
// Resolve, Core API?
// Deprecated?????
/**
* Resolves the help Request
*
*/
HelpRequest.prototype.resolveHelpRequest = function(callback = null) {
// make an ajax request because CoreAPI doesn't handle this
var that = this;
var params = {
"command": "odh_resolve",
"help_request": that.help_request.id,
}
console.log(params);
that.task_session.socket.send(JSON.stringify(params));
console.log("message sent");
if(callback) {
callback();
}
}
HelpRequest.prototype.resolveOnReply = function(helpRequest) {
return function(message) {
// get rid of old task session
var element = document.getElementById("chats");
element.outerHTML = "";
delete element;
helpRequest.task_session.socket.close(code=1000, reason="Changing session",{keepClosed: true});
// connect to original task session
helpRequest.client.task_session = new TaskSession();
var params = {"user": helpRequest.user.id, "task": helpRequest.task.id};
if(helpRequest.experiment){
params = jQuery.extend({'experiment': helpRequest.experiment.id}, params);
}
if(helpRequest.condition){
params = jQuery.extend({'condition': helpRequest.condition.id}, params);
}
params = jQuery.extend({'client': helpRequest.apiClient}, params);
helpRequest.client.task_session.init(params).then(function(){
// update task session info
helpRequest.client.task_session.task_session = {id: helpRequest.client.task_session.task_session, type: "TaskSession"};
// update help request info
helpRequest.help_request = {id: -1};
// handle assigning handlers
handlers = {
"odhCreated": helpRequest.CreateHelpRequestOnReply(helpRequest),
"odhResolved": helpRequest.resolveOnReply(helpRequest),
"odhSetFocus": helpRequest.setFocusOnReply(helpRequest),
}
helpRequest.client.task_session.setListeners(handlers);
// connect
helpRequest.task_session = helpRequest.client.task_session;
console.log(helpRequest.client.task_session.task_session.id);
helpRequest.task_session.connect(helpRequest.task_session.task_session.id);
});
helpRequest.needsResolving = false;
}
}
// Leave, Core API?
/*
* probably, run JS disconnect?
*/
HelpRequest.prototype.leaveHelpRequest = function(callback) {
var that = this;
print("Disconnected from chat socket");
that.task_session.socket.send(JSON.stringify({
"command": "odh_leave", // determines which handler will be used (see chat/routing.py)
"task_session": that.task_session.id,
"task": that.task.id,
"experiment": that.experiment.id
}));
console.log("cookies:" + document.cookie);
console.log("csrf:" + getCookie("csrftoken"));
xhttpRequest('/onDemandHelp/' + that.help_request.id + "/leave/", null);
}
// Notifications?
/**
* Gotta be done through Collab/WebSockets
*
*
*/
/**
* Sets up all the required internal variables in a help request object
*
* @param: user
* @param: task
* @param: experiment
* @param: condition
*/
HelpRequest.prototype.init = function(params, callback) {
var that = this;
// append to CSS
var style=document.createElement('style');
style.type='text/css';
if(style.styleSheet){
style.styleSheet.cssText=CSS;
}else{
style.appendChild(document.createTextNode(CSS));
}
document.getElementsByTagName('head')[0].appendChild(style);
// Set up VARS
that.apiClient = params['apiClient'];
that.client = params["client"];
// get Task, Experiment, Condition info.
// set (1) task and (2) experiment vars
that.user = {id: params['user'], type: 'User'};
that.task = {id: params['task'], type: 'Task'};
if(params['experiment']){
that.experiment = {id: params['experiment'], type: 'Experiment'}
} else {
that.experiment = null;
}
if(params["condition"]) {
that.condition = {id: params["condition"], type: "Condition"}
} else {
that.condition = null;
}
// make a taskSession
that.task_session = params['task_session'];
/*
that.task_session = new TaskSession();
that.task_session.init(params).then(function(){
resolve();
});
*/
that.help_request = {id: 0}
// handle assigning handlers
handlers = {
"odhCreated": that.CreateHelpRequestOnReply(that),
"odhResolved": that.resolveOnReply(that),
"odhSetFocus": that.setFocusOnReply(that),
}
that.task_session.setListeners(handlers);
if(callback){
callback(that);
}
}
/**