// Version: 2 | Author: Matt | Date: 12/5/24 // DEBUG MODE -- Set to true if you want console logs to show. var approve_debug_mode = true; approve_debug_log("APPROVE SCRIPT LOADED"); // Checks if we are on a mobile device or not // Use boolean to render different properties whether a user is on mobile or not var isMobile = window.innerWidth <= 600; var button_styling = { button_color: '', hover_color: '', button_height: '', button_border: '', border_radius: '', background_gradient: '', background_gradient_hover: '', font: '', font_color: '', font_hover_color: '', font_size: '12px', font_weight: '', text_transform: '', powered_by_color: '', powered_by_size: '11px', max_width: '', min_width: '', width: '100%' }; var minimum_threshold = null; // Global setup var approve_gallery_wrapper_ele = ' .w-dyn-list'; // Driver function to run integration function start_approve_integration() { // Checks if we are on a gallery page if (document.querySelector(approve_gallery_wrapper_ele)) { approve_gallery_page_integration(); } else { approve_debug_log('approve_gallery_wrapper_ele not found', 1); } } // Gallery page integration function approve_gallery_page_integration() { if (approve_gallery_wrapper_ele && document.querySelector(approve_gallery_wrapper_ele)) { function init_approve_gallery_page_buttons(gallery_wrapper_ele_name) { // ****************************************************************** // PRODUCT GALLERY Variable Configuration // ****************************************************************** var approve_gallery_product = { page_check_ele_name: gallery_wrapper_ele_name, item_ele: gallery_wrapper_ele_name + ' .w-dyn-item', item_model_ele_name: ' .flex.vertical.align-center .bold.capitalize', item_price_ele_name: ' .flex.vertical.align-center .bold.capitalize', item_insert_after_ele_name: ' .image-wrap-2', // optional item_qty_ele_name: '', item_inc_quantity_ele_name: '', item_dec_quantity_ele_name: '', item_options_wrapper_ele_name: '', item_select_ele_name: '', item_button_to_remove_ele: '', item_button_display_style: 'block', item_button_display_margin: '10px 0px' }; // PRODUCT GALLERY // check for items, loop through and init button function with each var approve_product_gallery_page_check = (approve_gallery_product.page_check_ele_name ? document.querySelector(approve_gallery_product.page_check_ele_name) : null); if (approve_product_gallery_page_check) { var approve_gallery_product_array = document.querySelectorAll(approve_gallery_product.item_ele); approve_gallery_product_array.forEach(function (item, idx) { init_approve_product_gallery_item_button(item, idx); }); } function init_approve_product_gallery_item_button(item, idx) { // model var approve_product_gallery_item_model_ele = (approve_gallery_product.item_model_ele_name ? item.querySelector(approve_gallery_product.item_model_ele_name) : null); if (!approve_product_gallery_item_model_ele) { approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_model_ele found.", 0, 1); return; } var approve_product_gallery_item_model = approve_product_gallery_item_model_ele.textContent.split(' - ')[0] // price var approve_product_gallery_item_price_ele = (approve_gallery_product.item_price_ele_name ? item.querySelector(approve_gallery_product.item_price_ele_name) : null); if (!approve_product_gallery_item_price_ele) { approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_price_ele found.", 0, 1); return; } var approve_product_gallery_item_price = approve_product_gallery_item_model_ele.textContent.split(' - ')[1].replace(/[^0-9.]/g, ''); approve_product_gallery_item_price = parseFloat(approve_product_gallery_item_price); if (!approve_product_gallery_item_price || approve_product_gallery_item_price == 0) { approve_debug_log("APPROVE: approve_product_gallery_item_price not found (or is 0) after removing non-numerical characters.", 1); } // qty var approve_product_gallery_item_qty_ele = (approve_gallery_product.item_qty_ele_name ? item.querySelector(approve_gallery_product.item_qty_ele_name) : null); var approve_product_gallery_item_qty = 1; if (!approve_product_gallery_item_qty_ele) { approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_qty_ele found.", 1); } else { approve_product_gallery_item_qty = approve_product_gallery_item_qty_ele.value; } approve_product_gallery_item_qty = parseInt(approve_product_gallery_item_qty); // element to insert after var approve_product_gallery_item_insert_after_ele = (approve_gallery_product.item_insert_after_ele_name ? item.querySelector(approve_gallery_product.item_insert_after_ele_name) : null); if (!approve_product_gallery_item_insert_after_ele) { approve_debug_log("APPROVE: No dynamic approve_product_gallery_item_insert_after_ele found.", 0, 1); return; } // Options if (approve_gallery_product.item_options_wrapper_ele_name) { var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_gallery_product.item_options_wrapper_ele_name); if (!approve_product_gallery_item_options_wrapper_ele) { approve_debug_log("No approve_product_gallery_item_options_wrapper_ele found."); } else { // TRH - added else to prevent js error when options dont exist var approve_product_gallery_item_selected_options = approve_product_gallery_item_options_wrapper_ele.querySelectorAll(approve_gallery_product.item_select_ele_name); if (!approve_product_gallery_item_selected_options) { approve_debug_log("No approve_product_gallery_item_selected_options found."); } approve_product_gallery_item_selected_options.forEach(function (item, index) { // CONFIGURE OPTIONS HERE }); } } // console.log("CHECK FOR ITEM BUTTON ", item, item.querySelector('#approve_product_gallery_item_button_'+idx)) var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_' + idx); if (!approve_product_gallery_item_button) { // If event listener is needed for options, here is where it can be initiated. if (approve_gallery_product.item_options_wrapper_ele_name) { var approve_product_gallery_item_options_wrapper_ele = document.querySelector(approve_gallery_product.item_options_wrapper_ele_name); if (approve_product_gallery_item_options_wrapper_ele) { // If a change listener is needed for options, it is initliazed here. var approve_product_gallery_item_options1 = approve_product_gallery_item_options_wrapper_ele.querySelectorAll(approve_gallery_product.item_select_ele_name); approve_product_gallery_item_options1.forEach(function (opt_item) { opt_item.addEventListener('change', event => { init_approve_product_gallery_item_button(item, idx); }) }); } } // Insert Button var approve_product_gallery_item_button = document.createElement('approve-button'); approve_product_gallery_item_button.id = "approve_product_gallery_item_button_" + idx; // BUTTON CSS if (approve_gallery_product.item_button_display_style) { approve_product_gallery_item_button.style.display = approve_gallery_product.item_button_display_style; } if (approve_gallery_product.item_button_display_margin) { approve_product_gallery_item_button.style.margin = approve_gallery_product.item_button_display_margin; } approve_product_gallery_item_button.setAttribute('application-type', "embedded_app"); var approve_product_gallery_item_btn_wrapper = document.createElement("div"); approve_product_gallery_item_btn_wrapper.appendChild(approve_product_gallery_item_button); approve_product_gallery_item_insert_after_ele.after(approve_product_gallery_item_btn_wrapper); } // Removing a button if (approve_gallery_product.item_button_to_remove_ele) { var approve_product_gallery_item_button_to_remove = document.querySelector(approve_gallery_product.item_button_to_remove_ele); if (approve_product_gallery_item_button_to_remove) { approve_product_gallery_item_button_to_remove.style.display = "none"; } } if (button_styling.button_color) approve_product_gallery_item_button.style.setProperty('--button_color', button_styling.button_color); if (button_styling.hover_color) approve_product_gallery_item_button.style.setProperty('--button_hover_color', button_styling.hover_color); if (button_styling.button_height) approve_product_gallery_item_button.style.setProperty('--button_height', button_styling.button_height); if (button_styling.button_border) approve_product_gallery_item_button.style.setProperty('--button_border', button_styling.button_border); if (button_styling.border_radius) approve_product_gallery_item_button.style.setProperty('--button_border_radius', button_styling.border_radius); if (button_styling.font) approve_product_gallery_item_button.style.setProperty('--button_font', button_styling.font); if (button_styling.font_color) approve_product_gallery_item_button.style.setProperty('--button_font_color', button_styling.font_color); if (button_styling.font_size) approve_product_gallery_item_button.style.setProperty('--button_font_size', button_styling.font_size); if (button_styling.text_transform) approve_product_gallery_item_button.style.setProperty('--button_text_transform', button_styling.text_transform); if (button_styling.font_hover_color) approve_product_gallery_item_button.style.setProperty('--button_font_hover_color', button_styling.font_hover_color); if (button_styling.background_gradient) approve_product_gallery_item_button.style.setProperty('--button_background_gradient', button_styling.background_gradient); if (button_styling.background_gradient_hover) approve_product_gallery_item_button.style.setProperty('--button_background_gradient_hover', button_styling.background_gradient_hover); if (button_styling.font_weight) approve_product_gallery_item_button.style.setProperty('--button_font_weight', button_styling.font_weight); if (button_styling.powered_by_color) approve_product_gallery_item_button.style.setProperty('--button_powered_by_color', button_styling.powered_by_color); if (button_styling.powered_by_size) approve_product_gallery_item_button.style.setProperty('--button_powered_by_size', button_styling.powered_by_size); // If price is below $200, teaser rate breaks. Let's ensure that it's above $500. if ((parseFloat(approve_product_gallery_item_price) * parseInt(approve_product_gallery_item_qty)) < 500) { approve_debug_log("Price is below $500."); approve_product_gallery_item_button.style.display = "none"; return; } // Set approve button variables. approve_product_gallery_item_button.setAttribute('price', approve_product_gallery_item_price); approve_product_gallery_item_button.setAttribute('model', approve_product_gallery_item_model); approve_product_gallery_item_button.setAttribute('qty', approve_product_gallery_item_qty); approve_product_gallery_item_button.setAttribute('type', "new_product"); if (approve_product_gallery_item_qty_ele) { // Add change watcher to qty input approve_product_gallery_item_qty_ele.addEventListener('change', event => { var this_item = item; approve_product_gallery_item_update_qty(approve_product_gallery_item_qty_ele.value, this_item, idx); }) if (approve_gallery_product.item_inc_quantity_ele_name && approve_gallery_product.item_dec_quantity_ele_name ) { approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx); } } } // ****************************************************************** // Simple set button qty. Must pass the new QTY to the button. // ****************************************************************** function approve_product_gallery_item_update_qty(new_qty, item, idx) { var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_' + idx); if (approve_product_gallery_item_button) { var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); if (new_qty != approve_product_gallery_item_btn_qty) { approve_product_gallery_item_button.setAttribute('qty', new_qty); } } } function approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx, number_of_tries = 0) { // console.log("INSIDE FUNCTION approve_product_gallery_item_activate_increase_and_decrease_buttons ", item, idx, number_of_tries); number_of_tries++; // buttons that raise or lower the quantity. var approve_product_gallery_item_qty_btn_inc = item.querySelector(approve_gallery_product.item_inc_quantity_ele_name); var approve_product_gallery_item_qty_btn_dec = item.querySelector(approve_gallery_product.item_dec_quantity_ele_name); if ((!approve_product_gallery_item_qty_btn_inc || !approve_product_gallery_item_qty_btn_dec ) && number_of_tries < 10 ) { setTimeout(() => { approve_product_gallery_item_activate_increase_and_decrease_buttons(item, idx, number_of_tries) }, 500); return; } // Assign click events to these buttons so that qty is updated on click. if (approve_product_gallery_item_qty_btn_inc && approve_product_gallery_item_qty_btn_dec ) { approve_product_gallery_item_qty_btn_inc.addEventListener('click', event => { var this_item = item; // console.log("INSIDE CLICK EVENT FOR BUTTON ", item); var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_' + idx); if (approve_product_gallery_item_button) { var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); approve_product_gallery_item_btn_qty = approve_product_gallery_item_btn_qty + 1; approve_product_gallery_item_update_qty(approve_product_gallery_item_btn_qty, item, idx); } }); approve_product_gallery_item_qty_btn_dec.addEventListener('click', event => { var approve_product_gallery_item_button = item.querySelector('#approve_product_gallery_item_button_' + idx); if (approve_product_gallery_item_button) { var approve_product_gallery_item_btn_qty = parseInt(approve_product_gallery_item_button.getAttribute('qty')); if (approve_product_gallery_item_btn_qty > 1) { approve_product_gallery_item_btn_qty = approve_product_gallery_item_btn_qty - 1; approve_product_gallery_item_update_qty(approve_product_gallery_item_btn_qty, item, idx); } } }); } else { approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.", 1); } } } init_approve_gallery_page_buttons(approve_gallery_wrapper_ele); } } // APPROVE color debug log function approve_debug_log(log, warn, err) { warn = warn || false; err = err || false; let css = "padding: 5px 20px; "; if (err) { css += "background:#8B0000; color:#fff; "; } else if (warn) { css += "background:#FFBF00; color:#000; "; } else { css += "background:#418AC9; color:#fff; "; } if (approve_debug_mode) console.log("%c" + log, css); } /** * Activates quantity buttons. * QTY buttons are, sometimes added to the page after a page has been rendered. This will wait for that to happen. */ function activate_increase_and_decrease_buttons(number_of_tries = 0, inc_quantity_ele, dec_quantity_ele) { number_of_tries++; // buttons that raise or lower the quantity. var approve_qty_btn_inc = document.querySelector(inc_quantity_ele); var approve_qty_btn_dec = document.querySelector(dec_quantity_ele); if ((!approve_qty_btn_inc || !approve_qty_btn_dec) && number_of_tries < 10) { setTimeout(() => (activate_increase_and_decrease_buttons(number_of_tries, inc_quantity_ele, dec_quantity_ele)), 500); return } // Assign click events to these buttons so that qty is updated on click. if (approve_qty_btn_inc && approve_qty_btn_dec) { approve_qty_btn_inc.addEventListener('click', event => { var approve_button = document.getElementById('approve_button_id'); if (approve_button) { var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); approve_btn_qty = approve_btn_qty + 1; approve_update_qty(approve_btn_qty); approve_update_text_qty(approve_btn_qty); } }); approve_qty_btn_dec.addEventListener('click', event => { var approve_button = document.getElementById('approve_button_id'); if (approve_button) { var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); if (approve_btn_qty > 1) { approve_btn_qty = approve_btn_qty - 1; approve_update_qty(approve_btn_qty); approve_update_text_qty(approve_btn_qty); } } }); } else { approve_debug_log("APPROVE: No approve_qty_btn_inc or approve_qty_btn_dec found.", 1); } } // Function for updating button quantity function approve_update_qty(approve_qty) { var approve_button = document.getElementById('approve_button_id'); if (approve_button) { var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); if (approve_qty != approve_btn_qty) { approve_button.setAttribute('qty', approve_qty); } } } // Function for updating teaser text quantity function approve_update_text_qty(approve_qty) { console.log(approve_qty); var approve_button = document.querySelector('.approve-text-teaser'); console.log(approve_button); if (approve_button) { var approve_btn_qty = parseInt(approve_button.getAttribute('approve-qty')); if (approve_qty != approve_btn_qty) { approve_button.setAttribute('approve-qty', approve_qty); var teaser = approve_button.querySelector('.teaser-rate'); teaser.setAttribute('approve-total', teaser.getAttribute('default-price') * approve_qty) window.kwipped_approve.core.activate_approve_teaser_rates(); } } } // Run integration start_approve_integration();