@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.
- package/CHANGELOG.md +29 -0
- package/DOM/Class.ts +10 -12
- package/DOM/DocumentSize.ts +3 -1
- package/DOM/Filter.ts +0 -2
- package/DOM/Index.ts +1 -3
- package/DOM/Manipulation.ts +0 -3
- package/DOM/Matrix.ts +12 -12
- package/DOM/Offset.ts +4 -5
- package/DOM/OuterSize.ts +12 -12
- package/DOM/PageToDOM.ts +2 -2
- package/DOM/Position.ts +3 -3
- package/DOM/Size.ts +3 -7
- package/DOM/StrToDOM.ts +0 -1
- package/DOM/Styles.ts +3 -12
- package/DOM/Traversing.ts +0 -6
- package/DOM/WindowScroll.ts +3 -1
- package/DOM/WindowSize.ts +3 -1
- package/DOM/Wrap.ts +0 -1
- package/Events/DeviceOrientation.ts +27 -31
- package/Events/EventsManager.ts +10 -25
- package/Events/Gesture.ts +36 -37
- package/Events/HistoryController.ts +12 -18
- package/Events/ImageLoad.ts +35 -19
- package/Events/IntersectObserver.ts +15 -19
- package/Events/KeyboardHandler.ts +10 -8
- package/Events/MediaQueriesEvents.ts +53 -51
- package/Events/OnAnimationEnd.ts +2 -3
- package/Events/OnTransitionEnd.ts +3 -3
- package/Events/PubSub.ts +5 -8
- package/Events/TouchHover.ts +18 -21
- package/Events/WindowEvents.ts +26 -30
- package/Helpers/Colors.ts +7 -23
- package/Helpers/Cookie.ts +5 -12
- package/Helpers/Coordinates.ts +0 -7
- package/Helpers/Debounce.ts +3 -7
- package/Helpers/Defer.ts +1 -1
- package/Helpers/GetValue.ts +0 -2
- package/Helpers/Insert.ts +0 -1
- package/Helpers/Sequential.ts +0 -2
- package/Helpers/Slice.ts +0 -3
- package/Helpers/Throttle.ts +0 -1
- package/Helpers/TransitionHelpers.ts +8 -6
- package/Helpers/Type.ts +1 -14
- package/Helpers/Unique.ts +3 -3
- package/Helpers/UrlParser.ts +14 -23
- package/Helpers/Wait.ts +1 -1
- package/Modules/Accordion/Tab.ts +19 -17
- package/Modules/Accordion/index.ts +27 -19
- package/Modules/Autocomplete.ts +95 -101
- package/Modules/DragSlider.ts +72 -63
- package/Modules/GlobalState.ts +60 -53
- package/Modules/LoadGMap.ts +4 -7
- package/Modules/Notifications/Notification.ts +28 -32
- package/Modules/Notifications/index.ts +43 -36
- package/Modules/Popin/Popin.ts +100 -110
- package/Modules/Popin/PopinAccessibility.ts +8 -8
- package/Modules/Popin/PopinBackground.ts +12 -12
- package/Modules/Popin/PopinController.ts +24 -30
- package/Modules/Popin/Tools.ts +18 -19
- package/Modules/QuickTemplate.ts +4 -6
- package/Modules/ScrollSnap.ts +110 -104
- package/Modules/SkinCheckbox.ts +52 -38
- package/Modules/SkinFile.ts +63 -53
- package/Modules/SkinRadio.ts +63 -43
- package/Modules/SkinSelect.ts +117 -101
- package/Modules/Slider/Slide.ts +49 -52
- package/Modules/Slider/Slider.ts +100 -136
- package/Modules/Slider/SliderControls.ts +60 -59
- package/Modules/Tabs/Tab.ts +28 -24
- package/Modules/Tabs/index.ts +36 -26
- package/Modules/Validator/Date.ts +2 -1
- package/Modules/Validator/Equals.ts +1 -1
- package/Modules/Validator/Internal/Input.ts +366 -0
- package/Modules/Validator/Internal/InputValidator.ts +99 -0
- package/Modules/Validator/Internal/ValidatorFunctionsController.ts +58 -0
- package/Modules/Validator/Max.ts +1 -1
- package/Modules/Validator/Min.ts +1 -1
- package/Modules/Validator/MultiRequired.ts +5 -5
- package/Modules/Validator/Number.ts +1 -0
- package/Modules/Validator/Pattern.ts +1 -1
- package/Modules/Validator/Recaptcha.ts +1 -0
- package/Modules/Validator/Required.ts +3 -2
- package/Modules/Validator/ServerCheck.ts +23 -15
- package/Modules/Validator/Tools/GetQueryFromForm.ts +1 -6
- package/Modules/Validator/Tools/IsDate.ts +2 -3
- package/Modules/Validator/Tools/IsEmail.ts +2 -5
- package/Modules/Validator/Tools/IsEmpty.ts +0 -2
- package/Modules/Validator/Tools/IsNumber.ts +0 -2
- package/Modules/Validator/Tools/IsUrl.ts +1 -3
- package/Modules/Validator/Tools/Label.ts +5 -15
- package/Modules/Validator/Tools/RadioButton.ts +4 -10
- package/Modules/Validator/Url.ts +1 -0
- package/Modules/Validator/index.ts +36 -36
- package/Modules/YouTubePlayer.ts +18 -16
- package/README.md +1 -1
- package/Types/Accordion.d.ts +41 -0
- package/Types/Autocomplete.d.ts +94 -0
- package/Types/DOM.d.ts +69 -0
- package/Types/DragSlider.d.ts +56 -0
- package/Types/EventsHelpers.d.ts +390 -0
- package/Types/GLImageTransition.d.ts +47 -43
- package/Types/GlobalState.d.ts +9 -0
- package/Types/Helpers.d.ts +63 -0
- package/Types/Notifications.d.ts +48 -44
- package/Types/Popin.d.ts +113 -0
- package/Types/ScrollSnap.d.ts +66 -0
- package/Types/SkinCheckbox.d.ts +35 -0
- package/Types/SkinFile.d.ts +45 -0
- package/Types/SkinRadio.d.ts +37 -0
- package/Types/SkinSelect.d.ts +72 -0
- package/Types/Slider.d.ts +106 -0
- package/Types/Tabs.d.ts +33 -0
- package/Types/Validator.d.ts +99 -0
- package/Types/YouTubePlayer.d.ts +27 -0
- package/Types/index.d.ts +22 -21
- package/WebGL/GLImageTransition/GLImageTransition.ts +83 -80
- package/WebGL/GLImageTransition/Presets/Cellular.ts +11 -11
- package/WebGL/GLImageTransition/Presets/Fade.ts +2 -3
- package/WebGL/GLImageTransition/Presets/HorizontalMovingGrid.ts +9 -9
- package/WebGL/GLImageTransition/Presets/PresetSample.ts +11 -9
- package/WebGL/GLImageTransition/Presets/Solarisation.ts +7 -12
- package/package.json +1 -1
- package/Modules/Template.ts +0 -209
- package/Types/AccordionTypes.d.ts +0 -29
- package/Types/AutocompleteTypes.d.ts +0 -85
- package/Types/DOMTypes.d.ts +0 -62
- package/Types/DragSliderTypes.d.ts +0 -52
- package/Types/EventsHelpersTypes.d.ts +0 -370
- package/Types/GlobalStateTypes.d.ts +0 -5
- package/Types/HelpersTypes.d.ts +0 -23
- package/Types/MediaPreloaderTypes.d.ts +0 -12
- package/Types/PopinTypes.d.ts +0 -83
- package/Types/ScrollSnapTypes.d.ts +0 -62
- package/Types/SkinCheckboxTypes.d.ts +0 -23
- package/Types/SkinFileTypes.d.ts +0 -34
- package/Types/SkinRadioTypes.d.ts +0 -24
- package/Types/SkinSelectTypes.d.ts +0 -48
- package/Types/SliderTypes.d.ts +0 -97
- package/Types/TabsTypes.d.ts +0 -22
- package/Types/ValidatorTypes.d.ts +0 -61
- 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();
|
package/Modules/Validator/Max.ts
CHANGED
|
@@ -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
|
});
|
package/Modules/Validator/Min.ts
CHANGED
|
@@ -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,
|
|
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 );
|
|
@@ -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' )
|
|
11
|
+
"isValid": value === '' || new RegExp( $input.getAttribute( 'pattern' ) as string ).test( value as string ),
|
|
12
12
|
"label": "pattern",
|
|
13
13
|
isLiveValidation
|
|
14
14
|
});
|
|
@@ -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
|
-
|
|
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 );
|