init
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
define([
|
||||
'jquery',
|
||||
'mage/validation'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $form = $(element);
|
||||
var $submit = $form.find('[type="submit"]');
|
||||
var $status = $form.find('[data-role="application-status"]');
|
||||
|
||||
$form.validation();
|
||||
|
||||
$form.on('submit.agentApplication', function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
if (!$form.validation('isValid')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$submit.prop('disabled', true).addClass('disabled');
|
||||
$status.removeClass('success error').empty();
|
||||
|
||||
$.ajax({
|
||||
url: $form.attr('action'),
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: $form.serialize()
|
||||
}).done(function (response) {
|
||||
if (response.success) {
|
||||
$form[0].reset();
|
||||
$status.addClass('success').text(response.message);
|
||||
} else {
|
||||
$status.addClass('error').text(response.message);
|
||||
}
|
||||
}).fail(function (xhr) {
|
||||
var response = xhr.responseJSON || {};
|
||||
|
||||
$status.addClass('error').text(
|
||||
response.message || $.mage.__('Unable to submit your application. Please try again later.')
|
||||
);
|
||||
}).always(function () {
|
||||
$submit.prop('disabled', false).removeClass('disabled');
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,450 @@
|
||||
define([
|
||||
'jquery',
|
||||
'mage/translate'
|
||||
], function ($, $t) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $root = $(element);
|
||||
var agents = (config.data.agents || []).slice();
|
||||
var continents = config.data.continents || [];
|
||||
var continentByCode = {};
|
||||
var state = {
|
||||
continent: '',
|
||||
country: '',
|
||||
keyword: '',
|
||||
popupCountry: '',
|
||||
page: 1
|
||||
};
|
||||
var pageSize = 9;
|
||||
var $continent = $root.find('[data-role="continent-filter"]');
|
||||
var $country = $root.find('[data-role="country-filter"]');
|
||||
var $keyword = $root.find('[data-role="keyword-filter"]');
|
||||
var $popup = $root.find('[data-role="map-popup"]');
|
||||
|
||||
agents.sort(function (a, b) {
|
||||
return Number(a.sort_order) - Number(b.sort_order);
|
||||
});
|
||||
|
||||
continents.forEach(function (continent) {
|
||||
continentByCode[continent.code] = continent;
|
||||
});
|
||||
|
||||
$root.find('[data-role="map-image"]').attr('src', config.mapUrl);
|
||||
buildMarkers();
|
||||
buildContinentOptions();
|
||||
restoreStateFromUrl();
|
||||
buildCountryOptions();
|
||||
renderDirectory(false);
|
||||
bindEvents();
|
||||
|
||||
function agentsForContinent(code) {
|
||||
return agents.filter(function (agent) {
|
||||
return agent.continent === code;
|
||||
});
|
||||
}
|
||||
|
||||
function agentsForCountry(code) {
|
||||
return agents.filter(function (agent) {
|
||||
return agent.country_code === code;
|
||||
});
|
||||
}
|
||||
|
||||
function buildMarkers() {
|
||||
var $markers = $root.find('[data-role="map-markers"]').empty();
|
||||
|
||||
var countries = {};
|
||||
agents.forEach(function (agent) {
|
||||
if (agent.map_x === null || agent.map_y === null) {
|
||||
return;
|
||||
}
|
||||
if (!countries[agent.country_code]) {
|
||||
countries[agent.country_code] = {
|
||||
code: agent.country_code,
|
||||
label: agent.country,
|
||||
x: Number(agent.map_x),
|
||||
y: Number(agent.map_y),
|
||||
count: 0
|
||||
};
|
||||
}
|
||||
countries[agent.country_code].count++;
|
||||
});
|
||||
|
||||
Object.keys(countries).forEach(function (countryCode) {
|
||||
var country = countries[countryCode];
|
||||
$('<button/>', {
|
||||
type: 'button',
|
||||
'class': 'agent-map__marker',
|
||||
'data-country': country.code,
|
||||
'aria-label': $t('Show %1 agents in %2').replace('%1', country.count).replace('%2', country.label)
|
||||
}).css({
|
||||
'--marker-x': country.x + '%',
|
||||
'--marker-y': country.y + '%'
|
||||
}).append($('<span/>', {
|
||||
'class': 'agent-map__marker-core',
|
||||
'aria-hidden': 'true'
|
||||
})).appendTo($markers);
|
||||
});
|
||||
}
|
||||
|
||||
function buildContinentOptions() {
|
||||
continents.forEach(function (continent) {
|
||||
if (!agentsForContinent(continent.code).length) {
|
||||
return;
|
||||
}
|
||||
|
||||
$('<option/>', {
|
||||
value: continent.code,
|
||||
text: continent.label
|
||||
}).appendTo($continent);
|
||||
});
|
||||
}
|
||||
|
||||
function buildCountryOptions() {
|
||||
var selected = state.country;
|
||||
var countries = {};
|
||||
var countryLabel = $country.find('option:first').text();
|
||||
|
||||
agents.forEach(function (agent) {
|
||||
if (!state.continent || agent.continent === state.continent) {
|
||||
countries[agent.country_code] = agent.country;
|
||||
}
|
||||
});
|
||||
|
||||
$country.empty().append($('<option/>', {value: '', text: countryLabel}));
|
||||
Object.keys(countries).sort(function (a, b) {
|
||||
return countries[a].localeCompare(countries[b]);
|
||||
}).forEach(function (code) {
|
||||
$('<option/>', {value: code, text: countries[code]}).appendTo($country);
|
||||
});
|
||||
|
||||
if (selected && countries[selected]) {
|
||||
$country.val(selected);
|
||||
} else {
|
||||
state.country = '';
|
||||
$country.val('');
|
||||
}
|
||||
}
|
||||
|
||||
function openPopup(countryCode) {
|
||||
var countryAgents = agentsForCountry(countryCode);
|
||||
var featured = countryAgents.slice(0, 5);
|
||||
var $items = $root.find('[data-role="popup-agents"]').empty();
|
||||
|
||||
if (!featured.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.popupCountry = countryCode;
|
||||
$root.find('[data-role="popup-title"]').text(featured[0].country + ' ' + config.labels.agents);
|
||||
$root.find('[data-role="popup-count"]').text(countryAgents.length);
|
||||
|
||||
featured.forEach(function (agent) {
|
||||
var $item = $('<div/>', {'class': 'agent-map__featured-item'});
|
||||
$('<img/>', {
|
||||
src: agent.logo_url,
|
||||
alt: '',
|
||||
loading: 'lazy'
|
||||
}).appendTo($item);
|
||||
$('<span/>').text(agent.name).appendTo($item);
|
||||
$item.appendTo($items);
|
||||
});
|
||||
|
||||
$root.find('.agent-map__marker').attr('aria-pressed', 'false');
|
||||
$root.find('.agent-map__marker[data-country="' + countryCode + '"]').attr('aria-pressed', 'true');
|
||||
$popup.prop('hidden', false).addClass('is-open');
|
||||
}
|
||||
|
||||
function closePopup() {
|
||||
state.popupCountry = '';
|
||||
$popup.prop('hidden', true).removeClass('is-open');
|
||||
$root.find('.agent-map__marker').attr('aria-pressed', 'false');
|
||||
}
|
||||
|
||||
function renderDirectory(updateUrl) {
|
||||
var keyword = normalize(state.keyword);
|
||||
var filtered = agents.filter(function (agent) {
|
||||
if (state.continent && agent.continent !== state.continent) {
|
||||
return false;
|
||||
}
|
||||
if (state.country && agent.country_code !== state.country) {
|
||||
return false;
|
||||
}
|
||||
if (!keyword) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return normalize([
|
||||
agent.name,
|
||||
agent.system_name,
|
||||
agent.country,
|
||||
agent.address,
|
||||
agent.note
|
||||
].join(' ')).indexOf(keyword) !== -1;
|
||||
});
|
||||
var $list = $root.find('[data-role="agent-list"]').empty();
|
||||
var $empty = $root.find('[data-role="empty-state"]');
|
||||
var totalPages = Math.max(1, Math.ceil(filtered.length / pageSize));
|
||||
var pageAgents;
|
||||
|
||||
state.page = Math.min(Math.max(1, state.page), totalPages);
|
||||
pageAgents = filtered.slice((state.page - 1) * pageSize, state.page * pageSize);
|
||||
|
||||
pageAgents.forEach(function (agent) {
|
||||
renderAgentCard(agent).appendTo($list);
|
||||
});
|
||||
|
||||
renderPagination(filtered.length, totalPages);
|
||||
|
||||
$root.find('[data-role="result-count"]').text(
|
||||
config.labels.filteredResults.replace('%1', filtered.length)
|
||||
);
|
||||
$empty.text(config.labels.noResults).prop('hidden', filtered.length !== 0);
|
||||
|
||||
if (updateUrl) {
|
||||
updateBrowserUrl();
|
||||
}
|
||||
}
|
||||
|
||||
function renderPagination(totalResults, totalPages) {
|
||||
var $pagination = $root.find('[data-role="pagination"]').empty();
|
||||
var startPage;
|
||||
var endPage;
|
||||
var page;
|
||||
|
||||
if (totalResults <= pageSize) {
|
||||
$pagination.prop('hidden', true);
|
||||
return;
|
||||
}
|
||||
|
||||
$pagination.prop('hidden', false);
|
||||
appendPageButton($pagination, state.page - 1, config.labels.previousPage, 'previous', state.page === 1);
|
||||
|
||||
if (totalPages <= 5) {
|
||||
startPage = 1;
|
||||
endPage = totalPages;
|
||||
} else if (state.page <= 3) {
|
||||
startPage = 1;
|
||||
endPage = 5;
|
||||
} else if (state.page >= totalPages - 2) {
|
||||
startPage = totalPages - 4;
|
||||
endPage = totalPages;
|
||||
} else {
|
||||
startPage = state.page - 2;
|
||||
endPage = state.page + 2;
|
||||
}
|
||||
|
||||
for (page = startPage; page <= endPage; page++) {
|
||||
appendPageButton($pagination, page, config.labels.page.replace('%1', page), 'page', false, page === state.page);
|
||||
}
|
||||
|
||||
appendPageButton($pagination, state.page + 1, config.labels.nextPage, 'next', state.page === totalPages);
|
||||
}
|
||||
|
||||
function appendPageButton($pagination, page, label, type, disabled, current) {
|
||||
var $button = $('<button/>', {
|
||||
type: 'button',
|
||||
'class': 'agent-pagination__button agent-pagination__button--' + type,
|
||||
'data-page': page,
|
||||
'aria-label': label,
|
||||
disabled: disabled
|
||||
});
|
||||
|
||||
if (current) {
|
||||
$button.attr('aria-current', 'page');
|
||||
}
|
||||
if (type === 'previous') {
|
||||
$button.append($('<span/>', {'aria-hidden': 'true', text: '‹'}));
|
||||
} else if (type === 'next') {
|
||||
$button.append($('<span/>', {'aria-hidden': 'true', text: '›'}));
|
||||
} else {
|
||||
$button.append($('<span/>', {'class': 'agent-sr-only', text: label}));
|
||||
}
|
||||
$button.appendTo($pagination);
|
||||
}
|
||||
|
||||
function renderAgentCard(agent) {
|
||||
var $card = $('<article/>', {'class': 'agent-card'});
|
||||
var $header = $('<div/>', {'class': 'agent-card__header'}).appendTo($card);
|
||||
var $body = $('<div/>', {'class': 'agent-card__body'}).appendTo($card);
|
||||
|
||||
$('<img/>', {
|
||||
'class': 'agent-card__logo',
|
||||
src: agent.logo_url,
|
||||
alt: '',
|
||||
loading: 'lazy'
|
||||
}).appendTo($header);
|
||||
$('<h3/>', {'class': 'agent-card__name'}).text(agent.name).appendTo($header);
|
||||
|
||||
$('<p/>', {'class': 'agent-card__country'})
|
||||
.append($('<span/>', {'class': 'agent-card__flag', text: countryFlag(agent.country_code), 'aria-hidden': 'true'}))
|
||||
.append($('<span/>').text(agent.country))
|
||||
.appendTo($body);
|
||||
|
||||
if (agent.address) {
|
||||
$('<p/>', {'class': 'agent-card__address'})
|
||||
.append($('<span/>', {'class': 'agent-card__pin', 'aria-hidden': 'true'}))
|
||||
.append($('<span/>').text(agent.address))
|
||||
.appendTo($body);
|
||||
}
|
||||
|
||||
if (agent.website) {
|
||||
$('<p/>', {'class': 'agent-card__website'})
|
||||
.append($('<span/>').text(config.labels.website + ': '))
|
||||
.append($('<a/>', {
|
||||
href: agent.website,
|
||||
target: '_blank',
|
||||
rel: 'noopener noreferrer',
|
||||
text: displayWebsite(agent.website)
|
||||
}))
|
||||
.appendTo($body);
|
||||
}
|
||||
|
||||
return $card;
|
||||
}
|
||||
|
||||
function applyCountry(countryCode, shouldScroll) {
|
||||
var countryAgents = agentsForCountry(countryCode);
|
||||
if (!countryAgents.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.continent = countryAgents[0].continent;
|
||||
state.country = countryCode;
|
||||
state.keyword = '';
|
||||
state.page = 1;
|
||||
$continent.val(state.continent);
|
||||
$keyword.val('');
|
||||
buildCountryOptions();
|
||||
$country.val(countryCode);
|
||||
renderDirectory(true);
|
||||
closePopup();
|
||||
|
||||
if (shouldScroll) {
|
||||
var target = $root.find('#global-agents').get(0);
|
||||
if (target) {
|
||||
target.scrollIntoView({behavior: 'smooth', block: 'start'});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function bindEvents() {
|
||||
$root.on('click', '.agent-map__marker', function (event) {
|
||||
event.stopPropagation();
|
||||
openPopup($(this).data('country'));
|
||||
});
|
||||
|
||||
$root.find('[data-role="popup-close"]').on('click', closePopup);
|
||||
$root.find('[data-role="explore-all"]').on('click', function () {
|
||||
if (state.popupCountry) {
|
||||
applyCountry(state.popupCountry, true);
|
||||
}
|
||||
});
|
||||
|
||||
$root.find('[data-role="map-canvas"]').on('click', function (event) {
|
||||
if (event.target === this || $(event.target).is('[data-role="map-image"]')) {
|
||||
closePopup();
|
||||
}
|
||||
});
|
||||
|
||||
$continent.on('change', function () {
|
||||
state.continent = String($(this).val() || '');
|
||||
state.country = '';
|
||||
state.page = 1;
|
||||
buildCountryOptions();
|
||||
renderDirectory(true);
|
||||
});
|
||||
|
||||
$country.on('change', function () {
|
||||
state.country = String($(this).val() || '');
|
||||
state.page = 1;
|
||||
renderDirectory(true);
|
||||
});
|
||||
|
||||
$keyword.on('input', function () {
|
||||
state.keyword = String($(this).val() || '');
|
||||
state.page = 1;
|
||||
renderDirectory(true);
|
||||
});
|
||||
|
||||
$root.find('[data-role="pagination"]').on('click', '[data-page]', function () {
|
||||
if (this.disabled) {
|
||||
return;
|
||||
}
|
||||
state.page = Number($(this).data('page')) || 1;
|
||||
renderDirectory(true);
|
||||
$root.find('[data-role="agent-list"]').get(0).scrollIntoView({behavior: 'smooth', block: 'start'});
|
||||
});
|
||||
|
||||
$root.find('[data-role="reset-filters"]').on('click', function () {
|
||||
state.continent = '';
|
||||
state.country = '';
|
||||
state.keyword = '';
|
||||
state.page = 1;
|
||||
$continent.val('');
|
||||
$keyword.val('');
|
||||
buildCountryOptions();
|
||||
renderDirectory(true);
|
||||
});
|
||||
|
||||
$(document).on('keydown.agentLocator', function (event) {
|
||||
if (event.key === 'Escape' && state.popupCountry) {
|
||||
closePopup();
|
||||
}
|
||||
});
|
||||
|
||||
$(window).on('popstate.agentLocator', function () {
|
||||
restoreStateFromUrl();
|
||||
buildCountryOptions();
|
||||
renderDirectory(false);
|
||||
});
|
||||
}
|
||||
|
||||
function restoreStateFromUrl() {
|
||||
var params = new URL(window.location.href).searchParams;
|
||||
var requestedContinent = params.get('continent') || '';
|
||||
var requestedCountry = params.get('country') || '';
|
||||
|
||||
state.continent = continentByCode[requestedContinent] ? requestedContinent : '';
|
||||
state.country = requestedCountry;
|
||||
state.keyword = params.get('q') || '';
|
||||
state.page = Math.max(1, parseInt(params.get('page'), 10) || 1);
|
||||
$continent.val(state.continent);
|
||||
$keyword.val(state.keyword);
|
||||
}
|
||||
|
||||
function updateBrowserUrl() {
|
||||
var url = new URL(window.location.href);
|
||||
setUrlParam(url, 'continent', state.continent);
|
||||
setUrlParam(url, 'country', state.country);
|
||||
setUrlParam(url, 'q', state.keyword);
|
||||
setUrlParam(url, 'page', state.page > 1 ? state.page : '');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
}
|
||||
|
||||
function setUrlParam(url, name, value) {
|
||||
if (value) {
|
||||
url.searchParams.set(name, value);
|
||||
} else {
|
||||
url.searchParams.delete(name);
|
||||
}
|
||||
}
|
||||
|
||||
function normalize(value) {
|
||||
return String(value || '').toLocaleLowerCase().trim();
|
||||
}
|
||||
|
||||
function displayWebsite(url) {
|
||||
return url.replace(/^https?:\/\//i, '').replace(/\/$/, '');
|
||||
}
|
||||
|
||||
function countryFlag(code) {
|
||||
if (!/^[A-Z]{2}$/.test(code)) {
|
||||
return '🌐';
|
||||
}
|
||||
return String.fromCodePoint.apply(null, code.split('').map(function (character) {
|
||||
return 127397 + character.charCodeAt(0);
|
||||
}));
|
||||
}
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user