-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChoiceTableGroup.js
More file actions
1354 lines (1202 loc) · 41.6 KB
/
ChoiceTableGroup.js
File metadata and controls
1354 lines (1202 loc) · 41.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
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
/**
* # ChoiceTableGroup
* Copyright(c) 2021 Stefano Balietti
* MIT Licensed
*
* Creates a table that groups together several choice tables widgets
*
* @see ChoiceTable
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('ChoiceTableGroup', ChoiceTableGroup);
// ## Meta-data
ChoiceTableGroup.version = '1.8.0';
ChoiceTableGroup.description = 'Groups together and manages sets of ' +
'ChoiceTable widgets.';
ChoiceTableGroup.title = 'Make your choice';
ChoiceTableGroup.className = 'choicetable choicetablegroup';
ChoiceTableGroup.separator = '::';
ChoiceTableGroup.texts = {
autoHint: function(w) {
if (w.requiredChoice) return '*';
else return false;
},
error: 'Selection required.'
};
// ## Dependencies
ChoiceTableGroup.dependencies = {
JSUS: {}
};
/**
* ## ChoiceTableGroup constructor
*
* Creates a new instance of ChoiceTableGroup
*
* @param {object} options Optional. Configuration options.
* If a `table` option is specified, it sets it as the clickable
* table. All other options are passed to the init method.
*/
function ChoiceTableGroup() {
var that;
that = this;
/**
* ### ChoiceTableGroup.dl
*
* The clickable table containing all the cells
*/
this.table = null;
/**
* ### ChoiceTableGroup.trs
*
* Collection of all trs created
*
* Useful when shuffling items/choices
*
* @see ChoiceTableGroup.shuffle
*/
this.trs = [];
/**
* ## ChoiceTableGroup.listener
*
* The main listener function
*
* @see ChoiceTableGroup.enable
* @see ChoiceTableGroup.disable
* @see ChoiceTableGroup.onclick
*/
this.listener = function(e) {
var name, value, item, td, oldSelected;
var time, removed;
// Relative time.
if ('string' === typeof that.timeFrom) {
time = node.timer.getTimeSince(that.timeFrom);
}
// Absolute time.
else {
time = Date.now ? Date.now() : new Date().getTime();
}
e = e || window.event;
td = e.target || e.srcElement;
// Not a clickable choice.
if ('undefined' === typeof that.choicesById[td.id]) {
// It might be a nested element, try the parent.
td = td.parentNode;
if (!td || 'undefined' === typeof that.choicesById[td.id]) {
return;
}
}
// if (!that.choicesById[td.id]) return;
// Id of elements are in the form of name_value or name_item_value.
value = td.id.split(that.separator);
// Separator not found, not a clickable cell.
if (value.length === 1) return;
name = value[0];
value = value[1];
item = that.itemsById[name];
// Not a clickable cell.
if (!item) return;
item.timeCurrentChoice = time;
// One more click.
item.numberOfClicks++;
// If only 1 selection allowed, remove selection from oldSelected.
if (!item.selectMultiple) {
oldSelected = item.selected;
if (oldSelected) J.removeClass(oldSelected, 'selected');
if (item.isChoiceCurrent(value)) {
item.unsetCurrentChoice(value);
removed = true;
}
else {
item.currentChoice = value;
J.addClass(td, 'selected');
item.selected = td;
}
}
// Remove any warning/error from form on click.
if (that.isHighlighted()) that.unhighlight();
// Call onclick, if any.
if (that.onclick) {
// TODO: Should we parseInt it anyway when we store
// the current choice?
value = parseInt(value, 10);
that.onclick.call(that, name, value, removed, td);
}
};
/**
* ## ChoiceTableGroup.onclick
*
* The user-defined onclick function
*
* Receives 4 input parameters: the name of the choice table clicked,
* the value of the clicked choice, whether it was a remove action,
* and the reference to the TD object.
*
* @see ChoiceTableGroup.listener
*/
this.onclick = null;
/**
* ### ChoiceTableGroup.mainText
*
* The main text introducing the choices
*
* @see ChoiceTableGroup.spanMainText
*/
this.mainText = null;
/**
* ### ChoiceTableGroup.spanMainText
*
* The span containing the main text
*/
this.spanMainText = null;
/**
* ### ChoiceTableGroup.hint
*
* An additional text with information about how to select items
*
* If not specified, it may be auto-filled, e.g. '(pick 2)'.
*
* @see Feedback.texts.autoHint
*/
this.hint = null;
/**
* ### ChoiceTableGroup.errorBox
*
* An HTML element displayed when a validation error occurs
*/
this.errorBox = null;
/**
* ### ChoiceTableGroup.items
*
* The array of available items
*/
this.items = null;
/**
* ### ChoiceTableGroup.itemsById
*
* Map of items ids to items
*/
this.itemsById = {};
/**
* ### ChoiceTableGroup.itemsMap
*
* Maps items ids to the position in the items array
*/
this.itemsMap = {};
/**
* ### ChoiceTableGroup.choices
*
* Array of default choices (if passed as global parameter)
*/
this.choices = null;
/**
* ### ChoiceTableGroup.choicesById
*
* Map of items choices ids to corresponding cell
*
* Useful to detect clickable cells.
*/
this.choicesById = {};
/**
* ### ChoiceTableGroup.itemsSettings
*
* The array of settings for each item
*/
this.itemsSettings = null;
/**
* ### ChoiceTableGroup.order
*
* The current order of display of choices
*
* May differ from `originalOrder` if shuffled.
*
* @see ChoiceTableGroup.originalOrder
*/
this.order = null;
/**
* ### ChoiceTableGroup.originalOrder
*
* The initial order of display of choices
*
* @see ChoiceTable.order
*/
this.originalOrder = null;
/**
* ### ChoiceTableGroup.shuffleItems
*
* If TRUE, items are inserted in random order
*
* @see ChoiceTableGroup.order
*/
this.shuffleItems = null;
/**
* ### ChoiceTableGroup.requiredChoice
*
* The number of required choices.
*/
this.requiredChoice = null;
/**
* ### ChoiceTableGroup.orientation
*
* Orientation of display of items: vertical ('V') or horizontal ('H')
*
* Default orientation is horizontal.
*/
this.orientation = 'H';
/**
* ### ChoiceTableGroup.group
*
* The name of the group where the table belongs, if any
*/
this.group = null;
/**
* ### ChoiceTableGroup.groupOrder
*
* The order of the choice table within the group
*/
this.groupOrder = null;
/**
* ### ChoiceTableGroup.freeText
*
* If truthy, a textarea for free-text comment will be added
*
* If 'string', the text will be added inside the the textarea
*/
this.freeText = null;
/**
* ### ChoiceTableGroup.textarea
*
* Textarea for free-text comment
*/
this.textarea = null;
/**
* ### ChoiceTableGroup.header
*
* Header to be displayed above the table
*
* @experimental
*/
this.header = null;
// Options passed to each individual item.
/**
* ### ChoiceTableGroup.timeFrom
*
* Time is measured from timestamp as saved by node.timer
*
* Default event is a new step is loaded (user can interact with
* the screen). Set it to FALSE, to have absolute time.
*
* This option is passed to each individual item.
*
* @see mixinSettings
*
* @see node.timer.getTimeSince
*/
this.timeFrom = 'step';
/**
* ### ChoiceTableGroup.selectMultiple
*
* If TRUE, it allows to select multiple cells
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.selectMultiple = null;
/**
* ### ChoiceTableGroup.renderer
*
* A callback that renders the content of each cell
*
* The callback must accept three parameters:
*
* - a td HTML element,
* - a choice
* - the index of the choice element within the choices array
*
* and optionally return the _value_ for the choice (otherwise
* the order in the choices array is used as value).
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.renderer = null;
/**
* ### ChoiceTableGroup.separator
*
* Symbol used to separate tokens in the id attribute of every cell
*
* Default ChoiceTableGroup.separator
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.separator = ChoiceTableGroup.separator;
/**
* ### ChoiceTableGroup.shuffleChoices
*
* If TRUE, choices in items are shuffled
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.shuffleChoices = null;
/**
* ### ChoiceTableGroup.tabbable
*
* If TRUE, the elements of each choicetable can be accessed with TAB
*
* Clicking is simulated upon pressing space or enter.
*
* Default TRUE
*
* @see ChoiceTable.tabbable
*/
this.tabbable = null;
}
// ## ChoiceTableGroup methods
/**
* ### ChoiceTableGroup.init
*
* Initializes the instance
*
* Available options are:
*
* - className: the className of the table (string, array), or false
* to have none.
* - orientation: orientation of the table: vertical (v) or horizontal (h)
* - group: the name of the group (number or string), if any
* - groupOrder: the order of the table in the group, if any
* - listener: a custom function executed at every click. Context is
* `this` instance.
* - onclick: a function executed after the listener function. Context is
* `this` instance
* - mainText: a text to be displayed above the table
* - shuffleItems: if TRUE, items are shuffled before being added
* to the table
* - freeText: if TRUE, a textarea will be added under the table,
* if 'string', the text will be added inside the the textarea
* - timeFrom: The timestamp as recorded by `node.timer.setTimestamp`
* or FALSE, to measure absolute time for current choice
* - tabbable: if TRUE, each cell can be reached with TAB and clicked
* with SPACE or ENTER. Default: TRUE.
*
* @param {object} opts Configuration options
*/
ChoiceTableGroup.prototype.init = function(opts) {
var tmp;
// TODO: many options checking are replicated. Skip them all?
// Have a method in ChoiceTable?
if (!this.id) {
throw new TypeError('ChoiceTableGroup.init: id ' +
'is missing.');
}
// Option orientation, default 'H'.
if ('undefined' === typeof opts.orientation) {
tmp = 'H';
}
else if ('string' !== typeof opts.orientation) {
throw new TypeError('ChoiceTableGroup.init: orientation ' +
'must be string, or undefined. Found: ' +
opts.orientation);
}
else {
tmp = opts.orientation.toLowerCase().trim();
if (tmp === 'horizontal' || tmp === 'h') {
tmp = 'H';
}
else if (tmp === 'vertical' || tmp === 'v') {
tmp = 'V';
}
else {
throw new Error('ChoiceTableGroup.init: orientation ' +
'is invalid: ' + tmp);
}
}
this.orientation = tmp;
// Option shuffleItems, default false.
if ('undefined' === typeof opts.shuffleItems) tmp = false;
else tmp = !!opts.shuffleItems;
this.shuffleItems = tmp;
// Option requiredChoice, if any.
if ('number' === typeof opts.requiredChoice) {
this.requiredChoice = opts.requiredChoice;
}
else if ('boolean' === typeof opts.requiredChoice) {
this.requiredChoice = opts.requiredChoice ? 1 : 0;
}
else if ('undefined' !== typeof opts.requiredChoice) {
throw new TypeError('ChoiceTableGroup.init: ' +
'opts.requiredChoice ' +
'be number or boolean or undefined. Found: ' +
opts.requiredChoice);
}
// Set the group, if any.
if ('string' === typeof opts.group ||
'number' === typeof opts.group) {
this.group = opts.group;
}
else if ('undefined' !== typeof opts.group) {
throw new TypeError('ChoiceTableGroup.init: group must ' +
'be string, number or undefined. Found: ' +
opts.group);
}
// Set the groupOrder, if any.
if ('number' === typeof opts.groupOrder) {
this.groupOrder = opts.groupOrder;
}
else if ('undefined' !== typeof opts.group) {
throw new TypeError('ChoiceTableGroup.init: groupOrder ' +
'must be number or undefined. Found: ' +
opts.groupOrder);
}
// Set the main onclick listener, if any.
if ('function' === typeof opts.listener) {
this.listener = function(e) {
opts.listener.call(this, e);
};
}
else if ('undefined' !== typeof opts.listener) {
throw new TypeError('ChoiceTableGroup.init: listener ' +
'must be function or undefined. Found: ' +
opts.listener);
}
// Set an additional onclick, if any.
if ('function' === typeof opts.onclick) {
this.onclick = opts.onclick;
}
else if ('undefined' !== typeof opts.onclick) {
throw new TypeError('ChoiceTableGroup.init: onclick must ' +
'be function or undefined. Found: ' +
opts.onclick);
}
// Set the mainText, if any.
if ('string' === typeof opts.mainText) {
this.mainText = opts.mainText;
}
else if ('undefined' !== typeof opts.mainText) {
throw new TypeError('ChoiceTableGroup.init: mainText ' +
'must be string or undefined. Found: ' +
opts.mainText);
}
// Set the hint, if any.
if ('string' === typeof opts.hint || false === opts.hint) {
this.hint = opts.hint;
}
else if ('undefined' !== typeof opts.hint) {
throw new TypeError('ChoiceTableGroup.init: hint must ' +
'be a string, false, or undefined. Found: ' +
opts.hint);
}
else {
// Returns undefined if there are no constraints.
this.hint = this.getText('autoHint');
}
// Set the timeFrom, if any.
if (opts.timeFrom === false ||
'string' === typeof opts.timeFrom) {
this.timeFrom = opts.timeFrom;
}
else if ('undefined' !== typeof opts.timeFrom) {
throw new TypeError('ChoiceTableGroup.init: timeFrom ' +
'must be string, false, or undefined. Found: ' +
opts.timeFrom);
}
// Option shuffleChoices, default false.
if ('undefined' !== typeof opts.shuffleChoices) {
this.shuffleChoices = !!opts.shuffleChoices;
}
// Set the renderer, if any.
if ('function' === typeof opts.renderer) {
this.renderer = opts.renderer;
}
else if ('undefined' !== typeof opts.renderer) {
throw new TypeError('ChoiceTableGroup.init: renderer ' +
'must be function or undefined. Found: ' +
opts.renderer);
}
// Set default choices, if any.
if ('undefined' !== typeof opts.choices) {
this.choices = opts.choices;
}
// Set the className, if not use default.
if ('undefined' === typeof opts.className) {
this.className = ChoiceTableGroup.className;
}
else if (opts.className === false ||
'string' === typeof opts.className ||
J.isArray(opts.className)) {
this.className = opts.className;
}
else {
throw new TypeError('ChoiceTableGroup.init: ' +
'className must be string, array, ' +
'or undefined. Found: ' + opts.className);
}
if (opts.tabbable !== false) this.tabbable = true;
// Separator checked by ChoiceTable.
if (opts.separator) this.separator = opts.separator;
// After all configuration opts are evaluated, add items.
if ('object' === typeof opts.table) {
this.table = opts.table;
}
else if ('undefined' !== typeof opts.table &&
false !== opts.table) {
throw new TypeError('ChoiceTableGroup.init: table ' +
'must be object, false or undefined. ' +
'Found: ' + opts.table);
}
this.table = opts.table;
this.freeText = 'string' === typeof opts.freeText ?
opts.freeText : !!opts.freeText;
if (opts.header) {
if (!J.isArray(opts.header) ||
opts.header.length !== opts.choices.length) {
throw new Error('ChoiceTableGroup.init: header ' +
'must be an array of length ' +
opts.choices.length +
' or undefined. Found: ' + opts.header);
}
this.header = opts.header;
}
// Add the items.
if ('undefined' !== typeof opts.items) this.setItems(opts.items);
};
/**
* ### ChoiceTableGroup.setItems
*
* Sets the available items and optionally builds the table
*
* @param {array} items The array of items
*
* @see ChoiceTableGroup.table
* @see ChoiceTableGroup.order
* @see ChoiceTableGroup.shuffleItems
* @see ChoiceTableGroup.buildTable
*/
ChoiceTableGroup.prototype.setItems = function(items) {
var len;
if (!J.isArray(items)) {
throw new TypeError('ChoiceTableGroup.setItems: ' +
'items must be array. Found: ' + items);
}
if (!items.length) {
throw new Error('ChoiceTableGroup.setItems: ' +
'items is an empty array.');
}
len = items.length;
this.itemsSettings = items;
this.items = new Array(len);
// Save the order in which the items will be added.
this.order = J.seq(0, len-1);
if (this.shuffleItems) this.order = J.shuffle(this.order);
this.originalOrder = this.order;
// Build the table and items at once (faster).
if (this.table) this.buildTable();
};
/**
* ### ChoiceTableGroup.buildTable
*
* Builds the table of clickable items and enables it
*
* Must be called after items have been set already.
*
* @see ChoiceTableGroup.setChoiceTables
* @see ChoiceTableGroup.order
*/
ChoiceTableGroup.prototype.buildTable = function() {
var i, len, tr, H, ct;
var j, lenJ, lenJOld, hasRight, cell;
H = this.orientation === 'H';
i = -1, len = this.itemsSettings.length;
if (H) {
if (this.header) {
tr = W.add('tr', this.table);
W.add('td', tr, {
className: 'header'
});
for ( ; ++i < this.header.length ; ) {
W.add('td', tr, {
innerHTML: this.header[i],
className: 'header'
});
}
i = -1;
}
for ( ; ++i < len ; ) {
// Get item.
ct = getChoiceTable(this, i);
// Add new TR.
tr = createTR(this, ct.id);
// Append choices for item.
tr.appendChild(ct.leftCell);
j = -1, lenJ = ct.choicesCells.length;
// Make sure all items have same number of choices.
if (i === 0) {
lenJOld = lenJ;
}
else if (lenJ !== lenJOld) {
throw new Error('ChoiceTableGroup.buildTable: item ' +
'do not have same number of choices: ' +
ct.id);
}
// TODO: might optimize. There are two loops (+1 inside ct).
for ( ; ++j < lenJ ; ) {
cell = ct.choicesCells[j];
tr.appendChild(cell);
this.choicesById[cell.id] = cell;
}
if (ct.rightCell) tr.appendChild(ct.rightCell);
}
}
else {
// Add new TR.
// TODO: rename, this is not the header as from options.
tr = createTR(this, 'header');
// Build all items first.
for ( ; ++i < len ; ) {
// Get item, append choices for item.
ct = getChoiceTable(this, i);
// Make sure all items have same number of choices.
lenJ = ct.choicesCells.length;
if (i === 0) {
lenJOld = lenJ;
}
else if (lenJ !== lenJOld) {
throw new Error('ChoiceTableGroup.buildTable: item ' +
'do not have same number of choices: ' +
ct.id);
}
if ('undefined' === typeof hasRight) {
hasRight = !!ct.rightCell;
}
else if ((!ct.rightCell && hasRight) ||
(ct.rightCell && !hasRight)) {
throw new Error('ChoiceTableGroup.buildTable: either all ' +
'items or no item must have the right ' +
'cell: ' + ct.id);
}
// Add left.
tr.appendChild(ct.leftCell);
}
if (hasRight) lenJ++;
j = -1;
for ( ; ++j < lenJ ; ) {
// Add new TR.
tr = createTR(this, 'row' + (j+1));
i = -1;
// TODO: might optimize. There are two loops (+1 inside ct).
for ( ; ++i < len ; ) {
if (hasRight && j === (lenJ-1)) {
tr.appendChild(this.items[i].rightCell);
}
else {
cell = this.items[i].choicesCells[j];
tr.appendChild(cell);
this.choicesById[cell.id] = cell;
}
}
}
}
// Enable onclick listener.
this.enable(true);
};
/**
* ### ChoiceTableGroup.append
*
* Implements Widget.append
*
* Checks that id is unique.
*
* Appends (all optional):
*
* - mainText: a question or statement introducing the choices
* - table: the table containing the choices
* - freeText: a textarea for comments
*
* @see Widget.append
*/
ChoiceTableGroup.prototype.append = function() {
// Id must be unique.
if (W.getElementById(this.id)) {
throw new Error('ChoiceTableGroup.append: id ' +
'is not unique: ' + this.id);
}
// MainText.
if (this.mainText) {
this.spanMainText = W.append('span', this.bodyDiv, {
className: 'choicetable-maintext',
innerHTML: this.mainText
});
}
// Hint.
if (this.hint) {
W.append('span', this.spanMainText || this.bodyDiv, {
className: 'choicetable-hint',
innerHTML: this.hint
});
}
// Create/set table, if requested.
if (this.table !== false) {
if ('undefined' === typeof this.table) {
this.table = document.createElement('table');
if (this.items) this.buildTable();
}
// Set table id.
this.table.id = this.id;
if (this.className) J.addClass(this.table, this.className);
else this.table.className = '';
// Append table.
this.bodyDiv.appendChild(this.table);
}
this.errorBox = W.append('div', this.bodyDiv, { className: 'errbox' });
// Creates a free-text textarea, possibly with placeholder text.
if (this.freeText) {
this.textarea = document.createElement('textarea');
if (this.id) this.textarea.id = this.id + '_text';
this.textarea.className = ChoiceTableGroup.className + '-freetext';
if ('string' === typeof this.freeText) {
this.textarea.placeholder = this.freeText;
}
// Append textarea.
this.bodyDiv.appendChild(this.textarea);
}
};
/**
* ### ChoiceTableGroup.listeners
*
* Implements Widget.listeners
*
* Adds two listeners two disable/enable the widget on events:
* INPUT_DISABLE, INPUT_ENABLE
*
* Notice! Nested choice tables listeners are not executed.
*
* @see Widget.listeners
* @see mixinSettings
*/
ChoiceTableGroup.prototype.listeners = function() {
var that = this;
node.on('INPUT_DISABLE', function() {
that.disable();
});
node.on('INPUT_ENABLE', function() {
that.enable();
});
};
/**
* ### ChoiceTableGroup.disable
*
* Disables clicking on the table and removes CSS 'clicklable' class
*/
ChoiceTableGroup.prototype.disable = function() {
if (this.disabled === true || !this.table) return;
this.disabled = true;
J.removeClass(this.table, 'clickable');
this.table.removeEventListener('click', this.listener);
// Remove listener to make cells clickable with the keyboard.
if (this.tabbable) J.makeClickable(this.table, false);
this.emit('disabled');
};
/**
* ### ChoiceTableGroup.enable
*
* Enables clicking on the table and adds CSS 'clicklable' class
*
* @return {function} cb The event listener function
*/
ChoiceTableGroup.prototype.enable = function(force) {
if (!this.table || (!force && !this.disabled)) return;
this.disabled = false;
J.addClass(this.table, 'clickable');
this.table.addEventListener('click', this.listener);
// Add listener to make cells clickable with the keyboard.
if (this.tabbable) J.makeClickable(this.table);
this.emit('enabled');
};
/**
* ### ChoiceTableGroup.verifyChoice
*
* Compares the current choice/s with the correct one/s
*
* @param {boolean} markAttempt Optional. If TRUE, the value of
* current choice is added to the attempts array. Default
*
* @return {boolean|null} TRUE if current choice is correct,
* FALSE if it is not correct, or NULL if no correct choice
* was set
*
* @see ChoiceTableGroup.attempts
* @see ChoiceTableGroup.setCorrectChoice
*/
ChoiceTableGroup.prototype.verifyChoice = function(markAttempt) {
var i, len, out;
out = {};
// Mark attempt by default.
markAttempt = 'undefined' === typeof markAttempt ? true : markAttempt;
i = -1, len = this.items.length;
for ( ; ++i < len ; ) {
out[this.items[i].id] = this.items[i].verifyChoice(markAttempt);
}
return out;
};
/**
* ### ChoiceTable.setCurrentChoice
*
* Marks a choice as current in each item
*
* If the item allows it, multiple choices can be set as current.
*
* @param {number|string} The choice to mark as current
*
* @see ChoiceTable.currentChoice
* @see ChoiceTable.selectMultiple
*/
ChoiceTableGroup.prototype.setCurrentChoice = function(choice) {
var i, len;
i = -1, len = this.items[i].length;
for ( ; ++i < len ; ) {
this.items[i].setCurrentChoice(choice);
}
};
/**
* ### ChoiceTableGroup.unsetCurrentChoice
*
* Deletes the value for currentChoice from every item
*
* If `ChoiceTableGroup.selectMultiple` is set the
*
* @param {number|string} Optional. The choice to delete from currentChoice
* when multiple selections are allowed