-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomInputGroup.js
1172 lines (1052 loc) · 36.1 KB
/
CustomInputGroup.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
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
/**
* # CustomInputGroup
* Copyright(c) 2021 Stefano Balietti
* MIT Licensed
*
* Creates a table that groups together several custom input widgets
*
* @see CustomInput
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('CustomInputGroup', CustomInputGroup);
// ## Meta-data
CustomInputGroup.version = '0.3.0';
CustomInputGroup.description = 'Groups together and manages sets of ' +
'CustomInput widgets.';
CustomInputGroup.title = false;
CustomInputGroup.className = 'custominput custominputgroup';
CustomInputGroup.separator = '::';
CustomInputGroup.texts.autoHint = function(w) {
if (w.requiredChoice) return '*';
else return false;
};
CustomInputGroup.texts.inputErr = 'One or more errors detected.';
// ## Dependencies
CustomInputGroup.dependencies = {
JSUS: {}
};
/**
* ## CustomInputGroup constructor
*
* Creates a new instance of CustomInputGroup
*
* @param {object} options Optional. Configuration options.
* If a `table` option is specified, it sets it as main
* table. All other options are passed to the init method.
*/
function CustomInputGroup() {
/**
* ### CustomInputGroup.dl
*
* The table containing all the custom inputs
*/
this.table = null;
/**
* ### CustomInputGroup.trs
*
* Collection of all trs created
*
* Useful when shuffling items/choices
*
* @see CustomInputGroup.shuffle
*/
this.trs = [];
/**
* ### CustomInputGroup.mainText
*
* The main text introducing the choices
*
* @see CustomInputGroup.spanMainText
*/
this.mainText = null;
/**
* ### CustomInputGroup.spanMainText
*
* The span containing the main text
*/
this.spanMainText = null;
/**
* ### CustomInputGroup.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;
/**
* ### CustomInputGroup.items
*
* The array of available items
*/
this.items = null;
/**
* ### CustomInputGroup.itemsById
*
* Map of items ids to items
*/
this.itemsById = {};
/**
* ### CustomInputGroup.itemsMap
*
* Maps items ids to the position in the items array
*/
this.itemsMap = {};
/**
* ### CustomInputGroup.choices
*
* Array of default choices (if passed as global parameter)
*/
this.choices = null;
/**
* ### CustomInputGroup.choicesById
*
* Map of items choices ids to corresponding cell
*
* Useful to detect clickable cells.
*/
this.choicesById = {};
/**
* ### CustomInputGroup.itemsSettings
*
* The array of settings for each item
*/
this.itemsSettings = null;
/**
* ### CustomInputGroup.order
*
* The current order of display of choices
*
* May differ from `originalOrder` if shuffled.
*
* @see CustomInputGroup.originalOrder
*/
this.order = null;
/**
* ### CustomInputGroup.originalOrder
*
* The initial order of display of choices
*
* @see CustomInput.order
*/
this.originalOrder = null;
/**
* ### CustomInputGroup.shuffleItems
*
* If TRUE, items are inserted in random order
*
* @see CustomInputGroup.order
*/
this.shuffleItems = null;
/**
* ### CustomInputGroup.requiredChoice
*
* The number of required choices.
*/
this.requiredChoice = null;
/**
* ### ChoiceManager.required
*
* TRUE if widget should be checked upon node.done.
*
* TODO: Harmonize required and requiredChoice
*/
this.required = null;
/**
* ### CustomInputGroup.orientation
*
* Orientation of display of items: vertical ('V') or horizontal ('H')
*
* Default orientation is vertical.
*/
this.orientation = 'V';
/**
* ### CustomInputGroup.group
*
* The name of the group where the table belongs, if any
*/
this.group = null;
/**
* ### CustomInputGroup.groupOrder
*
* The order of the choice table within the group
*/
this.groupOrder = null;
/**
* ### CustomInputGroup.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;
/**
* ### CustomInputGroup.textarea
*
* Textarea for free-text comment
*/
this.textarea = null;
// Options passed to each individual item.
/**
* ### CustomInputGroup.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';
/**
* ### CustomInputGroup.separator
*
* Symbol used to separate tokens in the id attribute of every cell
*
* Default CustomInputGroup.separator
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.separator = CustomInputGroup.separator;
/**
* ### CustomInputGroup.shuffleChoices
*
* If TRUE, choices in items are shuffled
*
* This option is passed to each individual item.
*
* @see mixinSettings
*/
this.shuffleChoices = null;
/**
* ### CustomInputGroup.sharedOptions
*
* An object containing options to be added to every custom input
*
* Options are added only if forms are specified as object literals,
* and can be overriden by each individual form.
*/
this.sharedOptions = {};
/**
* ### CustomInputGroup.summaryInput
*
* A summary custom input added last which can be updated in real time
*
* @see CustomInputGroup.summaryInputCb
*/
this.summaryInput = null;
/**
* ### CustomInputGroup.errorBox
*
* An HTML element displayed when a validation error occurs
*/
this.errorBox = null;
/**
* ### CustomInputGroup.validation
*
* The callback validating all the inputs at once
*
* The callback is executed by getValues.
*
* Input paramers:
*
* - res: the validation result of all inputs
* - values: object literal containing the current value of each input
* - widget: a reference to this widget
*
* Return value:
*
* - res: the result object as it is on success, or with with an err
* property containing the error message on failure. Any change
* to the result object is carried over.
*
* @see CustomInputGroup.oninput
*/
this.validation = null;
/**
* ### CustomInputGroup._validation
*
* Reference to the user defined validation function
*
* @api private
*/
this._validation = null;
/**
* ### CustomInputGroup.oninput
*
* Callback called when any input has changed
*
* Input paramers:
*
* - res: the validation result of the single input
* - input: the custom input that fired oninput
* - widget: a reference to this widget
*/
this.oninput = null;
/**
* ### CustomInputGroup._oninput
*
* Reference to the user defined oninput function
*
* @api private
*/
this._oninput = null;
}
// ## CustomInputGroup methods
/**
* ### CustomInputGroup.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
* - onclick: a custom onclick 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
* - sharedOptions: Options shared across all inputs
* - summary: An object containing the options to instantiate a custom
* input summary field.
* - validation: A validation callback for all inputs.
* - oninput: A callback called when any input is changed
*
* @param {object} opts Configuration options
*/
CustomInputGroup.prototype.init = function(opts) {
var tmp, that;
that = this;
// TODO: many options checking are replicated. Skip them all?
// Have a method in CustomInput?
if (!this.id) {
throw new TypeError('CustomInputGroup.init: id ' +
'is missing.');
}
// Option orientation, default 'H'.
if ('undefined' === typeof opts.orientation) {
tmp = 'V';
}
else if ('string' !== typeof opts.orientation) {
throw new TypeError('CustomInputGroup.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('CustomInputGroup.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('CustomInputGroup.init: ' +
'requiredChoice ' +
'be number or boolean or undefined. Found: ' +
opts.requiredChoice);
}
if ('undefined' !== typeof opts.required) {
this.required = !!opts.required;
}
// 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('CustomInputGroup.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('CustomInputGroup.init: groupOrder ' +
'must be number or undefined. Found: ' +
opts.groupOrder);
}
// Set the validation function.
if ('function' === typeof opts.validation) {
this._validation = opts.validation;
this.validation = function(res, values) {
if (!values) values = that.getValues({ valuesOnly: true });
return that._validation(res || {}, values, that)
};
}
else if ('undefined' !== typeof opts.validation) {
throw new TypeError('CustomInputGroup.init: validation must ' +
'be function or undefined. Found: ' +
opts.validation);
}
// Set the validation function.
if ('function' === typeof opts.oninput) {
this._oninput = opts.oninput;
this.oninput = function(res, input) {
that._oninput(res, input, that);
};
}
else if ('undefined' !== typeof opts.oninput) {
throw new TypeError('CustomInputGroup.init: oninput must ' +
'be function or undefined. Found: ' +
opts.oninput);
}
// Set the mainText, if any.
if ('string' === typeof opts.mainText) {
this.mainText = opts.mainText;
}
else if ('undefined' !== typeof opts.mainText) {
throw new TypeError('CustomInputGroup.init: mainText ' +
'must be string or undefined. Found: ' +
opts.mainText);
}
// Set the hint, if any.
if ('string' === typeof opts.hint) {
this.hint = opts.hint;
if (this.requiredChoice) this.hint += ' *';
}
else if ('undefined' !== typeof opts.hint) {
throw new TypeError('CustomInputGroup.init: hint must ' +
'be a string, 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('CustomInputGroup.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 className, if not use default.
if ('undefined' === typeof opts.className) {
this.className = CustomInputGroup.className;
}
else if (opts.className === false ||
'string' === typeof opts.className ||
J.isArray(opts.className)) {
this.className = opts.className;
}
else {
throw new TypeError('CustomInputGroup.init: ' +
'className must be string, array, ' +
'or undefined. Found: ' + opts.className);
}
// sharedOptions.
if ('undefined' !== typeof opts.sharedOptions) {
if ('object' !== typeof opts.sharedOptions) {
throw new TypeError('CustomInputGroup.init: sharedOptions' +
' must be object or undefined. Found: ' +
opts.sharedOptions);
}
if (opts.sharedOptions.hasOwnProperty('name')) {
throw new Error('CustomInputGroup.init: sharedOptions ' +
'cannot contain property name. Found: ' +
opts.sharedOptions);
}
this.sharedOptions = J.mixin(this.sharedOptions,
opts.sharedOptions);
}
if ('undefined' !== typeof opts.summary) {
if ('string' === typeof opts.summary) {
opts.summary = { mainText: opts.summary }
}
else if ('object' !== typeof opts.summary) {
throw new TypeError('CustomInputGroup.init: summary' +
' must be object or undefined. Found: ' +
opts.summary);
}
this.summaryInput = opts.summary;
}
// After all configuration options 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('CustomInputGroup.init: table ' +
'must be object, false or undefined. ' +
'Found: ' + opts.table);
}
// TODO: check this.
this.table = opts.table;
this.freeText = 'string' === typeof opts.freeText ?
opts.freeText : !!opts.freeText;
// Add the items.
if ('undefined' !== typeof opts.items) this.setItems(opts.items);
};
/**
* ### CustomInputGroup.setItems
*
* Sets the available items and optionally builds the table
*
* @param {array} items The array of items
*
* @see CustomInputGroup.table
* @see CustomInputGroup.order
* @see CustomInputGroup.shuffleItems
* @see CustomInputGroup.buildTable
*/
CustomInputGroup.prototype.setItems = function(items) {
var len;
if (!J.isArray(items)) {
throw new TypeError('CustomInputGroup.setItems: ' +
'items must be array. Found: ' + items);
}
if (!items.length) {
throw new Error('CustomInputGroup.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();
};
/**
* ### CustomInputGroup.buildTable
*
* Builds the table of clickable items and enables it
*
* Must be called after items have been set already.
*
* @see CustomInputGroup.setCustomInputs
*/
CustomInputGroup.prototype.buildTable = function() {
var i, len, tr, H;
H = this.orientation === 'H';
i = -1, len = this.itemsSettings.length;
if (H) tr = createTR(this, 'row1');
for ( ; ++i < len ; ) {
// Add new TR.
if (!H) tr = createTR(this, 'row' + (i+1));
addCustomInput(this, tr, i);
}
if (this.summaryInput) {
if (!H) tr = createTR(this, 'row' + (i+1));
addSummaryInput(this, tr, i);
}
var that = this;
this.table.onclick = function() {
// Remove any warning/error from form on click.
if (that.isHighlighted()) that.unhighlight();
};
this.enable(true);
};
/**
* ### CustomInputGroup.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
*/
CustomInputGroup.prototype.append = function() {
// Id must be unique.
if (W.getElementById(this.id)) {
throw new Error('CustomInputGroup.append: id ' +
'is not unique: ' + this.id);
}
// MainText.
if (this.mainText) {
this.spanMainText = W.append('span', this.bodyDiv, {
className: 'custominputgroup-maintext',
innerHTML: this.mainText
});
}
// Hint.
if (this.hint) {
W.append('span', this.spanMainText || this.bodyDiv, {
className: 'custominputgroup-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');
this.textarea.id = this.id + '_text';
this.textarea.className = CustomInputGroup.className + '-freetext';
if ('string' === typeof this.freeText) {
this.textarea.placeholder = this.freeText;
}
// Append textarea.
this.bodyDiv.appendChild(this.textarea);
}
};
/**
* ### CustomInputGroup.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
*/
CustomInputGroup.prototype.listeners = function() {
var that = this;
node.on('INPUT_DISABLE', function() {
that.disable();
});
node.on('INPUT_ENABLE', function() {
that.enable();
});
};
/**
* ### CustomInputGroup.disable
*
* Disables clicking on the table and removes CSS 'clicklable' class
*/
CustomInputGroup.prototype.disable = function() {
if (this.disabled === true || !this.table) return;
this.disabled = true;
this.emit('disabled');
};
/**
* ### CustomInputGroup.enable
*
* Enables clicking on the table and adds CSS 'clicklable' class
*
* @return {function} cb The event listener function
*/
CustomInputGroup.prototype.enable = function(force) {
if (!this.table || (!force && !this.disabled)) return;
this.disabled = false;
this.emit('enabled');
};
/**
* ### CustomInputGroup.highlight
*
* Highlights the choice table
*
* @param {string} The style for the table's border.
* Default '1px solid red'
*
* @see CustomInputGroup.highlighted
*/
CustomInputGroup.prototype.highlight = function(border) {
if (border && 'string' !== typeof border) {
throw new TypeError('CustomInputGroup.highlight: border must be ' +
'string or undefined. Found: ' + border);
}
if (!this.table || this.highlighted === true) return;
this.table.style.border = border || '3px solid red';
this.highlighted = true;
this.emit('highlighted', border);
};
/**
* ### CustomInputGroup.unhighlight
*
* Removes highlight from the choice table
*
* @see CustomInputGroup.highlighted
*/
CustomInputGroup.prototype.unhighlight = function() {
if (!this.table || this.highlighted !== true) return;
this.table.style.border = '';
this.highlighted = false;
this.errorBox.innerHTML = '';
this.emit('unhighlighted');
};
/**
* ### CustomInputGroup.getValues
*
* Returns the values for current selection and other paradata
*
* Paradata that is not set or recorded will be omitted
*
* @param {object} opts Optional. Configures the return value.
* Available optionts:
*
* - valuesOnly: just returns the current values, no other checkings.
* - markAttempt: If TRUE, getting the value counts as an attempt
* to find the correct answer. Default: TRUE.
* - highlight: If TRUE, if current value is not the correct
* value, widget will be highlighted. Default: TRUE.
* - reset: If TRUTHY and no item raises an error,
* then it resets the state of all items before
* returning it. Default: FALSE.
*
* @return {object} Object containing the choice and paradata
*
* @see CustomInputGroup.verifyChoice
* @see CustomInputGroup.reset
*/
CustomInputGroup.prototype.getValues = function(opts) {
var res, i, len, input, toReset, values;
opts = opts || {};
i = -1, len = this.items.length;
if (opts.valuesOnly) {
res = {};
for ( ; ++i < len ; ) {
res[this.items[i].id] =
this.items[i].getValues({ valuesOnly: true });
}
return res;
}
res = {
id: this.id,
order: this.order,
items: {},
isCorrect: true
};
if ('undefined' === typeof opts.highlight) opts.highlight = true;
// TODO: do we need markAttempt?
// if ('undefined' === typeof opts.markAttempt) opts.markAttempt = true;
// Make sure reset is done only at the end.
toReset = opts.reset;
opts.reset = false;
if (this.validation) values = {};
for ( ; ++i < len ; ) {
input = this.items[i];
res.items[input.id] = input.getValues(opts);
// TODO is null or empty?
if (res.items[input.id].value === "") {
res.missValues = true;
// TODO: check do we need to check for correctChoice?
if (input.requiredChoice || input.required ||
input.correctChoice) {
res.err = true;
res.isCorrect = false;
}
}
if (res.items[input.id].isCorrect === false && opts.highlight) {
res.err = true;
}
if (values) values[input.id] = res.items[input.id].value;
}
if (!res.err && values) {
// res.err = this.getText('inputErr');
this.validation(res, values);
if (opts.highlight && res.err) this.setError(res.err);
if (res.err) res.isCorrect = false;
}
else if (toReset) this.reset(toReset);
if (!res.isCorrect && opts.highlight) this.highlight();
// Restore opts.reset.
opts.reset = toReset;
if (this.textarea) res.freetext = this.textarea.value;
return res;
};
/**
* ### CustomInputGroup.setValues
*
* Sets values in the choice table group as specified by the options
*
* @param {object} options Optional. Options specifying how to set
* the values. If no parameter is specified, random values will
* be set.
*
* @see CustomInput.setValues
*
* @experimental
*/
CustomInputGroup.prototype.setValues = function(opts) {
var i, len;
if (!this.items || !this.items.length) {
throw new Error('CustomInputGroup.setValues: no items found.');
}
opts = opts || {};
i = -1, len = this.items.length;
for ( ; ++i < len ; ) {
this.items[i].setValues(opts);
}
// Make a random comment.
if (this.textarea) this.textarea.value = J.randomString(100, '!Aa0');
};
/**
* ### CustomInputGroup.reset
*
* Resets all the CustomInput items and textarea
*
* @param {object} options Optional. Options specifying how to set
* to reset each item
*
* @see CustomInput.reset
* @see CustomInputGroup.shuffle
*/
CustomInputGroup.prototype.reset = function(opts) {
var i, len;
opts = opts || {};
i = -1, len = this.items.length;
for ( ; ++i < len ; ) {
this.items[i].reset(opts);
}
// Delete textarea, if found.
if (this.textarea) this.textarea.value = '';
if (opts.shuffleItems) this.shuffle();
if (this.isHighlighted()) this.unhighlight();
};
/**
* ### CustomInputGroup.shuffle
*
* Shuffles the order of the displayed items
*
* Assigns the new order of items to `this.order`.
*
* @param {object} options Optional. Not used for now.
*
* TODO: shuffle choices in each item. (Note: can't use
* item.shuffle, because the cells are taken out, so
* there is no table and no tr in there)
*
* JSUS.shuffleElements
*/
CustomInputGroup.prototype.shuffle = function(opts) {
var order, i, len, that, cb, newOrder;
if (!this.items) return;
len = this.items.length;
if (!len) return;
that = this;
newOrder = new Array(len);
// Updates the groupOrder property of each item,
// and saves the order of items correctly.
cb = function(el, newPos, oldPos) {
var i;
i = el.id.split(that.separator);
i = that.orientation === 'H' ? i[2] : i[0];
i = that.itemsMap[i];
that.items[i].groupOrder = (newPos+1);
newOrder[newPos] = i;
};
order = J.shuffle(this.order);
if (this.orientation === 'H') {
J.shuffleElements(this.table, order, cb);
}
else {
// Here we maintain the columns manually. Each TR contains TD
// belonging to different items, we make sure the order is the
// same for all TR.