Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Common themes js code

givanz6d5e6d7805c8
diff --git a/public/js/app/cart.js b/public/js/app/cart.js
new file mode 100644
index 0000000..836a62c
--- /dev/null
+++ b/public/js/app/cart.js
@@ -0,0 +1,78 @@
+document.addEventListener('change', function (e) {
+ let element = e.target.closest('[name="quantity"]');
+ if (element) {
+ let product = element.closest("[data-v-cart-product]");
+ if (product) {
+ let key = product.dataset.key;
+ let product_id = product.dataset.product_id;
+ let quantity = element.value;
+ let updateElements = ['#cart-container [data-key="' + key + '"] .price', '#cart-container [data-key="' + key + '"] .total', ".cart-right-column", ".mini-cart"];
+
+ delay(() => VvvebTheme.Cart.update(key ?? product_id, {quantity}, element, updateElements), 1000);
+ }
+ }
+});
+
+document.addEventListener('click', function (e) {
+ let element = e.target.closest('.btn-coupon, .btn-remove-coupon');
+ if (element) {
+ let updateElements = [".cart-right-column", ".mini-cart"];
+ if (element.classList.contains("btn-remove-coupon")) {
+ let coupon = element.parentNode.querySelector(".code").innerHTML;
+ let container = e.target.closest("[data-v-cart-coupon]");
+
+ VvvebTheme.Cart.removeCoupon({coupon}, element, updateElements);
+ container.remove();
+ } else {
+ let coupon = document.querySelector("[name='coupon']").value;
+ VvvebTheme.Cart.coupon({coupon}, element, updateElements);
+ }
+ e.preventDefault();
+ } else
+ if (element = e.target.closest('.btn-remove')) {
+ let product = element.closest("[data-v-cart-product]");
+ if (product) {
+ let key = product.dataset.key;
+ let quantity = element.value;
+ let updateElements = [".cart-right-column", ".mini-cart"];
+
+ product.classList.add("opacity-50");
+ VvvebTheme.Cart.remove(key, element, updateElements, () => {
+ product.remove();
+
+ //if on cart page and cart empty refresh page
+ let cartContainer = document.getElementById("cart-container");
+ if (cartContainer &&
+ cartContainer.querySelectorAll("[data-v-cart-product]").length == 0 ) {
+ cartContainer.remove();
+ location.reload();
+ }
+
+ });
+
+ e.preventDefault();
+ }
+ } else
+ if (element = e.target.closest(".btn-plus")) {
+ let nrInput = element.parentNode.querySelector("input[type=number]");
+ nrInput.value = parseInt(nrInput.value) + 1;
+ nrInput.dispatchEvent(new KeyboardEvent("change", {
+ bubbles: true,
+ cancelable: true,
+ }));
+
+ e.preventDefault();
+ return false;
+ } else
+ if (element = e.target.closest(".btn-minus")) {
+ let nrInput = element.parentNode.querySelector("input[type=number]");
+ nrInput.value = Math.max(1, parseInt(nrInput.value) - 1);
+ nrInput.dispatchEvent(new KeyboardEvent("change", {
+ bubbles: true,
+ cancelable: true,
+ }));
+
+ e.preventDefault();
+ return false;
+ }
+});
diff --git a/public/js/app/checkout.js b/public/js/app/checkout.js
new file mode 100644
index 0000000..3623cf8
--- /dev/null
+++ b/public/js/app/checkout.js
@@ -0,0 +1,217 @@
+function refreshCart(parameters, element, update = false) {
+ VvvebTheme.Cart.module = 'checkout/checkout';
+ VvvebTheme.Cart.component_id = 1;
+ //action, parameters, element, selector, callback
+ if (!update) {
+ update = ['.cart-summary', '.container > .notifications'];
+ }
+ VvvebTheme.Cart.ajax('', parameters, element, update);
+ //cart-summary
+}
+
+function toggleBillingAddress(element) {
+
+ let address = document.querySelector(".billing_address");
+ if (element.value == 0) {
+ address.querySelectorAll("input,select,textarea").forEach(e => e.removeAttribute("disabled"));
+ address.style.display = "";
+ } else {
+ address.querySelectorAll("input,select,textarea").forEach(e => e.setAttribute("disabled", "true"));
+ address.style.display = "none";
+ }
+}
+
+function toggleShippingAddress(element) {
+ let address = document.querySelector(".shipping_address");
+ if (element) {
+ if (element.checked == 0) {
+ address.querySelectorAll("input,select,textarea").forEach(e => e.setAttribute("disabled", "true"));
+ address.style.display = "none";
+ } else {
+ address.querySelectorAll("input,select,textarea").forEach(e => e.removeAttribute("disabled"));
+ address.style.display = "";
+ }
+ }
+}
+
+function toggleRegister(element) {
+ let register = document.querySelector(".register-account");
+
+ if (element.value == 'false') {
+ register.querySelectorAll("input,select,textarea").forEach(e => e.setAttribute("disabled", "true"));
+ register.style.display = "none";
+ } else {
+ register.querySelectorAll("input,select,textarea").forEach(e => e.removeAttribute("disabled"));
+ register.style.display = "";
+ }
+}
+
+window.addEventListener('DOMContentLoaded', (e) => {
+
+ //show billing address form if no address is selected
+ document.addEventListener("change", e => {
+ let element = e.target.closest("[name=billing_address_id]");
+ if (element) {
+ toggleBillingAddress(element);
+ }
+ });
+
+ if (!document.getElementById("billing_address_new")?.checked && !document.querySelector("[name=billing_address_id]:checked")?.value) {
+ //if new address is not checked and no address is selected
+ document.querySelector("[name=billing_address_id]")?.dispatchEvent(new MouseEvent('click'));//select first address
+ }
+
+ let billing_address = document.querySelector("[name=billing_address_id]:checked");
+ //if an address is selected hide billing address form
+ if (billing_address && billing_address.value != false) {
+ document.querySelector(".billing_address").style.display = "none";
+ }
+
+ //hide shipping address form if same as billing checkbox is checked
+ toggleShippingAddress(document.getElementById("shipping-form-check"));
+
+ //if login form is filled show form
+ if (document.querySelector("#checkout-login-form [name=password]")?.value) {
+ document.getElementById('checkout-login-container').style.display = "";
+ document.getElementById('login-form-check').checked = true;
+ }
+
+ //if shipping or payment method is selected collapse the accordion
+ document.querySelector('.accordion-item input[type="radio"]:checked')?.closest("label").dispatchEvent(new MouseEvent('click'));
+
+ document.addEventListener("click", function(e) {
+ let element = e.target.closest(".accordion-header label");
+ if (element) {
+ //e.stopPropagation();
+
+ let item = element.closest(".accordion-item");
+ let parent = item.closest(".accordion");
+ let collapse = item.querySelector(":scope > .collapse");
+
+ parent.querySelectorAll(".collapse.show").forEach(e => bootstrap.Collapse.getOrCreateInstance(e)?.hide());
+ parent.querySelectorAll(".accordion-button").forEach(e => e.classList.add("collapsed"));
+
+ item.querySelector(".accordion-button").classList.remove("collapsed");
+ bootstrap.Collapse.getOrCreateInstance(collapse)?.show();
+
+ //disable inputs for non selected payment and shipping methods to avoid form validation issues
+ parent.querySelectorAll(".accordion-body input, .accordion-body select, .accordion-body textarea").forEach(e => e.setAttribute("disabled", "true"));
+ //enable only for selected method
+ item.querySelectorAll(".accordion-body input, .accordion-body select, .accordion-body textarea").forEach(e => e.removeAttribute("disabled"));
+
+ let input = item.querySelector('[name="shipping_method"], [name="payment_method"]');
+ let parameters = {};
+ parameters[element.name] = element.value;
+ refreshCart(parameters, element);
+ }
+ });
+
+ //document.querySelector("[data-v-countries][readonly]")?.dispatchEvent(new Event('change', {bubbles:true}));
+ document.querySelector('input[name="register"]:checked')?.dispatchEvent(new MouseEvent('click', {bubbles:true}));
+});
+
+//load regions for region select when country changes
+let regions = [];
+
+function addRegionsToSelect(regionSelect, data, region_id = 0, countrySelect) {
+ regionSelect.replaceChildren();
+ for (const region of data) {
+ regionSelect.append(new Option(region.name, region.region_id));
+ }
+ regionSelect.value = region_id;
+ regionSelect.removeAttribute("readonly");
+ countrySelect.removeAttribute("readonly");
+}
+
+function reloadRegions(element){
+ let parameters = {};
+ if (element) {
+ document.querySelectorAll("[data-v-countries],[data-v-regions]").forEach(e => parameters[e.name] = e.value ?? 0);
+ }
+ document.querySelector("[data-v-countries][readonly]")?.dispatchEvent(new Event('change'));
+ refreshCart(parameters, element, ['.cart-summary', '[data-v-component-checkout-shipping]' ,'[data-v-component-checkout-payment]']);
+}
+
+document.addEventListener("change", function (e) {
+ let element = e.target.closest("[data-v-countries]");
+ if (element) {
+ let regionGroup = element.closest(".address");
+ let regionSelect = regionGroup.querySelector("[data-v-regions]");
+ let country_id = element.value;
+ let region_id = element.dataset.vRegionId;
+ let self = element;
+ element.readonly = false;
+
+ if (country_id) {
+ if (regions[country_id]) {
+ addRegionsToSelect(regionSelect, regions[country_id], region_id, self);
+ reloadRegions(element);
+ } else {
+ fetch(regionsUrl + "&country_id=" + country_id)
+ .then(response => {
+ if (!response.ok) { throw new Error(response) }
+ return response.json()
+ })
+ .then(data => {
+ regions[country_id] = data;
+ addRegionsToSelect(regionSelect, data, region_id, self);
+ document.querySelector("[data-v-countries][readonly]")?.dispatchEvent(new Event('change', {bubbles:true}));
+ reloadRegions(element);
+ })
+ .catch(error => {
+ console.log(error.statusText);
+ //displayToast("bg-danger", "Revision", "Error!");
+ });
+ }
+ }
+
+ element.dataset.vRegionId = 0;
+ } else {
+ let element = e.target.closest("[data-v-regions]");
+ if (element) {
+ reloadRegions(element);
+ }
+ }
+});
+
+function togglePasswordInput(element, input) {
+ let password = document.getElementById(input);
+ if (password.type == "password") {
+ password.type = "text";
+ let i = element.querySelector("i")
+ i.classList.add("la-eye")
+ i.classList.remove("la-eye-slash");
+ } else {
+ password.type = "password";
+ let i = element.querySelector("i")
+ i.classList.remove("la-eye")
+ i.classList.add("la-eye-slash");
+ }
+}
+
+
+document.addEventListener('click', function (e) {
+ let element = e.target.closest('.btn-coupon, .btn-remove-coupon');
+ if (element) {
+ let updateElements = [".cart-right-column", ".mini-cart", ".cart-summary"];
+ VvvebTheme.Cart.module= 'checkout/checkout';
+ if (element.classList.contains("btn-remove-coupon")) {
+ let coupon = element.parentNode.querySelector(".code").innerHTML;
+ let container = e.target.closest("[data-v-cart-coupon]");
+ VvvebTheme.Cart.removeCoupon({coupon}, element, updateElements);
+ container.remove();
+ } else {
+ let coupon = document.querySelector("[name='coupon']").value;
+ VvvebTheme.Cart.coupon({coupon}, element, updateElements);
+ }
+ e.preventDefault();
+ }
+});
+
+function toggleLoginForm() {
+ let container = document.getElementById('checkout-login-container');
+ container.style.display = container.style.display ? "" : "none";
+}
+
+
+document.querySelectorAll(".accordion-button .form-check-input:checked").forEach(e => e.parentNode.classList.remove("collapsed"));
diff --git a/public/js/app/theme.js b/public/js/app/theme.js
new file mode 100644
index 0000000..2eb1d3f
--- /dev/null
+++ b/public/js/app/theme.js
@@ -0,0 +1,343 @@
+/*
+ * Sticky navbar
+ *
+ */
+
+// When the user scrolls the page, execute navbarSticky
+window.onscroll = function() {navbarSticky()};
+
+// Get the navbar
+var navbar = document.querySelector((typeof navbarSelector !== 'undefined') ? navbarSelector : ".navbar");
+// Get the offset position of the navbar
+var sticky = navbar.offsetTop ? navbar.offsetTop : navbar.offsetHeight;
+
+function toggleNavbarTheme () {
+ if (navbar.classList.contains("navbar-dark")) {
+ navbar.classList.add("navbar-light");
+ navbar.classList.remove("navbar-dark");
+ } else if (navbar.classList.contains("navbar-light")) {
+ navbar.classList.add("navbar-dark");
+ navbar.classList.remove("navbar-light");
+ }
+}
+
+
+// Add the sticky class to the navbar when you reach its scroll position. Remove "sticky" when you leave the scroll position
+function navbarSticky(isSticky) {
+ if (isSticky == undefined) {
+ isSticky = (window.pageYOffset >= sticky);
+ }
+
+ if (isSticky) {
+ if (!navbar.classList.contains("sticky")) {
+ navbar.classList.add("sticky");
+ toggleNavbarTheme();
+ }
+ } else {
+ if (navbar.classList.contains("sticky")) {
+ navbar.classList.remove("sticky");
+ toggleNavbarTheme();
+ }
+ }
+}
+
+function setCookie(name, value) {
+ document.cookie = name + "=" + value + ";";
+ //try to set cookie to all subdomains
+ document.cookie = name + "=" + value + ";path=/;domain=." + window.location.host.replace(/^.*?\./, '') + ";";
+}
+
+let themeSwitch = document.querySelector("#color-theme-switch i");
+let theme = document.documentElement.dataset.bsTheme;
+let themeCookie = document.cookie.match(/theme=(.*?);/);
+if (themeCookie && themeCookie[1]) {
+ theme = themeCookie[1];
+}
+
+if (theme && themeSwitch) {
+ if (theme == "dark") {
+ let themeSwitch = document.querySelector("#color-theme-switch i");
+ themeSwitch.classList.remove("la-sun")
+ themeSwitch.classList.add("la-moon");
+ document.documentElement.dataset.bsTheme = theme;
+ }
+}
+
+
+document.addEventListener("click", function (e) {
+ let link = e.target.closest("#color-theme-switch");
+ if (link) {
+ let themeSwitch = link.querySelector("i");
+ let theme = document.documentElement.dataset.bsTheme;
+
+ if (theme == "dark") {
+ theme = "light";
+ themeSwitch.classList.remove("la-moon");
+ themeSwitch.classList.add("la-sun");
+ } else if (theme == "light" || theme == "auto" || !theme) {
+ theme = "dark";
+ themeSwitch.classList.remove("la-sun");
+ themeSwitch.classList.add("la-moon");
+ } else {
+ theme = "auto";
+ }
+
+ document.documentElement.dataset.bsTheme = theme;
+ //localStorage.setItem("theme", theme);
+ setCookie("theme", theme);
+ //serverStorage.setItem();
+ e.preventDefault();
+ }
+
+});
+
+const selectedProductOptions = {};
+let variantId = '';
+// product page
+function productPage() {
+ /*
+ document.querySelectorAll('.quantity').forEach(e => e.addEventListener('click', function (e) {
+ let btn = e.target.closest(".btn-plus");
+ if (btn) {
+ let nrInput = btn.parentNode.querySelector("input[type=number]");
+ nrInput.value = parseInt(nrInput.value) + 1;
+ nrInput.dispatchEvent(new KeyboardEvent("change", {
+ bubbles: true,
+ cancelable: true,
+ }));
+ }
+ return false;
+ }));
+
+ document.querySelectorAll('.quantity').forEach(e => e.addEventListener('click', function (e) {
+ let btn = e.target.closest(".btn-minus");
+ if (btn) {
+ let nrInput = btn.parentNode.querySelector("input[type=number]");
+ nrInput.value = Math.max(1, parseInt(nrInput.value) - 1);
+ nrInput.dispatchEvent(new KeyboardEvent("change", {
+ bubbles: true,
+ cancelable: true,
+ }));
+ }
+ return false;
+ }));
+ */
+ function zoom(e) {
+ let img = e.currentTarget;
+ let offsetX = e.offsetX || (e.touches ? e.touches[0].pageX : 0);
+ let offsetY = e.offsetY || (e.touches ? e.touches[0].pageY : 0);
+
+ let x = offsetX / img.offsetWidth * 100;
+ let y = offsetY / img.offsetHeight * 100;
+ img.style.backgroundPosition = x + "% " + y + "%";
+ }
+
+ document.querySelectorAll('div.zoom').forEach(e => e.addEventListener('mousemove', zoom));
+
+ let productOptionsContainer = document.getElementById("product-options");
+ if (typeof productVariants !== "undefined" && productVariants && productOptionsContainer) {
+ productOptionsContainer.querySelectorAll('input[type="radio"]:checked').forEach(radio => {
+ let id = radio.name.match(/(\d+)/)[1] ?? false;
+ if (id) {
+ selectedProductOptions[id] = radio.value;
+ }
+ });
+
+ productOptionsContainer.addEventListener("click", function (e) {
+
+ let element = e.target.closest('input[type="radio"]');
+ if (element) {
+ let id = element.name.match(/(\d+)/)[1] ?? false;
+ if (id) {
+ selectedProductOptions[id] = element.value;
+ }
+ let variantId = JSON.stringify(selectedProductOptions).replaceAll(/[^\d:,]+/g,'');
+
+ let variant = productVariants[variantId];
+ let isStock = variant && variant.stock_quantity > 0 ? true : false;
+
+ if (variant) {
+ productOptionsContainer.querySelectorAll('input[type="radio"]').forEach(radio => {
+ const currentOptions = Object.assign({}, selectedProductOptions);
+ let id = radio.name.match(/(\d+)/)[1] ?? false;
+ if (id) {
+ currentOptions[id] = radio.value;
+ let currentVariantId = JSON.stringify(currentOptions).replaceAll(/[^\d:,]+/g,'');
+ let variant = productVariants[currentVariantId];
+ let isStock = variant && variant.stock_quantity > 0 ? true : false;
+ let text = radio.parentNode.querySelector("[data-v-value-name]");
+
+ if (isStock) {
+ text.style.textDecoration = "";
+ text.style.opacity = "";
+ } else {
+ text.style.textDecoration = "line-through";
+ text.style.opacity = 0.5;
+ }
+ }
+ });
+ }
+
+ document.querySelector("[data-v-product-price_tax_formatted]").innerText = variant ? variant.price_formatted : "";
+ document.querySelector("#button-cart").disabled = !isStock;
+ document.querySelector("#buynow").disabled = !isStock;
+ const product_variant_id = variant ? variant.product_variant_id : "";
+ const searchParams = new URLSearchParams(location.search);
+
+ document.querySelector('[name="product_variant_id"]').value = product_variant_id;
+ if (product_variant_id) {
+ searchParams.set('product_variant_id', product_variant_id);
+ } else {
+ searchParams.delete('product_variant_id');
+ }
+
+ window.history.replaceState({}, "", location.pathname + (Array.from(searchParams).length > 0 ? "?" :"") + searchParams.toString());
+ }
+ });
+ }
+}
+
+productPage();
+
+if ('serviceWorker' in navigator) {
+ navigator.serviceWorker.register('/service-worker.js', { scope: '/' })
+ .then(function (registration){console.log('Service worker registered successfully');})
+ .catch(function (e){console.error('Error during service worker registration:', e);});
+}
+
+function togglePasswordInput(element, input) {
+ let password = document.getElementById(input);
+ if (password.type == "password") {
+ password.type = "text";
+ let i = element.querySelector("i")
+ i.classList.add("la-eye")
+ i.classList.remove("la-eye-slash");
+ } else {
+ password.type = "password";
+ let i = element.querySelector("i")
+ i.classList.remove("la-eye")
+ i.classList.add("la-eye-slash");
+ }
+}
+/*
+document.addEventListener("click", function (e) {
+ let link = e.target.closest(".dropdown-toggle");
+ if (link) {
+ let parent = link.closest(".nav-toggle");
+ if (link.classList.contains("show")) {
+ parent.classList.add("show");
+ } else {
+ parent.classList.remove("show");
+ }
+ }
+});
+*/
+
+function updateProgressStatus(percent) {
+ const progressElement = document.getElementById('page-loading-status');
+ if (progressElement) {
+ progressElement.style.width = percent + '%';
+ }
+/*
+ if (percent == 0) {
+ progressElement.classList.add("d-none");
+ } else {
+ progressElement.classList.remove("d-none");
+ }
+*/
+}
+/*
+function fixAnchorUrl() {
+ let pathname = location.href.split('#')[0];
+ [].forEach.call(document.querySelectorAll("a[href^='#']"), function(a) {
+ a.href = pathname + a.getAttribute("href");
+ });
+}
+
+document.addEventListener('DOMContentLoaded', fixAnchorUrl);
+*/
+//init specific page js code after page is loaded through ajax
+function afterPageLoad() {
+ //check if product page to add specific listners
+ if(document.querySelector("[data-v-component-product]")) {
+ productPage();
+ }
+
+ if(document.querySelector("[data-v-cart-page]")) {
+ //cartPage();
+ }
+ //get navbar for sticky
+ navbar = document.getElementsByClassName("navbar")[0];
+
+ //fixAnchorUrl();
+}
+
+//theme ajax configuration
+
+//include elements that will be updated on ajax calls, include body > section to trigger whole page update if sections mismatch between different page structures
+VvvebTheme.ajax.siteContainer = ["#site-content", ".inner-page-hero", "body > section", "body > nav"];
+VvvebTheme.ajax.progressStatus = updateProgressStatus;
+VvvebTheme.ajax.afterLoad = afterPageLoad;
+//include posts, product and menu items for ajax
+//VvvebTheme.ajax.selector = "a[data-url], a[data-page-url], a[data-v-url], a[data-v-menu-item-url], a[data-v-post-url], a[data-v-product-url], a[data-v-cat-url], a[data-v-archive-url], a[data-v-admin-url], a[data-v-post-author-url], a[data-v-breadcrumb-item-url], a[data-v-categories-cat-url], a[data-v-cart-product-url]";
+//skip home for dark hero and contact form for google js code
+VvvebTheme.ajax.skipUrl = ["/checkout"/*, "/page/contact"*/];
+
+//image lightbox
+if (typeof GLightbox !== 'undefined') {
+const lightbox = GLightbox();
+//let gloptions = {selector:".gallery a, .carousel-item a,a img, img"};
+document.addEventListener("click", function(event) {
+ let element = event.target.closest("img");
+
+ if (element) {
+ let elements = [];
+ let gallery = element.closest(".gallery");
+ let index = 0;
+ let count = 0;
+ let filter = function(img) {
+ let href = img?.parentNode?.attributes['href']?.nodeValue ?? img.src;
+ if (!href || href == "#") {
+ href = img.src;
+ }
+
+ if (img == element) {
+ index = count;
+ }
+
+ count++;
+ return {'type': 'image', href}
+ }
+
+ if (gallery) {
+ elements = [...gallery.querySelectorAll("img")].map(filter);
+ } else {
+ gallery = element.closest(".carousel");
+ if (gallery) {
+ elements = [...gallery.querySelectorAll("img")].map(filter);
+ }
+ }
+
+ if (elements.length == 0) {
+ if (element.parentNode && element.parentNode.tagName == "A") {
+ if (element.parentNode.href == "" || element.parentNode.href == "#") {
+ elements = [{"href": element.src}];
+ }
+
+ if (element.parentNode.href.split('.').pop() == element.src.split('.').pop()) {
+ elements = [{"href": element.parentNode.href}];
+ }
+ } else {
+ //elements = [{"href": element.src}];
+ }
+ }
+
+ if (elements && elements.length) {
+ lightbox.setElements(elements);
+ //lightbox.open();
+ lightbox.openAt(index);
+ event.preventDefault();
+ }
+ }
+});
+}