This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
control.js
940 lines (874 loc) 路 46 KB
/
control.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
/**
* control.js - representational metaphor
* Represents a control in the actual XForm. It only manages and stores data
* about its internal structure and properties - the general form structure
* is managed by the DOM.
*/
// <output value="xpath"/> for substitution inside question text
;(function($)
{
var validationNS = odkmaker.namespace.load('odkmaker.validation');
var controlNS = odkmaker.namespace.load('odkmaker.control');
// Globally active singleton facilities:
var $propertyList = $('.propertyList');
var drawPropertyList = function($this, properties)
{
// clear out and reconstruct property list
$propertyList.empty();
var $advancedContainer = $.tag({
_: 'li', 'class': 'advanced', contents: [
{ _: 'a', 'class': 'toggle', href: '#advanced', contents: [
{ _: 'div', 'class': 'icon' },
'Advanced'
] },
{ _: 'ul', 'class': 'advancedProperties toggleContainer' }
]
});
var $advancedList = $advancedContainer.find('.advancedProperties');
// add our hero's properties
_.each(properties, function(property, name)
{
if (name === 'metadata') return;
$('<li class="propertyItem"/>')
.propertyEditor(property, name, $this)
.appendTo((property.advanced === true) ? $advancedList : $propertyList);
});
// drop in advanced
$propertyList.append($advancedContainer);
};
kor.events.listen({ verb: 'control-selected', callback: function(event)
{
var numSelected = $('.control.selected').length;
if (numSelected === 1)
drawPropertyList(event.subject, event.subject.data('odkControl-properties'));
else
$propertyList
.empty()
.append('<li class="emptyData">(multiple questions selected)</li>');
} });
kor.events.listen({ verb: 'control-deselected', callback: function(event)
{
var $selected = $('.control.selected');
if ($selected.length === 0)
odkmaker.application.clearProperties();
else if ($selected.length === 1)
drawPropertyList($selected.eq(0), $selected.eq(0).data('odkControl-properties'));
} });
// Wired events
$(function()
{
$propertyList.on('click', '.toggle', function(event)
{
event.preventDefault();
$propertyList.toggleClass('showAdvanced');
});
});
// Private methods
// our own incrementing counter:
var untitledCount_ = 0;
var untitledCount = function() { return untitledCount_++; };
var refreshFromProperties = function($this, type, options, properties)
{
var $info = $this.children('.controlInfo');
var $headline = $info.children('.controlHeadline');
$info.children('.controlName').text(properties.name.value);
if (type == 'group')
{
$headline.children('.controlLabel').text($.emptyString(properties.label.value[odkmaker.i18n.displayLanguage()], '[no group caption text yet]'));
}
else if (type == 'metadata')
{
$headline.children('.controlLabel').text(properties.kind.value);
}
else
{
$headline.children('.controlLabel').text($.emptyString(properties.label.value[odkmaker.i18n.displayLanguage()], '[no caption text yet]'));
$headline.children('.controlShort').text(properties['short'].value ? properties['short'].value[odkmaker.i18n.displayLanguage()] : '');
$headline.children('.controlHint').text(properties.hint.value[odkmaker.i18n.displayLanguage()]);
$headline.children('.controlGuidance').text(properties.guidance.value ? properties.guidance.value[odkmaker.i18n.displayLanguage()] : '');
// Here we could indicate the presence of media labels, although their resource files are external to ODK Build
}
var $propertyList = $info.children('.controlProperties');
$propertyList.empty();
_.each(properties, function(property, name)
{
if ((name === 'metadata') || (property.summary === false) || (property.value !== true))
return;
$propertyList.append(
$('<li>' + property.name + '</li>')
);
});
_.each(properties, function(property)
{
if (property.bindControlClass != null)
$this.toggleClass(property.bindControlClass, ((property.value != null) && (property.value !== false)));
});
// SPECIAL CASES:
// update the followup question text from that value.
if ((properties.other != null) && (properties.other.value != null) && (properties.other.value !== false))
$info.find('.controlSuccessorCondition span').text(properties.other.value.join(' or '));
// add a cascading slave if cascade is checked.
if ((properties.cascading != null) && (properties.cascading.value === true) && !$this.next().hasClass('slave'))
{
var $slaves = null;
if (($slaves = $this.data('odkControl-cascadeBackup')) != null)
{
$this.after($slaves);
$slaves.trigger('odkControl-added');
}
else
$this.after($('#templates .control').clone().addClass(type).odkControl(type, { slave: true }));
}
// remove all subsequent cascading slaves if cascade is unchecked.
if ((properties.cascading == null) || (properties.cascading.value === false) && $this.next().hasClass('slave'))
{
var $slaves = $this.nextUntil(':not(.slave)');
$slaves.each(function()
{
var $slave = $(this);
validationNS.controlDestroyed($slave, $slave.data('odkControl-properties'));
});
$slaves.trigger('odkControl-removing');
$slaves.detach();
$slaves.trigger('odkControl-removed');
$this.data('odkControl-cascadeBackup', $slaves);
}
};
// gets all controls "between" two given controls, stepping in and out of groups
// as necessary. root and target themselves are never part of the set.
var getStack = function($root, $target)
{
var $result = null;
if ($root.is($target))
{
return $root;
}
else if ($root.siblings().is($target))
{
// root and target are siblings.
$result = $root.prevAll().is($target) ? $root.prevUntil($target) : $root.nextUntil($target);
}
else
{
// not siblings; compare the hierarchies to figure out the closest common parent.
var $rootLevels = $($root.parents('.workspace, .control').get().reverse()).add($root);
var $targetLevels = $($target.parents('.workspace, .control').get().reverse()).add($target);
// to figure out directionality, we have to first get pair of siblings to compare.
var $commonContainer = null;
for (var i = 0; i < $rootLevels.length; i++)
{
if ($rootLevels[i] !== $targetLevels[i]) break;
$commonContainer = $rootLevels[i];
}
var divergentIdx = i;
var $rootDiverge = $rootLevels.eq(divergentIdx);
var $targetDiverge = $targetLevels.eq(divergentIdx);
// first deal with everything at common-container level.
$result = getStack($rootDiverge, $targetDiverge);
// now select the head or tail of all intervening containers as appropriate.
// first we determine the direction.
if ($rootDiverge.nextAll().is($targetDiverge))
var $before = $rootLevels, $after = $targetLevels;
else
var $before = $targetLevels, $after = $rootLevels;
// we start with the container nested within the divergent one.
for (var i = (divergentIdx + 1); i < $before.length; i++)
$result = $result.add($before.eq(i).nextAll());
for (var i = (divergentIdx + 1); i < $after.length; i++)
$result = $result.add($after.eq(i).prevAll());
}
// find any containers within the linear selections and select their contents.
$result = $result.add($result.find('.control'));
return $result;
};
var select = function()
{
var $this = $(this);
$this.addClass('selected');
kor.events.fire({ subject: $this, verb: 'control-selected' });
};
var deselect = function()
{
var $this = $(this);
$this.removeClass('selected');
kor.events.fire({ subject: $this, verb: 'control-deselected' });
};
// perform a selection action. this might mean modifying an active multi-selection.
var $initiator = null; // the last control the user selected singly.
var $endStop = null;
var performSelection = function($this, type, options, properties, selectMany, selectOne)
{
if (selectMany === true)
{
if ($initiator == null)
// not sure what to do; just pretend shift isn't held.
return performSelection($this, type, options, properties, false, false);
if ($endStop == null)
{
// if we have no endstop, simply select from the initiator through target.
getStack($initiator, $this).add($this).each(select);
}
else if ($endStop.is($this))
{
// nothing would happen anyway; don't bother with computing it.
return;
}
else if (getStack($initiator, $endStop).is($this))
{
// if we are selecting somewhere inside the active range, deselect the now-excess.
getStack($endStop, $this).add($endStop).each(deselect);
}
else
{
// we are somewhere outside the active range entirely. the easiest thing to do
// rather than do a bunch of work determining where exactly is just to nuke it
// and start over.
getStack($initiator, $endStop).add($endStop).each(deselect);
getStack($initiator, $this).add($this).each(select);
}
// if either endpoint is a container itself, now is the time to also select them.
$initiator.find('.control').each(select);
$this.find('.control').each(select);
if ($initiator.is($this))
$endStop = null;
else
$endStop = $this;
}
else if (selectOne === true)
{
if ($this.hasClass('selected'))
$this.each(deselect);
else
{
$this.each(select);
$initiator = $this;
$endStop = null;
}
}
else
{
$('.workspace .control.selected').each(deselect);
$this.addClass('selected');
kor.events.fire({ subject: $this, verb: 'control-selected' });
$initiator = $this;
$endStop = null;
}
};
var deleteControl = function($control)
{
$control.slideUp('normal', function()
{
var $controlAndChildren = $control.find('.control').add($control);
$controlAndChildren.each(function()
{
var $this = $(this);
// don't do anything if this control has already been processed for deletion.
if (document.contains($this[0]))
validationNS.controlDestroyed($this, $this.data('odkControl-properties'));
});
$controlAndChildren.each(deselect);
$controlAndChildren.trigger('odkControl-removing');
$control.remove();
$controlAndChildren.trigger('odkControl-removed');
});
};
// Constructor
var loadTime = (new Date()).getTime(); // get time in nanos to ~ensure universal uniqueness of id.
$.fn.odkControl = function(type, options, defaultProperties)
{
// Abort for unknown types
if ($.fn.odkControl.controlProperties[type] === undefined)
return;
var options = $.extend({}, $.fn.odkControl.defaults, options);
return this.each(function()
{
var $this = $(this);
var id = loadTime + '_' + _.uniqueId();
$this.data('odkControl-id', id);
$this.attr('id', 'control' + id);
$this.data('odkControl-type', type);
// Deep clone the properties if relevant
var properties = null;
if (defaultProperties != null)
properties = defaultProperties;
else if ((type == 'group') || (type == 'branch') || (type == 'metadata'))
properties = $.extend(true, {}, $.fn.odkControl.controlProperties[type]);
else
{
properties = $.extend(true, {}, $.fn.odkControl.defaultProperties);
var controlProperties = $.fn.odkControl.controlProperties[type];
for (var prop in controlProperties)
delete properties[prop];
$.extend(true, properties, controlProperties);
}
// add metadata bag.
if (properties.metadata == null) properties.metadata = {};
// transmute slave option into permanent property, then act as appropriate.
if (options.slave === true) properties.metadata.slave = true;
if (properties.metadata.slave === true)
{
$this.addClass('slave');
$this.data('odkControl-parent', $this.prev().data('odkControl-properties'));
}
var match = null;
if (properties.name.value == 'untitled')
properties.name.value += (untitledCount() + 1);
else if ((match = /^untitled(\d+)$/.exec(properties.name.value)) != null)
if ((match = parseInt(match[1])) > untitledCount_)
untitledCount_ = match;
_.each(properties, function(property, name)
{
if (name === 'metadata') return;
property.id = name;
property.validations = [];
});
$this.data('odkControl-properties', properties);
$this.bind('odkControl-propertiesUpdated', function(event)
{
event.stopPropagation();
kor.events.fire({ subject: $this, verb: 'properties-updated' });
refreshFromProperties($this, type, options, properties);
});
$this.trigger('odkControl-propertiesUpdated');
validationNS.controlCreated($this, properties);
$this.bind('odkControl-reloadProperties odkControl-select', function(event)
{
performSelection($this, type, options, properties);
});
$this.bind('odkControl-deselect', deselect);
$this.on('click', function(event)
{
event.stopPropagation();
if ($.isMsft)
performSelection($this, type, options, properties, $.keys.selectMany, $.keys.selectOne);
else
performSelection($this, type, options, properties, event.shiftKey, $.isSelectOne(event));
});
$this.bind('odkControl-validationChanged', function(event, property, hasError)
{
if (this !== event.target) return;
if (hasError === true)
$this.addClass('error');
else
{
var validations = $this.data('odkControl-validations');
$this.removeClass('error warning');
_.each(validations, function(validation)
{
if (validation.failed === true)
$this.addClass((validation.isWarning === true) ? 'warning' : 'error');
});
}
});
// special treatment for groups and branches
if (type == 'group')
$('<div class="workspaceInnerWrapper"><div class="workspaceInner"></div></div><div class="groupFooter"></div>')
.insertAfter($this.children('.controlInfo'));
// event wireup
$this.find('.deleteControl').click(function(event)
{
event.stopPropagation();
event.preventDefault();
// if the control to which this delete button belongs is not selected at all,
// always just delete this one.
var $selected;
if (!$this.hasClass('selected'))
$selected = $this;
else
$selected = $('.control.selected');
if ($selected.length > 1)
odkmaker.application.ask('What do you want to delete?', {
'Just this question': function() { deleteControl($this); },
'All selected questions': function() { $selected.each(function() { deleteControl($(this)); }); },
'Cancel': null
});
else
deleteControl($this);
});
// set up dragging
$this.one('mouseenter', function()
{
$this.draggable({ handleAddedClass: 'dragging' });
});
});
};
// Plugin Defaults
$.fn.odkControl.defaults = {
};
// Default property fields
$.fn.odkControl.defaultProperties = {
name: { name: 'Data Name',
type: 'text',
description: 'The name of the column in the exported data. This is not shown during collection.',
tips: [ 'Must start with a letter, and may only include letters, numbers, hyphens, underscores, and periods.' ],
validation: [ 'required', 'xmlLegalChars', 'unique', 'alphaStart' ],
required: true,
value: 'untitled',
summary: false },
label: { name: 'Label',
type: 'uiText',
description: 'The title text that is presented to the person filling the form.',
tips: [ 'You can reference previous answers using <a href="https://getodk.github.io/xforms-spec/#xpath-paths" rel="external"><code>${data/path/field}</code> syntax</a>.' ],
required: true,
value: {},
summary: false },
hint: { name: 'Hint',
type: 'uiText',
description: 'Additional help information for this question for the person filling the form. Hints are always shown.',
tips: [ 'You can reference previous answers using <a href="https://getodk.github.io/xforms-spec/#xpath-paths" rel="external"><code>${/data/path/field}</code> syntax</a>.' ],
value: {},
summary: false },
defaultValue: { name: 'Default Value',
type: 'text',
description: 'The value this field is pre-filled with initially.',
value: '',
summary: false },
readOnly: { name: 'Read Only',
type: 'bool',
description: 'Whether or not this field can be edited by the person filling the form.',
tips: [ 'This is often used together with a Default Value or a Calculate.' ],
value: false,
summary: true },
required: { name: 'Required',
type: 'bool',
description: 'Whether this field must be filled in before continuing.',
value: false,
summary: true },
requiredText: { name: 'Required Text',
type: 'uiText',
bindDisplayIf: 'required',
description: 'The error text to display if this field is not filled in.',
value: {},
summary: false },
relevance: { name: 'Relevance',
label: 'Show question if',
type: 'text',
description: 'Specify a custom expression to evaluate to determine if this field is shown.',
tips: [ 'The <a href="https://getodk.github.io/xforms-spec/#xpath-functions" rel="external">ODK XForms Functions Spec</a> may be useful.' ],
value: '',
validation: [ 'fieldListExpr' ],
advanced: true,
summary: false },
constraint: { name: 'Constraint',
type: 'text',
description: 'Specify a custom expression to validate the user input.',
tips: [
'The <a href="https://getodk.github.io/xforms-spec/#xpath-functions" rel="external">ODK XForms Functions Spec</a> may be useful.',
'If this constraint check fails, the Invalid Text is displayed.'
],
value: '',
advanced: true,
summary: false },
// this is automatically overridden by controls that have their own specified invalidText:
invalidText: { name: 'Constraint Invalid Text',
type: 'uiText',
description: 'Message to display if the value fails the custom constraint check.',
value: {},
advanced: true,
summary: false },
calculate: { name: 'Calculate',
type: 'text',
description: 'Specify a custom expression to store a calculated value in this field.',
tips: [ 'The <a href="https://getodk.github.io/xforms-spec/#xpath-functions" rel="external">ODK XForms Functions Spec</a> may be useful.' ],
value: '',
advanced: true,
summary: false },
'short': { name: 'Short Label',
type: 'uiText',
description: 'A shorter version of the label, meant for very small screens, or to be shown in a summary of the form data.',
tips: [ 'You can reference previous answers using <a href="https://getodk.github.io/xforms-spec/#xpath-paths" rel="external"><code>${/data/path/field}</code> syntax</a>.' ],
value: {},
advanced: true,
summary: false },
image: { name: 'Image Label (filename)',
type: 'uiText',
description: 'Media labels can be used in addition to text labels or instead of text labels.',
tips: [ 'Provide a filename with extension, but without a folder path, e.g. "myimage.png".' ],
value: {},
advanced: true,
summary: false },
audio: { name: 'Audio Label (filename)',
type: 'uiText',
description: 'Media labels can be used in addition to text labels or instead of text labels.',
tips: [ 'Provide a filename with extension, but without a folder path, e.g. "myaudio.mp3".' ],
value: {},
advanced: true,
summary: false },
video: { name: 'Video Label (filename)',
type: 'uiText',
description: 'Media labels can be used in addition to text labels or instead of text labels.',
tips: [ 'Provide a filename with extension, but without a folder path, e.g. "myvideo.mp4".' ],
value: {},
advanced: true,
summary: false },
bigimage: { name: 'Clickable Image Label (filename)',
type: 'uiText',
description: 'Media labels can be used in addition to text labels or instead of text labels.',
tips: [ 'You must provide an "image" label in combination with "big-image". Provide a filename with extension, but without a folder path, e.g. "mybigimage.png".' ],
value: {},
advanced: true,
summary: false },
guidance: { name: 'Guidance',
type: 'uiText',
description: 'Guidance is ideal to include verbose training information or other context. Guidance can be always hidden, always shown, or shown collapsed to be expanded on demand in General Settings > Form management.',
tips: [ 'You can reference previous answers using <a href="https://getodk.github.io/xforms-spec/#xpath-paths" rel="external"><code>${/data/path/field}</code> syntax</a>.' ],
value: {},
advanced: true,
summary: false }
};
// Property fields per control type
$.fn.odkControl.controlProperties = {
inputText: {
length: { name: 'Length',
type: 'numericRange',
description: 'Valid input text length for this user input of this control.',
tips: [
'Numbers are inclusive, so a minimum of 2 would allow 2 or more characters.',
'For an open-ended range, fill in only a minimum or a maximum and leave the other blank.' ],
value: false,
summary: false },
invalidText:{ name: 'Invalid Text',
type: 'uiText',
description: 'Message to display if the value fails the length check.',
tips: [ 'It is also displayed if a custom constraint check is failed.' ],
value: {},
summary: false } },
inputNumeric: {
range: { name: 'Valid Range',
type: 'numericRange',
description: 'Valid numeric range for the user input of this control.',
tips: [
'Inclusive means the given number is valid; so a minimum of 3 without inclusive selected would prohibit 3 but allow 3.01 or 4.',
'For an open-ended range, fill in only a minimum or a maximum and leave the other blank.' ],
value: false,
summary: false },
invalidText:{ name: 'Invalid Text',
type: 'uiText',
description: 'Message to display if the value fails the range check.',
tips: [ 'It is also displayed if a custom constraint check is failed.' ],
value: {},
summary: false },
appearance: { name: 'Style',
type: 'enum',
description: 'Style of collection interface to present.',
tips: [
'A Picker, also known as a Spinner, is a little textbox with plus and minus buttons to change the number.',
'Bearing captures the devices orientation and allows the user to rotate the device to change the value.' ],
options: [ 'Textbox',
'Slider',
'Vertical Slider',
'Picker',
'Bearing'],
value: 'Textbox',
summary: true },
kind: { name: 'Kind',
type: 'enum',
bindDisplayIf: { appearance: 'Textbox' },
description: 'Type of number accepted.',
tips: [ 'In some data collection tools, this will affect the type of keypad shown.' ],
options: [ 'Integer',
'Decimal' ],
value: 'Integer',
summary: true },
selectRange:{ name: 'Selectable Range',
type: 'numericRange',
validation: [ 'rangeRequired' ],
bindDisplayIf: { appearance: [ 'Slider', 'Vertical Slider', 'Picker' ] },
optional: false,
inclusivity: false,
description: 'The lowest and highest selectable values, inclusive.',
tips: [ 'Integers and decimals are both valid.' ],
value: { min: "1", max: "10" },
summary: false },
selectStep: { name: 'Selectable Range: Step Between Choices',
type: 'text',
validation: [ 'required', 'numeric', 'stepDivision' ],
bindDisplayIf: { appearance: [ 'Slider', 'Vertical Slider', 'Picker' ] },
description: 'The gap between adjacent selectable values in the range.',
tips: [ 'The step must cleanly end at the low and high ends of the range; in other words, it must divide the selectable range perfectly.' ],
value: '1',
summary: false },
sliderTicks:{ name: 'Slider Ticks',
type: 'bool',
bindDisplayIf: { appearance: [ 'Slider', 'Vertical Slider' ] },
description: 'The lowest and highest selectable values, inclusive.',
tips: [ 'Integers and decimals are both valid.' ],
value: true,
summary: false } },
inputDate: {
range: { name: 'Range',
type: 'dateRange',
description: 'Valid range for the user input of this control.',
tips: [
'Inclusive means the given date is valid; so a minimum of 2015/01/01 without inclusive selected would prohibit 2015/01/01 but allow 2015/01/02 or 2015/01/01 00:00:01.',
'For an open-ended range, fill in only a minimum or a maximum and leave the other blank.' ],
value: false,
summary: false },
invalidText:{ name: 'Invalid Text',
type: 'uiText',
description: 'Message to display if the value fails the range check.',
tips: [ 'It is also displayed if a custom constraint check is failed.' ],
value: {},
summary: false },
kind: { name: 'Kind',
type: 'enum',
description: 'Type of value to prompt for.',
options: [ 'Full Date', 'Year and Month', 'Year', 'Full Date and Time' ],
value: 'Full Date',
summary: true } },
inputTime: {},
inputLocation: {
kind: { name: 'Kind',
type: 'enum',
description: 'Type of location information to collect.',
options: [ 'Point',
'Path',
'Shape' ],
tips: [ 'A path may be open-ended, while a shape will be closed automatically to form a polygon.' ],
value: 'Point',
summary: true },
appearance: { name: 'Style',
type: 'enum',
description: 'How to collect the information.',
options: [ 'Default (GPS)', 'Show Map (GPS)', 'Manual (No GPS)' ],
value: 'Default (GPS)',
summary: true } },
inputMedia: {
kind: { name: 'Kind',
type: 'enum',
description: 'Type of media to upload.',
options: [ 'Image',
'New Image',
'Selfie',
'Annotate',
'Draw',
'Signature',
'Audio',
'Video',
'Selfie Video' ],
value: 'Image' } },
inputBarcode: {},
inputSelectOne: {
options: { name: 'Options',
type: 'optionsEditor',
validation: [ 'underlyingRequired', 'underlyingLegalChars', 'underlyingLength', 'hasOptions' ],
description: 'The options the person filling the form may select from.',
tips: [
'If you have many options or reuse options frequently, use Bulk Edit.',
'The Underlying Value is the value saved to the exported data.' ],
value: [{ text: {}, cascade: [], val: 'untitled' }],
summary: false },
cascading: { name: 'Cascading',
type: 'bool',
description: 'Enable this to add a subsequent select question with filtered options.',
value: false,
summary: false },
other: { name: 'Follow-up Question',
type: 'otherEditor',
bindAllowedIf: { 'cascading': [ null, undefined, false ] },
validation: [ 'fieldListFollowup' ],
description: 'Ask the following question as additional information only if a particular response is chosen.',
tips: [
'You can use this to easily prompt for more information if the user selects "Other," for example.',
'Whatever the following question is, it will only be asked if the user selects the value you specify here.' ],
bindTo: 'options',
bindControlClass: 'hasSuccessorBinding',
value: false,
summary: false },
appearance: { name: 'Style',
type: 'enum',
description: 'What interface to present.',
options: [ 'Default', 'Minimal (spinner)', 'Table', 'Horizontal Layout' , 'Likert'],
value: 'Default',
summary: true } },
inputSelectMany: {
options: { name: 'Options',
type: 'optionsEditor',
description: 'The options the person filling the form may select from.',
tips: [
'If you have many options or reuse options frequently, use Bulk Edit.',
'The Underlying Value is the value saved to the exported data.' ],
validation: [ 'underlyingRequired', 'underlyingLegalChars', 'underlyingLength', 'hasOptions' ],
value: [{ text: {}, cascade: [], val: 'untitled' }],
summary: false },
other: { name: 'Follow-up Question',
type: 'otherEditor',
validation: [ 'fieldListExpr' ],
description: 'Ask the following question as additional information only if a particular response is chosen.',
tips: [
'You can use this to easily prompt for more information if the user selects "Other," for example.',
'Whatever the following question is, it will only be asked if the user selects the value you specify here.' ],
bindTo: 'options',
bindControlClass: 'hasSuccessorBinding',
value: false,
summary: false },
count: { name: 'Response Count',
type: 'numericRange',
description: 'This many options must be selected for the response to be valid.',
tips: [
'Numbers are inclusive, so a minimum of 2 would allow 2 or more selected options.',
'For an open-ended range, fill in only a minimum or a maximum and leave the other blank.' ],
value: false,
summary: true },
invalidText:{ name: 'Invalid Text',
type: 'uiText',
description: 'Message to display if the value fails the response count check.',
tips: [ 'It is also displayed if a custom constraint check is failed.' ],
value: {},
summary: false },
appearance: { name: 'Style',
type: 'enum',
description: 'What interface to present.',
options: [ 'Default', 'Minimal (spinner)', 'Table', 'Horizontal Layout' ],
value: 'Default',
summary: true } },
group: {
name: { name: 'Data Name',
type: 'text',
description: 'The name of the column in the exported data. This is not shown during collection.',
tips: [ 'Must start with a letter, and may only include letters, numbers, hyphens, underscores, and periods.' ],
validation: [ 'required', 'xmlLegalChars', 'unique', 'alphaStart' ],
required: true,
value: 'untitled',
summary: false },
label: { name: 'Label',
type: 'uiText',
description: 'The title text that is presented to the person filling the form.',
tips: [ 'You can reference previous answers using <a href="https://getodk.github.io/xforms-spec/#xpath-paths" rel="external"><code>${/xform/data/path}</code> syntax</a>.' ],
required: true,
value: {},
summary: false },
loop: { name: 'Looped',
type: 'bool',
description: 'Whether or not to allow this group to loop.',
tips: [ 'If enabled, the person filling the form will be prompted after each cycle if they wish to add another entry.' ],
value: false },
fieldList: { name: 'Display On One Screen',
type: 'bool',
description: 'Display all the controls in this group on one screen',
validation: [ 'fieldListChildren' ],
value: false },
relevance: { name: 'Relevance',
label: 'Show question if',
type: 'text',
description: 'Specify a custom expression to evaluate to determine if this group is shown.',
tips: [ 'The <a href="https://getodk.github.io/xforms-spec/#xpath-functions" rel="external">ODK XForms Functions Spec</a> may be useful.' ],
value: '',
validation: [ 'fieldListExpr' ],
advanced: true,
summary: false } },
branch: {
logic: { name: 'Rules',
type: 'logicEditor',
description: 'Specify the rules that decide how the form will branch.',
value: [],
summary: false } },
metadata: {
name: { name: 'Data Name',
type: 'text',
description: 'The name of the column in the exported data.',
tips: [ 'Must start with a letter, and may only include letters, numbers, hyphens, underscores, and periods.' ],
validation: [ 'required', 'xmlLegalChars', 'unique' ],
required: true,
value: 'untitled',
summary: false },
kind: { name: 'Kind',
type: 'enum',
description: 'Type of metadata to add.',
options: [ 'Device ID', 'Start Time', 'End Time', 'Today', 'Username', 'Email', 'Subscriber ID', 'SIM Serial', 'Phone Number', 'Start Geopoint' ],
value: 'Device ID',
summary: true } },
};
// TODO: combine this and the above hash into one when all these declarations move out into an impl file.
$.fn.odkControl.controlInformation = {
inputText: {
name: 'Text',
description: 'Collects textual information. Use this for names, long-form responses, and other free text information.'
},
inputNumeric: {
name: 'Number',
description: 'Collects numeric information. An appropriate keyboard will be presented on mobile devices.',
tips: [
'You can choose between integer-only or decimal-allowed responses with the Kind property.',
'If you wish to collect numeric codes, particularly with leading zeroes, you may wish to use Text instead.'
]
},
inputDate: {
name: 'Date/Time',
description: 'Collects a calendar date. You can choose the granularity from year-only through full date and time.',
tips: [
'An appropriate calendar/time widget will be presented on mobile devices.',
'If you wish to collect only a time, use the Time type instead.'
]
},
inputTime: {
name: 'Time',
description: 'Collects just a time of day. If you wish to collect a time on a certain date, use Date/Time instead.',
tips: [ 'An appropriate time widget will be presented on mobile devices.' ]
},
inputLocation: {
name: 'Location',
description: 'Collects a geospatial point, path, or shape on the Earth. Use the Kind property to specify which.',
tips: [
'You can choose whether information is gathered automatically with the GPS location of the collection device ' +
'or selected manually on a map with the Style property.'
]
},
inputMedia: {
name: 'Media',
description: 'Takes a photo, or records an audio or video recording with the collection device.'
},
inputBarcode: {
name: 'Barcode',
description: 'Scans a barcode with an appropriate application on the collection device and stores the result.',
tips: [
'You will need a third-party application like the <a href="https://play.google.com/store/apps/details?id=com.google.zxing.client.android" rel="external">Zxing Barcode Scanner</a> installed on the collection device to use this feature.'
]
},
inputSelectOne: {
name: 'Choose One',
description: 'Allows the form filler to choose one from a set of predetermined choices.',
tips: [
// TODO: someday maybe create a feature that automatically fudges this in more easily for the form author.
// (probably wouldn't be hard, actually.)
'To create a freeform "Other" option, add one here, then follow it up with a Text question with the Relevance ' +
'property set to reference this question; for example: <code>${/xpath/to/this/choose/one} = \'other\'</code>'
]
},
inputSelectMany: {
name: 'Select Many',
description: 'Allows the form filler to select multiple options from a set of predetermined options.',
tips: [
// TODO: ditto.
'To create a freeform "Other" option, add one here, then follow it up with a Text question with the Relevance ' +
'property set to reference this question; for example: <code>${/xpath/to/this/select/many} = \'other\'</code>',
'You can specify how many options the form filler is allowed to choose with the Response Count property.'
]
},
group: {
name: 'Group',
description: 'Groups many questions together into a logical block.',
tips: [
'You can use groups to create looping sets of questions using the Looped option.',
'You can also display multiple questions at a time using the Display On One Screen option, but be careful ' +
'using Relevance declarations if you do so; they may not work as you expect.'
]
},
metadata: {
name: 'Metadata',
description: 'Metadata questions silently and automatically collect information about the session.',
}
};
controlNS.upgrade = {
2: function(form) {
var processControl = function(control)
{
if (_.isArray(control.children))
_.each(control.children, processControl);
if ((control.type === 'inputNumeric') && (control.appearance == null))
control.appearance = 'Textbox';
};
_.each(form.controls, processControl);
}
};
})(jQuery);