-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsent.js
More file actions
216 lines (174 loc) · 6 KB
/
Copy pathConsent.js
File metadata and controls
216 lines (174 loc) · 6 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
/**
* # Consent
* Copyright(c) 2021 Stefano Balietti
* MIT Licensed
*
* Displays a consent form with buttons to accept/reject it
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('Consent', Consent);
// ## Meta-data
Consent.version = '0.3.0';
Consent.description = 'Displays a configurable consent form.';
Consent.title = false;
Consent.panel = false;
Consent.className = 'consent';
Consent.texts = {
areYouSure: 'You did not consent and are about to leave the ' +
'study. Are you sure?',
printText:
'<br/><p>If you need a copy of this consent form, you may ' +
'print a copy of this page for your records.</p>',
printBtn: 'Print this page',
consentTerms: 'Do you understand and consent to these terms?',
agree: 'Yes, I agree',
notAgree: 'No, I do not agree',
showHideConsent: function(w, s) {
return (s === 'hide' ? 'Hide' : 'Show') + ' Consent Form';
}
};
/**
* ## Consent constructor
*
* Creates a new instance of Consent
*
* @param {object} options Optional. Configuration options
* which is forwarded to Consent.init.
*
* @see Consent.init
*/
function Consent() {
/**
* ## Consent.consent
*
* The object containing the variables to substitute
*
* Default: node.game.settings.CONSENT
*/
this.consent = null;
/**
* ## Consent.showPrint
*
* If TRUE, the print button is shown
*
* Default: TRUE
*/
this.showPrint = null;
}
// ## Consent methods.
/**
* ### Consent.init
*
* Initializes the widget
*
* @param {object} opts Optional. Configuration options.
*/
Consent.prototype.init = function(opts) {
opts = opts || {};
this.consent = opts.consent || node.game.settings.CONSENT;
if (this.consent && 'object' !== typeof this.consent) {
throw new TypeError('Consent: consent must be object or ' +
'undefined. Found: ' + this.consent);
}
this.showPrint = opts.showPrint === false ? false : true;
};
Consent.prototype.enable = function() {
var a, na;
if (this.notAgreed) return;
a = W.gid('agree');
if (a) a.disabled = false;
na = W.gid('notAgree');
if (na) na.disabled = false;
};
Consent.prototype.disable = function() {
var a, na;
if (this.notAgreed) return;
a = W.gid('agree');
if (a) a.disabled = true;
na = W.gid('notAgree');
if (na) na.disabled = true;
};
Consent.prototype.append = function() {
var consent, html;
// Hide not agreed div.
W.hide('notAgreed');
consent = W.gid('consent');
html = '';
// Print.
if (this.showPrint) {
html = this.getText('printText');
html += '<input class="btn" type="button" value="' +
this.getText('printBtn') +
'" onclick="window.print()" /><br/><br/>';
}
// Header for buttons.
html += '<strong>' + this.getText('consentTerms') + '</strong><br/>';
// Buttons.
html += '<div style="margin-top: 20px;">' +
'<button class="btn btn-lg btn-info" id="agree" ' +
'style="margin-right: 30px">' + this.getText('agree') +
'</button><button class="btn btn-lg btn-danger" id="notAgree">' +
this.getText('notAgree') + '</button></div>';
consent.innerHTML += html;
setTimeout(function() { W.adjustFrameHeight(); });
};
Consent.prototype.listeners = function() {
var that = this;
var consent = this.consent;
node.on('FRAME_LOADED', function() {
var a, na, p, id;
// Replace all texts.
if (consent) {
for (p in consent) {
if (consent.hasOwnProperty(p)) {
// Making lower-case and replacing underscore
// s with dashes.
id = p.toLowerCase();
id = id.replace(new RegExp("_", 'g'), "-");
W.setInnerHTML(id, consent[p]);
}
}
}
// Add listeners on buttons.
a = W.gid('agree');
na = W.gid('notAgree');
if (!a) throw new Error('Consent: agree button not found');
if (!na) throw new Error('Consent: notAgree button not found');
a.onclick = function() { node.done({ consent: true }); };
na.onclick = function() {
var showIt, confirmed;
confirmed = confirm(that.getText('areYouSure'));
if (!confirmed) return;
node.emit('CONSENT_REJECTING');
that.notAgreed = true;
node.set({
consent: false,
// Need to send these two because it's not a DONE msg.
time: node.timer.getTimeSince('step'),
timeup: false
});
a.disabled = true;
na.disabled = true;
a.onclick = null;
na.onclick = null;
node.socket.disconnect();
W.hide('consent');
W.show('notAgreed');
// If a show-consent button is found enable it.
showIt = W.gid('show-consent');
if (showIt) {
showIt.onclick = function() {
var div, s;
div = W.toggle('consent');
s = div.style.display === '' ? 'hide' : 'show';
this.innerHTML = that.getText('showHideConsent', s);
};
}
node.emit('CONSENT_REJECTED');
};
});
};
})(node);