-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_collection_analyzer.js
62 lines (57 loc) · 1.67 KB
/
sf_collection_analyzer.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
/**
* SFCollectionAnalyzer v1.0.0
* JavaScript module to convert CollectionType of Symfony to HTML template
* @author Arnaout slimen <arnaout.slimen@sbc.tn>
* @link https://github.com/SlimenTN/SFCollectionAnalyzer
*/
var SFCollectionAnalyzer = (function () {
var inputs = ['input', 'select', 'checkbox', 'radio', 'textarea'];
/**
* Decompose collection dom to children
* @param dom
* @returns array of doms
*/
function decompose(dom) {
var data = [];
for(var i in inputs){
dom.find(inputs[i]).each(function(i) {
item = {
name: $(this).attr('data-name'),
dom: $(this)
};
data[$(this).attr('data-name')] = $(this);
});
}
return data;
}
/**
* Build template
* @param dom
* @returns string
*/
function build (dom, template) {
var data = decompose(dom);
for (var k in data){
if (data.hasOwnProperty(k)) {
html = domToHTML(data[k]);
template = template.replace('#'+k+'#', html);
}
}
return template;
}
/**
* Convert dom to html
* @param dom
* @returns html
*/
function domToHTML(dom) {
return dom.clone().wrap('<div/>').parent().html()
}
return {
buildTemplate: function (dom, template) {
const prototype = dom.attr('data-prototype');
const domPrototype = $('<div/>').html(prototype).contents();
return build(domPrototype, template);
}
}
})();