|
1 | | -const TYPE_BOOLEAN = 'boolean'; |
2 | | -const TYPE_STRING = 'string'; |
3 | | -const TYPE_NUMBER = 'number'; |
4 | | -const TYPE_STRING_LIST = 'string[]'; |
5 | | -const TYPE_BOOLEAN_UPDATE = "boolean | 'update_only'"; |
6 | | -const TYPE_DURATION = "number | 'page' | 'session'"; |
| 1 | +import settingType, { |
| 2 | + BOOLEAN, |
| 3 | + MODE_NUMBER, |
| 4 | + NUMBER, |
| 5 | + STRING, |
| 6 | + WIDGET_LIST, |
| 7 | + WIDGET_MODE, |
| 8 | + WIDGET_NUMBER, |
| 9 | + WIDGET_SELECT, |
| 10 | + WIDGET_TEXT, |
| 11 | + WIDGET_TOGGLE, |
| 12 | +} from '../utils/settingType'; |
| 13 | + |
| 14 | +const ERROR_PROPERTY = 'property'; |
| 15 | +const ERROR_VALUE = 'value'; |
| 16 | +const ERROR_INVALID = 'invalid'; |
| 17 | +const ERROR_URL = 'url'; |
| 18 | + |
| 19 | +/** |
| 20 | + * The reference declares no format, so the `…Url` suffix is the only signal. |
| 21 | + * |
| 22 | + * @param {string} property Property name. |
| 23 | + * @return {boolean} True when the value has to be a URL. |
| 24 | + */ |
| 25 | +function expectsUrl( property ) { |
| 26 | + return property.toLowerCase().endsWith( 'url' ); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Only absolute http(s) URLs are accepted, mirroring Advanced_Settings::is_url(). |
| 31 | + * |
| 32 | + * @param {string} value Value to check. |
| 33 | + * @return {boolean} True when the value is an absolute http(s) URL. |
| 34 | + */ |
| 35 | +function isUrl( value ) { |
| 36 | + try { |
| 37 | + const { protocol, hostname } = new URL( value ); |
| 38 | + |
| 39 | + if ( protocol !== 'http:' && protocol !== 'https:' ) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + return hostname.includes( '.' ); |
| 44 | + } catch { |
| 45 | + return false; |
| 46 | + } |
| 47 | +} |
7 | 48 |
|
8 | 49 | const advancedSettings = function( config ) { |
9 | 50 | return { |
10 | 51 | reference: config.reference || [], |
11 | 52 | i18n: config.i18n || {}, |
| 53 | + tab: config.tab, |
12 | 54 | rows: [], |
13 | 55 | nextUid: 0, |
14 | 56 | openSelect: null, |
15 | 57 | focusedProperty: null, |
16 | 58 | keySearch: '', |
| 59 | + showErrors: false, |
17 | 60 |
|
18 | | - TYPE_BOOLEAN, |
19 | | - TYPE_STRING, |
20 | | - TYPE_NUMBER, |
21 | | - TYPE_STRING_LIST, |
22 | | - TYPE_BOOLEAN_UPDATE, |
23 | | - TYPE_DURATION, |
| 61 | + WIDGET_TOGGLE, |
| 62 | + WIDGET_SELECT, |
| 63 | + WIDGET_MODE, |
| 64 | + WIDGET_NUMBER, |
| 65 | + WIDGET_LIST, |
| 66 | + WIDGET_TEXT, |
| 67 | + MODE_NUMBER, |
24 | 68 |
|
25 | 69 | init() { |
26 | 70 | this.rows = ( config.pairs || [] ).map( ( pair ) => this.makeRow( pair ) ); |
| 71 | + |
| 72 | + this.$el |
| 73 | + .closest( 'form' ) |
| 74 | + ?.addEventListener( 'submit', ( event ) => this.onSubmit( event ) ); |
27 | 75 | }, |
28 | 76 |
|
29 | 77 | makeRow( pair = {} ) { |
@@ -39,7 +87,27 @@ const advancedSettings = function( config ) { |
39 | 87 | return `axeptio_settings[advanced_settings][${ index }][${ key }]`; |
40 | 88 | }, |
41 | 89 |
|
| 90 | + addRowHint() { |
| 91 | + if ( this.rows.some( ( row ) => ! row.property ) ) { |
| 92 | + return this.i18n.errors.pending_row; |
| 93 | + } |
| 94 | + |
| 95 | + if ( ! this.availableOptions().length ) { |
| 96 | + return this.i18n.errors.all_used; |
| 97 | + } |
| 98 | + |
| 99 | + return ''; |
| 100 | + }, |
| 101 | + |
| 102 | + canAddRow() { |
| 103 | + return ! this.addRowHint(); |
| 104 | + }, |
| 105 | + |
42 | 106 | addRow() { |
| 107 | + if ( ! this.canAddRow() ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
43 | 111 | this.rows.push( this.makeRow() ); |
44 | 112 | this.$nextTick( () => { |
45 | 113 | this.$refs.rows?.lastElementChild |
@@ -103,8 +171,12 @@ const advancedSettings = function( config ) { |
103 | 171 | return option ? option.description : ''; |
104 | 172 | }, |
105 | 173 |
|
| 174 | + widget( row ) { |
| 175 | + return settingType( row.type ).widget; |
| 176 | + }, |
| 177 | + |
106 | 178 | placeholderFor( row ) { |
107 | | - if ( row.type === TYPE_STRING_LIST ) { |
| 179 | + if ( this.widget( row ) === WIDGET_LIST ) { |
108 | 180 | return this.i18n.list_placeholder || ''; |
109 | 181 | } |
110 | 182 |
|
@@ -114,35 +186,52 @@ const advancedSettings = function( config ) { |
114 | 186 | return fallback === null || fallback === undefined ? '' : String( fallback ); |
115 | 187 | }, |
116 | 188 |
|
117 | | - onPropertyChange( row ) { |
118 | | - const option = this.findOption( row.property ); |
119 | | - row.type = option ? option.type : TYPE_STRING; |
120 | | - row.value = this.defaultValueForType( row.type ); |
121 | | - }, |
122 | | - |
123 | | - defaultValueForType( type ) { |
124 | | - if ( type === TYPE_BOOLEAN ) { |
125 | | - return '0'; |
| 189 | + /** |
| 190 | + * Build the entries of a value select from the resolved type. A literal |
| 191 | + * with no translation shows as-is, so one added by Axeptio stays usable |
| 192 | + * without a plugin release. |
| 193 | + * |
| 194 | + * @param {Object} row Row being edited. |
| 195 | + * @return {Array<{value: string, label: string}>} Select entries. |
| 196 | + */ |
| 197 | + optionsFor( row ) { |
| 198 | + const { primitive, literals } = settingType( row.type ); |
| 199 | + const options = []; |
| 200 | + |
| 201 | + if ( primitive === BOOLEAN ) { |
| 202 | + options.push( { value: '1', label: this.i18n.bool.on } ); |
| 203 | + options.push( { value: '0', label: this.i18n.bool.off } ); |
126 | 204 | } |
127 | | - if ( type === TYPE_BOOLEAN_UPDATE ) { |
128 | | - return 'false'; |
| 205 | + |
| 206 | + if ( primitive === NUMBER ) { |
| 207 | + options.push( { |
| 208 | + value: MODE_NUMBER, |
| 209 | + label: this.i18n.number_modes?.[ row.property ] || this.i18n.number_mode, |
| 210 | + } ); |
129 | 211 | } |
130 | | - return ''; |
| 212 | + |
| 213 | + literals.forEach( ( literal ) => { |
| 214 | + options.push( { |
| 215 | + value: literal, |
| 216 | + label: this.i18n.literals?.[ literal ] || literal, |
| 217 | + } ); |
| 218 | + } ); |
| 219 | + |
| 220 | + return options; |
131 | 221 | }, |
132 | 222 |
|
133 | | - toggleBoolean( row ) { |
134 | | - row.value = row.value === '1' ? '0' : '1'; |
| 223 | + onPropertyChange( row ) { |
| 224 | + const option = this.findOption( row.property ); |
| 225 | + row.type = option ? option.type : STRING; |
| 226 | + row.value = settingType( row.type ).defaultValue; |
135 | 227 | }, |
136 | 228 |
|
137 | | - durationMode( row ) { |
138 | | - if ( row.value === 'page' || row.value === 'session' ) { |
139 | | - return row.value; |
140 | | - } |
141 | | - return 'days'; |
| 229 | + toggleBoolean( row ) { |
| 230 | + row.value = row.value === '1' ? '0' : '1'; |
142 | 231 | }, |
143 | 232 |
|
144 | | - setDurationMode( row, mode ) { |
145 | | - row.value = mode === 'days' ? '' : mode; |
| 233 | + modeOf( row ) { |
| 234 | + return settingType( row.type ).isLiteral( row.value ) ? row.value : MODE_NUMBER; |
146 | 235 | }, |
147 | 236 |
|
148 | 237 | toggleSelect( key ) { |
@@ -171,15 +260,79 @@ const advancedSettings = function( config ) { |
171 | 260 | this.closeSelect(); |
172 | 261 | }, |
173 | 262 |
|
174 | | - selectDurationMode( row, mode ) { |
175 | | - this.setDurationMode( row, mode ); |
| 263 | + selectMode( row, mode ) { |
| 264 | + row.value = mode === MODE_NUMBER ? '' : mode; |
176 | 265 | this.closeSelect(); |
177 | 266 | }, |
178 | 267 |
|
179 | 268 | labelFor( options, value ) { |
180 | 269 | const match = ( options || [] ).find( ( option ) => option.value === value ); |
181 | 270 | return match ? match.label : ''; |
182 | 271 | }, |
| 272 | + |
| 273 | + rowError( row ) { |
| 274 | + if ( ! row.property ) { |
| 275 | + return ERROR_PROPERTY; |
| 276 | + } |
| 277 | + |
| 278 | + if ( row.value === '' ) { |
| 279 | + return ERROR_VALUE; |
| 280 | + } |
| 281 | + |
| 282 | + if ( ! settingType( row.type ).accepts( row.value ) ) { |
| 283 | + return ERROR_INVALID; |
| 284 | + } |
| 285 | + |
| 286 | + if ( expectsUrl( row.property ) && ! isUrl( row.value ) ) { |
| 287 | + return ERROR_URL; |
| 288 | + } |
| 289 | + |
| 290 | + return ''; |
| 291 | + }, |
| 292 | + |
| 293 | + errorFor( row ) { |
| 294 | + const error = this.showErrors ? this.rowError( row ) : ''; |
| 295 | + |
| 296 | + return error ? this.i18n.errors[ error ] : ''; |
| 297 | + }, |
| 298 | + |
| 299 | + /** |
| 300 | + * Hold the save back while a row is incomplete. The tab is switched |
| 301 | + * first: the panel is hidden behind it, so from any other tab the save |
| 302 | + * button would just look inert. |
| 303 | + * |
| 304 | + * @param {SubmitEvent} event Form submission. |
| 305 | + */ |
| 306 | + onSubmit( event ) { |
| 307 | + const index = this.rows.findIndex( ( row ) => this.rowError( row ) !== '' ); |
| 308 | + |
| 309 | + if ( index < 0 ) { |
| 310 | + return; |
| 311 | + } |
| 312 | + |
| 313 | + event.preventDefault(); |
| 314 | + this.showErrors = true; |
| 315 | + this.currentTab = this.tab; |
| 316 | + this.$nextTick( () => this.focusRow( index ) ); |
| 317 | + }, |
| 318 | + |
| 319 | + focusRow( index ) { |
| 320 | + // The x-for template sits in the same parent, so rows are read by |
| 321 | + // element rather than by child index. |
| 322 | + const element = this.$refs.rows?.querySelectorAll( ':scope > div' )[ index ]; |
| 323 | + |
| 324 | + if ( ! element ) { |
| 325 | + return; |
| 326 | + } |
| 327 | + |
| 328 | + const target = |
| 329 | + this.rowError( this.rows[ index ] ) === ERROR_PROPERTY |
| 330 | + ? null |
| 331 | + : element.querySelector( '[data-value-input]' ); |
| 332 | + |
| 333 | + element.scrollIntoView( { block: 'center', behavior: 'smooth' } ); |
| 334 | + ( target || element.querySelector( '[data-select-trigger]' ) )?.focus(); |
| 335 | + }, |
183 | 336 | }; |
184 | 337 | }; |
185 | 338 |
|
|