@@ -120,20 +120,44 @@ function newElement(elementType) {
120
120
return document . createElement ( elementType ) ;
121
121
}
122
122
123
- function addInputFieldAfter ( selector , type , name , value ) {
123
+ function createInput ( attributes ) {
124
124
let input = newElement ( 'input' ) ;
125
- input . type = type ;
126
- input . name = name ;
127
- input . value = value ;
128
- getElement ( selector ) . after ( input ) ;
129
- }
130
-
131
- function addInputFieldBefore ( selector , type , name , value ) {
132
- let input = newElement ( 'input' ) ;
133
- input . type = type ;
134
- input . name = name ;
135
- input . value = value ;
136
- getElement ( selector ) . before ( input ) ;
125
+ if ( attributes . name === undefined ) throw 'Name attribute is mandatory' ;
126
+ if ( attributes . type === undefined ) throw 'Type attribute is mandatory' ;
127
+ Object . keys ( attributes ) . forEach ( attribute => {
128
+ if ( attribute === 'required' ) {
129
+ input . required = true ;
130
+ } else if ( attribute === 'readonly' ) {
131
+ input . readOnly = true ;
132
+ } else if ( attribute === 'disabled' ) {
133
+ input . disabled = true ;
134
+ } else {
135
+ input [ attribute ] = attributes [ attribute ] ;
136
+ }
137
+ } ) ;
138
+ return input ;
139
+ }
140
+
141
+ function addInputFieldAfter ( selector , attributes ) {
142
+ let element = createInput ( attributes ) ;
143
+ getElement ( selector ) . after ( element ) ;
144
+ }
145
+
146
+ function addInputFieldBefore ( selector , attributes ) {
147
+ let element = createInput ( attributes ) ;
148
+ getElement ( selector ) . after ( element ) ;
149
+ }
150
+
151
+
152
+ function cloneInput ( selector ) {
153
+ let element = getElement ( selector ) ;
154
+ if ( element . nodeName !== 'INPUT' ) throw `Element is not an input: ${ selector } ` ;
155
+ let clone = element . cloneNode ( ) ;
156
+ if ( element . id !== undefined ) {
157
+ clone . id = element . id + Date . now ( ) ;
158
+ }
159
+ clone . value = '' ;
160
+ element . after ( clone ) ;
137
161
}
138
162
139
163
async function postRequest ( url , parameters = { } , loading = true ) {
0 commit comments