PluginProbe
Booking Calendar / 11.0
Booking Calendar v11.0
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / __js / client / front_end_form / autofill_fields.js

autofill_fields.js in Booking Calendar 11.0, at includes/__js/client/front_end_form/autofill_fields.js

116 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( w ) {
2
3 'use strict';
4
5 if ( ! w.WPBC_FE ) {
6 w.WPBC_FE = {};
7 }
8
9 /**
10 * Auto-fill booking form fields (text/email) based on input "name" patterns.
11 *
12 * Form ID format: booking_form{resource_id}
13 * Skips date field: date_booking{resource_id}
14 *
15 * @param {number} resource_id Booking resource ID.
16 * @param {Object} fill_values Values to inject (strings).
17 *
18 * @return {boolean} True if form found and processed, false otherwise.
19 */
20 w.WPBC_FE.autofill_booking_form_fields = function( resource_id, fill_values ) {
21
22 resource_id = parseInt( resource_id, 10 ) || 0;
23 fill_values = fill_values || {};
24
25 var form_id = 'booking_form' + resource_id;
26 var date_name = 'date_booking' + resource_id;
27
28 var submit_form = document.getElementById( form_id );
29
30 if ( ! submit_form ) {
31 /* eslint-disable no-console */
32 console.error( 'WPBC: No booking form: ' + form_id );
33 /* eslint-enable no-console */
34 return false;
35 }
36
37 // Keep same regex rules and priority order as legacy inline JS.
38 var rules = array_rules( fill_values );
39
40 var elements = submit_form.elements || [];
41 var count = elements.length;
42 var el;
43 var i;
44 var j;
45
46 for ( i = 0; i < count; i++ ) {
47
48 el = elements[ i ];
49
50 if ( ! el || ! el.name ) {
51 continue;
52 }
53
54 // Only text/email inputs.
55 if ( ( el.type !== 'text' ) && ( el.type !== 'email' ) ) {
56 continue;
57 }
58
59 // Skip date field.
60 if ( el.name === date_name ) {
61 continue;
62 }
63
64 // Fill only empty values (legacy behavior: == "").
65 if ( el.value !== '' ) {
66 continue;
67 }
68
69 for ( j = 0; j < rules.length; j++ ) {
70
71 if ( rules[ j ].re.test( el.name ) ) {
72
73 if ( rules[ j ].val !== '' ) {
74 el.value = rules[ j ].val;
75 }
76
77 break; // Stop at first matching rule (priority).
78 }
79 }
80 }
81
82 return true;
83 };
84
85 /**
86 * Build rules array for autofill.
87 *
88 * @param {Object} fill_values Values to inject.
89 *
90 * @return {Array} Rules list.
91 */
92 function array_rules( fill_values ) {
93
94 // Normalize to strings (prevent "undefined" in fields).
95 var nickname = ( fill_values.nickname != null ) ? String( fill_values.nickname ) : '';
96 var last_name = ( fill_values.last_name != null ) ? String( fill_values.last_name ) : '';
97 var first_name = ( fill_values.first_name != null ) ? String( fill_values.first_name ) : '';
98 var email = ( fill_values.email != null ) ? String( fill_values.email ) : '';
99 var phone = ( fill_values.phone != null ) ? String( fill_values.phone ) : '';
100 var nb_enfant = ( fill_values.nb_enfant != null ) ? String( fill_values.nb_enfant ) : '';
101 var url = ( fill_values.url != null ) ? String( fill_values.url ) : '';
102
103 return [
104 { re: /^([A-Za-z0-9_\-\.])*(nickname){1}([A-Za-z0-9_\-\.])*$/, val: nickname },
105 { re: /^([A-Za-z0-9_\-\.])*(last|second){1}([_\-\.])?name([A-Za-z0-9_\-\.])*$/, val: last_name },
106 { re: /^name([0-9_\-\.])*$/, val: first_name },
107 { re: /^([A-Za-z0-9_\-\.])*(first|my){1}([_\-\.])?name([A-Za-z0-9_\-\.])*$/, val: first_name },
108 { re: /^(e)?([_\-\.])?mail([0-9_\-\.]*)$/, val: email },
109 { re: /^([A-Za-z0-9_\-\.])*(phone|fone){1}([A-Za-z0-9_\-\.])*$/, val: phone },
110 { re: /^(e)?([_\-\.])?nb_enfant([0-9_\-\.]*)$/, val: nb_enfant },
111 { re: /^([A-Za-z0-9_\-\.])*(URL|site|web|WEB){1}([A-Za-z0-9_\-\.])*$/, val: url }
112 ];
113 }
114
115 })( window );
116