@careevolution/mydatahelps-js 2.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MyDataHelps.d.ts +70 -0
- package/MyDataHelps.js +796 -686
- package/MyDataHelps.min.js +1 -1
- package/MyDataHelps.min.js.map +1 -1
- package/package.json +6 -3
- package/types.d.ts +257 -0
package/MyDataHelps.js
CHANGED
|
@@ -1,690 +1,800 @@
|
|
|
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
|
-
else {
|
|
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
|
-
function
|
|
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
|
-
MyDataHelps.
|
|
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
|
-
|
|
1
|
+
class MyDataHelps {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.allowedApplicationHosts = [
|
|
4
|
+
'localhost',
|
|
5
|
+
'careevolution.com',
|
|
6
|
+
'internal',
|
|
7
|
+
'b3-deploys.com',
|
|
8
|
+
'mydatahelps.org',
|
|
9
|
+
'platform.joinallofus.org',
|
|
10
|
+
'careevolution.dev',
|
|
11
|
+
'ce.dev'
|
|
12
|
+
];
|
|
13
|
+
this.accessTokenRenewalBufferSeconds = 120;
|
|
14
|
+
this.apiBasePath = 'api/v1/delegated/';
|
|
15
|
+
this.unsupportedActions = ["SetHeight", "ResearchKit"];
|
|
16
|
+
this.supportedEvents = ["surveyDidFinish", "applicationDidBecomeVisible", "externalAccountSyncComplete", "tokenWillExpire"];
|
|
17
|
+
this.currentMessageID = 1;
|
|
18
|
+
this.messageHandlers = [];
|
|
19
|
+
this.registeredEventHandlers = {};
|
|
20
|
+
this.refreshTokenPromise = null;
|
|
21
|
+
let mdh = this;
|
|
22
|
+
let applicationHost = getParentOrigin();
|
|
23
|
+
let supportedActions;
|
|
24
|
+
function getParentOrigin() {
|
|
25
|
+
let ancestorOrigins = document.location.ancestorOrigins;
|
|
26
|
+
if (ancestorOrigins && ancestorOrigins[0]) {
|
|
27
|
+
return ancestorOrigins[0];
|
|
28
|
+
}
|
|
29
|
+
return '*';
|
|
30
|
+
}
|
|
31
|
+
function acceptableOrigin(origin) {
|
|
32
|
+
if (origin === '*')
|
|
33
|
+
return true;
|
|
34
|
+
let sourceURL = new URL(origin);
|
|
35
|
+
for (let k = 0; k < mdh.allowedApplicationHosts.length; k++) {
|
|
36
|
+
if (sourceURL.hostname === mdh.allowedApplicationHosts[k] || sourceURL.hostname.endsWith("." + mdh.allowedApplicationHosts[k])) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
function validateWindowMessageOrigin(message) {
|
|
43
|
+
if (applicationHost === "*") {
|
|
44
|
+
return acceptableOrigin(message.origin);
|
|
45
|
+
}
|
|
46
|
+
return message.origin === applicationHost;
|
|
47
|
+
}
|
|
48
|
+
function receiveWindowMessage(message) {
|
|
49
|
+
if (!validateWindowMessageOrigin(message)) {
|
|
50
|
+
console.error("message.origin '" + message.origin + "' is not allowed.");
|
|
51
|
+
throw "message.origin '" + message.origin + "' is not allowed.";
|
|
52
|
+
}
|
|
53
|
+
if (message.data.messageID) {
|
|
54
|
+
mdh.setActionResult(message.data);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
mdh.triggerEvent(message.data);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!acceptableOrigin(applicationHost)) {
|
|
61
|
+
console.error("Application is not hosted at an approved origin.");
|
|
62
|
+
throw "Application is not hosted at an approved origin.";
|
|
63
|
+
}
|
|
64
|
+
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.ResearchKit) {
|
|
65
|
+
supportedActions = {
|
|
66
|
+
"GetDelegatedAccessToken": function (data) {
|
|
67
|
+
window.parent.postMessage({
|
|
68
|
+
name: 'GetDelegatedAccessToken',
|
|
69
|
+
messageID: data.messageID
|
|
70
|
+
}, applicationHost);
|
|
71
|
+
},
|
|
72
|
+
"GetDeviceInfo": function (data) {
|
|
73
|
+
window.parent.postMessage({ name: 'GetDeviceInfo', messageID: data.messageID }, applicationHost);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
supportedActions = {
|
|
79
|
+
"GetDelegatedAccessToken": function (data) {
|
|
80
|
+
window.parent.postMessage({
|
|
81
|
+
name: 'GetDelegatedAccessToken',
|
|
82
|
+
messageID: data.messageID
|
|
83
|
+
}, applicationHost);
|
|
84
|
+
},
|
|
85
|
+
"GetDeviceInfo": function (data) {
|
|
86
|
+
window.parent.postMessage({ name: 'GetDeviceInfo', messageID: data.messageID }, applicationHost);
|
|
87
|
+
},
|
|
88
|
+
"StartParticipantSurvey": function (data) {
|
|
89
|
+
window.parent.postMessage({
|
|
90
|
+
name: 'StartParticipantSurvey',
|
|
91
|
+
messageID: data.messageID,
|
|
92
|
+
surveyName: data.surveyName
|
|
93
|
+
}, applicationHost);
|
|
94
|
+
},
|
|
95
|
+
"OpenExternalLink": function (data) {
|
|
96
|
+
window.parent.postMessage({ name: 'OpenExternalUrl', url: data }, applicationHost);
|
|
97
|
+
},
|
|
98
|
+
"OpenEmbeddedLink": function (data) {
|
|
99
|
+
window.parent.postMessage({ name: 'OpenEmbeddedUrl', url: data }, applicationHost);
|
|
100
|
+
},
|
|
101
|
+
"OpenExternalApplication": function (data) {
|
|
102
|
+
window.parent.postMessage({
|
|
103
|
+
name: 'OpenApplication',
|
|
104
|
+
messageID: data.messageID,
|
|
105
|
+
url: data.url,
|
|
106
|
+
modal: data.modal
|
|
107
|
+
}, applicationHost);
|
|
108
|
+
},
|
|
109
|
+
"Dismiss": function () {
|
|
110
|
+
window.parent.postMessage({ name: 'Dismiss' }, applicationHost);
|
|
111
|
+
},
|
|
112
|
+
"PopNavigation": function () {
|
|
113
|
+
window.parent.postMessage({ name: 'Back' }, applicationHost);
|
|
114
|
+
},
|
|
115
|
+
"ShowTab": function (data) {
|
|
116
|
+
window.parent.postMessage({ name: 'ShowTab', tabKey: data }, applicationHost);
|
|
117
|
+
},
|
|
118
|
+
"ShowParticipantDashboard": function (data) {
|
|
119
|
+
window.parent.postMessage({
|
|
120
|
+
name: 'ShowParticipantDashboard',
|
|
121
|
+
dashboardKey: data.dashboardKey,
|
|
122
|
+
modal: data.modal
|
|
123
|
+
}, applicationHost);
|
|
124
|
+
},
|
|
125
|
+
"ShowParticipantWebVisualization": function (data) {
|
|
126
|
+
window.parent.postMessage({
|
|
127
|
+
name: 'ShowParticipantWebVisualization',
|
|
128
|
+
visualizationKey: data.visualizationKey,
|
|
129
|
+
parameters: data.parameters,
|
|
130
|
+
modal: data.modal
|
|
131
|
+
}, applicationHost);
|
|
132
|
+
},
|
|
133
|
+
"ShowParticipantWebVisualizationPDF": function (data) {
|
|
134
|
+
window.parent.postMessage({
|
|
135
|
+
name: 'ShowParticipantWebVisualizationPDF',
|
|
136
|
+
visualizationKey: data.visualizationKey,
|
|
137
|
+
parameters: data.parameters,
|
|
138
|
+
modal: data.modal,
|
|
139
|
+
landscape: data.landscape,
|
|
140
|
+
htmlViewerZoom: data.htmlViewerZoom
|
|
141
|
+
}, applicationHost);
|
|
142
|
+
},
|
|
143
|
+
"DeleteProviderAccount": function (data) {
|
|
144
|
+
window.parent.postMessage({
|
|
145
|
+
name: 'DeleteProviderAccount',
|
|
146
|
+
messageID: data.messageID,
|
|
147
|
+
accountID: data.accountID
|
|
148
|
+
}, applicationHost);
|
|
149
|
+
},
|
|
150
|
+
"ConnectExternalAccount": function (data) {
|
|
151
|
+
window.parent.postMessage({
|
|
152
|
+
name: 'ConnectExternalAccount',
|
|
153
|
+
externalAccountProviderID: data.externalAccountProviderID
|
|
154
|
+
}, applicationHost);
|
|
155
|
+
},
|
|
156
|
+
"ShowProject": function (data) {
|
|
157
|
+
window.parent.postMessage({ name: 'ShowProject', code: data.code }, applicationHost);
|
|
158
|
+
},
|
|
159
|
+
"JoinProject": function (data) {
|
|
160
|
+
window.parent.postMessage({ name: 'JoinProject', code: data.code }, applicationHost);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function windowHasAnyActions() {
|
|
165
|
+
for (let action in supportedActions) {
|
|
166
|
+
if (window.webkit.messageHandlers[action]) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
for (let action in mdh.unsupportedActions) {
|
|
171
|
+
if (window.webkit.messageHandlers[mdh.unsupportedActions[action]]) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (!window.webkit || !window.webkit.messageHandlers || !windowHasAnyActions()) {
|
|
178
|
+
window.webkit = {
|
|
179
|
+
messageHandlers: {}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
for (let action in supportedActions) {
|
|
183
|
+
window.webkit.messageHandlers[action] = window.webkit.messageHandlers[action] || { postMessage: supportedActions[action] };
|
|
184
|
+
}
|
|
185
|
+
window.addEventListener("message", receiveWindowMessage, false);
|
|
186
|
+
}
|
|
187
|
+
// Surveys
|
|
188
|
+
startSurvey(surveyName) {
|
|
189
|
+
let messageID = this.nextMessageID();
|
|
190
|
+
window.webkit.messageHandlers.StartParticipantSurvey.postMessage({
|
|
191
|
+
messageID: messageID,
|
|
192
|
+
surveyName: surveyName
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
completeStep(answer) {
|
|
196
|
+
if (answer === undefined || answer === null)
|
|
197
|
+
answer = '';
|
|
198
|
+
if (window.webkit.messageHandlers.ResearchKit) {
|
|
199
|
+
window.webkit.messageHandlers.ResearchKit.postMessage(answer);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
querySurveyAnswers(queryParameters) {
|
|
203
|
+
if (queryParameters.hasOwnProperty("surveyName")) {
|
|
204
|
+
queryParameters.surveyName = MyDataHelps.reduceArrayAndEscape(queryParameters.surveyName);
|
|
205
|
+
}
|
|
206
|
+
if (queryParameters.hasOwnProperty("stepIdentifier")) {
|
|
207
|
+
queryParameters.stepIdentifier = MyDataHelps.reduceArrayAndEscape(queryParameters.stepIdentifier);
|
|
208
|
+
}
|
|
209
|
+
if (queryParameters.hasOwnProperty("resultIdentifier")) {
|
|
210
|
+
queryParameters.resultIdentifier = MyDataHelps.reduceArrayAndEscape(queryParameters.resultIdentifier);
|
|
211
|
+
}
|
|
212
|
+
if (queryParameters.hasOwnProperty("answer")) {
|
|
213
|
+
queryParameters.answer = MyDataHelps.reduceArrayAndEscape(queryParameters.answer);
|
|
214
|
+
}
|
|
215
|
+
if (queryParameters.hasOwnProperty("before")) {
|
|
216
|
+
queryParameters.before = MyDataHelps.convertDateToIsoString(queryParameters.before);
|
|
217
|
+
}
|
|
218
|
+
if (queryParameters.hasOwnProperty("after")) {
|
|
219
|
+
queryParameters.after = MyDataHelps.convertDateToIsoString(queryParameters.after);
|
|
220
|
+
}
|
|
221
|
+
const queryString = new URLSearchParams(queryParameters).toString();
|
|
222
|
+
const endpoint = 'surveyanswers?' + queryString;
|
|
223
|
+
const mdh = this;
|
|
224
|
+
return this
|
|
225
|
+
.connect()
|
|
226
|
+
.then(function () {
|
|
227
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
228
|
+
})
|
|
229
|
+
.then(function (response) {
|
|
230
|
+
return MyDataHelps.validateResponse(response);
|
|
231
|
+
})
|
|
232
|
+
.then(function (response) {
|
|
233
|
+
return response.json();
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
deleteSurveyResult(resultID) {
|
|
237
|
+
const endpoint = 'surveyresults/' + encodeURIComponent(resultID);
|
|
238
|
+
const mdh = this;
|
|
239
|
+
return this
|
|
240
|
+
.connect()
|
|
241
|
+
.then(function () {
|
|
242
|
+
return mdh.makeRequest(endpoint, 'DELETE', null);
|
|
243
|
+
})
|
|
244
|
+
.then(function (response) {
|
|
245
|
+
return MyDataHelps.validateResponse(response);
|
|
246
|
+
})
|
|
247
|
+
.then(function () {
|
|
248
|
+
return;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
querySurveyTasks(queryParameters) {
|
|
252
|
+
if (queryParameters.hasOwnProperty("status")) {
|
|
253
|
+
queryParameters.status = MyDataHelps.reduceArrayAndEscape(queryParameters.status);
|
|
254
|
+
}
|
|
255
|
+
if (queryParameters.hasOwnProperty("surveyName")) {
|
|
256
|
+
queryParameters.surveyName = MyDataHelps.reduceArrayAndEscape(queryParameters.surveyName);
|
|
257
|
+
}
|
|
258
|
+
const queryString = new URLSearchParams(queryParameters).toString();
|
|
259
|
+
const endpoint = 'surveytasks?' + queryString;
|
|
260
|
+
const mdh = this;
|
|
261
|
+
return this
|
|
262
|
+
.connect()
|
|
263
|
+
.then(function () {
|
|
264
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
265
|
+
})
|
|
266
|
+
.then(function (response) {
|
|
267
|
+
return MyDataHelps.validateResponse(response);
|
|
268
|
+
})
|
|
269
|
+
.then(function (response) {
|
|
270
|
+
return response.json();
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
startEmbeddedSurvey(linkIdentifier, surveyName, language) {
|
|
274
|
+
if (!language) {
|
|
275
|
+
language = this.getCurrentLanguage();
|
|
276
|
+
}
|
|
277
|
+
let surveyUrl = this.getBaseUrl() + "mydatahelps/" + linkIdentifier + '/surveylink/' + surveyName + "?lang=" + language;
|
|
278
|
+
return this.startEmbeddedSurveyInternal(surveyUrl);
|
|
279
|
+
}
|
|
280
|
+
startEmbeddedTask(linkIdentifier, taskLinkIdentifier, language) {
|
|
281
|
+
if (!language) {
|
|
282
|
+
language = this.getCurrentLanguage();
|
|
283
|
+
}
|
|
284
|
+
let surveyUrl = this.getBaseUrl() + "mydatahelps/" + linkIdentifier + '/tasklink/' + taskLinkIdentifier + "?lang=" + language;
|
|
285
|
+
return this.startEmbeddedSurveyInternal(surveyUrl);
|
|
286
|
+
}
|
|
287
|
+
// Navigation
|
|
288
|
+
openExternalUrl(url) {
|
|
289
|
+
window.webkit.messageHandlers.OpenExternalLink.postMessage(url);
|
|
290
|
+
}
|
|
291
|
+
openEmbeddedUrl(url) {
|
|
292
|
+
window.webkit.messageHandlers.OpenEmbeddedLink.postMessage(url);
|
|
293
|
+
}
|
|
294
|
+
showTab(tabKey) {
|
|
295
|
+
window.webkit.messageHandlers.ShowTab.postMessage(tabKey);
|
|
296
|
+
}
|
|
297
|
+
openApplication(url, options) {
|
|
298
|
+
let messageID = this.nextMessageID();
|
|
299
|
+
window.webkit.messageHandlers.OpenExternalApplication.postMessage({
|
|
300
|
+
messageID: messageID,
|
|
301
|
+
url: url,
|
|
302
|
+
modal: !!(options && options.modal)
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
dismiss() {
|
|
306
|
+
window.webkit.messageHandlers.Dismiss.postMessage({});
|
|
307
|
+
}
|
|
308
|
+
back() {
|
|
309
|
+
window.webkit.messageHandlers.PopNavigation.postMessage({});
|
|
310
|
+
}
|
|
311
|
+
showDashboard(dashboardKey, options) {
|
|
312
|
+
window.webkit.messageHandlers.ShowParticipantDashboard.postMessage({
|
|
313
|
+
dashboardKey: dashboardKey,
|
|
314
|
+
modal: !!(options && options.modal),
|
|
315
|
+
title: options && options.title ? options.title : undefined
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
showWebVisualization(visualizationKey, parameters, options) {
|
|
319
|
+
window.webkit.messageHandlers.ShowParticipantWebVisualization.postMessage({
|
|
320
|
+
visualizationKey: visualizationKey,
|
|
321
|
+
parameters: parameters,
|
|
322
|
+
modal: !!(options && options.modal),
|
|
323
|
+
title: options && options.title ? options.title : undefined
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
showWebVisualizationPdf(visualizationKey, parameters, options) {
|
|
327
|
+
window.webkit.messageHandlers.ShowParticipantWebVisualizationPDF.postMessage({
|
|
328
|
+
visualizationKey: visualizationKey,
|
|
329
|
+
parameters: parameters,
|
|
330
|
+
landscape: !!(options && options.landscape),
|
|
331
|
+
htmlViewerZoom: options && options.htmlViewerZoom
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
showProject(projectCode) {
|
|
335
|
+
if (window.webkit.messageHandlers.ShowProject) {
|
|
336
|
+
window.webkit.messageHandlers.ShowProject.postMessage({ code: projectCode });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
joinProject(projectCode) {
|
|
340
|
+
if (window.webkit.messageHandlers.JoinProject) {
|
|
341
|
+
window.webkit.messageHandlers.JoinProject.postMessage({ code: projectCode });
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
//Events
|
|
345
|
+
on(eventName, callback) {
|
|
346
|
+
if (!this.supportedEvents.includes(eventName)) {
|
|
347
|
+
throw new Error(eventName + " is not a supported event type.");
|
|
348
|
+
}
|
|
349
|
+
if (!this.registeredEventHandlers[eventName]) {
|
|
350
|
+
this.registeredEventHandlers[eventName] = [];
|
|
351
|
+
}
|
|
352
|
+
this.registeredEventHandlers[eventName].push(callback);
|
|
353
|
+
}
|
|
354
|
+
off(eventName, callback) {
|
|
355
|
+
if (!this.supportedEvents.includes(eventName)) {
|
|
356
|
+
throw new Error(eventName + " is not a supported event type.");
|
|
357
|
+
}
|
|
358
|
+
if (!this.registeredEventHandlers[eventName])
|
|
359
|
+
return;
|
|
360
|
+
let eventHandlerIndex = this.registeredEventHandlers[eventName].indexOf(callback);
|
|
361
|
+
if (eventHandlerIndex !== -1) {
|
|
362
|
+
this.registeredEventHandlers[eventName].splice(eventHandlerIndex, 1);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
triggerEvent(event) {
|
|
366
|
+
let eventName = event.type;
|
|
367
|
+
if (this.supportedEvents.includes(eventName) && this.registeredEventHandlers[eventName]) {
|
|
368
|
+
this.registeredEventHandlers[eventName].forEach(function (handler) {
|
|
369
|
+
handler(event);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
setActionResult(data) {
|
|
374
|
+
this.messageHandlers[data.messageID](data);
|
|
375
|
+
}
|
|
376
|
+
// Participant Info
|
|
377
|
+
getParticipantInfo() {
|
|
378
|
+
const mdh = this;
|
|
379
|
+
const endpoint = 'participant';
|
|
380
|
+
return this
|
|
381
|
+
.connect()
|
|
382
|
+
.then(function () {
|
|
383
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
384
|
+
})
|
|
385
|
+
.then(function (response) {
|
|
386
|
+
return MyDataHelps.validateResponse(response);
|
|
387
|
+
})
|
|
388
|
+
.then(function (response) {
|
|
389
|
+
return response.json();
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
getProjectInfo() {
|
|
393
|
+
const mdh = this;
|
|
394
|
+
const endpoint = 'project';
|
|
395
|
+
return this
|
|
396
|
+
.connect()
|
|
397
|
+
.then(function () {
|
|
398
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
399
|
+
})
|
|
400
|
+
.then(function (response) {
|
|
401
|
+
return MyDataHelps.validateResponse(response);
|
|
402
|
+
})
|
|
403
|
+
.then(function (response) {
|
|
404
|
+
return response.json();
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
// Device Data
|
|
408
|
+
queryDeviceData(queryParameters) {
|
|
409
|
+
if (queryParameters.hasOwnProperty("type")) {
|
|
410
|
+
queryParameters.type = MyDataHelps.reduceArrayAndEscape(queryParameters.type);
|
|
411
|
+
}
|
|
412
|
+
if (queryParameters.hasOwnProperty("observedBefore")) {
|
|
413
|
+
queryParameters.observedBefore = MyDataHelps.convertDateToIsoString(queryParameters.observedBefore);
|
|
414
|
+
}
|
|
415
|
+
if (queryParameters.hasOwnProperty("observedAfter")) {
|
|
416
|
+
queryParameters.observedAfter = MyDataHelps.convertDateToIsoString(queryParameters.observedAfter);
|
|
417
|
+
}
|
|
418
|
+
const queryString = new URLSearchParams(queryParameters).toString();
|
|
419
|
+
const endpoint = 'devicedata?' + queryString;
|
|
420
|
+
const mdh = this;
|
|
421
|
+
return this
|
|
422
|
+
.connect()
|
|
423
|
+
.then(function () {
|
|
424
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
425
|
+
})
|
|
426
|
+
.then(function (response) {
|
|
427
|
+
return MyDataHelps.validateResponse(response);
|
|
428
|
+
})
|
|
429
|
+
.then(function (response) {
|
|
430
|
+
return response.json();
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
persistDeviceData(deviceDataPoints) {
|
|
434
|
+
const endpoint = 'devicedata';
|
|
435
|
+
const mdh = this;
|
|
436
|
+
return this
|
|
437
|
+
.connect()
|
|
438
|
+
.then(function () {
|
|
439
|
+
return mdh.makeRequest(endpoint, 'POST', deviceDataPoints);
|
|
440
|
+
})
|
|
441
|
+
.then(function (response) {
|
|
442
|
+
return MyDataHelps.validateResponse(response);
|
|
443
|
+
})
|
|
444
|
+
.then(function () {
|
|
445
|
+
return;
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
// External Accounts
|
|
449
|
+
getExternalAccountProviders(search, category, pageSize, pageNumber) {
|
|
450
|
+
let searchParameters = {};
|
|
451
|
+
if (!!search)
|
|
452
|
+
searchParameters.search = search;
|
|
453
|
+
if (!!category)
|
|
454
|
+
searchParameters.category = category;
|
|
455
|
+
if (!!pageSize)
|
|
456
|
+
searchParameters.pageSize = pageSize;
|
|
457
|
+
if (!!pageNumber)
|
|
458
|
+
searchParameters.pageNumber = pageNumber;
|
|
459
|
+
const queryString = new URLSearchParams(searchParameters).toString();
|
|
460
|
+
const endpoint = 'externalaccountproviders?' + queryString;
|
|
461
|
+
const mdh = this;
|
|
462
|
+
return this
|
|
463
|
+
.connect()
|
|
464
|
+
.then(function () {
|
|
465
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
466
|
+
})
|
|
467
|
+
.then(function (response) {
|
|
468
|
+
return MyDataHelps.validateResponse(response);
|
|
469
|
+
})
|
|
470
|
+
.then(function (response) {
|
|
471
|
+
return response.json();
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
connectExternalAccount(externalAccountProviderID) {
|
|
475
|
+
if (window.webkit.messageHandlers.ConnectExternalAccount) {
|
|
476
|
+
window.webkit.messageHandlers.ConnectExternalAccount.postMessage({ externalAccountProviderID: externalAccountProviderID });
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
getExternalAccounts() {
|
|
480
|
+
const endpoint = 'externalaccounts';
|
|
481
|
+
const mdh = this;
|
|
482
|
+
return this
|
|
483
|
+
.connect()
|
|
484
|
+
.then(function () {
|
|
485
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
486
|
+
})
|
|
487
|
+
.then(function (response) {
|
|
488
|
+
return MyDataHelps.validateResponse(response);
|
|
489
|
+
})
|
|
490
|
+
.then(function (response) {
|
|
491
|
+
return response.json();
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
refreshExternalAccount(accountID) {
|
|
495
|
+
const endpoint = 'externalaccounts/refresh/' + accountID;
|
|
496
|
+
const mdh = this;
|
|
497
|
+
return this
|
|
498
|
+
.connect()
|
|
499
|
+
.then(function () {
|
|
500
|
+
return mdh.makeRequest(endpoint, 'POST', null);
|
|
501
|
+
})
|
|
502
|
+
.then(function (response) {
|
|
503
|
+
return MyDataHelps.validateResponse(response);
|
|
504
|
+
})
|
|
505
|
+
.then(function () {
|
|
506
|
+
return;
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
deleteExternalAccount(accountID) {
|
|
510
|
+
let mdh = this;
|
|
511
|
+
return new Promise(function (resolve, reject) {
|
|
512
|
+
let messageID = mdh.nextMessageID();
|
|
513
|
+
mdh.messageHandlers[messageID] = function (deleteProviderAccountResponse) {
|
|
514
|
+
if (!deleteProviderAccountResponse.success) {
|
|
515
|
+
reject(deleteProviderAccountResponse);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
resolve(deleteProviderAccountResponse);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
if (window.webkit.messageHandlers.DeleteProviderAccount) {
|
|
522
|
+
window.webkit.messageHandlers.DeleteProviderAccount.postMessage({
|
|
523
|
+
messageID: messageID,
|
|
524
|
+
accountID: accountID
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
reject();
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
// Notifications
|
|
533
|
+
queryNotifications(queryParameters) {
|
|
534
|
+
if (queryParameters.hasOwnProperty("sentBefore")) {
|
|
535
|
+
queryParameters.sentBefore = MyDataHelps.convertDateToIsoString(queryParameters.sentBefore);
|
|
536
|
+
}
|
|
537
|
+
if (queryParameters.hasOwnProperty("sentAfter")) {
|
|
538
|
+
queryParameters.sentAfter = MyDataHelps.convertDateToIsoString(queryParameters.sentAfter);
|
|
539
|
+
}
|
|
540
|
+
const queryString = new URLSearchParams(queryParameters).toString();
|
|
541
|
+
const endpoint = 'notifications?' + queryString;
|
|
542
|
+
const mdh = this;
|
|
543
|
+
return this
|
|
544
|
+
.connect()
|
|
545
|
+
.then(function () {
|
|
546
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
547
|
+
})
|
|
548
|
+
.then(function (response) {
|
|
549
|
+
return MyDataHelps.validateResponse(response);
|
|
550
|
+
})
|
|
551
|
+
.then(function (response) {
|
|
552
|
+
return response.json();
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
// Authorization
|
|
556
|
+
connect() {
|
|
557
|
+
const mdh = this;
|
|
558
|
+
let refreshDelegatedAccessToken = function () {
|
|
559
|
+
mdh.token = null;
|
|
560
|
+
mdh.refreshTokenPromise = new Promise(function (resolve, reject) {
|
|
561
|
+
let messageID = mdh.nextMessageID();
|
|
562
|
+
mdh.messageHandlers[messageID] = function (tokenResponse) {
|
|
563
|
+
if (tokenResponse.success) {
|
|
564
|
+
mdh.tokenExpires = Date.now() + (tokenResponse.data.expires_in * 1000);
|
|
565
|
+
mdh.token = tokenResponse.data;
|
|
566
|
+
mdh.baseUrl = tokenResponse.baseUrl;
|
|
567
|
+
setTimeout(refreshDelegatedAccessToken, ((tokenResponse.data.expires_in - mdh.accessTokenRenewalBufferSeconds) * 1000));
|
|
568
|
+
resolve(null);
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
mdh.token = null;
|
|
572
|
+
reject(tokenResponse.message);
|
|
573
|
+
}
|
|
574
|
+
mdh.messageHandlers[messageID] = null;
|
|
575
|
+
mdh.refreshTokenPromise = null;
|
|
576
|
+
};
|
|
577
|
+
window.webkit.messageHandlers.GetDelegatedAccessToken.postMessage({ messageID: messageID });
|
|
578
|
+
});
|
|
579
|
+
};
|
|
580
|
+
if (this.token && Date.now() < this.tokenExpires) {
|
|
581
|
+
return Promise.resolve();
|
|
582
|
+
}
|
|
583
|
+
if (!this.refreshTokenPromise) {
|
|
584
|
+
refreshDelegatedAccessToken();
|
|
585
|
+
}
|
|
586
|
+
// Re-use promise if refresh already in progress
|
|
587
|
+
return this.refreshTokenPromise;
|
|
588
|
+
}
|
|
589
|
+
setParticipantAccessToken(token, baseUrl) {
|
|
590
|
+
this.token = token;
|
|
591
|
+
if (baseUrl) {
|
|
592
|
+
this.baseUrl = baseUrl;
|
|
593
|
+
}
|
|
594
|
+
this.tokenExpires = Date.now() + (this.token.expires_in * 1000);
|
|
595
|
+
const mdh = this;
|
|
596
|
+
setTimeout(function () {
|
|
597
|
+
mdh.triggerEvent({ type: "tokenWillExpire" });
|
|
598
|
+
}, ((this.token.expires_in - this.accessTokenRenewalBufferSeconds) * 1000));
|
|
599
|
+
}
|
|
600
|
+
// Miscellaneous
|
|
601
|
+
setStatusBarStyle(style) {
|
|
602
|
+
if (window.webkit.messageHandlers.SetStatusBarStyle) {
|
|
603
|
+
window.webkit.messageHandlers.SetStatusBarStyle.postMessage({ style: style });
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
getDeviceInfo() {
|
|
607
|
+
let mdh = this;
|
|
608
|
+
return new Promise(function (resolve, reject) {
|
|
609
|
+
let messageID = mdh.nextMessageID();
|
|
610
|
+
mdh.messageHandlers[messageID] = function (deviceInfoResponse) {
|
|
611
|
+
resolve(deviceInfoResponse.data);
|
|
612
|
+
};
|
|
613
|
+
if (window.webkit.messageHandlers.GetDeviceInfo) {
|
|
614
|
+
window.webkit.messageHandlers.GetDeviceInfo.postMessage({ messageID: messageID });
|
|
615
|
+
}
|
|
616
|
+
else {
|
|
617
|
+
reject();
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
getCurrentLanguage() {
|
|
622
|
+
let searchParams = new URLSearchParams(window.location.search);
|
|
623
|
+
if (searchParams.has("lang")) {
|
|
624
|
+
return searchParams.get("lang");
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
return navigator.language;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
showGoogleFitSettings() {
|
|
631
|
+
if (window.webkit.messageHandlers.ShowGoogleFitSettings) {
|
|
632
|
+
window.webkit.messageHandlers.ShowGoogleFitSettings.postMessage({});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
requestReview(cooldownDays) {
|
|
636
|
+
if (window.webkit.messageHandlers.RequestReview) {
|
|
637
|
+
window.webkit.messageHandlers.RequestReview.postMessage({ cooldownDays: cooldownDays });
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
trackCustomEvent(event) {
|
|
641
|
+
const endpoint = 'customevents';
|
|
642
|
+
const mdh = this;
|
|
643
|
+
return this
|
|
644
|
+
.connect()
|
|
645
|
+
.then(function () {
|
|
646
|
+
return mdh.makeRequest(endpoint, 'POST', event);
|
|
647
|
+
})
|
|
648
|
+
.then(function (response) {
|
|
649
|
+
return MyDataHelps.validateResponse(response);
|
|
650
|
+
})
|
|
651
|
+
.then(function () {
|
|
652
|
+
return;
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
getDataCollectionSettings() {
|
|
656
|
+
const mdh = this;
|
|
657
|
+
const endpoint = 'datacollectionsettings';
|
|
658
|
+
return this
|
|
659
|
+
.connect()
|
|
660
|
+
.then(function () {
|
|
661
|
+
return mdh.makeRequest(endpoint, 'GET', null);
|
|
662
|
+
})
|
|
663
|
+
.then(function (response) {
|
|
664
|
+
return MyDataHelps.validateResponse(response);
|
|
665
|
+
})
|
|
666
|
+
.then(function (response) {
|
|
667
|
+
return response.json();
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
invokeCustomApi(customApi, method, queryParameters, jsonResponse) {
|
|
671
|
+
if (customApi.includes('?')) {
|
|
672
|
+
throw new Error("Cannot include query parameters directly in the \"customApi\" string. Provide as an argument for \"queryParameters\" instead.");
|
|
673
|
+
}
|
|
674
|
+
let endpoint = 'custom/' + customApi;
|
|
675
|
+
method = method.toUpperCase();
|
|
676
|
+
if (queryParameters !== null && queryParameters !== undefined && (method === 'GET' || method === 'DELETE')) {
|
|
677
|
+
let parameterString = new URLSearchParams(queryParameters).toString();
|
|
678
|
+
endpoint += "?" + parameterString;
|
|
679
|
+
queryParameters = null;
|
|
680
|
+
}
|
|
681
|
+
const mdh = this;
|
|
682
|
+
return this
|
|
683
|
+
.connect()
|
|
684
|
+
.then(function () {
|
|
685
|
+
return mdh.makeRequest(endpoint, method, queryParameters);
|
|
686
|
+
})
|
|
687
|
+
.then(function (response) {
|
|
688
|
+
return MyDataHelps.validateResponse(response);
|
|
689
|
+
})
|
|
690
|
+
.then(function (response) {
|
|
691
|
+
return jsonResponse ? response.json() : undefined;
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
nextMessageID() {
|
|
695
|
+
return this.currentMessageID++;
|
|
696
|
+
}
|
|
697
|
+
getBaseUrl() {
|
|
698
|
+
return this.baseUrl || 'https://mydatahelps.org/';
|
|
699
|
+
}
|
|
700
|
+
makeUrl(endpoint) {
|
|
701
|
+
if (!this.baseUrl) {
|
|
702
|
+
console.error("Cannot use makeUrl without MyDataHelps.baseUrl.");
|
|
703
|
+
throw "Cannot use makeUrl without MyDataHelps.baseUrl.";
|
|
704
|
+
}
|
|
705
|
+
return this.baseUrl + this.apiBasePath + endpoint;
|
|
706
|
+
}
|
|
707
|
+
makeRequest(endpoint, method, body) {
|
|
708
|
+
if (!this.token || !this.token.access_token) {
|
|
709
|
+
throw "No access_token available for request authorization.";
|
|
710
|
+
}
|
|
711
|
+
let url = this.makeUrl(endpoint);
|
|
712
|
+
let headers = new Headers();
|
|
713
|
+
headers.append('Authorization', 'Bearer ' + this.token.access_token);
|
|
714
|
+
headers.append('Accept', 'application/json, text/javascript, */*; q=0.01');
|
|
715
|
+
headers.append('Accept-Language', this.getCurrentLanguage());
|
|
716
|
+
if (!!body) {
|
|
717
|
+
headers.append('Content-Type', 'application/json');
|
|
718
|
+
}
|
|
719
|
+
let init = {
|
|
720
|
+
method: method,
|
|
721
|
+
headers: headers
|
|
722
|
+
};
|
|
723
|
+
if (!!body) {
|
|
724
|
+
init.body = JSON.stringify(body);
|
|
725
|
+
}
|
|
726
|
+
return fetch(url, init);
|
|
727
|
+
}
|
|
728
|
+
static validateResponse(response) {
|
|
729
|
+
if (!response.ok) {
|
|
730
|
+
throw response.statusText;
|
|
731
|
+
}
|
|
732
|
+
return response;
|
|
733
|
+
}
|
|
734
|
+
static escapeParam(param) {
|
|
735
|
+
return String(param)
|
|
736
|
+
.replace("\\", "\\\\")
|
|
737
|
+
.replace(",", "\\,");
|
|
738
|
+
}
|
|
739
|
+
static reduceArrayAndEscape(param) {
|
|
740
|
+
if (Array.isArray(param)) {
|
|
741
|
+
let escapedParams = param.map(function (elem) {
|
|
742
|
+
return MyDataHelps.escapeParam(elem);
|
|
743
|
+
});
|
|
744
|
+
return escapedParams.join(",");
|
|
745
|
+
}
|
|
746
|
+
return this.escapeParam(param);
|
|
747
|
+
}
|
|
748
|
+
static convertDateToIsoString(date) {
|
|
749
|
+
let paramAsDate = new Date(date);
|
|
750
|
+
if (isNaN(paramAsDate.getTime())) {
|
|
751
|
+
throw "Cannot interpret parameter as Date";
|
|
752
|
+
}
|
|
753
|
+
return paramAsDate.toISOString();
|
|
754
|
+
}
|
|
755
|
+
startEmbeddedSurveyInternal(surveyUrl) {
|
|
756
|
+
let mdhSurveyModalId = 'mydatahelps-survey-modal';
|
|
757
|
+
let mdh = this;
|
|
758
|
+
return new Promise(function (resolve, reject) {
|
|
759
|
+
if (document.getElementById(mdhSurveyModalId)) {
|
|
760
|
+
reject("Survey already in progress");
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
window.addEventListener("message", function listener(message) {
|
|
764
|
+
if (message.origin !== new URL(mdh.getBaseUrl()).origin) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
let frame = document.getElementById('mydatahelps-survey-frame').contentWindow;
|
|
768
|
+
if (message.source !== frame) {
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
if (message.data.name === 'SurveyWindowInitialized') {
|
|
772
|
+
document.getElementById(mdhSurveyModalId).className += " loaded";
|
|
773
|
+
}
|
|
774
|
+
else if (message.data.name === 'SurveyFinished') {
|
|
775
|
+
document.getElementById(mdhSurveyModalId).remove();
|
|
776
|
+
document.body.className = document.body.className.replace("no-scroll", "");
|
|
777
|
+
window.removeEventListener("message", listener, true);
|
|
778
|
+
resolve(message.data);
|
|
779
|
+
}
|
|
780
|
+
}, true);
|
|
781
|
+
let surveyModal = document.createElement('div');
|
|
782
|
+
surveyModal.className = 'mydatahelps-survey-modal';
|
|
783
|
+
surveyModal.id = mdhSurveyModalId;
|
|
784
|
+
surveyModal.innerHTML = "<div class='mydatahelps-survey'><div class='loader'>Loading...</div><iframe id='mydatahelps-survey-frame' allow='camera' sandbox='allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-scripts' src='" + surveyUrl + "'></iframe></div>";
|
|
785
|
+
document.body.append(surveyModal);
|
|
786
|
+
document.body.className += ' no-scroll';
|
|
787
|
+
});
|
|
788
|
+
}
|
|
683
789
|
}
|
|
684
|
-
|
|
685
790
|
//iOS/Android directly invoke window.MyDataHelps and window.RKStudioClient
|
|
686
791
|
//so it's necessary to explicitly ensure they are present on the window
|
|
687
|
-
|
|
688
|
-
window.
|
|
792
|
+
const mdh = new MyDataHelps();
|
|
793
|
+
window.MyDataHelps = mdh;
|
|
794
|
+
// For backwards compatibility
|
|
795
|
+
window.RKStudioClient = {};
|
|
796
|
+
window.RKStudioClient.setActionResult = function (data) {
|
|
797
|
+
window.MyDataHelps.setActionResult(data);
|
|
798
|
+
};
|
|
689
799
|
|
|
690
|
-
export { MyDataHelps as default };
|
|
800
|
+
export { MyDataHelps, mdh as default };
|