FleetCart/Themes/Storefront/resources/assets/public/js/storefront.js

334 lines
9.6 KiB
JavaScript
Raw Normal View History

2023-12-03 14:07:47 +00:00
import "./vendors/vendors";
2023-06-11 12:14:03 +00:00
$(() => {
/* variables
/*----------------------------------------*/
let _window = $(window),
2023-12-03 14:07:47 +00:00
body = $("body");
2023-06-11 12:14:03 +00:00
/* button loading
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
$("[data-loading]").on("click", (e) => {
e.currentTarget.classList.add("btn-loading");
2023-06-11 12:14:03 +00:00
});
/* select option
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let select = $(".custom-select-option");
2023-06-11 12:14:03 +00:00
select.niceSelect();
2023-12-03 14:07:47 +00:00
select.on("change", (e) => {
e.target.dispatchEvent(
new Event("nice-select-updated", { bubbles: true })
);
2023-06-11 12:14:03 +00:00
});
/* overlay
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let overlay = $(".overlay");
2023-06-11 12:14:03 +00:00
/* header
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let headerWrap = $(".header-wrap"),
headerWrapInner = $(".header-wrap-inner"),
2023-06-11 12:14:03 +00:00
headerWrapInnerHeight = headerWrapInner.outerHeight(),
2023-12-03 14:07:47 +00:00
headerSearchSm = $(".header-search-sm"),
searchInputSm = $(".search-input-sm"),
headerSearchSmClose = $(".header-search-sm-form .btn-close");
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
headerSearchSm.on("click", (e) => {
2023-06-11 12:14:03 +00:00
let target = $(e.currentTarget);
2023-12-03 14:07:47 +00:00
target.parents(".header-search").next().toggleClass("active");
searchInputSm.trigger("focus");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
headerSearchSmClose.on("click", (e) => {
2023-06-11 12:14:03 +00:00
let target = $(e.currentTarget);
2023-12-03 14:07:47 +00:00
target.parents(".header-search-sm-form").removeClass("active");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
_window.on("resize", () => {
2023-06-11 12:14:03 +00:00
headerWrapInnerHeight = headerWrapInner.outerHeight();
});
2023-12-03 14:07:47 +00:00
_window.on("load scroll resize", () => {
2023-06-11 12:14:03 +00:00
let headerWrapHeight = headerWrap.outerHeight(),
headerWrapOffsetTop = headerWrap.offset().top + headerWrapHeight;
function stickyHeader() {
let scrollTop = _window.scrollTop();
if (scrollTop > headerWrapOffsetTop) {
2023-12-03 14:07:47 +00:00
headerWrap.css("padding-top", `${headerWrapInnerHeight}px`);
headerWrapInner.addClass("sticky");
2023-06-11 12:14:03 +00:00
setTimeout(() => {
2023-12-03 14:07:47 +00:00
headerWrapInner.addClass("show");
2023-06-11 12:14:03 +00:00
});
return;
}
2023-12-03 14:07:47 +00:00
headerWrap.css("padding-top", 0);
headerWrapInner.removeClass("sticky show");
2023-06-11 12:14:03 +00:00
}
stickyHeader();
});
/* menu dropdown arrow
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let megaMenuItem = $(".mega-menu > li"),
subMenuDropdown = $(".sub-menu > .dropdown"),
sidebarMenuSubMenu = $(".sidebar-menu .sub-menu");
2023-06-11 12:14:03 +00:00
function menuDropdownArrow(parentSelector, childSelector) {
parentSelector.each(function () {
let self = $(this);
if (self.children().length > 1) {
if (window.FleetCart.rtl) {
2023-12-03 14:07:47 +00:00
self.children(`${childSelector}`).append(
'<i class="las la-angle-left"></i>'
);
2023-06-11 12:14:03 +00:00
return;
}
2023-12-03 14:07:47 +00:00
self.children(`${childSelector}`).append(
'<i class="las la-angle-right"></i>'
);
2023-06-11 12:14:03 +00:00
}
});
}
2023-12-03 14:07:47 +00:00
menuDropdownArrow(subMenuDropdown, "a");
menuDropdownArrow(megaMenuItem, ".menu-item");
2023-06-11 12:14:03 +00:00
/* navigation
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let moreCategories = $(".more-categories"),
categoryDropdown = $(".category-dropdown"),
categoryNavInner = $(".category-nav-inner"),
categoryDropdownWrap = $(".category-dropdown-wrap"),
verticalMegaMenuList = $(".vertical-megamenu > li");
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
categoryNavInner.on("click", () => {
categoryDropdownWrap.toggleClass("show");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
_window.on("load resize", () => {
2023-06-11 12:14:03 +00:00
let verticalMegaMenuListHeight = 0,
homeSliderHeight = homeSlider.height(),
categoryDropdownHeight = homeSliderHeight;
2023-12-03 14:07:47 +00:00
categoryDropdown.css("height", `${categoryDropdownHeight}px`);
2023-06-11 12:14:03 +00:00
verticalMegaMenuList.each(function () {
let self = $(this);
verticalMegaMenuListHeight += self.height();
if (verticalMegaMenuListHeight + 78 > categoryDropdownHeight) {
2023-12-03 14:07:47 +00:00
self.addClass("hide");
moreCategories.removeClass("hide");
2023-06-11 12:14:03 +00:00
return;
}
2023-12-03 14:07:47 +00:00
self.removeClass("hide");
moreCategories.addClass("hide");
2023-06-11 12:14:03 +00:00
});
});
/* sidebar menu
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let sidebarMenuIcon = $(".sidebar-menu-icon"),
sidebarMenuWrap = $(".sidebar-menu-wrap"),
sidebarMenuClose = $(".sidebar-menu-close"),
sidebarMenuTab = $(".sidebar-menu-tab a"),
sidebarMenuList = $(".sidebar-menu li"),
sidebarMenuLink = $(".sidebar-menu > li > a"),
sidebarMenuListUl = $(".sidebar-menu > li > ul"),
sidebarMenuDropdown = $(".sidebar-menu > .dropdown"),
sidebarMenuSubMenuUl = $(".sidebar-menu .sub-menu ul"),
sidebarMenuSubMenuLink = $(".sidebar-menu .sub-menu > a");
sidebarMenuIcon.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
2023-12-03 14:07:47 +00:00
overlay.addClass("active");
sidebarMenuWrap.addClass("active");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
sidebarMenuClose.on("click", (e) => {
overlay.removeClass("active");
sidebarMenuWrap.removeClass("active");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
sidebarMenuWrap.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
2023-12-03 14:07:47 +00:00
sidebarMenuTab.on("click", (e) => {
2023-06-11 12:14:03 +00:00
let target = $(e.currentTarget);
e.preventDefault();
2023-12-03 14:07:47 +00:00
target.tab("show");
2023-06-11 12:14:03 +00:00
});
sidebarMenuList.each(function () {
let self = $(this);
if (self.children().length > 1) {
if (window.FleetCart.rtl) {
2023-12-03 14:07:47 +00:00
self.children("a").after('<i class="las la-angle-left"></i>');
2023-06-11 12:14:03 +00:00
return;
}
2023-12-03 14:07:47 +00:00
self.children("a").after('<i class="las la-angle-right"></i>');
2023-06-11 12:14:03 +00:00
}
});
2023-12-03 14:07:47 +00:00
sidebarMenuDropdown.on("click", (e) => {
2023-06-11 12:14:03 +00:00
let target = $(e.currentTarget);
2023-12-03 14:07:47 +00:00
if (!target.hasClass("active")) {
$(".sidebar-menu > li").removeClass("active");
target.addClass("active");
2023-06-11 12:14:03 +00:00
} else {
2023-12-03 14:07:47 +00:00
$(".sidebar-menu > li").removeClass("active");
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
if (!target.children("ul").hasClass("open")) {
$(".sidebar-menu .open").removeClass("open").slideUp(300);
target.children("ul").addClass("open").slideDown(300);
2023-06-11 12:14:03 +00:00
return;
}
2023-12-03 14:07:47 +00:00
$(".sidebar-menu .open").removeClass("open").slideUp(300);
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
sidebarMenuLink.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
2023-12-03 14:07:47 +00:00
sidebarMenuListUl.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
2023-12-03 14:07:47 +00:00
sidebarMenuSubMenu.on("click", (e) => {
2023-06-11 12:14:03 +00:00
let target = $(e.currentTarget);
2023-12-03 14:07:47 +00:00
if (!target.hasClass("active")) {
target.addClass("active");
2023-06-11 12:14:03 +00:00
} else {
2023-12-03 14:07:47 +00:00
target.removeClass("active");
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
target.children("ul").slideToggle(300);
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
sidebarMenuSubMenuUl.on("click", function (e) {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
2023-12-03 14:07:47 +00:00
sidebarMenuSubMenuLink.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
/* slider
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
const homeSlider = $(".home-slider");
2023-06-11 12:14:03 +00:00
if (homeSlider.length !== 0) {
2023-12-03 14:07:47 +00:00
const { speed, autoplay, autoplaySpeed, fade, dots, arrows } =
homeSlider.data();
homeSlider
.slick({
rows: 0,
rtl: window.FleetCart.rtl,
cssEase: fade ? "cubic-bezier(0.7, 0, 0.3, 1)" : "ease",
speed,
autoplay,
autoplaySpeed,
fade,
dots,
arrows,
})
.slickAnimation();
2023-06-11 12:14:03 +00:00
}
/* sidebar filter
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
let mobileViewFilter = $(".mobile-view-filter");
let filterSectionWrap = $(".filter-section-wrap");
let sidebarFilterClose = $(".sidebar-filter-close");
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
mobileViewFilter.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
2023-12-03 14:07:47 +00:00
filterSectionWrap.addClass("active");
overlay.addClass("active");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
sidebarFilterClose.on("click", () => {
filterSectionWrap.removeClass("active");
overlay.removeClass("active");
2023-06-11 12:14:03 +00:00
});
2023-12-03 14:07:47 +00:00
filterSectionWrap.on("click", (e) => {
2023-06-11 12:14:03 +00:00
e.stopPropagation();
});
2023-12-03 14:07:47 +00:00
body.on("click", () => {
overlay.removeClass("active");
$(".sidebar-cart-wrap").removeClass("active");
sidebarMenuWrap.removeClass("active");
filterSectionWrap.removeClass("active");
2023-06-11 12:14:03 +00:00
});
/* browse categories
/*----------------------------------------*/
2023-12-03 14:07:47 +00:00
$(".browse-categories li").each((_, li) => {
if ($(li).children("ul").length > 0) {
$(li).addClass("parent");
2023-06-11 12:14:03 +00:00
}
});
2023-12-03 14:07:47 +00:00
let filterCategoriesLink = $(".browse-categories li.parent > a");
let parentUls = $(".browse-categories li.active").parentsUntil(
".browse-categories",
"ul"
);
2023-06-11 12:14:03 +00:00
if (window.FleetCart.rtl) {
filterCategoriesLink.before('<i class="las la-angle-left"></i>');
} else {
filterCategoriesLink.before('<i class="las la-angle-right"></i>');
}
2023-12-03 14:07:47 +00:00
parentUls.show().siblings("i").addClass("open");
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
$(".browse-categories li i").on("click", (e) => {
$(e.currentTarget).toggleClass("open").siblings("ul").slideToggle(300);
2023-06-11 12:14:03 +00:00
});
});