/**
* Swastarkencl is private software: you CANNOT redistribute it, sell it and/or
* modify it under or in any form without written authorization of its owner / developer.
*
* Swastarkencl is distributed / sell in the hope that it will be useful to you.
*
* You should have received a copy of its License along with Swastarkencl.
* If not, see https://www.softwareagil.com.
*/
// ATTENTION: Select2 is a problematic library with Starken's functionalies. It was removed intentionally!
jQuery(document).ready(function($) {
var Swastarkencl = {
current_state_agencies_list: null,
current_state_selected: null,
current_rut_entered: null,
current_agency_selected: null,
trigger_list_communes_change_event: true,
previous_selected_commune: null,
selected_commune_city_name: null,
selected_commune_agency_name: null,
loading_agencies: false,
selected_state_need_agencies: true,
current_shipping_city_value: null,
DEBUG_MODE: false,
scroll_to: function (element, callback) {
$([document.documentElement, document.body]).animate({
scrollTop: element.offset().top - 50
}, 500, callback);
},
load_agencies_by_state: function (state_id) {
Swastarkencl.current_state_selected = state_id;
$('#swastarkencl-list-of-agencies').addClass('swastarkencl-loading-agencies');
$('#swastarkencl-list-of-agencies').empty();
$.ajax({
type : "POST",
dataType : "json",
url : swastarkencl.url,
data : {
action: swastarkencl.commune_agencies_from_api_action,
state_id: state_id,
nonce: swastarkencl.nonce
},
beforeSend: function() {
$('#swastarkencl-loading-indicator').show();
Swastarkencl.loading_agencies = true;
},
success: Swastarkencl.handleAgenciesResponse
});
},
handleAgenciesResponse: function (response) {
var active_agencies = 0;
Swastarkencl.selected_state_need_agencies = true;
Swastarkencl.loading_agencies = false;
if (
(response != null && response.agencies.length == 0)
|| (response != null && response.city != null && response.city.destino_indirecto)
) {
Swastarkencl.selected_state_need_agencies = false;
Swastarkencl.selected_commune_city_name = response.city !== null ?? response.city.name;
Swastarkencl.save_customer_agency();
return;
}
Swastarkencl.selected_commune_city_name = response != null ?? response.city.name;
// Set state agencies holder to use in other parts
Swastarkencl.current_state_agencies_list = [];
Swastarkencl.current_state_agencies_list[Swastarkencl.current_state_selected] = response;
if (response != null && response.agencies.length > 0) {
// Visibility of agencies wrappers
$('#swastarkencl-agencies-wrapper-row').show();
$('#swastarkencl-agency-details-wrapper').hide();
$('#swastarkencl-list-of-agencies').append($('')
.insertBefore($('#swastarkencl-list-of-regions option:first-child'));
}
if ($('#swastarkencl-list-of-regions').data('current-region-id') == '') {
$('#swastarkencl-list-of-regions').prop("selectedIndex", 0);
}
// Select first commune option
if ($('#swastarkencl-list-of-communes').data('placeholder') !== undefined) {
$('')
.insertBefore($('#swastarkencl-list-of-communes option:first-child'));
}
if ($('#swastarkencl-list-of-communes').data('swastarkencl-current-cummune-id') == '') {
$('#swastarkencl-list-of-communes').prop("selectedIndex", 0);
}
// Set data-region-dls attribute at load because enable_region_commune_in_checkout_fields is yes
let communeRegion = $('#swastarkencl-list-of-communes').data('commune-region-dls-code');
$('#swastarkencl-list-of-communes option').each((i, o) => {
if (communeRegion !== undefined) {
if ($('#swastarkencl-list-of-regions').data('current-region-id') == communeRegion[o.value]) {
$(o).prop('hidden', false);
} else {
$(o).prop('hidden', true);
}
if (communeRegion[o.value] != undefined) {
$(o).attr('data-region-dls', communeRegion[o.value]);
}
}
});
}
},
run: function() {
$('#swastarkencl-list-of-communes, #swastarkencl-list-of-regions').addClass('form-control');
// Fire on update_checkout
$( document.body ).on( 'updated_checkout', (data) => {
if (document.querySelector('#swastarkencl-rating-message')) {
document.querySelector('#swastarkencl-rating-message').style.display = 'none';
}
});
// TODO: check if these line are already unnecessary
Swastarkencl.hideLodingIndicator();
if (swastarkencl.hide_starken_section_in_cart === 'yes' && swastarkencl.is_cart === '1') {
return;
}
if (swastarkencl.hide_deliver_to_agencies_options === 'yes') {
Swastarkencl.add_state_listener(true);
} else {
Swastarkencl.log('As agencies are enabled, listeners are added to states and agencies');
Swastarkencl.add_state_listener();
Swastarkencl.add_agency_listener();
}
Swastarkencl.regionAndCommuneInCheckoutFields();
if ($('[name=billing_rut]').val() === '') {
$('#swastarkencl-list-of-regions').prop("selectedIndex", 0);
}
// At load, hidden all previous shipping options
document.querySelectorAll(`
input[value='NORMAL-AGENCIA-3'],
input[value='NORMAL-DOMICILIO-3'],
input[value='EXPRESS-AGENCIA-3'],
input[value='EXPRESS-DOMICILIO-3'],
input[value='NORMAL-AGENCIA-2'],
input[value='NORMAL-DOMICILIO-2'],
input[value='EXPRESS-AGENCIA-2'],
input[value='EXPRESS-DOMICILIO-2'],
input[value='EXPRESO-AGENCIA-3'],
input[value='EXPRESO-DOMICILIO-3'],
input[value='EXPRESO-AGENCIA-2'],
input[value='EXPRESO-DOMICILIO-2'],
input[value='NORMAL-SUCURSAL-3'],
input[value='EXPRESS-SUCURSAL-3'],
input[value='NORMAL-SUCURSAL-2'],
input[value='EXPRESS-SUCURSAL-2'],
input[value='EXPRESO-SUCURSAL-3'],
input[value='EXPRESO-SUCURSAL-2']
`).forEach((i, o) => {
if (i !== undefined) {
i.parentNode.style.display = 'none';
}
});
if ($("[name='calc_shipping']").length === 0) {
// At load
Swastarkencl.tryToListShippingOptions();
}
setInterval(() => {
var selected_shipping_option = $('.woocommerce-shipping-methods input[type=radio]:checked');
if (selected_shipping_option.length > 0) {
const agency_needed = (
selected_shipping_option.val().toString().toLowerCase().split('-')[1] == 'agencia'
|| selected_shipping_option.val().toString().toLowerCase().split('-')[1] == 'sucursal'
);
if (Swastarkencl.current_state_agencies_list && Swastarkencl.current_state_selected) {
var state_id = $('#swastarkencl-list-of-communes').find('option:selected').val();
if (
$(document).find('#swastarkencl-list-of-agencies option').length == 0
&& !Swastarkencl.loading_agencies
) {
Swastarkencl.handleAgenciesResponse(
Swastarkencl.current_state_agencies_list[Swastarkencl.current_state_selected]
);
}
}
}
Swastarkencl.swastarkencl_on_arrival_payment_message();
}, 2000);
// TODO: check if these lines are already unnecessary
setInterval(() => {
// hide loading indicator if not loading agencies
if (
(Swastarkencl.current_state_agencies_list != null && Swastarkencl.current_state_agencies_list.length > 0)
&& !Swastarkencl.loading_agencies
|| (!Swastarkencl.selected_state_need_agencies && !Swastarkencl.loading_agencies)
) {
Swastarkencl.hideLodingIndicator();
}
}, 5000);
$(document).on('DOMSubtreeModified', '#order_review', function() {
Swastarkencl.handler_placing_order_button();
});
$(document).on('change', '#swastarkencl-list-of-regions', function(event) {
// reset commune select
$('#swastarkencl-list-of-communes').prop("selectedIndex", 0);
$('#swastarkencl-list-of-agencies').prop("selectedIndex", 0);
$('#swastarkencl-list-of-communes').children().each((i, o) => {
if ($(o).attr('value') != '') {
if ($(o).data('region-dls') == event.target.options[event.target.selectedIndex].value) {
$(o).prop('hidden', false);
$(o).css('display', 'inline-block');
} else {
$(o).prop('hidden', true);
$(o).css('display', 'none');
}
}
});
$('#swastarkencl-list-of-communes, #swastarkencl-list-of-communes-label').css({
'display': 'block',
'width': '100%'
});
});
Swastarkencl.current_agency_selected = $('#swastarkencl-list-of-agencies').data('selected-agency');
Swastarkencl.current_state_selected = $('#swastarkencl-list-of-communes').data('swastarkencl-current-cummune-id');
if (Swastarkencl.current_agency_selected && Swastarkencl.current_state_selected) {
Swastarkencl.log('At first load, if there is an previous selected agency and state, they are saved');
$('#swastarkencl-loading-indicator').show();
Swastarkencl.save_customer_agency();
}
},
log: function() {
if (Swastarkencl.DEBUG_MODE) {
console.log(...arguments);
}
}
};
Swastarkencl.run();
});