@creative-web-solution/front-library 7.0.7 → 7.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/DOM/Class.ts +10 -12
  3. package/DOM/DocumentSize.ts +3 -1
  4. package/DOM/Filter.ts +0 -2
  5. package/DOM/Index.ts +1 -3
  6. package/DOM/Manipulation.ts +0 -3
  7. package/DOM/Matrix.ts +12 -12
  8. package/DOM/Offset.ts +4 -5
  9. package/DOM/OuterSize.ts +12 -12
  10. package/DOM/PageToDOM.ts +2 -2
  11. package/DOM/Position.ts +3 -3
  12. package/DOM/Size.ts +3 -7
  13. package/DOM/StrToDOM.ts +0 -1
  14. package/DOM/Styles.ts +3 -12
  15. package/DOM/Traversing.ts +0 -6
  16. package/DOM/WindowScroll.ts +3 -1
  17. package/DOM/WindowSize.ts +3 -1
  18. package/DOM/Wrap.ts +0 -1
  19. package/Events/DeviceOrientation.ts +27 -31
  20. package/Events/EventsManager.ts +10 -25
  21. package/Events/Gesture.ts +36 -37
  22. package/Events/HistoryController.ts +12 -18
  23. package/Events/ImageLoad.ts +35 -19
  24. package/Events/IntersectObserver.ts +15 -19
  25. package/Events/KeyboardHandler.ts +10 -8
  26. package/Events/MediaQueriesEvents.ts +53 -51
  27. package/Events/OnAnimationEnd.ts +2 -3
  28. package/Events/OnTransitionEnd.ts +3 -3
  29. package/Events/PubSub.ts +5 -8
  30. package/Events/TouchHover.ts +18 -21
  31. package/Events/WindowEvents.ts +26 -30
  32. package/Helpers/Colors.ts +7 -23
  33. package/Helpers/Cookie.ts +5 -12
  34. package/Helpers/Coordinates.ts +0 -7
  35. package/Helpers/Debounce.ts +3 -7
  36. package/Helpers/Defer.ts +1 -1
  37. package/Helpers/GetValue.ts +0 -2
  38. package/Helpers/Insert.ts +0 -1
  39. package/Helpers/Sequential.ts +0 -2
  40. package/Helpers/Slice.ts +0 -3
  41. package/Helpers/Throttle.ts +0 -1
  42. package/Helpers/TransitionHelpers.ts +8 -6
  43. package/Helpers/Type.ts +1 -14
  44. package/Helpers/Unique.ts +3 -3
  45. package/Helpers/UrlParser.ts +14 -23
  46. package/Helpers/Wait.ts +1 -1
  47. package/Modules/Accordion/Tab.ts +19 -17
  48. package/Modules/Accordion/index.ts +27 -19
  49. package/Modules/Autocomplete.ts +95 -101
  50. package/Modules/DragSlider.ts +72 -63
  51. package/Modules/GlobalState.ts +60 -53
  52. package/Modules/LoadGMap.ts +4 -7
  53. package/Modules/Notifications/Notification.ts +28 -32
  54. package/Modules/Notifications/index.ts +43 -36
  55. package/Modules/Popin/Popin.ts +100 -110
  56. package/Modules/Popin/PopinAccessibility.ts +8 -8
  57. package/Modules/Popin/PopinBackground.ts +12 -12
  58. package/Modules/Popin/PopinController.ts +24 -30
  59. package/Modules/Popin/Tools.ts +18 -19
  60. package/Modules/QuickTemplate.ts +4 -6
  61. package/Modules/ScrollSnap.ts +110 -104
  62. package/Modules/SkinCheckbox.ts +52 -38
  63. package/Modules/SkinFile.ts +63 -53
  64. package/Modules/SkinRadio.ts +63 -43
  65. package/Modules/SkinSelect.ts +117 -101
  66. package/Modules/Slider/Slide.ts +49 -52
  67. package/Modules/Slider/Slider.ts +100 -136
  68. package/Modules/Slider/SliderControls.ts +60 -59
  69. package/Modules/Tabs/Tab.ts +28 -24
  70. package/Modules/Tabs/index.ts +36 -26
  71. package/Modules/Validator/Date.ts +2 -1
  72. package/Modules/Validator/Equals.ts +1 -1
  73. package/Modules/Validator/Internal/Input.ts +366 -0
  74. package/Modules/Validator/Internal/InputValidator.ts +99 -0
  75. package/Modules/Validator/Internal/ValidatorFunctionsController.ts +58 -0
  76. package/Modules/Validator/Max.ts +1 -1
  77. package/Modules/Validator/Min.ts +1 -1
  78. package/Modules/Validator/MultiRequired.ts +5 -5
  79. package/Modules/Validator/Number.ts +1 -0
  80. package/Modules/Validator/Pattern.ts +1 -1
  81. package/Modules/Validator/Recaptcha.ts +1 -0
  82. package/Modules/Validator/Required.ts +3 -2
  83. package/Modules/Validator/ServerCheck.ts +23 -15
  84. package/Modules/Validator/Tools/GetQueryFromForm.ts +1 -6
  85. package/Modules/Validator/Tools/IsDate.ts +2 -3
  86. package/Modules/Validator/Tools/IsEmail.ts +2 -5
  87. package/Modules/Validator/Tools/IsEmpty.ts +0 -2
  88. package/Modules/Validator/Tools/IsNumber.ts +0 -2
  89. package/Modules/Validator/Tools/IsUrl.ts +1 -3
  90. package/Modules/Validator/Tools/Label.ts +5 -15
  91. package/Modules/Validator/Tools/RadioButton.ts +4 -10
  92. package/Modules/Validator/Url.ts +1 -0
  93. package/Modules/Validator/index.ts +36 -36
  94. package/Modules/YouTubePlayer.ts +18 -16
  95. package/README.md +1 -1
  96. package/Types/Accordion.d.ts +41 -0
  97. package/Types/Autocomplete.d.ts +94 -0
  98. package/Types/DOM.d.ts +69 -0
  99. package/Types/DragSlider.d.ts +56 -0
  100. package/Types/EventsHelpers.d.ts +390 -0
  101. package/Types/GLImageTransition.d.ts +47 -43
  102. package/Types/GlobalState.d.ts +9 -0
  103. package/Types/Helpers.d.ts +63 -0
  104. package/Types/Notifications.d.ts +48 -44
  105. package/Types/Popin.d.ts +113 -0
  106. package/Types/ScrollSnap.d.ts +66 -0
  107. package/Types/SkinCheckbox.d.ts +35 -0
  108. package/Types/SkinFile.d.ts +45 -0
  109. package/Types/SkinRadio.d.ts +37 -0
  110. package/Types/SkinSelect.d.ts +72 -0
  111. package/Types/Slider.d.ts +106 -0
  112. package/Types/Tabs.d.ts +33 -0
  113. package/Types/Validator.d.ts +99 -0
  114. package/Types/YouTubePlayer.d.ts +27 -0
  115. package/Types/index.d.ts +22 -21
  116. package/WebGL/GLImageTransition/GLImageTransition.ts +83 -80
  117. package/WebGL/GLImageTransition/Presets/Cellular.ts +11 -11
  118. package/WebGL/GLImageTransition/Presets/Fade.ts +2 -3
  119. package/WebGL/GLImageTransition/Presets/HorizontalMovingGrid.ts +9 -9
  120. package/WebGL/GLImageTransition/Presets/PresetSample.ts +11 -9
  121. package/WebGL/GLImageTransition/Presets/Solarisation.ts +7 -12
  122. package/package.json +1 -1
  123. package/Modules/Template.ts +0 -209
  124. package/Types/AccordionTypes.d.ts +0 -29
  125. package/Types/AutocompleteTypes.d.ts +0 -85
  126. package/Types/DOMTypes.d.ts +0 -62
  127. package/Types/DragSliderTypes.d.ts +0 -52
  128. package/Types/EventsHelpersTypes.d.ts +0 -370
  129. package/Types/GlobalStateTypes.d.ts +0 -5
  130. package/Types/HelpersTypes.d.ts +0 -23
  131. package/Types/MediaPreloaderTypes.d.ts +0 -12
  132. package/Types/PopinTypes.d.ts +0 -83
  133. package/Types/ScrollSnapTypes.d.ts +0 -62
  134. package/Types/SkinCheckboxTypes.d.ts +0 -23
  135. package/Types/SkinFileTypes.d.ts +0 -34
  136. package/Types/SkinRadioTypes.d.ts +0 -24
  137. package/Types/SkinSelectTypes.d.ts +0 -48
  138. package/Types/SliderTypes.d.ts +0 -97
  139. package/Types/TabsTypes.d.ts +0 -22
  140. package/Types/ValidatorTypes.d.ts +0 -61
  141. package/Types/YouTubePlayerTypes.d.ts +0 -23
@@ -0,0 +1,366 @@
1
+ import { on, off } from '../../../Events/EventsManager';
2
+ import { unique } from '../../../Helpers/Unique';
3
+ import validatorFunctionsController from './ValidatorFunctionsController';
4
+ import InputValidator from './InputValidator';
5
+ import { getRadioList } from '../Tools/RadioButton';
6
+
7
+
8
+ /*
9
+ * Handle one input
10
+ */
11
+ export default class Input implements FLib.Validator.Input {
12
+ #isRadio: boolean;
13
+ #inputType: string;
14
+ #inputId: string;
15
+ #$input: HTMLElement;
16
+ #$group: FLib.Validator.CustomValidatorRadioInput[] | undefined;
17
+ #$otherRadioOfGroup;
18
+ #hasValidator: boolean;
19
+ #validatorsInErrors: InputValidator[] = [];
20
+ #inlineCustomErrorMessages: Record<string, string>;
21
+ #liveHookFunctionHash;
22
+ #$label: HTMLLabelElement | undefined;
23
+ #validators: InputValidator[];
24
+ #hasError: boolean;
25
+ #isLiveValidation = false;
26
+ #options;
27
+
28
+ get $input(): HTMLElement {
29
+ return this.#$input;
30
+ }
31
+
32
+ get $label(): HTMLElement | undefined {
33
+ return this.#$label;
34
+ }
35
+
36
+ get hasError(): boolean {
37
+ return this.#hasError;
38
+ }
39
+
40
+ get isLiveValidation(): boolean {
41
+ return this.#isLiveValidation;
42
+ }
43
+
44
+ get $radioGroup(): FLib.Validator.CustomValidatorRadioInput[] | undefined {
45
+ return this.#$group;
46
+ }
47
+
48
+ get $otherRadioOfGroup(): FLib.Validator.CustomValidatorRadioInput[] {
49
+ return this.#$otherRadioOfGroup;
50
+ }
51
+
52
+ get hasValidator(): boolean {
53
+ return this.#hasValidator;
54
+ }
55
+
56
+
57
+ constructor( $input: HTMLElement & { type?: string }, options: FLib.Validator.Options & { hasLiveValidation: boolean; } ) {
58
+
59
+ this.#options = options;
60
+
61
+ if ( $input.type === 'radio' || $input.type === 'checkbox' ) {
62
+ this.#inputType = 'optin';
63
+ }
64
+ else if ( $input.type === 'hidden' ) {
65
+ this.#inputType = 'hidden';
66
+ }
67
+ else if ( $input.nodeName === 'SELECT' ) {
68
+ this.#inputType = 'select';
69
+ }
70
+ else {
71
+ this.#inputType = 'inputText';
72
+ }
73
+
74
+ this.#isRadio = $input.type === 'radio';
75
+ this.#inputId = $input.id;
76
+
77
+ if ( this.#inputId ) {
78
+ this.#$label = document.querySelector( `label[for="${ this.#inputId }"]` ) as HTMLLabelElement;
79
+ }
80
+
81
+ if ( !this.#$label ) {
82
+ this.#$label = $input.closest( 'label' ) as HTMLLabelElement;
83
+ }
84
+
85
+ // Get all validators for this input
86
+ this.#validators = validatorFunctionsController.getValidators( $input, options );
87
+
88
+ this.#hasValidator = this.#validators.length > 0;
89
+ this.#inlineCustomErrorMessages = {};
90
+
91
+ if ( $input.hasAttribute( options.customErrorLabelPrefix ) ) {
92
+ this.#inlineCustomErrorMessages[ 'default' ] =
93
+ options.errorMessages[ $input.getAttribute( options.customErrorLabelPrefix ) as string ];
94
+ }
95
+
96
+ this.#validators.forEach( validator => {
97
+ const attrName = [ options.customErrorLabelPrefix, validator.name ].join( '-' );
98
+
99
+ if ( $input.hasAttribute( attrName ) ) {
100
+ this.#inlineCustomErrorMessages[ validator.name ] = $input.getAttribute( attrName ) as string;
101
+ }
102
+ })
103
+
104
+ // Cache all radio button with the same name attribute
105
+ if ( this.#isRadio ) {
106
+ this.#$group = getRadioList( $input ) as FLib.Validator.CustomValidatorRadioInput[];
107
+ this.#$otherRadioOfGroup = Array.from( this.#$group ).filter( $rd => {
108
+ return $rd !== $input;
109
+ } );
110
+
111
+ this.#$group.forEach( $rd => {
112
+ $rd.__$radioMaster = $input as FLib.Validator.CustomValidatorRadioInput;
113
+ $rd.__$radioGroup = this.#$group as FLib.Validator.CustomValidatorRadioInput[];
114
+ $rd.__$otherRadioOfGroup = ( Array.from( this.#$group as FLib.Validator.CustomValidatorRadioInput[] ) as FLib.Validator.CustomValidatorRadioInput[] ).filter( $rd2 => {
115
+ return $rd2 !== $rd;
116
+ } );
117
+ } );
118
+ }
119
+
120
+ this.#$input = $input;
121
+ this.#hasError = false;
122
+
123
+
124
+ if ( this.#hasValidator && options.hasLiveValidation && this.#inputType !== 'hidden' ) {
125
+ on( this.#isRadio ? this.#$group : $input, {
126
+ "eventsName": options.liveValidation?.eventsName?.[ this.#inputType ] as string,
127
+ "callback": this.#onLiveValidation
128
+ } );
129
+ }
130
+
131
+ // Add extra listener (focus, blur, ...) on the input
132
+ if ( options.liveValidation?.eventsHook ) {
133
+ this.#liveHookFunctionHash = {};
134
+
135
+ Object
136
+ .keys( options.liveValidation.eventsHook )
137
+ .forEach( key => {
138
+ this.#liveHookFunctionHash[ key ] = () => {
139
+ options.liveValidation?.eventsHook[ key ]( this, event );
140
+ }
141
+
142
+ on( this.#isRadio ? this.#$group : $input, {
143
+ "eventsName": key,
144
+ "callback": this.#liveHookFunctionHash[ key ]
145
+ } );
146
+ } );
147
+ }
148
+ }
149
+
150
+
151
+ /*
152
+ * Validate this input
153
+ */
154
+ #validate = ( isLiveValidation: boolean ): Promise<void> => {
155
+ this.#validatorsInErrors.length = 0;
156
+ this.#hasError = false;
157
+ this.#isLiveValidation = isLiveValidation;
158
+
159
+ // This input has no (known) validation
160
+ if ( !this.#hasValidator ) {
161
+ return Promise.resolve();
162
+ }
163
+
164
+ const promArray: Promise<void>[] = [];
165
+
166
+ // Call all validators
167
+ this.#validators.forEach( validator => {
168
+ promArray.push( validator.validate( isLiveValidation ) )
169
+ } );
170
+
171
+ return Promise.all( promArray ).then(() => {
172
+ this.#validators.forEach( validator => {
173
+ if ( !validator.isValid() ) {
174
+ this.#hasError = true
175
+ }
176
+ } );
177
+ } );
178
+ }
179
+
180
+
181
+ /*
182
+ * Return an array of validator in error
183
+ */
184
+ getErrors(): InputValidator[] {
185
+ this.#validatorsInErrors.length = 0;
186
+
187
+ // No validator => Field is valid
188
+ if ( !this.#hasValidator ) {
189
+ return this.#validatorsInErrors;
190
+ }
191
+
192
+ // Return only validator in invalid state
193
+ this.#validators.forEach( validator => {
194
+ if ( !validator.isValid() ) {
195
+ this.#validatorsInErrors.push( validator );
196
+ }
197
+ });
198
+
199
+ return this.#validatorsInErrors;
200
+ }
201
+
202
+
203
+ #labelToMessage = ( validatorName: string, _locale?: { [ key: string ]: string }, avoidDefaultMessage?: boolean ): string => {
204
+ let customInlineDefaultMessage,
205
+ customErrorDefaultMessage,
206
+ customInlineMessage;
207
+
208
+ if ( !avoidDefaultMessage ) {
209
+ // In attribute data-error-label
210
+ customInlineDefaultMessage = this.#inlineCustomErrorMessages[ 'default' ];
211
+ // In global options configuration
212
+ customErrorDefaultMessage = this.#options.errorMessages[ 'default' ];
213
+ }
214
+
215
+ // Forced in parameters of this function
216
+ const forcedJSMessage = _locale ? _locale[ validatorName ] : null;
217
+
218
+ if ( forcedJSMessage ) {
219
+ return forcedJSMessage;
220
+ }
221
+
222
+ // In attribute data-error-label-VALIDATOR_NAME
223
+ customInlineMessage = this.#inlineCustomErrorMessages[ validatorName ];
224
+ if ( customInlineMessage ) {
225
+ customInlineMessage =
226
+ this.#options.errorMessages[ customInlineMessage ] ||
227
+ customInlineMessage;
228
+ }
229
+
230
+ if ( customInlineMessage ) {
231
+ return customInlineMessage;
232
+ }
233
+
234
+ // In global options configuration
235
+ const customErrorMessage = this.#options.errorMessages[ validatorName ];
236
+
237
+ if ( customErrorMessage ) {
238
+ return customErrorMessage;
239
+ }
240
+
241
+ return (
242
+ customInlineDefaultMessage ||
243
+ customErrorDefaultMessage ||
244
+ validatorName
245
+ );
246
+ }
247
+
248
+
249
+ /*
250
+ * Return an array of error messages and labels
251
+ */
252
+ getErrorMessages( _locale?: { [ key: string ]: string } ): { message: string, label: string, type: string }[] {
253
+ this.getErrors();
254
+
255
+ if ( !this.#validatorsInErrors.length ) {
256
+ return [];
257
+ }
258
+
259
+ const messages: { message: string, label: string, type: string }[] = [];
260
+
261
+ this.#validatorsInErrors.forEach( validator => {
262
+ // BASIC MESSAGE (determined from validator name)
263
+ messages.push( {
264
+ "message": this.#labelToMessage( validator.name, _locale ),
265
+ "label": validator.name,
266
+ "type": "basic"
267
+ } );
268
+
269
+ // EXTRA MESSAGE (from the validator function)
270
+
271
+ validator.extraErrorMessages.forEach( extraErrorMessage => {
272
+ messages.push( {
273
+ "message": this.#labelToMessage( extraErrorMessage, _locale, true ),
274
+ "label": validator.name,
275
+ "type": "extra"
276
+ } );
277
+ });
278
+ });
279
+
280
+ return unique( messages, ( elem, resultArray ) => {
281
+ return !resultArray.find( resultElem => {
282
+ return elem.message === resultElem.message
283
+ } );
284
+ } );
285
+ }
286
+
287
+
288
+ /*
289
+ * Return a promise that resolve if the validation is successful
290
+ */
291
+ isValid(): Promise<void> {
292
+ return this.#validate( false );
293
+ }
294
+
295
+
296
+ /**
297
+ * Get custom data of all validators
298
+ */
299
+ getData(): any[] {
300
+ this.getErrors();
301
+
302
+ if ( !this.#validatorsInErrors.length ) {
303
+ return [];
304
+ }
305
+
306
+ const dataArray: any[] = [];
307
+
308
+ this.#validatorsInErrors.forEach( validator => {
309
+ const data = validator.getData();
310
+
311
+ if ( !data ) {
312
+ return;
313
+ }
314
+
315
+ dataArray.push( data );
316
+ })
317
+
318
+ return dataArray;
319
+ }
320
+
321
+
322
+ /*
323
+ * Remove events binding from the field
324
+ */
325
+ destroy(): this {
326
+
327
+ if ( this.#hasValidator && this.#options.hasLiveValidation && this.#inputType !== 'hidden' ) {
328
+ off( this.#isRadio ? this.#$group : this.#$input, {
329
+ "eventsName": this.#options.liveValidation.eventsName[ this.#inputType ],
330
+ "callback": this.#onLiveValidation
331
+ } );
332
+ }
333
+
334
+
335
+ if ( this.#options.liveValidation.eventsHook && this.#liveHookFunctionHash ) {
336
+ Object
337
+ .keys( this.#options.liveValidation.eventsHook )
338
+ .forEach( key => {
339
+ off( this.#isRadio ? this.#$group : this.#$input, {
340
+ "eventsName": key,
341
+ "callback": this.#liveHookFunctionHash[ key ]
342
+ } );
343
+ } );
344
+ }
345
+
346
+ return this;
347
+ }
348
+
349
+
350
+ #onLiveValidation = ( event: Event ): void => {
351
+ this.#validate( true )
352
+ .then( () => {
353
+ if ( this.hasError && this.#options.liveValidation.onInvalidate ) {
354
+ this.#options.liveValidation.onInvalidate( this, event );
355
+ }
356
+ else if ( !this.hasError && this.#options.liveValidation.onValidate ) {
357
+ this.#options.liveValidation.onValidate( this, event );
358
+ }
359
+ } )
360
+ .catch( err => {
361
+ if ( window.$$DEBUG$$ ) {
362
+ console.log( err );
363
+ }
364
+ } );
365
+ }
366
+ }
@@ -0,0 +1,99 @@
1
+ import { getValue } from '../../../Helpers/GetValue';
2
+
3
+
4
+ /*
5
+ * Used to make one validation on one input
6
+ * Store the state (valid or not) between validations
7
+ */
8
+ export default class InputValidator implements FLib.Validator.InputValidator {
9
+ #$input: HTMLElement;
10
+ #name: string;
11
+ #isAsynch: boolean;
12
+ #extraMessages;
13
+ #extraErrorMessages;
14
+ #data;
15
+ #validateFunc: FLib.Validator.ValidateFunction;
16
+ #state: FLib.Validator.ValidationState | undefined;
17
+ #validatorOptions;
18
+
19
+
20
+ get $input(): HTMLElement {
21
+ return this.#$input;
22
+ }
23
+
24
+ get name(): string {
25
+ return this.#name;
26
+ }
27
+
28
+ get isAsynch(): boolean {
29
+ return this.#isAsynch;
30
+ }
31
+
32
+ get extraMessages(): string[] {
33
+ return this.#extraMessages;
34
+ }
35
+
36
+ get extraErrorMessages(): string[] {
37
+ return this.#extraErrorMessages;
38
+ }
39
+
40
+ get data(): any {
41
+ return this.#data;
42
+ }
43
+
44
+
45
+ constructor( $input: HTMLElement, validatorParams: FLib.Validator.Validator, validatorOptions: FLib.Validator.Validator & { hasLiveValidation: boolean; } ) {
46
+ this.#validateFunc = validatorParams.validate;
47
+
48
+ this.#$input = $input;
49
+ this.#name = validatorParams.name;
50
+ this.#isAsynch = validatorParams.isAsynch;
51
+ // Extra message like warning, not error
52
+ this.#extraMessages = [];
53
+ // Extra error message rise by the validator
54
+ this.#extraErrorMessages = [];
55
+ // Extra data send by the validator
56
+ this.#data = null;
57
+ this.#validatorOptions = validatorOptions;
58
+ }
59
+
60
+
61
+ /*
62
+ * Validate the current input with one validator
63
+ */
64
+ validate( isLiveValidation: boolean ): Promise<void> {
65
+ const value = getValue( this.#$input );
66
+
67
+ this.#extraMessages.length = 0;
68
+ this.#extraErrorMessages.length = 0;
69
+ this.#data = null;
70
+
71
+ return new Promise<void>( resolve => {
72
+ this.#validateFunc( this.#$input, value, isLiveValidation, this.#validatorOptions ).then( _state => {
73
+ this.#state = _state;
74
+
75
+ this.#extraMessages = _state.extraMessages || this.#extraMessages;
76
+ this.#extraErrorMessages = _state.extraErrorMessages || this.#extraErrorMessages;
77
+ this.#data = _state.data;
78
+
79
+ resolve();
80
+ } );
81
+ } );
82
+ }
83
+
84
+
85
+ /**
86
+ * @returns true if the input is valid
87
+ */
88
+ isValid(): boolean {
89
+ return !!this.#state && this.#state.isValid;
90
+ }
91
+
92
+
93
+ /**
94
+ * Get custom data that can optionnaly be passed to the validator
95
+ */
96
+ getData(): any {
97
+ return this.#data;
98
+ }
99
+ }
@@ -0,0 +1,58 @@
1
+ import InputValidator from './InputValidator';
2
+
3
+
4
+ /*
5
+ * Object managing all validation functions
6
+ */
7
+ class ValidatorFunctions {
8
+ #validators: FLib.Validator.Validator[] = [];
9
+
10
+ /*
11
+ * Add new validator to the list
12
+ */
13
+ addValidator = ( name: string, selector: string, isAsynch: boolean | FLib.Validator.ValidateFunction, func?: FLib.Validator.ValidateFunction ) => {
14
+ if ( typeof func === 'undefined' ) {
15
+ func = (isAsynch as unknown) as FLib.Validator.ValidateFunction;
16
+ isAsynch = false;
17
+ }
18
+
19
+ this.#validators.push( {
20
+ name,
21
+ selector,
22
+ "validate": func,
23
+ "isAsynch": isAsynch as boolean
24
+ } );
25
+ }
26
+
27
+
28
+ /*
29
+ * Create and return all validators that apply to an inputs
30
+ */
31
+ getValidators( $input: HTMLElement, options: FLib.Validator.Options & { hasLiveValidation: boolean; } ) {
32
+ let validatorOptions;
33
+
34
+ const inputsValidators: InputValidator[] = [];
35
+
36
+ this.#validators.forEach( validatorParams => {
37
+ if ( $input.matches( validatorParams.selector ) ) {
38
+ validatorOptions =
39
+ options.validatorsOptions &&
40
+ options.validatorsOptions[ validatorParams.name ]
41
+ ? options.validatorsOptions[ validatorParams.name ]
42
+ : null;
43
+ inputsValidators.push(
44
+ new InputValidator(
45
+ $input,
46
+ validatorParams,
47
+ validatorOptions
48
+ )
49
+ );
50
+ }
51
+ })
52
+
53
+ return inputsValidators;
54
+ }
55
+ }
56
+
57
+
58
+ export default new ValidatorFunctions();
@@ -9,7 +9,7 @@ addValidator( 'max', '[max]', ( $input, value, isLiveValidation ) => {
9
9
  $input,
10
10
  value,
11
11
  "isValid": value === '' ||
12
- parseFloat( value as string ) <= parseFloat( $input.getAttribute( 'max' )! ),
12
+ parseFloat( value as string ) <= parseFloat( $input.getAttribute( 'max' ) as string ),
13
13
  "label": "max",
14
14
  isLiveValidation
15
15
  });
@@ -9,7 +9,7 @@ addValidator( 'min', '[min]', ( $input, value, isLiveValidation ) => {
9
9
  $input,
10
10
  value,
11
11
  "isValid": value === '' ||
12
- parseFloat( value as string ) >= parseFloat( $input.getAttribute( 'min' )! ),
12
+ parseFloat( value as string ) >= parseFloat( $input.getAttribute( 'min' ) as string ),
13
13
  "label": "min",
14
14
  isLiveValidation
15
15
  });
@@ -12,9 +12,9 @@ import { isRadioListChecked } from './Tools/RadioButton';
12
12
  *
13
13
  */
14
14
  addValidator( 'multirequired', '[data-multirequired]', ( $input, value, isLiveValidation ) => {
15
- let $group, arrayData, count, data, groupName, nbRequiredMax, nbRequiredMin;
15
+ let $group, count, nbRequiredMax;
16
16
 
17
- data = $input.getAttribute( 'data-multirequired' );
17
+ const data = $input.getAttribute( 'data-multirequired' ) as string;
18
18
  count = 0;
19
19
 
20
20
  if ( data.indexOf( '|' ) === -1 ) {
@@ -27,8 +27,8 @@ addValidator( 'multirequired', '[data-multirequired]', ( $input, value, isLiveVa
27
27
  });
28
28
  }
29
29
 
30
- arrayData = data.split( '|' );
31
- groupName = arrayData[ 0 ];
30
+ const arrayData = data.split( '|' );
31
+ const groupName = arrayData[ 0 ];
32
32
 
33
33
  $group = document.querySelectorAll( `[data-multirequired-group^="${ groupName }"]` );
34
34
 
@@ -42,7 +42,7 @@ addValidator( 'multirequired', '[data-multirequired]', ( $input, value, isLiveVa
42
42
  });
43
43
  }
44
44
 
45
- nbRequiredMin = parseInt( arrayData[ 1 ], 10 ) || 0
45
+ const nbRequiredMin = parseInt( arrayData[ 1 ], 10 ) || 0
46
46
 
47
47
  if ( arrayData.length > 2 ) {
48
48
  nbRequiredMax = parseInt( arrayData[ 2 ], 10 );
@@ -1,6 +1,7 @@
1
1
  import isNumber from './Tools/IsNumber';
2
2
  import { addValidator } from './index';
3
3
 
4
+
4
5
  /**
5
6
  * Number validation
6
7
  */
@@ -8,7 +8,7 @@ addValidator( 'pattern', '[pattern]', ( $input, value, isLiveValidation ) => {
8
8
  return Promise.resolve({
9
9
  $input,
10
10
  value,
11
- "isValid": value === '' || new RegExp( $input.getAttribute( 'pattern' )! ).test( value as string ),
11
+ "isValid": value === '' || new RegExp( $input.getAttribute( 'pattern' ) as string ).test( value as string ),
12
12
  "label": "pattern",
13
13
  isLiveValidation
14
14
  });
@@ -1,5 +1,6 @@
1
1
  import { addValidator } from './index';
2
2
 
3
+
3
4
  addValidator( 'recaptcha', '.js--recaptcha', ( $input, value, isLiveValidation ) => {
4
5
  return Promise.resolve({
5
6
  $input,
@@ -2,6 +2,7 @@ import isEmpty from './Tools/IsEmpty';
2
2
  import { addValidator } from './index';
3
3
  import { isRadioListChecked } from './Tools/RadioButton';
4
4
 
5
+
5
6
  /**
6
7
  * Required validation
7
8
  */
@@ -12,8 +13,8 @@ addValidator( 'required', '[required]', ( $input, value, isLiveValidation ) => {
12
13
  isValid = ($input as HTMLInputElement).checked;
13
14
  }
14
15
  else if ( ($input as HTMLInputElement).type === 'radio' ) {
15
- /** @ts-expect-error */
16
- isValid = isRadioListChecked( $input.__$radioGroup );
16
+
17
+ isValid = isRadioListChecked( ($input as FLib.Validator.CustomValidatorRadioInput).__$radioGroup );
17
18
  }
18
19
  else {
19
20
  isValid = !isEmpty( value );