-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathjquery.jsForm-1.5.2.js
3996 lines (3517 loc) · 105 KB
/
jquery.jsForm-1.5.2.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
/**
* jquery.jsForm
* -------------
* JsForm control for handling html UI with json objects
* @version 1.0
* @class
* @author Niko Berger
* @license MIT License GPL
*/
;(function( $, window, undefined ){
"use strict";
var JSFORM_INIT_FUNCTIONS = {}, // remember initialization functions
JSFORM_MAP = {}; // remember all forms
/**
* @param element {Node} the container node that should be converted to a jsForm
* @param options {object} the configuraton object
* @constructor
*/
function JsForm (element, options) {
var $this = $(element);
// create the options
this.options = $.extend({}, {
/**
* enable form control rendering (if jsForm.controls is available) and validation
*/
controls: true,
/**
* the object used to fill/collect data
*/
data: null,
/**
* the prefix used to annotate the input fields
*/
prefix: "data",
/**
* set to null to discourage the tracking of "changed" fields.
* Disabling this will increase performance, but disabled the "changed" functionality.
* This will add the given css class to changed fields.
*/
trackChanges: "changed",
/**
* set to false to only validate visible fields.
* This is discouraged especially when you have tabs or similar elements in your form.
*/
validateHidden: true,
/**
* skip empty values when getting an object
*/
skipEmpty: false,
/**
* an object with callback functions that act as renderer for data fields (class=object).
* ie. { infoRender: function(data){return data.id + ": " + data.name} }
*/
renderer: null,
/**
* an object with callback functions that act as pre-processors for data fields (class=object).
* ie. { idFilter: function(data){return data.id} }
*/
processors: null,
/**
* dataHandler will be called for each field filled.
*/
dataHandler: null, /*{
serialize: function(val, field, obj) {
if(field.hasClass("reverse"))
return val.reverse();
},
deserialize: function(val, field, obj) {
if(field.hasClass("reverse"))
return val.reverse();
}
}*/
/**
* optional array of elements that should be connected with the form. This
* allows the splitting of the form into different parts of the dom.
*/
connect: null,
/**
* The class used when calling preventEditing. This will replace all
* inputs with a span with the given field
*/
viewClass: "jsfValue"
}, options);
// read prefix from dom
if($this.attr("data-prefix") && (this.options.prefix === "data" || this.options.prefix === "")) {
if($this.attr("data-prefix") !== "") {
this.options.prefix = $this.attr("data-prefix");
}
}
this.element = element;
this._init();
}
/**
* init the portlet - load the config
* @private
*/
JsForm.prototype._init = function() {
// init the basic dom functionality
this._domInit();
// enable form controls
if(this.options.controls) {
if($.jsFormControls) {
// handle multiple form parts
$.each(this._getForm(), function(){
$(this).jsFormControls();
});
} else {
try {
if(typeof console !== "undefined") {
this._debug("jquery.JsForm.controls not available!");
}
} catch(ex) {
// ignore
}
}
}
// fill/init with the first data
this._fill();
};
/**
* Connect a dom element with an already existing form.
* @param ele the new part of the form
*/
JsForm.prototype.connect = function(ele) {
// collection lists with buttons
this._initCollection(ele, this.options.prefix);
// init conditionals
this._initConditional(ele, this.options.prefix, this.options);
// enable form controls
if(this.options.controls) {
if($.jsFormControls) {
// handle multiple form parts
$(ele).jsFormControls();
}
}
this._fillDom(ele);
if(!this.options.connect)
this.options.connect = [];
this.options.connect.push(ele);
};
/**
* @return all nodes for this jsform (main + connected)
*/
JsForm.prototype.getNodes = function() {
return this._getForm();
};
/**
* init the dom. This can be called multiple times.
* this will also enable "add", "insert" and "delete" for collections
* @private
*/
JsForm.prototype._domInit = function() {
var that = this;
// handle multiple form parts
$.each(this._getForm(), function(){
// collection lists with buttons
that._initCollection(this, that.options.prefix);
// init conditionals
that._initConditional(this, that.options.prefix, that.options);
});
};
/**
* simple debug helper
* @param msg the message to print
* @private
*/
JsForm.prototype._debug = function(msg, param) {
try {
var cons = console || (window?window.console:null);
if (!cons || !cons.log)
return;
var p = null;
if($.isPlainObject(param)) {
p = JSON.stringify(param, null, " ");
} else {
p = param;
}
if(!p) {
p = "";
}
cons.log(msg + p);
} catch(ex) {
// ignore
}
};
/**
* initialize conditionals.
* basic rule is:
* any dom element that has a conditional and either
* a data-show or data-hide attribute or a data-eval attribute
*
* @param form the base dom element
* @param prefix the prefix to check for
* @private
*/
JsForm.prototype._initConditional = function(form, prefix, options) {
var that = this;
var showEvaluator = function(ele, data, fields) {
// if any field has a value -> show
var show = false;
$.each(fields, function(){
var value = that._getValueWithArrays(data, this);
if($(that).data().condition && value !== $(that).data().condition)
return;
else if(!value || value === "" || value === 0 || value === -1)
return;
show = true;
// skip processing
return false;
});
if(show)
ele.show();
else
ele.hide();
}, hideEvaluator = function(ele, data, fields) {
// if any field has a value -> hide
var show = false;
$.each(fields, function(){
var value = that._getValueWithArrays(data, this);
if($(that).data().condition && value !== $(that).data().condition)
return;
else if(!value || value === "" || value === 0 || value === -1)
return;
show = true;
// skip processing
return false;
});
if(show)
ele.hide();
else
ele.show();
};
// remember the conditionals for faster dom access
this.conditionals = $(form).find(".conditional");
this.conditionals.each(function(){
$(this).data().conditionalEval = [];
var fields = $(this).attr("data-show");
if(fields && fields.length > 0) {
$(this).data().conditionalEval.push({
func: showEvaluator,
field: fields.split(" ")
});
}
fields = $(this).attr("data-hide");
if(fields && fields.length > 0) {
$(this).data().conditionalEval.push({
func: hideEvaluator,
field: fields.split(" ")
});
}
fields = $(this).attr("data-eval");
if(fields && fields.length > 0) {
// custom evaluator
if(options.conditionals[fields])
$(this).data().conditionalEval.push({
func: options.conditionals[fields]
});
}
});
};
/**
* evaluate conditionals on the form
* @param form the form to search for conditionals
* @param data the data
*/
JsForm.prototype._evaluateConditionals = function(form, data) {
this.conditionals.each(function(){
var ele = $(this);
// go throguh all evaluation functions
$.each(ele.data().conditionalEval, function() {
this.func(ele, data, this.field);
});
});
};
/**
* initialize collections
* @param form the base dom element
* @param prefix the prefix to check for
* @private
*/
JsForm.prototype._initCollection = function(form, prefix) {
// all collections
var collectionMap = {},
that = this;
$(form).data().collections = collectionMap;
$(".collection", form).each(function() {
var colName = $(this).attr("data-field");
// skip collections without a data-field mapping
if (!colName || colName.indexOf(prefix + ".") !== 0) {
return;
}
var container = $(this);
// remember the collection
var cols = collectionMap[colName];
if(cols) {
cols.push(container);
} else {
collectionMap[colName] = [container];
}
//init the collection
that._initList(container);
// after adding: check if we want reorder control
if(!container.hasClass("ui-sortable") && container.hasClass("sortable") && container.sortable) {
// get the config object
var config = container.attr("data-sortable");
if(!config) {
config = {};
} else {
config = JSON.parse(config);
}
container.sortable(config);
container.on("sortstop", function( event, ui ) {
that._reorder(container);
});
}
$(this).on("add", function(ev, pojo, fn){
var fieldName = $(this).attr("data-field");
// skip if fieldName doest match
if(fn && fieldName != fn)
return;
var tmpl = $(this).data("template");
if(!pojo) {
pojo = {};
}
// and has a template
if(tmpl) {
var line = tmpl.clone(true);
$(line).addClass("POJO");
$(line).data().pojo = pojo;
var prefill = $(this).data("prefill");
if(!prefill)
prefill = $(this).val("data-prefill");
// allow prefill
if(prefill){
if($.isFunction(prefill))
prefill($(line).data().pojo, $(line));
else if(prefill.substring)
$(line).data().pojo = JSON.parse(prefill);
else if($.isPlainObject(prefill))
$(line).data().pojo = prefill;
}
// init controls
that._enableTracking($("input,textarea,select", line));
// new line always has changes
if(that.options.trackChanges)
$("input,textarea,select", line).addClass(that.options.trackChanges);
that._addCollectionControls(line);
// its possible to have "sub" collections
that._initCollection(line, fieldName.substring(fieldName.indexOf('.')+1));
// trigger a callback
$(this).trigger("addCollection", [line, $(line).data().pojo]);
// the new entry has as index the count of all "lines"
var idx = $(this).children(".POJO").length;
$(line).on("refresh", function(){
// fill read only fields
that._fillFieldData($(this), $(this).data().pojo, fieldName.substring(fieldName.indexOf('.')+1), idx);
// "fill data"
that._fillData($(this), $(this).data().pojo, fieldName.substring(fieldName.indexOf('.')+1), idx);
}).trigger("refresh");
$(this).append(line);
// trigger a callback after the data has been rendered)
$(this).trigger("postAddCollection", [line, $(line).data().pojo]);
}
});
});
$(".add", form).each(function(){
var fieldName = $(this).attr("data-field");
if (!fieldName || fieldName.indexOf(prefix + ".") !== 0) {
return;
}
// add the collection
$(this).data().collections = collectionMap[fieldName];
// only init once
if($(this).data().hasJsForm) {
return;
}
$(this).data().hasJsForm = true;
$(this).click(function(ev){
ev.preventDefault();
// search for a collection with that name
$.each($(this).data("collections"), function() {
$(this).trigger("add", [null, fieldName]);
});
});
});
// insert: similar to add - but works with events
$(".insert", form).each(function(){
var fieldName = $(this).attr("data-field");
if (!fieldName || fieldName.indexOf(prefix + ".") !== 0) {
return;
}
// only init once
if($(this).data().isCollection) {
return;
}
$(this).data().isCollection = true;
// remember the collections
$(this).data("collections", collectionMap[$(this).attr("data-field")]);
$(this).on("insert", function(ev, pojo){
if(!pojo)
pojo = $(this).data().pojo;
// insert only works if there is a pojo
if(!pojo) {
return;
}
var beforeInsertCallback = $(this).data("beforeInsert");
if(beforeInsertCallback && $.isFunction(beforeInsertCallback)) {
pojo = beforeInsertCallback(pojo);
// insert only works if there is a pojo
if(!pojo) {
return;
}
}
// search for a collection with that name
$.each($(this).data("collections"), function() {
var tmpl = $(this).data("template");
// and has a template
if(tmpl) {
var line = tmpl.clone(true);
// mark that this is a pojo
line.addClass("POJO");
// add the pojo
line.data().pojo = pojo;
that._addCollectionControls(line);
// its possible to have "sub" collections
that._initCollection(line);
// trigger a callback
$(this).trigger("addCollection", [line, pojo]);
// the new entry has as index the count of all "lines"
var idx = $(this).children(".POJO").length;
// fill the "information"
$(line).on("refresh", function(){
// fill read only fields
that._fillFieldData($(this), $(this).data().pojo, fieldName.substring(fieldName.indexOf('.')+1), idx);
// "fill data"
that._fillData($(this), $(this).data().pojo, fieldName.substring(fieldName.indexOf('.')+1), idx);
}).trigger("refresh");
$(this).append(line);
// trigger a callback after the data has been rendered)
$(this).trigger("postAddCollection", [line, pojo]);
}
});
// empty field
$(this).val("");
$(this).data().pojo = null;
$(this).focus();
});
});
// insert: helper button (triggers insert)
$(".insertAction", form).each(function(){
var fieldName = $(this).attr("data-field");
if(!fieldName) {
return;
}
// only init once
if($(this).data("inserter")) {
return;
}
// find the insert element for this data-field
var inserter = $(this).parent().find(".insert");
if(!inserter) {
return;
}
// remember the inserter
$(this).data("inserter", inserter);
$(this).click(function(ev){
ev.preventDefault();
$(this).data("inserter").trigger("insert");
return false;
});
});
$("input.object", form).each(function(){
$(this).on("update", function(evt){
var pojo = $(this).data().pojo;
if($(this).attr("data-display") || $(this).attr("data-render")) {
$(this).val(that._renderObject(pojo, $(this).attr("data-display"), $(this).attr("data-render")));
}
});
});
// fileupload
$("input.blob", form).each(function(){
// only available on input type file
if($(this).attr("type") !== "file") {
return;
}
var blobInput = $(this);
// bind on change
$(this).on("change", function(evt){
//get file name
var fileName = $(this).val().split(/\\/).pop();
blobInput.data("name", fileName);
var files = evt.target.files; // FileList object
// Loop through the FileList (and render image files as thumbnails.(skip for ie < 9)
if(files && files.length) {
$.each(files, function() {
var reader = new FileReader();
// closure to capture the file information
reader.onload = function(e) {
// get the result
blobInput.data("blob", e.target.result);
};
// Read in the image file as a data URL.
reader.readAsDataURL(this);
$(this).trigger("fileChange");
});
}
});
});
};
/**
* init a container that has a tempalate child (first child).
* @param container the contianer element
* @private
*/
JsForm.prototype._initList = function(container) {
// avoid double initialisation
if(container.data("template")) {
return;
}
// get all children
var tmpl = container.children().detach();
// remove an id if there is one
tmpl.removeAttr("id");
container.data("template", tmpl);
};
/**
* generate the array with all DOM elements that are connected with
* the form.
* @private
*/
JsForm.prototype._getForm = function() {
var form = [$(this.element)];
if(this.options.connect)
$.each(this.options.connect, function(){
form.push($(this));
});
return form;
};
/**
* clear/reset a form. The prefix is normally predefined by init
* @param form the form
* @param prefix the optional prefix used to identify fields for this form
*/
JsForm.prototype._clear = function(form, prefix) {
// get the prefix from the form if not given
if(!prefix) {
prefix = this.options.prefix;
}
$(form).removeData("pojo");
$("input,select,textarea", form).each(function(){
var name = $(this).attr("name");
// empty name - ignore
if (!name || name.indexOf(prefix + ".") !== 0) {
return;
}
// cut away the prefix
name = name.substring((prefix+".").length);
// skip empty
if(name.length < 1) {
return;
}
// remove all pojos
$(this).removeData("pojo");
if($(this).attr("type") === "checkbox") {
$(this).prop("checked", false);
} else if($(this).attr("type") === "radio") {
$(this).prop("checked", false);
} else if($(this).data().valclass && $(this)[$(this).data().valclass].val){
$(this)[$(this).data().valclass](val, "");
} else {
$(this).val("");
}
if($(this).hasClass("blob")) {
$(this).removeData("blob");
}
// special type select box: select the FIRST child
if($(this).is("select")) {
$('option[selected="selected"]', this).prop('selected', false);
$('option:first', this).prop('selected', true);
$(this).val($("option:first", this).val());
$(this).change();
}
// trigger change
$(this).change();
});
$(".collection", form).each(function() {
var fieldname = $(this).attr("data-field");
// only collections with the correct prefix
if(!fieldname || fieldname.indexOf(prefix+".") !== 0) {
return;
}
// get rid of all
$(this).empty();
});
};
/**
* Handle arrays when creating pojos
* @param ele the element
* @param pojo the base object
* @param name the name of the field
* @param val the value to check agains
* @private
*/
JsForm.prototype._handleArrayInPojo = function(ele, pojo, name, val) {
// create an array out of this
if(!pojo[name]) {
pojo[name] = [];
}
if(ele.attr("type") === "checkbox" || ele.attr("type") === "CHECKBOX") {
// do we want the value of not
var use = ele.is(":checked");
var pushVal = true;
$.each(pojo[name], function(data, index){
if(this == val) {
// dont need to push
pushVal = false;
// we dont use it - remove it
if(!use)
pojo[name].splice(index, 1);
return false;
}
});
if(pushVal && use)
pojo[name].push(val);
} else {
var num = ele.attr("data-array");
if(!num || isNaN(num)) {
num = null;
} else
num = Number(num);
// no num -> add the array
if(num === null)
pojo[name].push(val);
else
pojo[name][num] = val;
}
};
/**
* set a value in a pojo
* @param pojo the data pojo
* @param name the name of the field to set (allows . syntax)
* @param val the value to set
* @param $this the object the val comes from for array check
*/
JsForm.prototype._setPojoVal = function(pojo, name, val, $this) {
var that = this;
// check if we have a . - if so split
if (name.indexOf(".") === -1)
{
// handle arrays
if($this && $this.hasClass("array")) {
that._handleArrayInPojo($this, pojo, name, val);
}
else
pojo[name] = val;
}
else
{
var parts = name.split(".");
var prev;
var current = pojo[parts[0]];
if (!current || !$.isPlainObject(current)) {
pojo[parts[0]] = {};
current = pojo[parts[0]];
}
for(var i = 1; i < parts.length - 1; i++) {
prev = current;
current = prev[parts[i]];
if(current === undefined || current === null) {
current = {};
prev[parts[i]] = current;
}
}
// set prev as the name
prev = parts[parts.length - 1];
// handle arrays
if($this && $this.hasClass("array")) {
that._handleArrayInPojo($(this), current, prev, val);
} else {
current[prev] = val;
}
}
};
/**
* ceate a pojo from a form. Takes special data definition classes into account:
* <ul>
* <li>number|currency: the content will be transformed into a number (default string</li>
* <li>transient: will be ignored</li>
* <li>prefix.fieldname.value: will create the whole object subtree</li>
* </ul>
* @param start the element to start from (ie. the form or tr)
* @param pojo the pojo to write everything to
* @param prefix a prefix: only fields with the given prefix will be included in the pojo
* @private
*/
JsForm.prototype._createPojoFromInput = function (start, prefix, pojo) {
// check if we have an "original" pojo
var startObj = null;
var that = this;
// normally we edit the pojo on ourselves - so result is null
var result = null;
// get it from the starting dom element
if($(start).data("pojo")) {
startObj = $(start).data("pojo");
}
// if we have an object, use this as base and fill the pojo
if(startObj) {
$.extend(true, pojo, startObj);
}
$(start).find("input,select,textarea,button,.jsobject").each(function(){
var name = $(this).attr("data-name");
if(!name) {
name = $(this).attr("name");
}
// empty name - ignore
if (!name) {
return;
}
// skip grayed (=calculated) or transient fields
if($(this).hasClass("transient") || $(this).hasClass("grayed")) {
return;
}
// must start with prefix
if(name.indexOf(prefix + ".") !== 0) {
return;
}
$(this).trigger("validate", true);
// cut away the prefix
name = name.substring((prefix+".").length);
var val = $(this).val();
if($(this).data().valclass && $(this)[$(this).data().valclass]){
val = $(this)[$(this).data().valclass]("val");
}
// jsobject use the pojo data directly - ignore the rest
if($(this).hasClass("jsobject")) {
val = $(this).data().pojo;
}
else {
// ignore empty values when skipEmpty is set
if(that.options.skipEmpty && (!val || val === "" || val.trim() === "")) {
return;
}
if($(this).hasClass("emptynull") && (!val || val === "" || val === "null" || val.trim() === "")) { // nullable fields do not send empty string
val = null;
} else if($(this).hasClass("object") || $(this).hasClass("POJO")) {
if($("option:selected", this).data() && $("option:selected", this).data().pojo) {
if($("option:selected", this).data().pojo)
val = $("option:selected", this).data().pojo;
else if($("option:selected", this).attr("data-obj"))
val = JSON.parse($("option:selected", this).attr("data-obj"));
} else {
val = $(this).data().pojo;
}
// object can also have a processor
if($.isFunction($(this).data().processor)) {
val = $(this).data().processor(val);
} else {
var processor = $(this).attr("data-processor");
if(processor && that.options.processors[processor]) {
val = that.options.processors[processor](val);
}
}
} else if($(this).hasClass("blob")) { // file upload blob
val = $(this).data("blob");
} else
// set empty numbers or dates to null
if(val === "" && ($(this).hasClass("number") || $(this).hasClass("percent") || $(this).hasClass("integer") || $(this).hasClass("dateFilter")|| $(this).hasClass("dateTimeFilter"))) {
val = null;
}
// we might have a value processor on this: this is added by the jsForm.controls
if($(this).data().processor) {
val = $(this).data().processor(val);
}
else if ($(this).hasClass("number") || $(this).hasClass("integer")) {
if($(this).hasClass("date") && isNaN(val)) {
if($.format) {
var d = $.format.date(val);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
val = d.getTime();
} else
val = new Date(val).getTime();
} else
val = that._getNumber(val);
if(isNaN(val)) {
val = 0;
}
}
else if($(this).attr("type") === "checkbox" || $(this).attr("type") === "CHECKBOX") {
// a checkbox as an array
if($(this).hasClass("array")) {
// the special case: array+checkbox is handled on the actual setting
val = $(this).val();
if($(this).attr("data-obj")) {
val = JSON.parse($(this).attr("data-obj"));
}
} else {
val = $(this).is(':checked');
}
}
else if($(this).attr("type") === "radio" || $(this).attr("type") === "RADIO") {
if(!$(this).is(':checked')) {
return;
}
if($(this).hasClass("bool") || $(this).hasClass("boolean")) {
val = $(this).val() === "true";
}
}
else if($(this).hasClass("bool")) {
val = ($(this).val() === "true");
}
else if($(this).hasClass("boolean")) {
switch($(this).val()) {
case "true": val = true; break;
case "false": val = false; break;
default:
val = null;
}
}
}
// we got the value - send it to the processor
if(that.options.dataHandler) {
val = that.options.dataHandler.serialize(val, $(this), pojo);
}
// handle simple collection
if(name.length < 1) {
// handle simple collection: we want the val as result
result = val;
return false;
}
that._setPojoVal(pojo, name, val, $(this));
});
// for "selection" collection
$(start).find(".selectcollection").each(function(){
var name = $(this).attr("data-field");
// empty name - ignore
if (!name) {
return;
}
// skip grayed (=calculated) or transient fields
if($(this).hasClass("transient")) {
return;
}
// must start with prefix
if(name.indexOf(prefix + ".") !== 0) {
return;
}
$(this).trigger("validate", true);
// cut away the prefix
name = name.substring((prefix+".").length);
// always an array (reset current data)
var arrVal = [];
// see if we go by checkbox or by css class (if both -> class wins)
var selectedClass = $(this).attr("data-selected");
var id = $(this).attr("data-id");
$(this).children().each(function(){
// check selection
if(selectedClass) {
if(!$(this).hasClass(selectedClass))
return;
} else {
if(!$("input[name='"+name+"']", this).prop('checked'))
return;
}
// get the "id"/object
var cobj = null;
// no id given - check the value of the checkbox