@@ -4,11 +4,11 @@ import { ajaxShowMessage } from './ajax-message.ts';
44import isStorageSupported from './functions/isStorageSupported.ts' ;
55import formatDateTime from './functions/formatDateTime.ts' ;
66
7- window . configInlineParams ;
8- window . configScriptLoaded ;
7+ let configInlineParams : any [ ] | undefined ;
8+ let configScriptLoaded : boolean = false ;
99
1010// default values for fields
11- window . defaultValues = { } ;
11+ const defaultValues : object = { } ;
1212
1313/**
1414 * Returns field type
@@ -170,21 +170,21 @@ function getAllValues () {
170170function checkFieldDefault ( field , type ) {
171171 var $field = $ ( field ) ;
172172 var fieldId = $field . attr ( 'id' ) ;
173- if ( typeof window . defaultValues [ fieldId ] === 'undefined' ) {
173+ if ( typeof defaultValues [ fieldId ] === 'undefined' ) {
174174 return true ;
175175 }
176176
177177 var isDefault = true ;
178178 var currentValue = getFieldValue ( $field , type ) ;
179179 if ( type !== 'select' ) {
180- isDefault = currentValue === window . defaultValues [ fieldId ] ;
180+ isDefault = currentValue === defaultValues [ fieldId ] ;
181181 } else {
182182 // compare arrays, will work for our representation of select values
183- if ( currentValue . length !== window . defaultValues [ fieldId ] . length ) {
183+ if ( currentValue . length !== defaultValues [ fieldId ] . length ) {
184184 isDefault = false ;
185185 } else {
186186 for ( var i = 0 ; i < currentValue . length ; i ++ ) {
187- if ( currentValue [ i ] !== window . defaultValues [ fieldId ] [ i ] ) {
187+ if ( currentValue [ i ] !== defaultValues [ fieldId ] [ i ] ) {
188188 isDefault = false ;
189189 break ;
190190 }
@@ -313,7 +313,7 @@ const validators = {
313313 * @param {boolean } onKeyUp whether fire on key up
314314 * @param {any[] } params validation function parameters
315315 */
316- function registerFieldValidator ( id , type , onKeyUp , params ) {
316+ function registerFieldValidator ( id , type , onKeyUp , params = undefined ) {
317317 if ( typeof window . validators [ type ] === 'undefined' ) {
318318 return ;
319319 }
@@ -509,21 +509,44 @@ function validateFieldAndFieldset (field, isKeyUp) {
509509}
510510
511511function loadInlineConfig ( ) {
512- if ( ! Array . isArray ( window . configInlineParams ) ) {
512+ if ( ! Array . isArray ( configInlineParams ) ) {
513513 return ;
514514 }
515515
516- for ( var i = 0 ; i < window . configInlineParams . length ; ++ i ) {
517- if ( typeof window . configInlineParams [ i ] === 'function' ) {
518- window . configInlineParams [ i ] ( ) ;
516+ for ( var i = 0 ; i < configInlineParams . length ; ++ i ) {
517+ if ( typeof configInlineParams [ i ] === 'function' ) {
518+ configInlineParams [ i ] ( ) ;
519519 }
520520 }
521521}
522522
523523function setupValidation ( ) {
524524 validate = { } ;
525- window . configScriptLoaded = true ;
526- if ( window . configScriptLoaded && typeof window . configInlineParams !== 'undefined' ) {
525+
526+ const configInlineParamsData = $ ( '#configInlineParamsData' ) ;
527+ if ( configInlineParamsData . length > 0 ) {
528+ const fieldValidators = configInlineParamsData . data ( 'fieldValidators' ) ;
529+ const inlineDefaultValues = configInlineParamsData . data ( 'defaultValues' ) ;
530+
531+ if ( typeof configInlineParams === 'undefined' || ! Array . isArray ( configInlineParams ) ) {
532+ configInlineParams = [ ] ;
533+ }
534+
535+ configInlineParams . push ( function ( ) {
536+ for ( const validator of fieldValidators ) {
537+ if ( validator . args ) {
538+ registerFieldValidator ( validator . fieldId , validator . name , true , validator . args ) ;
539+ } else {
540+ registerFieldValidator ( validator . fieldId , validator . name , true ) ;
541+ }
542+ }
543+ } ) ;
544+
545+ $ . extend ( defaultValues , inlineDefaultValues ) ;
546+ }
547+
548+ configScriptLoaded = true ;
549+ if ( configScriptLoaded && typeof configInlineParams !== 'undefined' ) {
527550 Config . loadInlineConfig ( ) ;
528551 }
529552
@@ -598,11 +621,11 @@ function adjustPrefsNotification () {
598621 */
599622function restoreField ( fieldId ) : void {
600623 var $field = $ ( '#' + fieldId ) ;
601- if ( $field . length === 0 || window . defaultValues [ fieldId ] === undefined ) {
624+ if ( $field . length === 0 || defaultValues [ fieldId ] === undefined ) {
602625 return ;
603626 }
604627
605- setFieldValue ( $field , getFieldType ( $field ) , window . defaultValues [ fieldId ] ) ;
628+ setFieldValue ( $field , getFieldType ( $field ) , defaultValues [ fieldId ] ) ;
606629}
607630
608631function setupRestoreField ( ) {
@@ -755,7 +778,7 @@ function on () {
755778 $ ( '.optbox input[type=button][name=submit_reset]' ) . on ( 'click' , function ( ) {
756779 var fields = $ ( this ) . closest ( 'fieldset' ) . find ( 'input, select, textarea' ) ;
757780 for ( var i = 0 , imax = fields . length ; i < imax ; i ++ ) {
758- setFieldValue ( fields [ i ] , getFieldType ( fields [ i ] ) , window . defaultValues [ fields [ i ] . id ] ) ;
781+ setFieldValue ( fields [ i ] , getFieldType ( fields [ i ] ) , defaultValues [ fields [ i ] . id ] ) ;
759782 }
760783
761784 setDisplayError ( ) ;
@@ -833,7 +856,6 @@ function on () {
833856const Config = {
834857 getAllValues : getAllValues ,
835858 getIdPrefix : getIdPrefix ,
836- registerFieldValidator : registerFieldValidator ,
837859 displayErrors : displayErrors ,
838860 loadInlineConfig : loadInlineConfig ,
839861 setupValidation : setupValidation ,
@@ -844,9 +866,6 @@ const Config = {
844866
845867declare global {
846868 interface Window {
847- configInlineParams : any [ ] | undefined ;
848- configScriptLoaded : boolean | undefined ;
849- defaultValues : object ;
850869 validators : typeof validators ;
851870 Config : typeof Config ;
852871 }
0 commit comments