// ****************************************************************** // DATA WRAPPERS -- FOR INITIAL CHECKS // ****************************************************************** // Find data wrappers -- ideally with syntax so that there will not be a console error thrown on non-product pages [ie. no multiple query selectors / finds]. var approve_product_wrapper = document.querySelector('.single_product__info'); var approve_price_wrapper = document.querySelector("#single_product__price-template-product"); var approve_model_wrapper = document.querySelector('.single_product__title'); var approve_qty_ele = document.querySelector('.quantity_input'); // ****************************************************************** // DEBUG MODE -- Set to true if you want console logs to show. // ****************************************************************** var approve_debug_mode = false; // ****************************************************************** // Handles both the creation and update of the approve button. // ****************************************************************** function init_approve_button(){ if (!approve_price_wrapper || !approve_model_wrapper){ if (approve_debug_mode){ if (!approve_price_wrapper){ console.log("The approve price wrapper is missing from the page."); } if (!approve_model_wrapper){ console.log("The approve model wrapper is missing from the page."); } } return; } // Find price & clean // var approve_price_wrapper2 = approve_price_wrapper.querySelector(".money"); // if (!approve_price_wrapper2){ // if (approve_debug_mode){ // console.log("The approve price wrapper2 is missing from the page."); // } // } var approve_price = approve_price_wrapper.innerHTML; if (!approve_price){ if (approve_debug_mode){ console.log("No price found."); } return; } approve_price = approve_price.replace(/[^0-9-.]/g, ''); approve_price = parseInt(approve_price); // Find model. Clean if necessary. var approve_model = approve_model_wrapper.textContent; if (!approve_model){ if (approve_debug_mode){ console.log("No model found."); } return; } // If qty wrapper exists, find qty. Otherwise, qty is set to 1. if (approve_qty_ele){ var approve_qty = approve_qty_ele.value; } else { if (approve_debug_mode){ console.log("No qty element found."); } var approve_qty = 1; } // ****************************************************************** // FIND SELECT OPTIONS // ****************************************************************** // approve_selected_options is options that are SELECTED. Here we will add their label to the model name. // var approve_selected_options = document.querySelectorAll("input[type='radio']:checked"); // approve_selected_options.forEach(function (item, index) { // var approve_option_name = item.getAttribute("aria-label"); // approve_model = approve_model + " / " + approve_option_name; // }); // // If a change listener is needed for options, it is initliazed here. // var approve_options = document.querySelectorAll("input[type='radio']"); // approve_options.forEach(function (item, index) { // item.addEventListener('change',event => { // init_approve_button(); // }) // }); if(isNaN(approve_price) || !approve_model || !approve_qty){ if (approve_debug_mode){ console.log(approve_price); console.log(approve_model); console.log(approve_qty); console.log("Something went wrong in the configuration of button variables"); } return; } // Find the element where we want to insert our button. var approve_insert_after_ele = document.querySelector('.details_row-button'); if(!approve_insert_after_ele){ if (approve_debug_mode){ console.log("Element to insert approve button after not found."); } return; } // Check to see if the approve button is already on the page. If it isn't, we will create it now. var approve_button = document.getElementById('approve_button_id'); if (!approve_button){ var approve_button = document.createElement('approve-button'); approve_button.id = "approve_button_id"; approve_button.style.display="inline-block"; approve_button.style.marginTop="30px"; approve_button.style.textAlign="center"; approve_button.style.marginBottom="30px"; approve_button.setAttribute('application-type',"embedded_app"); approve_insert_after_ele.after(approve_button); } // If price is below $200, teaser rate breaks. Let's ensure that it's above $200. if ((parseFloat(approve_price) * parseInt(approve_qty)) < 200){ if (approve_debug_mode){ console.log("Price is below $200."); } approve_button.style.display = "none"; return; } // Set approve button variables. approve_button.setAttribute('price',approve_price); approve_button.setAttribute('model',approve_model); approve_button.setAttribute('qty',approve_qty); approve_button.setAttribute('type',"new_product"); } // ****************************************************************** // Simple set button qty. Must pass the new QTY to the button. // ****************************************************************** 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); } } } // ****************************************************************** // If timing is an issue, this function creates an interval for setting button qty. // ****************************************************************** var approve_timer = null; function approve_update_qty_timer(){ var approve_button = document.getElementById('approve_button_id'); if (approve_button){ if(approve_timer) { clearInterval(approve_timer); approve_timer = null; } var approve_btn_qty = parseInt(approve_button.getAttribute('qty')); var number_of_checks = 0; approve_timer = setInterval(function(){ if(number_of_checks>=5){ clearInterval(approve_timer); approve_timer = null; } if(approve_btn_qty != parseInt(approve_qty_ele.value)){ approve_button.setAttribute('qty',parseInt(approve_qty_ele.value)); clearInterval(approve_timer); approve_timer = null; } number_of_checks++; },500) } } if (approve_product_wrapper){ init_approve_button(); /// If there is a qty element, we need to set watchers/listeners. if (approve_qty_ele){ // Add change watcher to qty input approve_qty_ele.addEventListener('change',event => { var approve_qty = parseInt(approve_qty_ele.value); approve_update_qty_timer(); }) // buttons that raise or lower the quantity. var approve_qty_btn_inc = document.querySelector('.quantity_up'); var approve_qty_btn_dec = document.querySelector('.quantity_down'); // 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_qty = parseInt(approve_qty_ele.value); approve_update_qty_timer(); }); approve_qty_btn_dec.addEventListener('click',event => { var approve_qty = parseInt(approve_qty_ele.value); approve_update_qty_timer(); }); } else { if (approve_debug_mode){ console.log("No qty buttons found."); } } // Add mutation watcher to qty input // if (approve_debug_mode){ // console.log("Qty element found. Adding mutation observer"); // } // const approve_qty_observer = new MutationObserver(function() { // if (approve_debug_mode){ // console.log("Watcher called"); // } // init_approve_button(); // }); // approve_qty_observer.observe(approve_qty_ele,{subtree: true, childList: true}); } else { if (approve_debug_mode){ console.log("No qty element found."); } } if (approve_price_wrapper){ // Add observer to price element const approve_price_observer = new MutationObserver(function() { init_approve_button(); }); approve_price_observer.observe(approve_price_wrapper,{subtree: false, childList: true}); } } else { if (!approve_product_wrapper){ console.log("No approve_product_wrapper found"); } }