Skip to content

Instantly share code, notes, and snippets.

@yesnik
Created November 16, 2017 06:57
Show Gist options
  • Save yesnik/dcd5636382cf0e3e2544cc5ec603379e to your computer and use it in GitHub Desktop.
Save yesnik/dcd5636382cf0e3e2544cc5ec603379e to your computer and use it in GitHub Desktop.
Jquery modules
// We need to understand how to organise this piece of code better
var Delivery = (function () {
var selectedCityName,
$textDelivery,
$textNoDelivery;
function init() {
initElements();
initVars();
initListeners();
initInterface();
}
function initElements() {
$textDelivery = $('.js-text-delivery');
$textNoDelivery = $('.js-text-no-delivery');
}
function initVars() {
var cityName;
if (Ubrr.page == 'index') {
cityName = Ubrr.geoipCityName;
} else {
cityName = localStorage.getItem('selectedCityName');
}
setSelectedCityName(cityName);
}
function initInterface() {
updateDeviveryTextVisibility();
}
function setSelectedCityName(cityName) {
selectedCityName = cityName;
setLocalStorage('selectedCityName', cityName);
}
function onCitySelect(_event, data) {
setSelectedCityName(data.cityName);
}
function onOfficeSelect(_event, data) {
updateDeviveryTextVisibility();
}
function updateDeviveryTextVisibility() {
if (needShowDeliveryText(selectedCityName)) {
showDeliveryText();
} else {
hideDeliveryText();
}
}
function setLocalStorage(key, value) {
localStorage.setItem(key, value);
}
function getLocalStorage(key) {
localStorage.getItem(key);
}
function needShowDeliveryText(selectedCityName) {
return $.inArray(selectedCityName, Ubrr.deliveryCityNames) !== -1;
}
function showDeliveryText() {
$textDelivery.show();
$textNoDelivery.hide();
}
function hideDeliveryText() {
$textDelivery.hide();
$textNoDelivery.show();
}
function initListeners() {
$('body').on({
'modalCityOfficeChoice.city.selected': onCitySelect,
'modalCityOfficeChoice.office.selected': onOfficeSelect
});
}
return {
init: init
}
}());
$(function () {
FixedFooter.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment