@@ -17,18 +17,7 @@ class DisplayJS {
1717 for ( let i = 0 ; i < elements . length ; i ++ ) {
1818 const attr = elements [ i ] . getAttribute ( "var" ) ;
1919
20- if ( ! attr . includes ( "." ) ) {
21- elements [ i ] . innerHTML = this . obj [ attr ] ;
22- } else {
23- const parts = attr . split ( "." ) ;
24- let val = this . obj [ parts [ 0 ] ] ;
25-
26- for ( let p = 1 ; p < parts . length ; p ++ ) {
27- val = val [ parts [ p ] ] ;
28- }
29-
30- elements [ i ] . innerHTML = val ;
31- }
20+ elements [ i ] . innerHTML = this . getAtPath ( this . obj , attr ) ;
3221 }
3322 } ;
3423 // push var cheking
@@ -80,51 +69,32 @@ class DisplayJS {
8069 } ;
8170 } ) ( ) ) ;
8271 const obj = this . obj ;
72+ const setValue = ( x , element ) => {
73+ const attr = x . getAttribute ( "target" ) ;
74+
75+ if ( element . type == "checkbox" ) {
76+ this . setAtPath ( obj , attr , element . checked ) ;
77+ } else if ( element . type == "select" ) {
78+ this . setAtPath ( obj , attr , element . options [ element . selectedIndex ] . value ) ;
79+ } else {
80+ this . setAtPath ( obj , attr , element . value ) ;
81+ }
82+
83+ callback ( x ) ;
84+ } ;
8385 [ ] . forEach . call ( document . querySelectorAll ( "[target]" ) , ( x ) => {
84- addEventListener ( x , "change" , function ( ) {
85- const attr1 = x . getAttribute ( "target" ) ;
86- if ( this . type == "checkbox" ) {
87- obj [ attr1 ] = this . checked ;
88- } else if ( this . type == "select" ) {
89- obj [ attr1 ] = this . options [ this . selectedIndex ] . value ;
90- } else {
91- obj [ attr1 ] = this . value ;
92- }
93- callback ( x ) ;
94- } ) ;
95- addEventListener ( x , "keydown" , function ( ) {
96- const attr2 = x . getAttribute ( "target" ) ;
97- if ( this . type == "checkbox" ) {
98- obj [ attr2 ] = this . checked ;
99- } else if ( this . type == "select" ) {
100- obj [ attr2 ] = this . options [ this . selectedIndex ] . value ;
101- } else {
102- obj [ attr2 ] = this . value ;
103- }
104- callback ( x ) ;
105- } ) ;
106- addEventListener ( x , "input" , function ( ) {
107- const attr3 = x . getAttribute ( "target" ) ;
108- if ( this . type == "checkbox" ) {
109- obj [ attr3 ] = this . checked ;
110- } else if ( this . type == "select" ) {
111- obj [ attr3 ] = this . options [ this . selectedIndex ] . value ;
112- } else {
113- obj [ attr3 ] = this . value ;
114- }
115- callback ( x ) ;
116- } ) ;
117- addEventListener ( x , "paste" , function ( ) {
118- const attr4 = x . getAttribute ( "target" ) ;
119- if ( this . type == "checkbox" ) {
120- obj [ attr4 ] = this . checked ;
121- } else if ( this . type == "select" ) {
122- obj [ attr4 ] = this . options [ this . selectedIndex ] . value ;
123- } else {
124- obj [ attr4 ] = this . value ;
125- }
126- callback ( x ) ;
127- } ) ;
86+ addEventListener ( x , "change" , function ( ) {
87+ return setValue ( x , this ) ;
88+ } ) ;
89+ addEventListener ( x , "keydown" , function ( ) {
90+ return setValue ( x , this ) ;
91+ } ) ;
92+ addEventListener ( x , "input" , function ( ) {
93+ return setValue ( x , this ) ;
94+ } ) ;
95+ addEventListener ( x , "paste" , function ( ) {
96+ return setValue ( x , this ) ;
97+ } ) ;
12898 } ) ;
12999 }
130100 // if...else function
@@ -218,6 +188,28 @@ class DisplayJS {
218188 } ) ;
219189 }
220190 }
191+ // Set value at path of object
192+ setAtPath ( obj , path , value ) {
193+ // Taken from https://stackoverflow.com/a/6842900
194+ let i ;
195+ path = path . split ( "." ) ;
196+ for ( i = 0 ; i < path . length - 1 ; i ++ ) {
197+ obj = obj [ path [ i ] ] ;
198+ }
199+
200+ obj [ path [ i ] ] = value ;
201+ }
202+ // Get value at path of object
203+ getAtPath ( obj , path ) {
204+ const parts = path . split ( "." ) ;
205+ let val = obj [ parts [ 0 ] ] ;
206+
207+ for ( let p = 1 ; p < parts . length ; p ++ ) {
208+ val = val [ parts [ p ] ] ;
209+ }
210+
211+ return val ;
212+ }
221213 // apply function to each elements selected
222214 all ( el , callback ) {
223215 el = this . s ( el ) ;
0 commit comments