1
- let preview = {
2
- // default values
1
+ /*global jscolor*/
2
+ /*eslint no-undef: "error"*/
3
+
4
+ const preview = {
5
+ /**
6
+ * Default values - if set to these values, the params do not need to appear in the query string
7
+ */
3
8
defaults : {
4
9
theme : "default" ,
5
10
hide_border : "false" ,
6
11
date_format : "" ,
7
12
locale : "en" ,
8
13
} ,
9
- // update the preview
10
- update : function ( ) {
11
- // get parameter values from all .param elements
12
-
13
- const params = objectFromElements ( document . querySelectorAll ( ".param" ) )
14
14
15
+ /**
16
+ * Update the preview with the current parameters
17
+ */
18
+ update ( ) {
19
+ // get parameter values from all .param elements
20
+ const params = this . objectFromElements ( document . querySelectorAll ( ".param" ) ) ;
15
21
// convert parameters to query string
16
22
const encode = encodeURIComponent ;
17
23
const query = Object . keys ( params )
@@ -29,56 +35,52 @@ let preview = {
29
35
document . querySelector ( ".md code" ) . innerText = md ;
30
36
// disable copy button if username is invalid
31
37
const copyButton = document . querySelector ( ".copy-button" ) ;
32
- copyButton . disabled = ! ! document . querySelectorAll ( "#user:invalid" ) . length ;
38
+ copyButton . disabled = Boolean ( document . querySelectorAll ( "#user:invalid" ) . length ) ;
33
39
} ,
34
- addProperty : function ( property , value = "#DD2727FF" ) {
40
+
41
+ /**
42
+ * Add a property in the advanced section
43
+ * @param {string } property - the name of the property, selected element is used if not provided
44
+ * @param {string } value - the value to set the property to
45
+ * @returns {false } false to prevent the default action
46
+ */
47
+ addProperty ( property , value = "#DD2727FF" ) {
35
48
const selectElement = document . querySelector ( "#properties" ) ;
36
49
// if no property passed, get the currently selected property
37
- if ( ! property ) {
38
- property = selectElement . value ;
39
- }
50
+ const propertyName = property || selectElement . value ;
40
51
if ( ! selectElement . disabled ) {
41
52
// disable option in menu
42
- Array . prototype . find . call (
43
- selectElement . options ,
44
- ( o ) => o . value == property
45
- ) . disabled = true ;
53
+ Array . prototype . find . call ( selectElement . options , ( o ) => o . value === propertyName ) . disabled = true ;
46
54
// select first unselected option
47
- const firstAvailable = Array . prototype . find . call (
48
- selectElement . options ,
49
- ( o ) => ! o . disabled
50
- ) ;
55
+ const firstAvailable = Array . prototype . find . call ( selectElement . options , ( o ) => ! o . disabled ) ;
51
56
if ( firstAvailable ) {
52
57
firstAvailable . selected = true ;
53
58
} else {
54
59
selectElement . disabled = true ;
55
60
}
56
61
// label
57
62
const label = document . createElement ( "label" ) ;
58
- label . innerText = property ;
59
- label . setAttribute ( "data-property" , property ) ;
63
+ label . innerText = propertyName ;
64
+ label . setAttribute ( "data-property" , propertyName ) ;
60
65
// color picker
61
66
const jscolorConfig = {
62
67
format : "hexa" ,
63
- onChange : ' pickerChange(this, "' + property + '")' ,
64
- onInput : ' pickerChange(this, "' + property + '")' ,
68
+ onChange : `preview. pickerChange(this, ' ${ propertyName } ')` ,
69
+ onInput : `preview. pickerChange(this, ' ${ propertyName } ')` ,
65
70
} ;
66
71
const input = document . createElement ( "input" ) ;
67
72
input . className = "param jscolor" ;
68
- input . id = property ;
69
- input . name = property ;
70
- input . setAttribute ( "data-property" , property ) ;
73
+ input . id = propertyName ;
74
+ input . name = propertyName ;
75
+ input . setAttribute ( "data-property" , propertyName ) ;
71
76
input . setAttribute ( "data-jscolor" , JSON . stringify ( jscolorConfig ) ) ;
72
77
input . value = value ;
73
78
// removal button
74
79
const minus = document . createElement ( "button" ) ;
75
80
minus . className = "minus btn" ;
76
- minus . setAttribute (
77
- "onclick" ,
78
- "return preview.removeProperty(this.getAttribute('data-property'));"
79
- ) ;
81
+ minus . setAttribute ( "onclick" , "return preview.removeProperty(this.getAttribute('data-property'));" ) ;
80
82
minus . innerText = "−" ;
81
- minus . setAttribute ( "data-property" , property ) ;
83
+ minus . setAttribute ( "data-property" , propertyName ) ;
82
84
// add elements
83
85
const parent = document . querySelector ( ".advanced .parameters" ) ;
84
86
parent . appendChild ( label ) ;
@@ -89,35 +91,99 @@ let preview = {
89
91
jscolor . install ( parent ) ;
90
92
91
93
// check initial color value
92
- checkColor ( value , property ) ;
94
+ this . checkColor ( value , propertyName ) ;
93
95
94
96
// update and exit
95
97
this . update ( ) ;
96
98
}
97
99
return false ;
98
100
} ,
99
- removeProperty : function ( property ) {
101
+
102
+ /**
103
+ * Remove a property from the advanced section
104
+ * @param {string } property - the name of the property to remove
105
+ * @returns {false } false to prevent the default action
106
+ */
107
+ removeProperty ( property ) {
100
108
const parent = document . querySelector ( ".advanced .parameters" ) ;
101
109
const selectElement = document . querySelector ( "#properties" ) ;
102
110
// remove all elements for given property
103
- parent
104
- . querySelectorAll ( `[data-property="${ property } "]` )
105
- . forEach ( ( x ) => parent . removeChild ( x ) ) ;
111
+ parent . querySelectorAll ( `[data-property="${ property } "]` ) . forEach ( ( x ) => parent . removeChild ( x ) ) ;
106
112
// enable option in menu
107
- const option = Array . prototype . find . call (
108
- selectElement . options ,
109
- ( o ) => o . value == property
110
- ) ;
113
+ const option = Array . prototype . find . call ( selectElement . options , ( o ) => o . value === property ) ;
111
114
selectElement . disabled = false ;
112
115
option . disabled = false ;
113
116
// update and exit
114
117
this . update ( ) ;
115
118
return false ;
116
119
} ,
120
+
121
+ /**
122
+ * Create a key-value mapping of ids to values from all elements in a Node list
123
+ * @param {NodeList } elements - the elements to get the values from
124
+ * @returns {Object } the key-value mapping
125
+ */
126
+ objectFromElements ( elements ) {
127
+ return Array . from ( elements ) . reduce ( ( acc , next ) => {
128
+ const obj = { ...acc } ;
129
+ let value = next . value ;
130
+ if ( value . indexOf ( "#" ) >= 0 ) {
131
+ // if the value is colour, remove the hash sign
132
+ value = value . replace ( / # / g, "" ) ;
133
+ if ( value . length > 6 ) {
134
+ // if the value is in hexa and opacity is 1, remove FF
135
+ value = value . replace ( / [ F f ] { 2 } $ / , "" ) ;
136
+ }
137
+ }
138
+ obj [ next . id ] = value ;
139
+ return obj ;
140
+ } , { } ) ;
141
+ } ,
142
+
143
+ /**
144
+ * Export the advanced parameters to PHP code for creating a new theme
145
+ */
146
+ exportPhp ( ) {
147
+ const params = this . objectFromElements ( document . querySelectorAll ( ".advanced .param.jscolor" ) ) ;
148
+ const mappings = Object . keys ( params )
149
+ . map ( ( key ) => ` "${ key } " => "#${ params [ key ] } ",` )
150
+ . join ( "\n" ) ;
151
+ const output = `[\n${ mappings } \n]` ;
152
+
153
+ const textarea = document . getElementById ( "exportedPhp" ) ;
154
+ textarea . value = output ;
155
+ textarea . hidden = false ;
156
+ } ,
157
+
158
+ /**
159
+ * Remove "FF" from a hex color if opacity is 1
160
+ * @param {string } color - the hex color
161
+ * @param {string } input - the property name, or id of the element to update
162
+ */
163
+ checkColor ( color , input ) {
164
+ if ( color . length === 9 && color . slice ( - 2 ) === "FF" ) {
165
+ // if color has hex alpha value -> remove it
166
+ document . getElementById ( input ) . value = color . slice ( 0 , - 2 ) ;
167
+ }
168
+ } ,
169
+
170
+ /**
171
+ * Check a color when the picker changes
172
+ * @param {Object } picker - the JSColor picker object
173
+ * @param {string } input - the property name, or id of the element to update
174
+ */
175
+ pickerChange ( picker , input ) {
176
+ // color was changed by picker - check it
177
+ this . checkColor ( picker . toHEXAString ( ) , input ) ;
178
+ } ,
117
179
} ;
118
180
119
- let clipboard = {
120
- copy : function ( el ) {
181
+ const clipboard = {
182
+ /**
183
+ * Copy the content of an element to the clipboard
184
+ * @param {Element } el - the element to copy
185
+ */
186
+ copy ( el ) {
121
187
// create input box to copy from
122
188
const input = document . createElement ( "input" ) ;
123
189
input . value = document . querySelector ( ".md code" ) . innerText ;
@@ -134,8 +200,12 @@ let clipboard = {
134
200
} ,
135
201
} ;
136
202
137
- let tooltip = {
138
- reset : function ( el ) {
203
+ const tooltip = {
204
+ /**
205
+ * Reset the tooltip text
206
+ * @param {Element } el - the element to reset the tooltip for
207
+ */
208
+ reset ( el ) {
139
209
// remove tooltip text
140
210
el . removeAttribute ( "title" ) ;
141
211
} ,
@@ -151,7 +221,7 @@ window.addEventListener(
151
221
( ) => {
152
222
// set input boxes to match URL parameters
153
223
new URLSearchParams ( window . location . search ) . forEach ( ( val , key ) => {
154
- let paramInput = document . querySelector ( `#${ key } ` ) ;
224
+ const paramInput = document . querySelector ( `#${ key } ` ) ;
155
225
if ( paramInput ) {
156
226
// set parameter value
157
227
paramInput . value = val ;
@@ -166,46 +236,3 @@ window.addEventListener(
166
236
} ,
167
237
false
168
238
) ;
169
- function objectFromElements ( elements )
170
- {
171
- // create a key value mapping of parameter values from all elements in a Node list
172
- return Array . from ( elements ) . reduce ( ( acc , next ) => {
173
- let obj = { ...acc } ;
174
- let value = next . value ;
175
- if ( value . indexOf ( "#" ) >= 0 ) {
176
- // if the value is colour, remove the hash sign
177
- value = value . replace ( / # / g, "" ) ;
178
- if ( value . length > 6 ) {
179
- // if the value is in hexa and opacity is 1, remove FF
180
- value = value . replace ( / ( F | f ) { 2 } $ / , "" ) ;
181
- }
182
- }
183
- obj [ next . id ] = value ;
184
- return obj ;
185
- } , { } ) ;
186
- }
187
- function exportPhp ( ) {
188
- let params = objectFromElements ( document . querySelectorAll ( ".advanced .param.jscolor" ) )
189
- const output =
190
- "[\n" +
191
- Object . keys ( params )
192
- . map ( ( key ) => ` "${ key } " => "#${ params [ key ] } ",\n` )
193
- . join ( "" ) +
194
- "]" ;
195
-
196
- let textarea = document . getElementById ( 'exportedPhp' ) ;
197
- textarea . value = output ;
198
- textarea . hidden = false ;
199
- }
200
-
201
- function checkColor ( color , input ) {
202
- if ( color . length == 9 && color . slice ( - 2 ) == "FF" ) {
203
- // if color has hex alpha value -> remove it
204
- document . getElementById ( input ) . value = color . slice ( 0 , - 2 ) ;
205
- }
206
- }
207
-
208
- function pickerChange ( picker , input ) {
209
- // color was changed by picker - check it
210
- checkColor ( picker . toHEXAString ( ) , input ) ;
211
- }
0 commit comments