@@ -7,24 +7,9 @@ let preview = {
7
7
// update the preview
8
8
update : function ( ) {
9
9
// get parameter values from all .param elements
10
- const params = Array . from ( document . querySelectorAll ( ".param" ) ) . reduce (
11
- ( acc , next ) => {
12
- let obj = { ...acc } ;
13
- let value = next . value ;
14
10
15
- if ( value . indexOf ( '#' ) >= 0 ) {
16
- // if the value is colour, remove the hash sign
17
- value = value . replace ( / # / g, "" ) ;
18
- if ( value . length > 6 ) {
19
- // if the value is in hexa and opacity is 1, remove FF
20
- value = value . replace ( / ( F | f ) { 2 } $ / , "" ) ;
21
- }
22
- }
23
- obj [ next . id ] = value ;
24
- return obj ;
25
- } ,
26
- { }
27
- ) ;
11
+ const params = objectFromElements ( document . querySelectorAll ( ".param" ) )
12
+
28
13
// convert parameters to query string
29
14
const encode = encodeURIComponent ;
30
15
const query = Object . keys ( params )
@@ -72,9 +57,9 @@ let preview = {
72
57
label . setAttribute ( "data-property" , property ) ;
73
58
// color picker
74
59
const jscolorConfig = {
75
- format : ' hexa' ,
76
- onChange : 'pickerChange(this, "' + property + '")' ,
77
- onInput : 'pickerChange(this, "' + property + '")'
60
+ format : " hexa" ,
61
+ onChange : 'pickerChange(this, "' + property + '")' ,
62
+ onInput : 'pickerChange(this, "' + property + '")' ,
78
63
} ;
79
64
const input = document . createElement ( "input" ) ;
80
65
input . className = "param jscolor" ;
@@ -179,8 +164,39 @@ window.addEventListener(
179
164
} ,
180
165
false
181
166
) ;
167
+ function objectFromElements ( elements )
168
+ {
169
+ // create a key value mapping of parameter values from all elements in a Node list
170
+ return Array . from ( elements ) . reduce ( ( acc , next ) => {
171
+ let obj = { ...acc } ;
172
+ let value = next . value ;
173
+ if ( value . indexOf ( "#" ) >= 0 ) {
174
+ // if the value is colour, remove the hash sign
175
+ value = value . replace ( / # / g, "" ) ;
176
+ if ( value . length > 6 ) {
177
+ // if the value is in hexa and opacity is 1, remove FF
178
+ value = value . replace ( / ( F | f ) { 2 } $ / , "" ) ;
179
+ }
180
+ }
181
+ obj [ next . id ] = value ;
182
+ return obj ;
183
+ } , { } ) ;
184
+ }
185
+ function exportPhp ( ) {
186
+ let params = objectFromElements ( document . querySelectorAll ( ".advanced .param.jscolor" ) )
187
+ const output =
188
+ "[\n" +
189
+ Object . keys ( params )
190
+ . map ( ( key ) => ` "${ key } " => "#${ params [ key ] } ",\n` )
191
+ . join ( "" ) +
192
+ "]" ;
193
+
194
+ let textarea = document . getElementById ( 'exportedPhp' ) ;
195
+ textarea . value = output ;
196
+ textarea . hidden = false ;
197
+ }
182
198
183
- function checkColor ( color , input ) {
199
+ function checkColor ( color , input ) {
184
200
if ( color . length == 9 && color . slice ( - 2 ) == "FF" ) {
185
201
// if color has hex alpha value -> remove it
186
202
document . getElementById ( input ) . value = color . slice ( 0 , - 2 ) ;
0 commit comments