-
Notifications
You must be signed in to change notification settings - Fork 553
/
jsonform.js
3857 lines (3549 loc) · 135 KB
/
jsonform.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
/* Copyright (c) 2012 Joshfire - MIT license */
/**
* @fileoverview Core of the JSON Form client-side library.
*
* Generates an HTML form from a structured data model and a layout description.
*
* The library may also validate inputs entered by the user against the data model
* upon form submission and create the structured data object initialized with the
* values that were submitted.
*
* The library depends on:
* - jQuery
* - the underscore library
* - a JSON parser/serializer. Nothing to worry about in modern browsers.
* - the JSONFormValidation library (in jsv.js) for validation purpose
*
* See documentation at:
* http://developer.joshfire.com/doc/dev/ref/jsonform
*
* The library creates and maintains an internal data tree along with the DOM.
* That structure is necessary to handle arrays (and nested arrays!) that are
* dynamic by essence.
*/
/*global window*/
(function(serverside, global, $, _, JSON) {
if (serverside && !_) {
_ = require('underscore');
}
/**
* Regular expressions used to extract array indexes in input field names
*/
var reArray = /\[([0-9]*)\](?=\[|\.|$)/g;
/**
* Template settings for form views
*/
var fieldTemplateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g
};
/**
* Template settings for value replacement
*/
var valueTemplateSettings = {
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
};
/**
* Returns true if given value is neither "undefined" nor null
*/
var isSet = function (value) {
return !(_.isUndefined(value) || _.isNull(value));
};
/**
* Returns true if given property is directly property of an object
*/
var hasOwnProperty = function (obj, prop) {
return typeof obj === 'object' && obj.hasOwnProperty(prop);
}
/**
* The jsonform object whose methods will be exposed to the window object
*/
var jsonform = {util:{}};
// From backbonejs
var escapeHTML = function (string) {
if (!isSet(string)) {
return '';
}
string = '' + string;
if (!string) {
return '';
}
return string
.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/');
};
/**
* Escapes selector name for use with jQuery
*
* All meta-characters listed in jQuery doc are escaped:
* http://api.jquery.com/category/selectors/
*
* @function
* @param {String} selector The jQuery selector to escape
* @return {String} The escaped selector.
*/
var escapeSelector = function (selector) {
return selector.replace(/([ \!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;<\=\>\?\@\[\\\]\^\`\{\|\}\~])/g, '\\$1');
};
/**
*
* Slugifies a string by replacing spaces with _. Used to create
* valid classnames and ids for the form.
*
* @function
* @param {String} str The string to slugify
* @return {String} The slugified string.
*/
var slugify = function(str) {
return str.replace(/\ /g, '_');
}
/**
* Initializes tabular sections in forms. Such sections are generated by the
* 'selectfieldset' type of elements in JSON Form.
*
* Input fields that are not visible are automatically disabled
* not to appear in the submitted form. That's on purpose, as tabs
* are meant to convey an alternative (and not a sequence of steps).
*
* The tabs menu is not rendered as tabs but rather as a select field because
* it's easier to grasp that it's an alternative.
*
* Code based on bootstrap-tabs.js, updated to:
* - react to option selection instead of tab click
* - disable input fields in non visible tabs
* - disable the possibility to have dropdown menus (no meaning here)
* - act as a regular function instead of as a jQuery plug-in.
*
* @function
* @param {Object} tabs jQuery object that contains the tabular sections
* to initialize. The object may reference more than one element.
*/
var initializeTabs = function (tabs) {
var activate = function (element, container) {
container
.find('> .active')
.removeClass('active');
element.addClass('active');
};
var enableFields = function ($target, targetIndex) {
// Enable all fields in the targeted tab
$target.find('input, textarea, select').removeAttr('disabled');
// Disable all fields in other tabs
$target.parent()
.children(':not([data-idx=' + targetIndex + '])')
.find('input, textarea, select')
.attr('disabled', 'disabled');
};
var optionSelected = function (e) {
var $option = $("option:selected", $(this)),
$select = $(this),
// do not use .attr() as it sometimes unexplicably fails
targetIdx = $option.get(0).getAttribute('data-idx') || $option.attr('value'),
$target;
e.preventDefault();
if ($option.hasClass('active')) {
return;
}
$target = $(this).parents('.tabbable').eq(0).find('> .tab-content > [data-idx=' + targetIdx + ']');
activate($option, $select);
activate($target, $target.parent());
enableFields($target, targetIdx);
};
var tabClicked = function (e) {
var $a = $('a', $(this));
var $content = $(this).parents('.tabbable').first()
.find('.tab-content').first();
var targetIdx = $(this).index();
// The `>` here is to prevent activating selectfieldsets inside a tabarray
var $target = $content.find('> [data-idx=' + targetIdx + ']');
e.preventDefault();
activate($(this), $(this).parent());
activate($target, $target.parent());
if ($(this).parent().hasClass('jsonform-alternative')) {
enableFields($target, targetIdx);
}
};
tabs.each(function () {
$(this).delegate('select.nav', 'change', optionSelected);
$(this).find('select.nav').each(function () {
$(this).val($(this).find('.active').attr('value'));
// do not use .attr() as it sometimes unexplicably fails
var targetIdx = $(this).find('option:selected').get(0).getAttribute('data-idx') ||
$(this).find('option:selected').attr('value');
var $target = $(this).parents('.tabbable').eq(0).find('> .tab-content > [data-idx=' + targetIdx + ']');
enableFields($target, targetIdx);
});
$(this).delegate('ul.nav li', 'click', tabClicked);
$(this).find('ul.nav li.active').click();
});
};
// Twitter bootstrap-friendly HTML boilerplate for standard inputs
jsonform.fieldTemplate = function(inner) {
return '<div ' +
'<% for(var key in elt.htmlMetaData) {%>' +
'<%= key %>="<%= elt.htmlMetaData[key] %>" ' +
'<% }%>' +
'class="form-group jsonform-error-<%= keydash %>' +
'<%= elt.htmlClass ? " " + elt.htmlClass : "" %>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " jsonform-required" : "") %>' +
'<%= (node.readOnly ? " jsonform-readonly" : "") %>' +
'<%= (node.disabled ? " jsonform-disabled" : "") %>' +
'">' +
'<% if (!elt.notitle) { %>' +
'<label for="<%= node.id %>"><%= node.title ? node.title : node.name %></label>' +
'<% } %>' +
'<div class="controls">' +
'<% if (node.prepend || node.append) { %>' +
'<div class="<% if (node.prepend) { %>input-group<% } %>' +
'<% if (node.append) { %> input-group<% } %>">' +
'<% if (node.prepend) { %>' +
'<span class="input-group-addon"><%= node.prepend %></span>' +
'<% } %>' +
'<% } %>' +
inner +
'<% if (node.append) { %>' +
'<span class="input-group-addon"><%= node.append %></span>' +
'<% } %>' +
'<% if (node.prepend || node.append) { %>' +
'</div>' +
'<% } %>' +
'<% if (node.description) { %>' +
'<span class="help-block"><%= node.description %></span>' +
'<% } %>' +
'<span class="help-block jsonform-errortext" style="display:none;"></span>' +
'</div></div>';
};
var fileDisplayTemplate = '<div class="_jsonform-preview">' +
'<% if (value.type=="image") { %>' +
'<img class="jsonform-preview" id="jsonformpreview-<%= id %>" src="<%= value.url %>" />' +
'<% } else { %>' +
'<a href="<%= value.url %>"><%= value.name %></a> (<%= Math.ceil(value.size/1024) %>kB)' +
'<% } %>' +
'</div>' +
'<a href="#" class="btn btn-default _jsonform-delete"><i class="glyphicon glyphicon-remove" title="Remove"></i></a> ';
var inputFieldTemplate = function (type) {
return {
'template': '<input type="' + type + '" ' +
'class=\'form-control<%= (fieldHtmlClass ? " " + fieldHtmlClass : "") %>\'' +
'name="<%= node.name %>" value="<%= escape(value) %>" id="<%= id %>"' +
' aria-label="<%= node.title ? escape(node.title) : node.name %>"' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.readOnly ? " readonly=\'readonly\'" : "") %>' +
'<%= (node.schemaElement && (node.schemaElement.step > 0 || node.schemaElement.step == "any") ? " step=\'" + node.schemaElement.step + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.minLength ? " minlength=\'" + node.schemaElement.minLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.maxLength ? " maxlength=\'" + node.schemaElement.maxLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " required=\'required\'" : "") %>' +
'<%= (node.schemaElement && (node.schemaElement.type === "number") && !isNaN(node.schemaElement.maximum) ? " max=" + \'"\' + node.schemaElement.maximum + \'"\' : "")%>' +
'<%= (node.schemaElement && (node.schemaElement.type === "number") && !isNaN(node.schemaElement.minimum) ? " min=" + \'"\' + node.schemaElement.minimum + \'"\' : "")%>' +
'<%= (node.placeholder? " placeholder=" + \'"\' + escape(node.placeholder) + \'"\' : "")%>' +
' />',
'fieldtemplate': true,
'inputfield': true
}
};
jsonform.elementTypes = {
'none': {
'template': ''
},
'root': {
'template': '<div><%= children %></div>'
},
'text': inputFieldTemplate('text'),
'password': inputFieldTemplate('password'),
'date': inputFieldTemplate('date'),
'datetime': inputFieldTemplate('datetime'),
'datetime-local': inputFieldTemplate('datetime-local'),
'email': inputFieldTemplate('email'),
'month': inputFieldTemplate('month'),
'number': inputFieldTemplate('number'),
'search': inputFieldTemplate('search'),
'tel': inputFieldTemplate('tel'),
'time': inputFieldTemplate('time'),
'url': inputFieldTemplate('url'),
'week': inputFieldTemplate('week'),
'range': {
'template': '<div class="range"><input type="range" ' +
'<%= (fieldHtmlClass ? "class=\'" + fieldHtmlClass + "\' " : "") %>' +
'name="<%= node.name %>" value="<%= escape(value) %>" id="<%= id %>"' +
' aria-label="<%= node.title ? escape(node.title) : node.name %>"' +
'<%= (node.disabled? " disabled" : "")%>' +
' min=<%= range.min %>' +
' max=<%= range.max %>' +
' step=<%= range.step %>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
' /><% if (range.indicator) { %><span class="range-value" rel="<%= id %>"><%= escape(value) %></span><% } %></div>',
'fieldtemplate': true,
'inputfield': true,
'onInput': function(evt, elt) {
const valueIndicator = document.querySelector('span.range-value[rel="' + elt.id + '"]');
if (valueIndicator) {
valueIndicator.innerText = evt.target.value;
}
},
'onBeforeRender': function (data, node) {
data.range = {
min: 1,
max: 100,
step: 1,
indicator: false
};
if (!node || !node.schemaElement) return;
if (node.formElement && node.formElement.step) {
data.range.step = node.formElement.step;
}
if (node.formElement && node.formElement.indicator) {
data.range.indicator = node.formElement.indicator;
}
if (typeof node.schemaElement.minimum !== 'undefined') {
if (node.schemaElement.exclusiveMinimum) {
data.range.min = node.schemaElement.minimum + data.range.step;
}
else {
data.range.min = node.schemaElement.minimum;
}
}
if (typeof node.schemaElement.maximum !== 'undefined') {
if (node.schemaElement.exclusiveMaximum) {
data.range.max = node.schemaElement.maximum - data.range.step;
}
else {
data.range.max = node.schemaElement.maximum;
}
}
}
},
'color':{
'template':'<input type="text" ' +
'<%= (fieldHtmlClass ? "class=\'" + fieldHtmlClass + "\' " : "") %>' +
'name="<%= node.name %>" value="<%= escape(value) %>" id="<%= id %>"' +
' aria-label="<%= node.title ? escape(node.title) : node.name %>"' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
' />',
'fieldtemplate': true,
'inputfield': true,
'onInsert': function(evt, node) {
$(node.el).find('#' + escapeSelector(node.id)).spectrum({
preferredFormat: "hex",
showInput: true
});
}
},
'textarea':{
'template':'<textarea id="<%= id %>" name="<%= node.name %>" ' +
'<%= (fieldHtmlClass ? "class=\'" + fieldHtmlClass + "\' " : "") %>' +
'style="height:<%= elt.height || "150px" %>;width:<%= elt.width || "100%" %>;"' +
' aria-label="<%= node.title ? escape(node.title) : node.name %>"' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.readOnly ? " readonly=\'readonly\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.minLength ? " minlength=\'" + node.schemaElement.minLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.maxLength ? " maxlength=\'" + node.schemaElement.maxLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'<%= (node.placeholder? " placeholder=" + \'"\' + escape(node.placeholder) + \'"\' : "")%>' +
'><%= value %></textarea>',
'fieldtemplate': true,
'inputfield': true
},
'wysihtml5':{
'template':'<textarea id="<%= id %>" name="<%= node.name %>" style="height:<%= elt.height || "300px" %>;width:<%= elt.width || "100%" %>;"' +
' aria-label="<%= node.title ? escape(node.title) : node.name %>"' +
'<%= (fieldHtmlClass ? "class=\'" + fieldHtmlClass + "\' " : "") %>' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.readOnly ? " readonly=\'readonly\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.minLength ? " minlength=\'" + node.schemaElement.minLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.maxLength ? " maxlength=\'" + node.schemaElement.maxLength + "\'" : "") %>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'<%= (node.placeholder? " placeholder=" + \'"\' + escape(node.placeholder) + \'"\' : "")%>' +
'><%= value %></textarea>',
'fieldtemplate': true,
'inputfield': true,
'onInsert': function (evt, node) {
var setup = function () {
//protect from double init
if ($(node.el).data("wysihtml5")) return;
$(node.el).data("wysihtml5_loaded",true);
$(node.el).find('#' + escapeSelector(node.id)).wysihtml5({
"html": true,
"link": true,
"font-styles":true,
"image": false,
"events": {
"load": function () {
// In chrome, if an element is required and hidden, it leads to
// the error 'An invalid form control with name='' is not focusable'
// See http://stackoverflow.com/questions/7168645/invalid-form-control-only-in-google-chrome
$(this.textareaElement).removeAttr('required');
}
}
});
};
// Is there a setup hook?
if (window.jsonform_wysihtml5_setup) {
window.jsonform_wysihtml5_setup(setup);
return;
}
// Wait until wysihtml5 is loaded
var itv = window.setInterval(function() {
if (window.wysihtml5) {
window.clearInterval(itv);
setup();
}
},1000);
}
},
'ace':{
'template':'<div id="<%= id %>" style="position:relative;height:<%= elt.height || "300px" %>;"><div id="<%= id %>__ace" style="width:<%= elt.width || "100%" %>;height:<%= elt.height || "300px" %>;"></div><input type="hidden" name="<%= node.name %>" id="<%= id %>__hidden" value="<%= escape(value) %>"/></div>',
'fieldtemplate': true,
'inputfield': true,
'onInsert': function (evt, node) {
var setup = function () {
var formElement = node.formElement || {};
var ace = window.ace;
var editor = ace.edit($(node.el).find('#' + escapeSelector(node.id) + '__ace').get(0));
var idSelector = '#' + escapeSelector(node.id) + '__hidden';
// Force editor to use "\n" for new lines, not to bump into ACE "\r" conversion issue
// (ACE is ok with "\r" on pasting but fails to return "\r" when value is extracted)
editor.getSession().setNewLineMode('unix');
editor.renderer.setShowPrintMargin(false);
editor.setTheme("ace/theme/"+(formElement.aceTheme||"twilight"));
if (formElement.aceMode) {
editor.getSession().setMode("ace/mode/"+formElement.aceMode);
}
editor.getSession().setTabSize(2);
// Set the contents of the initial manifest file
editor.getSession().setValue(node.value||"");
//TODO: this is clearly sub-optimal
// 'Lazily' bind to the onchange 'ace' event to give
// priority to user edits
var lazyChanged = _.debounce(function () {
$(node.el).find(idSelector).val(editor.getSession().getValue());
$(node.el).find(idSelector).change();
}, 600);
editor.getSession().on('change', lazyChanged);
editor.on('blur', function() {
$(node.el).find(idSelector).change();
$(node.el).find(idSelector).trigger("blur");
});
editor.on('focus', function() {
$(node.el).find(idSelector).trigger("focus");
});
};
// Is there a setup hook?
if (window.jsonform_ace_setup) {
window.jsonform_ace_setup(setup);
return;
}
// Wait until ACE is loaded
var itv = window.setInterval(function() {
if (window.ace) {
window.clearInterval(itv);
setup();
}
},1000);
}
},
'checkbox':{
'template': '<div class="checkbox"><label><input type="checkbox" id="<%= id %>" ' +
'<%= (fieldHtmlClass ? " class=\'" + fieldHtmlClass + "\'": "") %>' +
'name="<%= node.name %>" value="1" <% if (value) {%>checked<% } %>' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required && (node.schemaElement.type !== "boolean") ? " required=\'required\'" : "") %>' +
' /><%= node.inlinetitle || "" %>' +
'</label></div>',
'fieldtemplate': true,
'inputfield': true,
'getElement': function (el) {
return $(el).parent().get(0);
}
},
'file':{
'template':'<input class="input-file" id="<%= id %>" name="<%= node.name %>" type="file" ' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'<%= (node.formElement && node.formElement.accept ? (" accept=\'" + node.formElement.accept + "\'") : "") %>' +
'/>',
'fieldtemplate': true,
'inputfield': true
},
'file-hosted-public':{
'template':'<span><% if (value && (value.type||value.url)) { %>'+fileDisplayTemplate+'<% } %><input class="input-file" id="_transloadit_<%= id %>" type="file" name="<%= transloaditname %>" /><input data-transloadit-name="_transloadit_<%= transloaditname %>" type="hidden" id="<%= id %>" name="<%= node.name %>" value=\'<%= escape(JSON.stringify(node.value)) %>\' /></span>',
'fieldtemplate': true,
'inputfield': true,
'getElement': function (el) {
return $(el).parent().get(0);
},
'onBeforeRender': function (data, node) {
if (!node.ownerTree._transloadit_generic_public_index) {
node.ownerTree._transloadit_generic_public_index=1;
} else {
node.ownerTree._transloadit_generic_public_index++;
}
data.transloaditname = "_transloadit_jsonform_genericupload_public_"+node.ownerTree._transloadit_generic_public_index;
if (!node.ownerTree._transloadit_generic_elts) node.ownerTree._transloadit_generic_elts = {};
node.ownerTree._transloadit_generic_elts[data.transloaditname] = node;
},
'onChange': function(evt,elt) {
// The "transloadit" function should be called only once to enable
// the service when the form is submitted. Has it already been done?
if (elt.ownerTree._transloadit_bound) {
return false;
}
elt.ownerTree._transloadit_bound = true;
// Call the "transloadit" function on the form element
var formElt = $(elt.ownerTree.domRoot);
formElt.transloadit({
autoSubmit: false,
wait: true,
onSuccess: function (assembly) {
// Image has been uploaded. Check the "results" property that
// contains the list of files that Transloadit produced. There
// should be one image per file input in the form at most.
var results = _.values(assembly.results);
results = _.flatten(results);
_.each(results, function (result) {
// Save the assembly result in the right hidden input field
var id = elt.ownerTree._transloadit_generic_elts[result.field].id;
var input = formElt.find('#' + escapeSelector(id));
var nonEmptyKeys = _.filter(_.keys(result.meta), function (key) {
return !!isSet(result.meta[key]);
});
result.meta = _.pick(result.meta, nonEmptyKeys);
input.val(JSON.stringify(result));
});
// Unbind transloadit from the form
elt.ownerTree._transloadit_bound = false;
formElt.unbind('submit.transloadit');
// Submit the form on next tick
_.delay(function () {
elt.ownerTree.submit();
}, 10);
},
onError: function (assembly) {
// TODO: report the error to the user
console.log('assembly error', assembly);
}
});
},
'onInsert': function (evt, node) {
$(node.el).find('a._jsonform-delete').on('click', function (evt) {
$(node.el).find('._jsonform-preview').remove();
$(node.el).find('a._jsonform-delete').remove();
$(node.el).find('#' + escapeSelector(node.id)).val('');
evt.preventDefault();
return false;
});
},
'onSubmit':function(evt, elt) {
if (elt.ownerTree._transloadit_bound) {
return false;
}
return true;
}
},
'file-transloadit': {
'template': '<span><% if (value && (value.type||value.url)) { %>'+fileDisplayTemplate+'<% } %><input class="input-file" id="_transloadit_<%= id %>" type="file" name="_transloadit_<%= node.name %>" /><input type="hidden" id="<%= id %>" name="<%= node.name %>" value=\'<%= escape(JSON.stringify(node.value)) %>\' /></span>',
'fieldtemplate': true,
'inputfield': true,
'getElement': function (el) {
return $(el).parent().get(0);
},
'onChange': function (evt, elt) {
// The "transloadit" function should be called only once to enable
// the service when the form is submitted. Has it already been done?
if (elt.ownerTree._transloadit_bound) {
return false;
}
elt.ownerTree._transloadit_bound = true;
// Call the "transloadit" function on the form element
var formElt = $(elt.ownerTree.domRoot);
formElt.transloadit({
autoSubmit: false,
wait: true,
onSuccess: function (assembly) {
// Image has been uploaded. Check the "results" property that
// contains the list of files that Transloadit produced. Note
// JSONForm only supports 1-to-1 associations, meaning it
// expects the "results" property to contain only one image
// per file input in the form.
var results = _.values(assembly.results);
results = _.flatten(results);
_.each(results, function (result) {
// Save the assembly result in the right hidden input field
var input = formElt.find('input[name="' +
result.field.replace(/^_transloadit_/, '') +
'"]');
var nonEmptyKeys = _.filter(_.keys(result.meta), function (key) {
return !!isSet(result.meta[key]);
});
result.meta = _.pick(result.meta, nonEmptyKeys);
input.val(JSON.stringify(result));
});
// Unbind transloadit from the form
elt.ownerTree._transloadit_bound = false;
formElt.unbind('submit.transloadit');
// Submit the form on next tick
_.delay(function () {
elt.ownerTree.submit();
}, 10);
},
onError: function (assembly) {
// TODO: report the error to the user
console.log('assembly error', assembly);
}
});
},
'onInsert': function (evt, node) {
$(node.el).find('a._jsonform-delete').on('click', function (evt) {
$(node.el).find('._jsonform-preview').remove();
$(node.el).find('a._jsonform-delete').remove();
$(node.el).find('#' + escapeSelector(node.id)).val('');
evt.preventDefault();
return false;
});
},
'onSubmit': function (evt, elt) {
if (elt.ownerTree._transloadit_bound) {
return false;
}
return true;
}
},
'select':{
'template':'<select name="<%= node.name %>" id="<%= id %>"' +
'class=\'form-control<%= (fieldHtmlClass ? " " + fieldHtmlClass : "") %>\'' +
'<%= (node.schemaElement && node.schemaElement.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'> ' +
'<% _.each(node.options, function(key, val) { if(key instanceof Object) { if (value === key.value) { %> <option selected value="<%= key.value %>"><%= key.title %></option> <% } else { %> <option value="<%= key.value %>"><%= key.title %></option> <% }} else { if (value === key) { %> <option selected value="<%= key %>"><%= key %></option> <% } else { %><option value="<%= key %>"><%= key %></option> <% }}}); %> ' +
'</select>',
'fieldtemplate': true,
'inputfield': true
},
'imageselect': {
'template': '<div>' +
'<input type="hidden" name="<%= node.name %>" id="<%= node.id %>" value="<%= value %>" />' +
'<div class="dropdown">' +
'<a class="btn<% if (buttonClass && node.value) { %> <%= buttonClass %><% } else { %> btn-default<% } %>" data-toggle="dropdown" href="#"<% if (node.value) { %> style="max-width:<%= width %>px;max-height:<%= height %>px"<% } %>>' +
'<% if (node.value) { %><img src="<% if (!node.value.match(/^https?:/)) { %><%= prefix %><% } %><%= node.value %><%= suffix %>" alt="" /><% } else { %><%= buttonTitle %><% } %>' +
'</a>' +
'<div class="dropdown-menu navbar" id="<%= node.id %>_dropdown">' +
'<div>' +
'<% _.each(node.options, function(key, idx) { if ((idx > 0) && ((idx % columns) === 0)) { %></div><div><% } %><a class="btn<% if (buttonClass) { %> <%= buttonClass %><% } else { %> btn-default<% } %>" style="max-width:<%= width %>px;max-height:<%= height %>px"><% if (key instanceof Object) { %><img src="<% if (!key.value.match(/^https?:/)) { %><%= prefix %><% } %><%= key.value %><%= suffix %>" alt="<%= key.title %>" /></a><% } else { %><img src="<% if (!key.match(/^https?:/)) { %><%= prefix %><% } %><%= key %><%= suffix %>" alt="" /><% } %></a> <% }); %>' +
'</div>' +
'<div class="pagination-right"><a class="btn btn-default">Reset</a></div>' +
'</div>' +
'</div>' +
'</div>',
'fieldtemplate': true,
'inputfield': true,
'onBeforeRender': function (data, node) {
var elt = node.formElement || {};
var nbRows = null;
var maxColumns = elt.imageSelectorColumns || 5;
data.buttonTitle = elt.imageSelectorTitle || 'Select...';
data.prefix = elt.imagePrefix || '';
data.suffix = elt.imageSuffix || '';
data.width = elt.imageWidth || 32;
data.height = elt.imageHeight || 32;
data.buttonClass = elt.imageButtonClass || false;
if (node.options.length > maxColumns) {
nbRows = Math.ceil(node.options.length / maxColumns);
data.columns = Math.ceil(node.options.length / nbRows);
}
else {
data.columns = maxColumns;
}
},
'getElement': function (el) {
return $(el).parent().get(0);
},
'onInsert': function (evt, node) {
$(node.el).on('click', '.dropdown-menu a', function (evt) {
evt.preventDefault();
evt.stopPropagation();
var img = (evt.target.nodeName.toLowerCase() === 'img') ?
$(evt.target) :
$(evt.target).find('img');
var value = img.attr('src');
var elt = node.formElement || {};
var prefix = elt.imagePrefix || '';
var suffix = elt.imageSuffix || '';
var width = elt.imageWidth || 32;
var height = elt.imageHeight || 32;
if (value) {
if (value.indexOf(prefix) === 0) {
value = value.substring(prefix.length);
}
value = value.substring(0, value.length - suffix.length);
$(node.el).find('input').attr('value', value);
$(node.el).find('a[data-toggle="dropdown"]')
.addClass(elt.imageButtonClass)
.attr('style', 'max-width:' + width + 'px;max-height:' + height + 'px')
.html('<img src="' + (!value.match(/^https?:/) ? prefix : '') + value + suffix + '" alt="" />');
}
else {
$(node.el).find('input').attr('value', '');
$(node.el).find('a[data-toggle="dropdown"]')
.removeClass(elt.imageButtonClass)
.removeAttr('style')
.html(elt.imageSelectorTitle || 'Select...');
}
});
}
},
'iconselect': {
'template': '<div>' +
'<input type="hidden" name="<%= node.name %>" id="<%= node.id %>" value="<%= value %>" />' +
'<div class="dropdown">' +
'<a class="btn<% if (buttonClass && node.value) { %> <%= buttonClass %><% } %>" data-toggle="dropdown" href="#"<% if (node.value) { %> style="max-width:<%= width %>px;max-height:<%= height %>px"<% } %>>' +
'<% if (node.value) { %><i class="icon-<%= node.value %>" /><% } else { %><%= buttonTitle %><% } %>' +
'</a>' +
'<div class="dropdown-menu navbar" id="<%= node.id %>_dropdown">' +
'<div>' +
'<% _.each(node.options, function(key, idx) { if ((idx > 0) && ((idx % columns) === 0)) { %></div><div><% } %><a class="btn<% if (buttonClass) { %> <%= buttonClass %><% } %>" ><% if (key instanceof Object) { %><i class="icon-<%= key.value %>" alt="<%= key.title %>" /></a><% } else { %><i class="icon-<%= key %>" alt="" /><% } %></a> <% }); %>' +
'</div>' +
'<div class="pagination-right"><a class="btn">Reset</a></div>' +
'</div>' +
'</div>' +
'</div>',
'fieldtemplate': true,
'inputfield': true,
'onBeforeRender': function (data, node) {
var elt = node.formElement || {};
var nbRows = null;
var maxColumns = elt.imageSelectorColumns || 5;
data.buttonTitle = elt.imageSelectorTitle || 'Select...';
data.buttonClass = elt.imageButtonClass || false;
if (node.options.length > maxColumns) {
nbRows = Math.ceil(node.options.length / maxColumns);
data.columns = Math.ceil(node.options.length / nbRows);
}
else {
data.columns = maxColumns;
}
},
'getElement': function (el) {
return $(el).parent().get(0);
},
'onInsert': function (evt, node) {
$(node.el).on('click', '.dropdown-menu a', function (evt) {
evt.preventDefault();
evt.stopPropagation();
var i = (evt.target.nodeName.toLowerCase() === 'i') ?
$(evt.target) :
$(evt.target).find('i');
var value = i.attr('class');
var elt = node.formElement || {};
if (value) {
value = value;
$(node.el).find('input').attr('value', value);
$(node.el).find('a[data-toggle="dropdown"]')
.addClass(elt.imageButtonClass)
.html('<i class="'+ value +'" alt="" />');
}
else {
$(node.el).find('input').attr('value', '');
$(node.el).find('a[data-toggle="dropdown"]')
.removeClass(elt.imageButtonClass)
.html(elt.imageSelectorTitle || 'Select...');
}
});
}
},
'radios':{
'template': '<div id="<%= node.id %>"><% _.each(node.options, function(key, val) { %><div class="radio"><label><input<%= (fieldHtmlClass ? " class=\'" + fieldHtmlClass + "\'": "") %> type="radio" <% if (((key instanceof Object) && (value === key.value)) || (value === key)) { %> checked="checked" <% } %> name="<%= node.name %>" value="<%= (key instanceof Object ? key.value : key) %>"' +
'<%= (node.disabled? " disabled" : "")%>' +
'<%= (node.schemaElement && node.schemaElement.required ? " required=\'required\'" : "") %>' +
'/><%= (key instanceof Object ? key.title : key) %></label></div> <% }); %></div>',
'fieldtemplate': true,
'inputfield': true
},
'radiobuttons': {
'template': '<div id="<%= node.id %>">' +
'<% _.each(node.options, function(key, val) { %>' +
'<label class="btn btn-default">' +
'<input<%= (fieldHtmlClass ? " class=\'" + fieldHtmlClass + "\'": "") %> type="radio" style="position:absolute;left:-9999px;" ' +
'<% if (((key instanceof Object) && (value === key.value)) || (value === key)) { %> checked="checked" <% } %> name="<%= node.name %>" value="<%= (key instanceof Object ? key.value : key) %>" />' +
'<span><%= (key instanceof Object ? key.title : key) %></span></label> ' +
'<% }); %>' +
'</div>',
'fieldtemplate': true,
'inputfield': true,
'onInsert': function (evt, node) {
var activeClass = 'active';
var elt = node.formElement || {};
if (elt.activeClass) {
activeClass += ' ' + elt.activeClass;
}
$(node.el).find('label').on('click', function () {
$(this).parent().find('label').removeClass(activeClass);
$(this).addClass(activeClass);
});
// Set active on insert
$(node.el).find('input:checked').parent().addClass(activeClass)
}
},
'checkboxes':{
'template': '<div><%= choiceshtml %></div>',
'fieldtemplate': true,
'inputfield': true,
'onBeforeRender': function (data, node) {
// Build up choices from the enumeration list
var choices = null;
var choiceshtml = null;
var template = '<div class="checkbox"><label>' +
'<input type="checkbox" <% if (value) { %> checked="checked" <% } %> name="<%= name %>" value="1"' +
'<%= (node.disabled? " disabled" : "")%>' +
'/><%= title %></label></div>';
if (!node || !node.schemaElement) return;
if (node.schemaElement.items) {
choices =
node.schemaElement.items["enum"] ||
node.schemaElement.items[0]["enum"];
} else {
choices = node.schemaElement["enum"];
}
if (!choices) return;
choiceshtml = '';
_.each(choices, function (choice, idx) {
choiceshtml += _.template(template, fieldTemplateSettings)({
name: node.key + '[' + idx + ']',
value: _.include(node.value, choice),
title: hasOwnProperty(node.formElement.titleMap, choice) ? node.formElement.titleMap[choice] : choice,
node: node
});
});
data.choiceshtml = choiceshtml;
}
},
'array': {
'template': '<div id="<%= id %>"><ul class="_jsonform-array-ul" style="list-style-type:none;"><%= children %></ul>' +
'<span class="_jsonform-array-buttons">' +
'<a href="#" class="btn btn-default _jsonform-array-addmore"><i class="glyphicon glyphicon-plus-sign" title="Add new"></i></a> ' +
'<a href="#" class="btn btn-default _jsonform-array-deletelast"><i class="glyphicon glyphicon-minus-sign" title="Delete last"></i></a>' +
'</span>' +
'</div>',
'fieldtemplate': true,
'array': true,
'childTemplate': function (inner, enableDrag) {
if ($('').sortable) {
// Insert a "draggable" icon
// floating to the left of the main element
return '<li data-idx="<%= node.childPos %>">' +
// only allow drag of children if enabled
(enableDrag ? '<span class="draggable line"><i class="glyphicon glyphicon-list" title="Move item"></i></span>' : '') +
inner +
'<span class="_jsonform-array-buttons">' +
'<a href="#" class="btn btn-default _jsonform-array-deletecurrent"><i class="glyphicon glyphicon-minus-sign" title="Delete current"></i></a>' +
'</span>' +
'</li>';
}
else {
return '<li data-idx="<%= node.childPos %>">' +
inner +
'<span class="_jsonform-array-buttons">' +
'<a href="#" class="btn btn-default _jsonform-array-deletecurrent"><i class="glyphicon glyphicon-minus-sign" title="Delete current"></i></a>' +
'</span>' +
'</li>';
}
},
'onInsert': function (evt, node) {
var $nodeid = $(node.el).find('#' + escapeSelector(node.id));
var boundaries = node.getArrayBoundaries();
// Switch two nodes in an array
var moveNodeTo = function (fromIdx, toIdx) {
// Note "switchValuesWith" extracts values from the DOM since field
// values are not synchronized with the tree data structure, so calls
// to render are needed at each step to force values down to the DOM
// before next move.
// TODO: synchronize field values and data structure completely and
// call render only once to improve efficiency.
if (fromIdx === toIdx) return;
var incr = (fromIdx < toIdx) ? 1: -1;
var i = 0;
var parentEl = $('> ul', $nodeid);
for (i = fromIdx; i !== toIdx; i += incr) {
node.children[i].switchValuesWith(node.children[i + incr]);
node.children[i].render(parentEl.get(0));
node.children[i + incr].render(parentEl.get(0));
}
// No simple way to prevent DOM reordering with jQuery UI Sortable,
// so we're going to need to move sorted DOM elements back to their
// origin position in the DOM ourselves (we switched values but not
// DOM elements)
var fromEl = $(node.children[fromIdx].el);
var toEl = $(node.children[toIdx].el);
fromEl.detach();
toEl.detach();
if (fromIdx < toIdx) {
if (fromIdx === 0) parentEl.prepend(fromEl);
else $(node.children[fromIdx-1].el).after(fromEl);
$(node.children[toIdx-1].el).after(toEl);
}
else {
if (toIdx === 0) parentEl.prepend(toEl);
else $(node.children[toIdx-1].el).after(toEl);
$(node.children[fromIdx-1].el).after(fromEl);
}
};
$('> span > a._jsonform-array-addmore', $nodeid).click(function (evt) {
evt.preventDefault();
evt.stopPropagation();
var idx = node.children.length;
if (boundaries.maxItems >= 0) {
if (node.children.length > boundaries.maxItems - 2) {
$nodeid.find('> span > a._jsonform-array-addmore')
.addClass('disabled');
}
if (node.children.length > boundaries.maxItems - 1) {
return false;
}
}
node.insertArrayItem(idx, $('> ul', $nodeid).get(0));
if ((boundaries.minItems <= 0) ||
((boundaries.minItems > 0) &&
(node.children.length > boundaries.minItems - 1))) {
$nodeid.find('a._jsonform-array-deletecurrent')
.removeClass('disabled');
$nodeid.find('> span > a._jsonform-array-deletelast')
.removeClass('disabled');
}
});
//Simulate Users click to setup the form with its minItems
var curItems = $('> ul > li', $nodeid).length;
if ((boundaries.minItems > 0) &&
(curItems < boundaries.minItems)) {
for (var i = 0; i < (boundaries.minItems - 1) && ($nodeid.find('> ul > li').length < boundaries.minItems); i++) {
node.insertArrayItem(curItems, $nodeid.find('> ul').get(0));
}
}
if ((boundaries.minItems > 0) &&
(node.children.length <= boundaries.minItems)) {
$nodeid.find('a._jsonform-array-deletecurrent')
.addClass('disabled');
$nodeid.find('> span > a._jsonform-array-deletelast')
.addClass('disabled');
}
function deleteArrayItem (idx) {
if (boundaries.minItems > 0) {
if (node.children.length < boundaries.minItems + 2) {
$nodeid.find('> span > a._jsonform-array-deletelast')
.addClass('disabled');
}
if (node.children.length <= boundaries.minItems) {
return false;
}
}
else if (node.children.length === 1) {
$nodeid.find('a._jsonform-array-deletecurrent')
.addClass('disabled');
$nodeid.find('> span > a._jsonform-array-deletelast')
.addClass('disabled');