GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #83 from axeptio/feat/82784_advanced_configuration… · axeptio/wordpress-plugin@d33056d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • assets/js/backend
    • components
      • accountIDComponent.js
      • advancedSettings.js
    • utils
      • settingType.js
  • composer.json
  • includes
    • classes
      • admin/pages
        • class-admin-callbacks.php
      • backend
        • class-settings.php
      • models
        • class-advanced-settings.php
        • class-setting-type.php
    • data
      • settings-reference.json
  • languages
    • axeptio-sdk-integration-de_DE.mo
    • axeptio-sdk-integration-de_DE.po
    • axeptio-sdk-integration-en_US.l10n.php
    • axeptio-sdk-integration-en_US.mo
    • axeptio-sdk-integration-en_US.po
    • axeptio-sdk-integration-es_ES.mo
    • axeptio-sdk-integration-es_ES.po
    • axeptio-sdk-integration-fr_FR.mo
    • axeptio-sdk-integration-fr_FR.po
    • axeptio-sdk-integration-it_IT.mo
    • axeptio-sdk-integration-it_IT.po
    • axeptio-sdk-integration-nl_NL.mo
    • axeptio-sdk-integration-nl_NL.po
    • axeptio-sdk-integration.pot
  • phpunit.xml
  • templates/admin
    • main/fields
      • advanced-settings.php
      • advanced-value-input.php
      • google-consent-mode.php
      • proxy-sdk.php
      • sdk-active.php
      • send-datas.php
    • plugins/fields
      • toggle.php
    • sections
      • main.php
  • tests
    • AdvancedSettingsTest.php
    • SettingTypeTest.php
    • bootstrap.php
    • constants.php
    • fixtures
      • setting-types.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎assets/js/backend/components/accountIDComponent.js‎

Copy file name to clipboardExpand all lines: assets/js/backend/components/accountIDComponent.js
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import Alpine from 'alpinejs';
22

3+
const serializeForm = ( form ) =>
4+
new URLSearchParams( new FormData( form ) ).toString();
5+
36
const instance = function( args ) {
7+
let savedState = '';
8+
49
return {
510
axeptioSettings: {},
611
accountID: args.accountID,
@@ -15,6 +20,7 @@ const instance = function( args ) {
1520
sendDatas: args.sendDatas,
1621
proxySdk: args.proxySdk,
1722
currentTab: Alpine.$persist( 'main-settings' ),
23+
isDirty: false,
1824
selectedOption: args.selectedOption,
1925
options: ( () => {
2026
try {
@@ -26,6 +32,7 @@ const instance = function( args ) {
2632
optionsJson: args.optionsJson,
2733
init() {
2834
this.axeptioSettings = window.Axeptio;
35+
this.watchChanges();
2936

3037
// Récupération de la valeur du champ caché et affectation à accountID
3138
if ( this.accountID === '' ) {
@@ -39,6 +46,37 @@ const instance = function( args ) {
3946
this.validateAccountID();
4047
}
4148
},
49+
watchChanges() {
50+
this.$nextTick( () => {
51+
savedState = serializeForm( this.$el );
52+
} );
53+
54+
[ 'input', 'change', 'click', 'keydown' ].forEach( ( event ) =>
55+
this.$el.addEventListener( event, () =>
56+
this.$nextTick( () => {
57+
this.isDirty = serializeForm( this.$el ) !== savedState;
58+
} )
59+
)
60+
);
61+
62+
// On window: the form runs its handlers first, so a save cancelled by a
63+
// failed validation still reads as pending. Target check: other forms.
64+
window.addEventListener( 'submit', ( event ) => {
65+
if ( event.target === this.$el && ! event.defaultPrevented ) {
66+
this.isDirty = false;
67+
}
68+
} );
69+
70+
window.addEventListener( 'beforeunload', ( event ) => {
71+
if ( ! this.isDirty ) {
72+
return;
73+
}
74+
75+
event.preventDefault();
76+
event.returnValue = '';
77+
} );
78+
},
79+
4280
restoreHistorizedVersion() {
4381
this.selectedOption = this.historizedVersions[ this.accountID ];
4482
this.isHistorizedVersion = false;
Collapse file

‎assets/js/backend/components/advancedSettings.js‎

Copy file name to clipboardExpand all lines: assets/js/backend/components/advancedSettings.js
+189-36Lines changed: 189 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,77 @@
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+
}
748

849
const advancedSettings = function( config ) {
950
return {
1051
reference: config.reference || [],
1152
i18n: config.i18n || {},
53+
tab: config.tab,
1254
rows: [],
1355
nextUid: 0,
1456
openSelect: null,
1557
focusedProperty: null,
1658
keySearch: '',
59+
showErrors: false,
1760

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,
2468

2569
init() {
2670
this.rows = ( config.pairs || [] ).map( ( pair ) => this.makeRow( pair ) );
71+
72+
this.$el
73+
.closest( 'form' )
74+
?.addEventListener( 'submit', ( event ) => this.onSubmit( event ) );
2775
},
2876

2977
makeRow( pair = {} ) {
@@ -39,7 +87,27 @@ const advancedSettings = function( config ) {
3987
return `axeptio_settings[advanced_settings][${ index }][${ key }]`;
4088
},
4189

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+
42106
addRow() {
107+
if ( ! this.canAddRow() ) {
108+
return;
109+
}
110+
43111
this.rows.push( this.makeRow() );
44112
this.$nextTick( () => {
45113
this.$refs.rows?.lastElementChild
@@ -103,8 +171,12 @@ const advancedSettings = function( config ) {
103171
return option ? option.description : '';
104172
},
105173

174+
widget( row ) {
175+
return settingType( row.type ).widget;
176+
},
177+
106178
placeholderFor( row ) {
107-
if ( row.type === TYPE_STRING_LIST ) {
179+
if ( this.widget( row ) === WIDGET_LIST ) {
108180
return this.i18n.list_placeholder || '';
109181
}
110182

@@ -114,35 +186,52 @@ const advancedSettings = function( config ) {
114186
return fallback === null || fallback === undefined ? '' : String( fallback );
115187
},
116188

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 } );
126204
}
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+
} );
129211
}
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;
131221
},
132222

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;
135227
},
136228

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';
142231
},
143232

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;
146235
},
147236

148237
toggleSelect( key ) {
@@ -171,15 +260,79 @@ const advancedSettings = function( config ) {
171260
this.closeSelect();
172261
},
173262

174-
selectDurationMode( row, mode ) {
175-
this.setDurationMode( row, mode );
263+
selectMode( row, mode ) {
264+
row.value = mode === MODE_NUMBER ? '' : mode;
176265
this.closeSelect();
177266
},
178267

179268
labelFor( options, value ) {
180269
const match = ( options || [] ).find( ( option ) => option.value === value );
181270
return match ? match.label : '';
182271
},
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+
},
183336
};
184337
};
185338

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.