init
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
define(['jquery', 'mage/translate'], function ($, $t) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $root = $(element),
|
||||
items = config.items || [],
|
||||
archives = config.archives || [],
|
||||
brands = config.brands || [],
|
||||
modelsByBrand = config.modelsByBrand || {},
|
||||
pageSize = 20,
|
||||
currentPage = 1,
|
||||
$brand = $root.find('[data-role="spec-brand"]'),
|
||||
$model = $root.find('[data-role="spec-model"]'),
|
||||
$count = $root.find('[data-role="spec-count"]'),
|
||||
$empty = $root.find('[data-role="spec-empty"]'),
|
||||
$list = $root.find('[data-role="spec-list"]'),
|
||||
$pagination = $root.find('[data-role="spec-pagination"]');
|
||||
|
||||
function renderBrands() {
|
||||
$.each(brands, function (_, brandItem) {
|
||||
$('<option/>', {
|
||||
value: brandItem.id,
|
||||
text: brandItem.name
|
||||
}).appendTo($brand);
|
||||
});
|
||||
}
|
||||
|
||||
function renderModels() {
|
||||
var brandId = $brand.val(),
|
||||
models = brandId ? (modelsByBrand[brandId] || []).slice() : [];
|
||||
|
||||
if (!brandId) {
|
||||
$.each(modelsByBrand, function (_, brandModels) {
|
||||
$.each(brandModels, function (_, modelName) {
|
||||
if ($.inArray(modelName, models) === -1) {
|
||||
models.push(modelName);
|
||||
}
|
||||
});
|
||||
});
|
||||
models.sort();
|
||||
}
|
||||
|
||||
$model.empty().append($('<option/>', {
|
||||
value: '',
|
||||
text: $t('All Models')
|
||||
}));
|
||||
$.each(models, function (_, modelName) {
|
||||
$('<option/>', {
|
||||
value: modelName,
|
||||
text: modelName
|
||||
}).appendTo($model);
|
||||
});
|
||||
}
|
||||
|
||||
function filterItems() {
|
||||
var brandId = $brand.val(),
|
||||
modelName = $model.val(),
|
||||
results;
|
||||
|
||||
if (!brandId && !modelName) {
|
||||
return archives.slice();
|
||||
}
|
||||
|
||||
results = $.grep(items, function (item) {
|
||||
return (!brandId || String(item.brand_id) === String(brandId)) &&
|
||||
(!modelName || item.model_name === modelName);
|
||||
});
|
||||
|
||||
if (brandId) {
|
||||
results = $.grep(archives, function (archive) {
|
||||
return String(archive.brand_id) === String(brandId);
|
||||
}).concat(results);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
function createPageButton(label, page, options) {
|
||||
var settings = options || {},
|
||||
$button = $('<button/>', {
|
||||
'class': 'esun-download-specifications__page' + (settings.active ? ' is-active' : ''),
|
||||
type: 'button',
|
||||
text: label,
|
||||
disabled: settings.disabled || false,
|
||||
'aria-label': settings.ariaLabel || null,
|
||||
'aria-current': settings.active ? 'page' : null
|
||||
});
|
||||
|
||||
if (!settings.disabled && !settings.active) {
|
||||
$button.on('click', function () {
|
||||
currentPage = page;
|
||||
renderList();
|
||||
$root.get(0).scrollIntoView({behavior: 'smooth', block: 'start'});
|
||||
});
|
||||
}
|
||||
|
||||
return $button;
|
||||
}
|
||||
|
||||
function renderPagination(totalPages) {
|
||||
var pages = [],
|
||||
previousPage = 0;
|
||||
|
||||
$pagination.empty();
|
||||
if (totalPages <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$pagination.append(createPageButton($t('Previous'), currentPage - 1, {
|
||||
disabled: currentPage === 1,
|
||||
ariaLabel: $t('Previous page')
|
||||
}));
|
||||
|
||||
for (var page = 1; page <= totalPages; page++) {
|
||||
if (page === 1 || page === totalPages || Math.abs(page - currentPage) <= 2) {
|
||||
pages.push(page);
|
||||
}
|
||||
}
|
||||
|
||||
$.each(pages, function (_, page) {
|
||||
if (previousPage && page - previousPage > 1) {
|
||||
$('<span/>', {
|
||||
'class': 'esun-download-specifications__ellipsis',
|
||||
text: '…',
|
||||
'aria-hidden': 'true'
|
||||
}).appendTo($pagination);
|
||||
}
|
||||
$pagination.append(createPageButton(String(page), page, {
|
||||
active: page === currentPage,
|
||||
ariaLabel: $t('Page %1').replace('%1', page)
|
||||
}));
|
||||
previousPage = page;
|
||||
});
|
||||
|
||||
$pagination.append(createPageButton($t('Next'), currentPage + 1, {
|
||||
disabled: currentPage === totalPages,
|
||||
ariaLabel: $t('Next page')
|
||||
}));
|
||||
}
|
||||
|
||||
function renderItem(item) {
|
||||
var $item = $('<a/>', {
|
||||
'class': 'esun-download-specifications__item',
|
||||
href: item.file_url,
|
||||
download: item.file_name
|
||||
}),
|
||||
$logo = $('<span/>', {'class': 'esun-download-specifications__logo'}),
|
||||
$body = $('<span/>', {'class': 'esun-download-specifications__body'}),
|
||||
$meta = $('<span/>', {'class': 'esun-download-specifications__meta'}),
|
||||
$info = $('<span/>', {'class': 'esun-download-specifications__info'}),
|
||||
$action = $('<span/>', {'class': 'esun-download-specifications__action'});
|
||||
|
||||
if (item.brand_logo_url) {
|
||||
$('<img/>', {src: item.brand_logo_url, alt: ''}).appendTo($logo);
|
||||
}
|
||||
|
||||
$('<span/>', {'class': 'esun-download-specifications__brand', text: item.brand_name}).appendTo($meta);
|
||||
if (item.model_name) {
|
||||
$('<span/>', {'class': 'esun-download-specifications__model', text: item.model_name}).appendTo($meta);
|
||||
}
|
||||
$('<strong/>', {text: item.file_name}).appendTo($body);
|
||||
$('<span/>', {text: item.file_size_label}).appendTo($info);
|
||||
if (item.file_date) {
|
||||
$('<span/>', {text: item.file_date}).appendTo($info);
|
||||
}
|
||||
$('<span/>', {
|
||||
'class': 'esun-download-specifications__download-text',
|
||||
text: $t('Download')
|
||||
}).appendTo($action);
|
||||
$('<span/>', {'class': 'esun-download-specifications__download-icon'}).appendTo($action);
|
||||
|
||||
$body.prepend($meta).append($info);
|
||||
$item.append($logo, $body, $action).appendTo($list);
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
var results = filterItems(),
|
||||
totalPages = Math.ceil(results.length / pageSize),
|
||||
start;
|
||||
|
||||
if (totalPages > 0 && currentPage > totalPages) {
|
||||
currentPage = totalPages;
|
||||
}
|
||||
|
||||
$count.text(results.length);
|
||||
$list.empty();
|
||||
$empty.toggle(results.length === 0);
|
||||
$list.toggle(results.length > 0);
|
||||
|
||||
start = (currentPage - 1) * pageSize;
|
||||
$.each(results.slice(start, start + pageSize), function (_, item) {
|
||||
renderItem(item);
|
||||
});
|
||||
renderPagination(totalPages);
|
||||
}
|
||||
|
||||
renderBrands();
|
||||
renderModels();
|
||||
renderList();
|
||||
|
||||
$brand.on('change', function () {
|
||||
currentPage = 1;
|
||||
renderModels();
|
||||
renderList();
|
||||
});
|
||||
$model.on('change', function () {
|
||||
currentPage = 1;
|
||||
renderList();
|
||||
});
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user