admin
1 week ago
front
1 week ago
actions.js
2 weeks ago
latecheckbox.js
1 year ago
lateselect.js
1 year ago
notifications.js
1 year ago
shared.js
1 year ago
time.js
1 year ago
shared.js
287 lines
| 1 | function latepoint_timestamped_ajaxurl(){ |
| 2 | let url = latepoint_helper.ajaxurl; |
| 3 | let timestamp = Date.now(); |
| 4 | |
| 5 | // Check if the URL already has GET parameters |
| 6 | if (url.includes('?')) { |
| 7 | return `${url}&t=${timestamp}`; |
| 8 | } else { |
| 9 | return `${url}?t=${timestamp}`; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | function latepoint_random_generator() { |
| 14 | var S4 = function () { |
| 15 | return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); |
| 16 | }; |
| 17 | return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()); |
| 18 | } |
| 19 | |
| 20 | function latepoint_validate_form($form) { |
| 21 | let errors = []; |
| 22 | $form.find('select[data-os-validate], input[data-os-validate], textarea[data-os-validate]').each(function () { |
| 23 | let validations = jQuery(this).data('os-validate').split(' '); |
| 24 | let $input = jQuery(this); |
| 25 | let label = $input.closest('.os-form-group').find('label').text(); |
| 26 | let field_has_errors = false; |
| 27 | if (validations) { |
| 28 | for (let i = 0; i < validations.length; i++) { |
| 29 | switch (validations[i]) { |
| 30 | case 'presence': |
| 31 | if($input.is(':checkbox')){ |
| 32 | if (!$input.is(':checked')) { |
| 33 | errors.push({message: label + ' ' + latepoint_helper.msg_validation_presence_checkbox}); |
| 34 | field_has_errors = true; |
| 35 | } |
| 36 | }else{ |
| 37 | if (!$input.val()) { |
| 38 | errors.push({message: label + ' ' + latepoint_helper.msg_validation_presence}); |
| 39 | field_has_errors = true; |
| 40 | } |
| 41 | } |
| 42 | break; |
| 43 | case 'phone': |
| 44 | if (!window.lp_intlTelInputGlobals.getInstance($input[0]).isValidNumber()) { |
| 45 | errors.push({message: label + ' ' + latepoint_helper.msg_validation_invalid}); |
| 46 | field_has_errors = true; |
| 47 | } |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | if (field_has_errors) { |
| 53 | $input.closest('.os-form-group').addClass('os-invalid'); |
| 54 | } else { |
| 55 | $input.closest('.os-form-group').removeClass('os-invalid'); |
| 56 | } |
| 57 | }); |
| 58 | return errors; |
| 59 | } |
| 60 | |
| 61 | function latepoint_create_form_data_from_non_form_element($elem) { |
| 62 | let formData = new FormData(); |
| 63 | // create objecte from all input fields that are inside of the element |
| 64 | let fields = $elem.find('select, input, textarea').serializeArray(); |
| 65 | if (fields) { |
| 66 | fields.forEach(field => formData.append(field.name, field.value)); |
| 67 | } |
| 68 | return formData; |
| 69 | } |
| 70 | |
| 71 | function latepoint_create_form_data($form, route_name = false, extra_params = false) { |
| 72 | let form_data = new FormData(); |
| 73 | let params = new FormData($form[0]); |
| 74 | |
| 75 | if (extra_params) { |
| 76 | Object.keys(extra_params).forEach(key => { |
| 77 | params.set(key, extra_params[key]); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | // get values from phone number fields |
| 82 | if (('lp_intlTelInputGlobals' in window) && ('lp_intlTelInputUtils' in window)) { |
| 83 | $form.find('input.os-mask-phone').each(function () { |
| 84 | const phoneInputName = this.getAttribute('name'); |
| 85 | const phoneInputValue = window.lp_intlTelInputGlobals.getInstance(this).getNumber(window.lp_intlTelInputUtils.numberFormat.E164); |
| 86 | // override value generated automatically by formdata with a formatted value of a phone field with country code |
| 87 | params.set(phoneInputName, phoneInputValue); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | form_data.append('params', latepoint_formdata_to_url_encoded_string(params)); |
| 92 | form_data.append('action', latepoint_helper.route_action); |
| 93 | form_data.append('route_name', route_name ? route_name : $form.data('route-name')); |
| 94 | form_data.append('layout', 'none'); |
| 95 | form_data.append('return_format', 'json'); |
| 96 | |
| 97 | let file_data; |
| 98 | // put file data into main form_data object, since we can't send them in "params" string |
| 99 | $form.find('input[type="file"]').each(function () { |
| 100 | file_data = this.files; // get multiple files from input file |
| 101 | let file_name = this.getAttribute("name"); |
| 102 | for (let i = 0; i < file_data.length; i++) { |
| 103 | form_data.append(file_name + '[]', file_data[i]); |
| 104 | } |
| 105 | }); |
| 106 | return form_data; |
| 107 | } |
| 108 | |
| 109 | function latepoint_mask_timefield($elem) { |
| 110 | if (jQuery().inputmask) { |
| 111 | $elem.inputmask({ |
| 112 | 'mask': '99:99', |
| 113 | 'placeholder': 'HH:MM' |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | function latepoint_formdata_to_url_encoded_string(form_data) { |
| 119 | let filtered_form_data = new FormData(); |
| 120 | // remove file fields from params, so we can serialize it into string, |
| 121 | // !important, this will not include file fields into the form_data, so you have to include them manually, see latepoint_create_form_data() that does it |
| 122 | // note: we don't use form_data.remove(key) on original object because we might want to preserve it |
| 123 | for (const [key, value] of form_data) { |
| 124 | if (value instanceof File) continue; |
| 125 | if (key.slice(-2) === '[]') { |
| 126 | // expecting array, append |
| 127 | filtered_form_data.append(key, value); |
| 128 | } else { |
| 129 | filtered_form_data.set(key, value); |
| 130 | } |
| 131 | } |
| 132 | return new URLSearchParams(filtered_form_data).toString(); |
| 133 | } |
| 134 | |
| 135 | function latepoint_mask_percent($elem) { |
| 136 | if (jQuery().inputmask) { |
| 137 | $elem.inputmask({ |
| 138 | 'alias': 'decimal', |
| 139 | 'radixPoint': latepoint_helper.decimal_separator, |
| 140 | 'digits': 4, |
| 141 | 'digitsOptional': false, |
| 142 | 'suffix': '%', |
| 143 | 'placeholder': '0', |
| 144 | 'rightAlign': false |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | function latepoint_mask_minutes($elem) { |
| 150 | if (jQuery().inputmask) { |
| 151 | $elem.inputmask({ |
| 152 | 'removeMaskOnSubmit': true, |
| 153 | 'alias': 'numeric', |
| 154 | 'digits': 0, |
| 155 | 'suffix': latepoint_helper.msg_minutes_suffix, |
| 156 | 'placeholder': '0', |
| 157 | 'rightAlign': false |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | function latepoint_mask_money($elem) { |
| 164 | if (jQuery().inputmask) { |
| 165 | $elem.inputmask({ |
| 166 | 'alias': 'currency', |
| 167 | 'groupSeparator': latepoint_helper.thousand_separator, |
| 168 | 'radixPoint': latepoint_helper.decimal_separator, |
| 169 | 'digits': latepoint_helper.number_of_decimals, |
| 170 | 'digitsOptional': false, |
| 171 | 'prefix': latepoint_helper.currency_symbol_before ? latepoint_helper.currency_symbol_before + ' ' : '', |
| 172 | 'suffix': latepoint_helper.currency_symbol_after ? ' ' + latepoint_helper.currency_symbol_after : '', |
| 173 | 'placeholder': '0', |
| 174 | 'rightAlign': false |
| 175 | }); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | function latepoint_mask_date($elem) { |
| 180 | if (jQuery().inputmask) { |
| 181 | $elem.inputmask({ |
| 182 | 'alias': 'datetime', |
| 183 | 'inputFormat': latepoint_helper.date_format_for_js |
| 184 | }); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | function latepoint_init_phone_masking_from_placeholder($input) { |
| 189 | if (!latepoint_helper.mask_phone_number_fields) return; |
| 190 | let format = $input.attr('placeholder'); |
| 191 | if (format && jQuery().inputmask) { |
| 192 | $input.inputmask(format.replace(/[0-9]/g, 9)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | function latepoint_mask_phone($elem) { |
| 197 | let jsElem = $elem[0]; |
| 198 | |
| 199 | // First priority is to prevent duplicates (common in non-document.body contexts) |
| 200 | if (jsElem && !window.lp_intlTelInputGlobals.getInstance(jsElem)) { |
| 201 | let dropdownContainer = document.body; |
| 202 | |
| 203 | let onlyCountries = JSON.parse(latepoint_helper.included_phone_countries); |
| 204 | // Remedy a quirk with json_encode(EMPTY_ARRAY) |
| 205 | if (onlyCountries.length === 1 && onlyCountries[0] === "") { |
| 206 | onlyCountries = []; |
| 207 | } |
| 208 | const preferredCountries = onlyCountries.length ? [] : window.lp_intlTelInputGlobals.defaults.preferredCountries; |
| 209 | |
| 210 | // remove country name in english and only use names in country language |
| 211 | var countryData = window.lp_intlTelInputGlobals.getCountryData(); |
| 212 | |
| 213 | for (var i = 0; i < countryData.length; i++) { |
| 214 | var country = countryData[i]; |
| 215 | country.name = country.name.replace(/ *\([^)]*\) */g, ""); |
| 216 | } |
| 217 | |
| 218 | let defaultCountryCode = latepoint_helper.default_phone_country; |
| 219 | if (onlyCountries.length && !onlyCountries.includes(defaultCountryCode)) { |
| 220 | defaultCountryCode = onlyCountries[0]; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | let iti = window.lp_intlTelInput(jsElem, { |
| 225 | dropdownContainer: dropdownContainer, |
| 226 | formatOnDisplay: true, |
| 227 | nationalMode: true, |
| 228 | autoPlaceholder: 'aggressive', |
| 229 | initialCountry: defaultCountryCode, |
| 230 | geoIpLookup: function (callback) { |
| 231 | const cookieName = 'latepoint_phone_country'; |
| 232 | |
| 233 | if (latepoint_has_cookie(cookieName)) { |
| 234 | callback(latepoint_get_cookie(cookieName)); |
| 235 | } else { |
| 236 | jQuery.get('https://ipinfo.io', function () { |
| 237 | }, 'jsonp').always(function (response) { |
| 238 | // Sensible default |
| 239 | let countryCode = defaultCountryCode; |
| 240 | |
| 241 | if (response && response.country) { |
| 242 | countryCode = response.country.toLowerCase(); |
| 243 | latepoint_set_cookie(cookieName, countryCode); |
| 244 | } |
| 245 | callback(countryCode); |
| 246 | }) |
| 247 | } |
| 248 | }, |
| 249 | allowDropdown: onlyCountries.length != 1, |
| 250 | onlyCountries: onlyCountries, |
| 251 | preferredCountries: preferredCountries, |
| 252 | separateDialCode: latepoint_helper.is_enabled_show_dial_code_with_flag |
| 253 | }); |
| 254 | |
| 255 | iti.promise.then(function () { |
| 256 | latepoint_init_phone_masking_from_placeholder($elem); |
| 257 | }); |
| 258 | |
| 259 | |
| 260 | $elem.on("countrychange", function (event) { |
| 261 | latepoint_init_phone_masking_from_placeholder(jQuery(this)); |
| 262 | }); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | function latepoint_show_booking_end_time() { |
| 267 | return (latepoint_helper.show_booking_end_time == 'yes'); |
| 268 | } |
| 269 | |
| 270 | function latepoint_set_cookie(name, value, days) { |
| 271 | let date = new Date; |
| 272 | date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * days); |
| 273 | document.cookie = name + "=" + value + ";path=/;expires=" + date.toGMTString(); |
| 274 | } |
| 275 | |
| 276 | function latepoint_get_cookie(name) { |
| 277 | let cookie = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)'); |
| 278 | return cookie ? cookie[2] : null; |
| 279 | } |
| 280 | |
| 281 | function latepoint_has_cookie(name) { |
| 282 | return latepoint_get_cookie(name) !== null; |
| 283 | } |
| 284 | |
| 285 | function latepoint_delete_cookie(name) { |
| 286 | latepoint_set_cookie(name, '', -1); |
| 287 | } |