-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnylon-form.html
104 lines (73 loc) · 3.04 KB
/
nylon-form.html
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
<link rel="import" href="../polymer/polymer-element.html">
<script>
class NylonForm extends Polymer.Element {
static get is() {
return 'nylon-form';
}
static get properties(){
return {
section: String
};
}
static get template() {
return Polymer.html`
<slot></slot>
`
}
makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
ready(){
super.ready()
this.section = this.makeid()
let child = this.querySelectorAll("[form-path]")
for(let i=0;i<child.length;i++){
child[i].setAttribute("form-section",this.section)
}
}
submit(){
let data = {}
let child = this.querySelectorAll("[form-path]")
for(let i=0;i<child.length;i++){
let path = child[i].getAttribute('form-path')
if(path!=null){
if(child[i].getAttribute('form-section')==this.section){
let prop = "value"
if(child[i].getAttribute('form-value')!=null){
prop = child[i].getAttribute('form-value')
}
let stackPath = ""
if(path.split('.').length>1){
path.split('.').map((key,index)=>{
stackPath = stackPath+"."+key
eval('data'+stackPath+'='+ 'data'+stackPath+' || {}')
})
}else{
stackPath = path
}
let valData = ""
if(child[i].getAttribute('form-child')!=null){
if(child[i].localName=="nylon-form" || child[i].localName=="nylon-form-group"){
child[i].submit()
valData = child[i].data
}else{
child[i].shadowRoot.querySelector("nylon-form").submit()
valData = child[i].shadowRoot.querySelector("nylon-form").data
}
}else{
valData = child[i][prop]
}
eval('data.' + path + '='+JSON.stringify(valData))
}
}
}
this.data = data
return data
}
}
window.customElements.define(NylonForm.is, NylonForm);
</script>