// source --> https://notaria42.cl/wp-content/plugins/appointment-booking/frontend/resources/js/bookly.js?ver=15.1 (function($) { window.bookly = function(Options) { var $container = $('#bookly-form-' + Options.form_id), timeZone = typeof Intl === 'object' ? Intl.DateTimeFormat().resolvedOptions().timeZone : undefined, timeZoneOffset = new Date().getTimezoneOffset(); Options.skip_steps.service = Options.skip_steps.service_part1 && Options.skip_steps.service_part2; // initialize if (Options.status.booking == 'finished') { stepComplete(); } else if (Options.status.booking == 'cancelled') { stepPayment(); } else { stepService({new_chain : true}); } if (Options.facebook.enabled) { initFacebookLogin(); } /** * Service step. */ function stepService(params) { if (Options.skip_steps.service) { if (!Options.skip_steps.extras) { stepExtras(params) } else { stepTime(params); } return; } var data = { action : 'bookly_render_service', csrf_token : BooklyL10n.csrf_token, form_id : Options.form_id }; if (Options.use_client_time_zone) { data.time_zone = timeZone; data.time_zone_offset = timeZoneOffset; } $.extend(data, params); $.ajax({ url : Options.ajaxurl, data : data, dataType : 'json', xhrFields : { withCredentials: true }, crossDomain : 'withCredentials' in new XMLHttpRequest(), success : function (response) { if (response.success) { BooklyL10n.csrf_token = response.csrf_token; $container.html(response.html); if (params === undefined) { // Scroll when returning to the step Service. default value {new_chain : true} scrollTo($container); } var $chain_item_draft = $('.bookly-js-chain-item.bookly-js-draft', $container), $select_location = $('.bookly-js-select-location', $container), $select_category = $('.bookly-js-select-category', $container), $select_service = $('.bookly-js-select-service', $container), $select_employee = $('.bookly-js-select-employee', $container), $select_duration = $('.bookly-js-select-units-duration', $container), $select_nop = $('.bookly-js-select-number-of-persons', $container), $select_quantity = $('.bookly-js-select-quantity', $container), $date_from = $('.bookly-js-date-from', $container), $week_day = $('.bookly-js-week-day', $container), $select_time_from = $('.bookly-js-select-time-from', $container), $select_time_to = $('.bookly-js-select-time-to', $container), $next_step = $('.bookly-js-next-step', $container), $mobile_next_step = $('.bookly-js-mobile-next-step', $container), $mobile_prev_step = $('.bookly-js-mobile-prev-step', $container), locations = response.locations, categories = response.categories, services = response.services, staff = response.staff, chain = response.chain, last_chain_key = 0, category_selected = false ; // Init Pickadate. $date_from.pickadate({ formatSubmit : 'yyyy-mm-dd', format : Options.date_format, min : response.date_min || true, max : response.date_max || true, clear : false, close : false, today : BooklyL10n.today, monthsFull : BooklyL10n.months, weekdaysFull : BooklyL10n.days, weekdaysShort : BooklyL10n.daysShort, labelMonthNext : BooklyL10n.nextMonth, labelMonthPrev : BooklyL10n.prevMonth, firstDay : Options.start_of_week, onSet : function(timestamp) { if ($.isNumeric(timestamp.select)) { // Checks appropriate day of the week var date = new Date(timestamp.select); $('.bookly-js-week-day[value="' + (date.getDay() + 1) + '"]:not(:checked)', $container).attr('checked', true).trigger('change'); } } }); $('.bookly-js-go-to-cart', $container).on('click', function (e) { e.preventDefault(); ladda_start(this); stepCart({from_step : 'service'}); }); // insert data into select function setSelect($select, data, value) { // reset select $('option:not([value=""])', $select).remove(); // and fill the new data var docFragment = document.createDocumentFragment(); function valuesToArray(obj) { return Object.keys(obj).map(function (key) { return obj[key]; }); } function compare(a, b) { if (parseInt(a.pos) < parseInt(b.pos)) return -1; if (parseInt(a.pos) > parseInt(b.pos)) return 1; return 0; } // sort select by position data = valuesToArray(data).sort(compare); $.each(data, function(key, object) { var option = document.createElement('option'); option.value = object.id; option.text = object.name; docFragment.appendChild(option); }); $select.append(docFragment); // set default value of select if ($select.find('option[value="' + value + '"]').length) { $select.val(value); } } function setSelects($chain_item, location_id, category_id, service_id, staff_id) { var _location_id = (Options.attributes.location_custom_settings && location_id) ? location_id : 0 var _staff = {}, _services = {}, _categories = {}, _nop = {}, _max_capacity = null, _min_capacity = null; $.each(staff, function(id, staff_member) { if (!location_id || locations[location_id].staff.hasOwnProperty(id)) { if (!service_id) { if (!category_id) { _staff[id] = $.extend({}, staff_member); } else { $.each(staff_member.services, function(s_id) { if (services[s_id].category_id == category_id) { _staff[id] = $.extend({}, staff_member); return false; } }); } } else if (staff_member.services.hasOwnProperty(service_id)) { if (staff_member.services[service_id].locations.hasOwnProperty(_location_id)) { _min_capacity = _min_capacity ? Math.min(_min_capacity, staff_member.services[service_id].locations[_location_id].min_capacity) : staff_member.services[service_id].locations[_location_id].min_capacity; _max_capacity = _max_capacity ? Math.max(_max_capacity, staff_member.services[service_id].locations[_location_id].max_capacity) : staff_member.services[service_id].locations[_location_id].max_capacity; if ( staff_member.services[service_id].locations[_location_id].price != null) { _staff[id] = { id : id, name : staff_member.name + ' (' + staff_member.services[service_id].locations[_location_id].price + ')', pos : staff_member.pos }; } else { _staff[id] = { id : id, name : staff_member.name, pos : staff_member.pos }; } } } } }); if (!location_id) { _categories = categories; $.each(services, function(id, service) { if (!category_id || service.category_id == category_id) { if (!staff_id || staff[staff_id].services.hasOwnProperty(id)) { _services[id] = service; } } }); } else { var category_ids = [], service_ids = []; $.each(staff, function (st_id) { $.each(staff[st_id].services, function (s_id) { if (staff[st_id].services[s_id].locations.hasOwnProperty(_location_id)) { category_ids.push(services[s_id].category_id); service_ids.push(s_id); } }); }); $.each(categories, function(id, category) { if ($.inArray(parseInt(id), category_ids) > -1) { _categories[id] = category; } }); $.each(services, function(id, service) { if ($.inArray(id, service_ids) > -1) { if (!category_id || service.category_id == category_id) { if (!staff_id || staff[staff_id].services.hasOwnProperty(id)) { _services[id] = service; } } } }); } var nop = $('.bookly-js-select-number-of-persons', $chain_item).val() || 1; var max_capacity = service_id ? (staff_id ? (staff[staff_id].services[service_id].locations.hasOwnProperty(_location_id) ? staff[staff_id].services[service_id].locations[_location_id].max_capacity : 1 ) : _max_capacity ? _max_capacity : 1) : 1; var min_capacity = service_id ? (staff_id ? (staff[staff_id].services[service_id].locations.hasOwnProperty(_location_id) ? staff[staff_id].services[service_id].locations[_location_id].min_capacity : 1 ) : _min_capacity ? _min_capacity : 1) : 1; for (var i = min_capacity; i <= max_capacity; ++ i) { _nop[i] = { id: i, name: i, pos: i }; } if (nop > max_capacity) { nop = max_capacity; } if (nop < min_capacity || !Options.attributes.show_number_of_persons) { nop = min_capacity; } // Add ratings to staff names if (Options.attributes.show_ratings) { $.each(staff, function (id, staff_member) { if (_staff.hasOwnProperty(staff_member.id)) { if (service_id) { if (staff_member.services.hasOwnProperty(service_id) && staff_member.services[service_id].rating) { _staff[staff_member.id].name = '★' + staff_member.services[service_id].rating + ' ' + _staff[staff_member.id].name; } } else if (staff_member.rating) { _staff[staff_member.id].name = '★' + staff_member.rating + ' ' + _staff[staff_member.id].name; } } }); } setSelect($chain_item.find('.bookly-js-select-category'), _categories, category_id); setSelect($chain_item.find('.bookly-js-select-service'), _services, service_id); setSelect($chain_item.find('.bookly-js-select-employee'), _staff, staff_id); setSelect($chain_item.find('.bookly-js-select-number-of-persons'), _nop, nop); } $container.off('click').off('change'); // Location select change $container.on('change', '.bookly-js-select-location', function () { var $chain_item = $(this).closest('.bookly-js-chain-item'), location_id = this.value, category_id = $chain_item.find('.bookly-js-select-category').val(), service_id = $chain_item.find('.bookly-js-select-service').val(), staff_id = $chain_item.find('.bookly-js-select-employee').val() ; // Validate selected values. if (location_id) { var _location_id = Options.attributes.location_custom_settings ? location_id : 0; if (staff_id) { if (!locations[location_id].staff.hasOwnProperty(staff_id)) { staff_id = ''; } else if (service_id && !staff[staff_id].services[service_id].locations.hasOwnProperty(_location_id)) { staff_id = ''; } } if (service_id) { var valid = false; $.each(locations[location_id].staff, function(id) { if (staff[id].services.hasOwnProperty(service_id)) { valid = true; return false; } }); if (!valid) { service_id = ''; } } if (category_id) { var valid = false; $.each(locations[location_id].staff, function(id) { $.each(staff[id].services, function(s_id) { if (services[s_id].category_id == category_id) { valid = true; return false; } }); if (valid) { return false; } }); if (!valid) { category_id = ''; } } } setSelects($chain_item, location_id, category_id, service_id, staff_id); }); // Category select change $container.on('change', '.bookly-js-select-category', function () { var $chain_item = $(this).closest('.bookly-js-chain-item'), location_id = $chain_item.find('.bookly-js-select-location').val(), category_id = this.value, service_id = $chain_item.find('.bookly-js-select-service').val(), staff_id = $chain_item.find('.bookly-js-select-employee').val() ; // Validate selected values. if (category_id) { category_selected = true; if (service_id) { if (services[service_id].category_id != category_id) { service_id = ''; } } if (staff_id) { var valid = false; $.each(staff[staff_id].services, function(id) { if (services[id].category_id == category_id) { valid = true; return false; } }); if (!valid) { staff_id = ''; } } } else { category_selected = false; } setSelects($chain_item, location_id, category_id, service_id, staff_id); }); function updateServiceDurationSelect($chain_item, service_id, staff_id, location_id) { var $units_duration = $chain_item.find('.bookly-js-select-units-duration'); $units_duration.find('option').remove(); if (service_id) { var getUnitsByStaffId = function (staff_id) { if (!staff_id) { return services[service_id]['units']; } var locationId = location_id ? location_id : 0, staffLocations = staff[staff_id].services[service_id].locations; if (!(staffLocations instanceof Array) || !staffLocations.length) { return []; } var staffLocation = staffLocations.indexOf(locationId) !== -1 ? staffLocations[locationId] : staffLocations[0]; return staffLocation.units || []; }; // add slots for picked service $.each(getUnitsByStaffId(staff_id), function (i, item) { $units_duration.append($('