-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoneButton.js
299 lines (268 loc) · 9.08 KB
/
DoneButton.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
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
/**
* # DoneButton
* Copyright(c) 2020 Stefano Balietti <[email protected]>
* MIT Licensed
*
* Creates a button that if pressed emits node.done()
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('DoneButton', DoneButton);
// ## Meta-data
DoneButton.version = '1.1.0';
DoneButton.description = 'Creates a button that if ' +
'pressed emits node.done().';
DoneButton.title = false;
DoneButton.className = 'donebutton';
DoneButton.texts.done = 'Done';
// ## Dependencies
DoneButton.dependencies = {
JSUS: {}
};
/**
* ## DoneButton constructor
*
* Creates a new instance of DoneButton
*
* @param {object} options Optional. Configuration options.
* If a `button` option is specified, it sets it as the clickable
* button. All other options are passed to the init method.
*
* @see DoneButton.init
*/
function DoneButton(options) {
var that;
that = this;
/**
* ### DoneButton.button
*
* The HTML element triggering node.done() when pressed
*/
if ('object' === typeof options.button) {
this.button = options.button;
}
else if ('undefined' === typeof options.button) {
this.button = document.createElement('input');
this.button.type = 'button';
}
else {
throw new TypeError('DoneButton constructor: options.button must ' +
'be object or undefined. Found: ' +
options.button);
}
this.button.onclick = function() {
if (that.onclick && false === that.onclick()) return;
if (node.done()) that.disable();
};
/**
* ### DoneButton.onclick
*
* A callback executed after the button is clicked
*
* If it return FALSE, node.done() is not called.
*/
this.onclick = null;
/**
* ### DoneButton.disableOnDisconnect
*
* If TRUE, the button is automatically disableb upon disconnection
*/
this.disableOnDisconnect = null;
/**
* ### DoneButton.delayOnPlaying
*
* The number of milliseconds to wait to enable at a new step
*
* A small delay prevents accidental double clicking between steps.
*/
this.delayOnPlaying = 800;
}
// ## DoneButton methods
/**
* ### DoneButton.init
*
* Initializes the instance
*
* Available options are:
*
* - id: id of the HTML button, or false to have none. Default:
* DoneButton.className
* - className: the className of the button (string, array), or false
* to have none. Default bootstrap classes: 'btn btn-lg btn-primary'
* - text: the text on the button. Default: DoneButton.text
* - onclick: a callback executed when the button is clicked. Default: null
* - disableOnDisconnect: TRUE to disable upon disconnection. Default: TRUE
* - delayOnPlaying: number of milliseconds to wait to enable after
* the `PLAYING` event is fired (e.g., a new step begins). Default: 800
*
* @param {object} opts Optional. Configuration options
*/
DoneButton.prototype.init = function(opts) {
var tmp;
opts = opts || {};
//Button
if ('undefined' === typeof opts.id) {
tmp = DoneButton.className;
}
else if ('string' === typeof opts.id) {
tmp = opts.id;
}
else if (false === opts.id) {
tmp = false;
}
else {
throw new TypeError('DoneButton.init: id must ' +
'be string, false, or undefined. Found: ' +
opts.id);
}
if (tmp) this.button.id = tmp;
// Button className.
if ('undefined' === typeof opts.className) {
tmp = 'btn btn-lg btn-primary';
}
else if (opts.className === false) {
tmp = '';
}
else if ('string' === typeof opts.className) {
tmp = opts.className;
}
else if (J.isArray(opts.className)) {
tmp = opts.className.join(' ');
}
else {
throw new TypeError('DoneButton.init: className must ' +
'be string, array, or undefined. Found: ' +
opts.className);
}
this.button.className = tmp;
// Button text.
this.button.value = 'string' === typeof opts.text ?
opts.text : this.getText('done');
this.disableOnDisconnect =
'undefined' === typeof opts.disableOnDisconnect ?
true : !! opts.disableOnDisconnect;
tmp = opts.delayOnPlaying;
if ('number' === typeof tmp) {
this.delayOnPlaying = tmp;
}
else if ('undefined' !== typeof tmp) {
throw new TypeError('DoneButton.init: delayOnPlaying must ' +
'be number or undefined. Found: ' + tmp);
}
tmp = opts.onclick;
if (tmp) {
if ('function' !== typeof tmp) {
throw new TypeError('DoneButton.init: onclick must function ' +
'or undefined. Found: ' + tmp);
}
this.onclick = tmp;
}
};
DoneButton.prototype.append = function() {
// If added in init, it must stay disabled until the step property
// of first step is evaluated.
if (!node.game.isReady()) {
this.disabled = true;
this.button.disabled = true;
}
this.bodyDiv.appendChild(this.button);
};
DoneButton.prototype.listeners = function() {
var that, disabled;
that = this;
// This is normally executed after the PLAYING listener of
// GameWindow where lockUnlockedInputs takes place.
// In case of a timeup, the donebutton will be locked and
// then unlocked by GameWindow, but otherwise it must be
// done here.
node.on('PLAYING', function() {
var prop, step, delay;
step = node.game.getCurrentGameStage();
prop = node.game.plot.getProperty(step, 'donebutton');
if (prop === false || (prop && prop.enableOnPlaying === false)) {
// It might be disabled already, but we do it again.
that.disable();
}
else {
if (prop && prop.hasOwnProperty &&
prop.hasOwnProperty('delayOnPlaying')) {
delay = prop.delayOnPlaying;
}
else {
delay = that.delayOnPlaying;
}
if (delay) {
setTimeout(function () {
// If not disabled because of a disconnection,
// enable it.
if (!disabled) that.enable();
}, delay);
}
else {
// It might be enabled already, but we do it again.
that.enable();
}
}
if ('string' === typeof prop) that.button.value = prop;
else if (prop && prop.text) that.button.value = prop.text;
});
if (this.disableOnDisconnect) {
node.on('SOCKET_DISCONNECT', function() {
if (!that.isDisabled()) {
that.disable();
disabled = true;
}
});
node.on('SOCKET_CONNECT', function() {
if (disabled) {
if (that.isDisabled()) that.enable();
disabled = false;
}
});
}
};
/**
* ### DoneButton.updateText
*
* Updates the text on the button, possibly for a given duration only
*
* @param {string} text The new text
* @param {number} duration Optional. The number of milliseconds the new
* text is displayed. If undefined, the new text stays indefinitely.
*/
DoneButton.prototype.updateText = function(text, duration) {
var oldText, that;
if (duration) {
that = this;
oldText = this.button.value;
node.timer.setTimeout(function() {
that.button.value = oldText;
}, duration);
}
this.button.value = text;
};
/**
* ### DoneButton.disable
*
* Disables the done button
*/
DoneButton.prototype.disable = function(opts) {
if (this.disabled) return;
this.disabled = true;
this.button.disabled = true;
this.emit('disabled', opts);
};
/**
* ### DoneButton.enable
*
* Enables the done button
*/
DoneButton.prototype.enable = function(opts) {
if (!this.disabled) return;
this.disabled = false;
this.button.disabled = false;
this.emit('enabled', opts);
};
})(node);