PluginProbe
Booking Calendar / 11.8.4
Booking Calendar v11.8.4
11.8.4 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 All 204 releases
booking / js / wpbc-free-date-hints.js

wpbc-free-date-hints.js in Booking Calendar 11.8.4, at js/wpbc-free-date-hints.js

234 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Free date hints runtime.
3 *
4 * Mirrors the public Pro cost-hint trigger names when they are not already
5 * present, so existing integrations can call the same functions.
6 */
7 // FixIn: 10.15.6.2.
8 (function ( w, $ ) {
9 'use strict';
10
11 var knownHints = [
12 'check_in_date_hint',
13 'check_out_date_hint',
14 'selected_dates_hint',
15 'days_number_hint',
16 'start_time_hint',
17 'end_time_hint'
18 ];
19
20 w.wpbc_free_date_hints__apply = function ( resourceId, hints ) {
21 resourceId = parseInt( resourceId, 10 ) || 1;
22 hints = hints || {};
23
24 $.each( hints, function ( hintName, hintValue ) {
25 var valueText = $( '<div />' ).html( hintValue ).text();
26
27 $( '#' + hintName + '_tip' + resourceId + ',.' + hintName + '_tip' + resourceId ).html( hintValue );
28 $( '#' + hintName + resourceId ).val( valueText );
29
30 if ( typeof w.wpbc__spin_loader__micro__hide === 'function' ) {
31 w.wpbc__spin_loader__micro__hide( hintName + '_tip' + resourceId );
32 }
33 } );
34 };
35
36 if ( typeof w.wpbc_send_ajax__show_cost_hints !== 'function' ) {
37 w.wpbc_send_ajax__show_cost_hints = function ( resourceId ) {
38 var allDates,
39 formData,
40 myBookingFormName = '',
41 nonceEl,
42 inst;
43
44 resourceId = parseInt( resourceId, 10 ) || 1;
45
46 $( '.booking_form_div' ).trigger( 'before_show_cost_hints', [ resourceId ] );
47
48 if ( document.getElementById( 'parent_of_additional_calendar' + resourceId ) !== null ) {
49 resourceId = parseInt( document.getElementById( 'parent_of_additional_calendar' + resourceId ).value, 10 ) || resourceId;
50 }
51
52 if ( 0 === $( '#booking_form' + resourceId ).find( '.wpbc_free_date_hint' ).length ) {
53 return false;
54 }
55
56 allDates = $( '#date_booking' + resourceId ).val() || '';
57
58 if (
59 'undefined' !== typeof _wpbc &&
60 'dynamic' === _wpbc.calendar__get_param_value( resourceId, 'days_select_mode' )
61 ) {
62 inst = ( typeof w.wpbc_calendar__get_inst === 'function' ) ? w.wpbc_calendar__get_inst( resourceId ) : null;
63 if ( null !== inst && inst.stayOpen === true ) {
64 $( '.wpbc_free_date_hint' ).html( '...' );
65 return false;
66 }
67 }
68
69 $.each( knownHints, function ( index, hintName ) {
70 var hintId = hintName + '_tip' + resourceId;
71
72 if ( typeof w.wpbc__spin_loader__micro__show__inside === 'function' ) {
73 w.wpbc__spin_loader__micro__show__inside( hintId, '#' + hintId + ',.' + hintId );
74 } else {
75 $( '#' + hintId + ',.' + hintId ).html( '...' );
76 }
77 } );
78
79 if ( document.getElementById( 'booking_form_type' + resourceId ) !== null ) {
80 myBookingFormName = document.getElementById( 'booking_form_type' + resourceId ).value;
81 }
82
83 nonceEl = document.getElementById( 'wpbc_nonceCALCULATE_THE_COST' + resourceId );
84 if ( ! nonceEl ) {
85 return false;
86 }
87
88 formData = wpbc_free_date_hints__get_form_fields_as_string( resourceId );
89
90 $.ajax( {
91 url: w.wpbc_url_ajax,
92 type: 'POST',
93 success: function ( data, textStatus ) {
94 if ( 'success' === textStatus ) {
95 $( '#ajax_respond_insert' + resourceId ).html( data );
96 }
97 $( '.booking_form_div' ).trigger( 'after_show_cost_hints', [ resourceId ] );
98 },
99 error: function ( XMLHttpRequest, textStatus ) {
100 w.status = 'Ajax sending Error status:' + textStatus;
101 if ( w.console && w.console.error ) {
102 w.console.error( XMLHttpRequest.status + ' ' + XMLHttpRequest.statusText );
103 }
104 },
105 data: {
106 action: 'CALCULATE_THE_COST',
107 form: formData,
108 all_dates: allDates,
109 bk_type: resourceId,
110 booking_form_type: myBookingFormName,
111 wpdev_active_locale: ( 'undefined' !== typeof _wpbc ) ? _wpbc.get_other_param( 'locale_active' ) : '',
112 wpbc_nonce: nonceEl.value
113 }
114 } );
115
116 $( '.booking_form_div' ).trigger( 'show_cost_hints', [ resourceId ] );
117 $( '.booking_form_div' ).trigger( 'wpbc_booking_date_or_option_selected', [ resourceId ] );
118
119 return false;
120 };
121 }
122
123 if ( typeof w.wpbc_show_cost_hints_after_few_seconds !== 'function' ) {
124 w.wpbc_show_cost_hints_after_few_seconds = (function () {
125 var closedTimer = 0;
126
127 return function ( resourceId, timerDelay ) {
128 timerDelay = typeof timerDelay !== 'undefined' ? timerDelay : 250;
129 clearTimeout( closedTimer );
130 closedTimer = setTimeout( w.wpbc_send_ajax__show_cost_hints.bind( null, resourceId ), timerDelay );
131 };
132 }());
133 }
134
135 if ( typeof w.showCostHintInsideBkForm !== 'function' ) {
136 w.showCostHintInsideBkForm = function ( resourceId ) {
137 w.wpbc_show_cost_hints_after_few_seconds( resourceId );
138 };
139 }
140
141 $( function () {
142 $( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function ( event, loadedResourceId ) {
143 w.wpbc_show_cost_hints_after_few_seconds( loadedResourceId );
144 } );
145
146 // Resource Selector and other workflows can insert native forms after document readiness.
147 $( document ).on(
148 'date_selected.wpbc_free_date_hints',
149 '.booking_form_div',
150 function ( event, resourceId ) {
151 w.wpbc_show_cost_hints_after_few_seconds( resourceId );
152 }
153 );
154
155 $( document ).on( 'change', '.booking_form_div input, .booking_form_div select, .booking_form_div textarea', function () {
156 var form = $( this ).closest( 'form.booking_form' ),
157 resourceId;
158
159 if ( ! form.length || /^date_booking/.test( this.name || '' ) ) {
160 return;
161 }
162
163 resourceId = parseInt( form.attr( 'id' ).replace( 'booking_form', '' ), 10 );
164 if ( resourceId > 0 ) {
165 w.wpbc_show_cost_hints_after_few_seconds( resourceId );
166 }
167 } );
168 } );
169
170 function wpbc_free_date_hints__get_form_fields_as_string( resourceId ) {
171 var form = document.getElementById( 'booking_form' + resourceId ),
172 formData = '',
173 count,
174 i,
175 element,
176 elementType,
177 inputValue;
178
179 if ( ! form || ! form.elements ) {
180 return '';
181 }
182
183 count = form.elements.length;
184 for ( i = 0; i < count; i++ ) {
185 element = form.elements[ i ];
186
187 if ( ! element || ! element.name ) {
188 continue;
189 }
190
191 if (
192 'button' === element.type ||
193 'hidden' === element.type ||
194 element.name === ( 'date_booking' + resourceId ) ||
195 element.name === ( 'captcha_input' + resourceId )
196 ) {
197 continue;
198 }
199
200 elementType = element.type;
201
202 if ( 'checkbox' === element.type ) {
203 inputValue = element.checked ? element.value : '';
204 } else if ( 'radio' === element.type ) {
205 if ( ! element.checked ) {
206 continue;
207 }
208 inputValue = element.value;
209 } else {
210 inputValue = $( element ).val();
211 }
212
213 if ( 'selectbox-multiple' === element.type || 'select-multiple' === element.type ) {
214 inputValue = $( '[name="' + element.name + '"]' ).val();
215 if ( inputValue === null || inputValue.toString() === '' ) {
216 inputValue = '';
217 }
218 }
219
220 elementType = ( element.className.indexOf( 'wpdev-validates-as-email' ) !== -1 ) ? 'email' : elementType;
221 elementType = ( element.className.indexOf( 'wpdev-validates-as-coupon' ) !== -1 ) ? 'coupon' : elementType;
222 elementType = ( 'select-one' === elementType ) ? 'selectbox-one' : elementType;
223 elementType = ( 'select-multiple' === elementType ) ? 'selectbox-multiple' : elementType;
224
225 if ( formData !== '' ) {
226 formData += '~';
227 }
228 formData += elementType + '^' + element.name + '^' + inputValue;
229 }
230
231 return formData;
232 }
233 }( window, jQuery ));
234