Advertisement
Guest User

Untitled

a guest
Aug 15th, 2019
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Section: VAT Exemption Modal
  3.  * ------------------------------------------------------------------------------
  4.  * A VAT Exemption file that contains scripts to function.
  5.  *
  6.  * @namespace vatExemptionModal
  7.  */
  8. import Cookies from 'js-cookie';
  9. import cssClasses from '../helpers/cssClasses';
  10. import {register} from '@shopify/theme-sections';
  11. import {on} from '../helpers/utils';
  12. import AjaxCart from '../helpers/ajax-cart';
  13. import Modal from '../components/modal';
  14.  
  15. /**
  16.  * DOM selectors.
  17.  */
  18. const selectors = {
  19.   modal: 'vat-exemption-modal',
  20.   addToCart: '[js-vat-exemption="addToCart',
  21.   charityAddToCart: '[js-vat-exemption="charityAddToCart"]',
  22.   continueShopping: '[js-vat-exemption="continueShopping"]',
  23.   form: '[js-product-form="container"]',
  24.   section: '[js-vat-exemption="section"]',
  25.   tabControl: '[js-vat-exemption="tabControl"]',
  26.   tabContent: '[js-vat-exemption="tabContent"]',
  27.   buttonGroups: '[js-vat-exemption="buttonGroups"]',
  28.   input: '[name="properties[VAT Relief]"]',
  29.   condition: '[js-vat-exemption="condition"]',
  30.   charity: '[js-vat-exemption="charity"]',
  31. };
  32.  
  33. /**
  34.  * Register the `vat-exemption-modal` section type.
  35.  */
  36. register('vat-exemption-modal', {
  37.  
  38.   /**
  39.    * DOM node selectors.
  40.    */
  41.   setNodeSelectors() {
  42.     this.form = null;
  43.     this.target = null;
  44.  
  45.     this.nodeSelectors = {
  46.       modal: document.querySelector(selectors.modal),
  47.       form: document.querySelector(selectors.form),
  48.       addToCart: document.querySelector(selectors.addToCart),
  49.       charityAddToCart: document.querySelector(selectors.charityAddToCart),
  50.       continueShopping: document.querySelector(selectors.continueShopping),
  51.       buttonGroups: document.querySelector(selectors.buttonGroups),
  52.       sections: [...document.querySelectorAll(selectors.section)],
  53.       tabControls: [...document.querySelectorAll(selectors.tabControl)],
  54.       tabContents: [...document.querySelectorAll(selectors.tabContent)],
  55.       condition: document.querySelector(selectors.condition),
  56.       charity: document.querySelector(selectors.charity),
  57.     };
  58.   },
  59.  
  60.   /**
  61.    * Construct the modal.
  62.    */
  63.   constructModal() {
  64.     this.modal = new Modal(selectors.modal);
  65.   },
  66.  
  67.   /**
  68.    * Initiate component.
  69.    */
  70.   init() {
  71.     this.setNodeSelectors();
  72.     this.constructModal();
  73.     this.setTriggerEvents();
  74.  
  75.     this.settings = {
  76.       namespace: 'vat_relief',
  77.     };
  78.  
  79.     this.modal.init();
  80.   },
  81.  
  82.   /**
  83.    * Set trigger listeners.
  84.    */
  85.   setTriggerEvents() {
  86.     Frame.EventBus.listen('AjaxCart:exemptItem:attempted', (target) => this.handleExemptAttempt(target));
  87.  
  88.     this.nodeSelectors.tabControls.forEach((control) => {
  89.       on('click', control, (event) => this.handleTabSelection(event));
  90.     });
  91.  
  92.     on('click', this.nodeSelectors.addToCart, (event) => {
  93.       this.handleAddToCart(event);
  94.     });
  95.  
  96.     on('click', this.nodeSelectors.charityAddToCart, (event) => {
  97.       this.handleCharityAddToCart(event);
  98.     });
  99.  
  100.     on('click', this.nodeSelectors.continueShopping, (event) => {
  101.       this.handleContinueShopping(event);
  102.     });
  103.   },
  104.  
  105.   /**
  106.    * Handle changing the selected tab
  107.    * @param {object} event - Click and key down event.
  108.    */
  109.   handleTabSelection(event) {
  110.     this.nodeSelectors.sections.forEach((control) => {
  111.       control.classList.remove(cssClasses.active);
  112.       control.lastElementChild.style.height = '0px';
  113.     });
  114.     event.currentTarget.closest(selectors.section).classList.add(cssClasses.active);
  115.     var content = event.currentTarget.nextElementSibling;
  116.     var newHeight = content.children[0].offsetHeight;
  117.     content.style.height = newHeight + 'px';
  118.   },
  119.  
  120.   /**
  121.    * Handle add to cart event and set loading state.
  122.    * @param {object} event - Click and key down event.
  123.    */
  124.   handleAddToCart(event) {
  125.     event.preventDefault();
  126.  
  127.     this.closeModal();
  128.     this.setExemptProperty();
  129.  
  130.     AjaxCart().submitForm(this.target, this.form);
  131.   },
  132.  
  133.   /**
  134.    * Handle charity add to cart event and set loading state.
  135.    * @param {object} event - Click and key down event.
  136.    */
  137.   handleCharityAddToCart(event) {
  138.     event.preventDefault();
  139.  
  140.     if (this.nodeSelectors.charity.value) {
  141.       this.closeModal();
  142.       this.setCharityExemptProperty();
  143.  
  144.       AjaxCart().submitForm(this.target, this.form);
  145.     }
  146.   },
  147.  
  148.   /**
  149.    * Handle continue shopping event.
  150.    * @param {object} event - Click and key down event.
  151.    */
  152.   handleContinueShopping(event) {
  153.     event.preventDefault();
  154.  
  155.     this.closeModal();
  156.   },
  157.  
  158.   /**
  159.    * Handle the exempt add attempt.
  160.    */
  161.   handleExemptAttempt(target) {
  162.     this.form = target.closest('form');
  163.     this.target = target;
  164.  
  165.     if (!this.form) {
  166.       throw new Error('Form element is required.');
  167.     }
  168.  
  169.     this.openModal();
  170.   },
  171.  
  172.   /**
  173.    * Set the exempt property.
  174.    */
  175.   setExemptProperty() {
  176.     var condition = this.nodeSelectors.condition.value.replace( /[\r\n]+/gm, "" );
  177.     var input = this.form.querySelector(selectors.input);
  178.     input.value = 'I declare that I am eligible for VAT relief. ' +
  179.       'MEDICAL CONDITION: ' + condition
  180.     Cookies.set(this.settings.namespace, input.value, {expires: 365});
  181.   },
  182.  
  183.   /**
  184.    * Set the charity exempt property.
  185.    */
  186.   setCharityExemptProperty() {
  187.     var charity = + this.nodeSelectors.charity.value.replace( /[\r\n]+/gm, "" );
  188.     var input = this.form.querySelector(selectors.input);
  189.     input.value = 'I am purchasing this item on behalf of a registered ' +
  190.       'charity. REGISTERED CHARITY NUMBER: ' + charity
  191.     Cookies.set(this.settings.namespace, input.value, {expires: 365});
  192.   },
  193.  
  194.   /**
  195.    * Open the modal.
  196.    */
  197.   openModal() {
  198.     this.modal.show();
  199.   },
  200.  
  201.   /**
  202.    * Close the modal.
  203.    */
  204.   closeModal() {
  205.     this.modal.close();
  206.   },
  207.  
  208.   /**
  209.    * Callback function when section is loaded via 'sections.load()' or by the Theme Editor 'shopify:section:load' event.
  210.    */
  211.   onLoad() {
  212.     this.init();
  213.   },
  214. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement