-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs2form.js
83 lines (82 loc) · 2.34 KB
/
js2form.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
/*
* requires:
* underscore.js
* jQuery.js
*/
var populateForm =
(function (){
function getFromPath(obj,travel){
//walks a path defined by an array of fields, returns the last field walked
if(_.isEmpty(travel)){
return obj;
}
if(!obj){
return null;
}
var prop = _.first(travel);
return getFromPath(obj[prop],_.rest(travel));
};
function assignFromPath(obj,travel,assignVal){
//walks a path defined by an array of fields, assigns a value to the last field walked
if(_.isEmpty(travel)){
obj = assignVal;
return obj;
}
if(!obj){
return null;
}
var prop = _.first(travel);
obj[prop] = assignFromPath(obj[prop],_.rest(travel),assignVal);
return obj;
};
function jsPather(pathStr){
//converts js obj notation into a path array
return pathStr
.replace(/\[/g,'.')
.replace(/\]/g,'')
.split(".");
};
function nodesProcessor(obj,$nodes,varAtt,callback,pather){
/*changes the shape of the obj to suit the form
*e.g. {i : ["1","2","3"]} -> {i:"123"}
*/
if(_.isUndefined(pather)){
var pathTranslator = jsPather;
}
else{
var pathTranslator = pather;
}
$($nodes).each(function(){
var varType = $(this).attr('var_type');
var nodeName = $(this).attr(varAtt);
var varPath = pathTranslator(nodeName);
var objPropToChange = getFromPath(obj,varPath);
if(nodeName || objPropToChange){
obj = assignFromPath(obj,
varPath,
callback(objPropToChange,$(this)));
}
});
return obj;
};
function formElementPopulator($node,value){
//sets the value of an element in a form. some elements require special treatment (checkboxs)
$node.val(value);
if($node.attr('type') == 'checkbox' && value){
$node.attr('checked',value);
}
};
return function populateForm($nodes,obj,varAtt,transformer,pather){
if(_.isUndefined(pather)){var pathTranslator = jsPather;}
else{var pathTranslator = pather;}
if(!_.isUndefined(transformer)){
obj = nodesProcessor(obj,$nodes,varAtt,transformer,pather);
}
$($nodes).each(function(){
var nameAtt = $(this).attr(varAtt);
var path = pathTranslator(nameAtt);
var valForForm = getFromPath(obj,path);
formElementPopulator($(this),valForForm);
});
};
})();