Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class DonationModule {
  2.     constructor(options) {
  3.         this.options = DonationModule.updateSettings(options);
  4.         this.donationAmmount;
  5.         this.giftaidAmmount;
  6.         this.DonationAmount = document.getElementById("DonationAmount");
  7.     }
  8.  
  9.     static updateSettings(userSettings) {
  10.         const defaultSettings = {
  11.             donationModuleEl: ".donation-module", //Dom El
  12.             donationAmountRadioEls: ".donation-amount-input.is-radio input", //String
  13.             donationAmountTextEl: ".donation-amount-input.is-text input", //String
  14.             giftaidValueEls: ".giftaid-value", //String,
  15.             tagsRadioEls: ".donation-tag-input input", //String
  16.             tagsErrorMsgEl: ".tag-error-msg", //String
  17.             giftaidPercentage: 25,
  18.             wallPlaquesMin: 250,
  19.             tributeTags: document.querySelectorAll(".donation-tribute-tag"),
  20.             wallPlaques: document.querySelectorAll(".donation-wall-plaque"),
  21.             donationTributeTag: "#donationTributeTag",
  22.             donationWallPlaque: "#donationWallPlaque"
  23.         };
  24.        
  25.         for (const attrname in userSettings) {
  26.             defaultSettings[attrname] = userSettings[attrname];
  27.         }
  28.  
  29.         defaultSettings.donationModuleEl = typeof defaultSettings.donationModuleEl === 'string' ? document.querySelector(defaultSettings.donationModuleEl) : defaultSettings.donationModuleEl;
  30.  
  31.         return defaultSettings;
  32.     }
  33.  
  34.     init() {
  35.         if (this.options.donationModuleEl) {
  36.             const _this = this;
  37.             this.options.donationAmountRadioEls = this.options.donationModuleEl.querySelectorAll(this.options.donationAmountRadioEls);
  38.             this.options.donationAmountTextEl = this.options.donationModuleEl.querySelector(this.options.donationAmountTextEl);
  39.             this.options.giftaidValueEls = this.options.donationModuleEl.querySelectorAll(this.options.giftaidValueEls);
  40.             this.options.tagsRadioEls = this.options.donationModuleEl.querySelectorAll(this.options.tagsRadioEls);
  41.             this.options.tagsErrorMsgEl = this.options.donationModuleEl.querySelector(this.options.tagsErrorMsgEl);
  42.  
  43.            
  44.             Array.prototype.forEach.call(this.options.donationAmountRadioEls, (donationAmountRadioEl, i) => {
  45.                 donationAmountRadioEl.addEventListener("change", (event) => {
  46.                     _this.getRadioValue();
  47.                 });
  48.             });
  49.  
  50.             Array.prototype.forEach.call(this.options.tagsRadioEls, (radioEl, i) => {
  51.                 radioEl.addEventListener("change", (event) => {
  52.                     //_this.uiHandler();
  53.                     _this.getRadioValues2();
  54.                 });
  55.             });
  56.  
  57.             ["keyup", "change"].forEach(eventType => {
  58.                 this.options.donationAmountTextEl.addEventListener(eventType, (event) => {
  59.                     this.getDonationAmountTextValue();
  60.                     console.log(this.options.donationAmountTextEl)
  61.                 });
  62.             });
  63.         }
  64.  
  65.         if (this.options.tributeTags.length > 0) {
  66.             this.tagsAndWallPlaquesHandler();
  67.         }
  68.     }
  69.  
  70.     // updating the donation input value
  71.     getDonationAmountTextValue() {
  72.         this.donationAmmount = this.options.donationAmountTextEl.value;
  73.         console.log(this.options.donationAmountTextEl.value)
  74.         Array.prototype.forEach.call(this.options.donationAmountRadioEls, (donationAmountRadioEl, i) => {
  75.             donationAmountRadioEl.checked = false;
  76.         });
  77.  
  78.         this.uiHandler();
  79.     }
  80.  
  81.     //Radio inputs for £3, £15, £20
  82.     getRadioValue() {
  83.        
  84.         Array.prototype.forEach.call(this.options.donationAmountRadioEls, (donationAmountRadioEl, i) => {
  85.             if (donationAmountRadioEl.checked) {
  86.                 this.donationAmmount = donationAmountRadioEl.value;
  87.                 this.uiHandler();
  88.             }
  89.         });  
  90.     }
  91.  
  92.     getRadioValues2() {
  93.  
  94.         Array.prototype.forEach.call(this.options.tagsRadioEls, (tagsRadioEl, i) => {
  95.             if (tagsRadioEl.checked) {
  96.                 this.donationAmmount = tagsRadioEl.value;
  97.                 this.uiHandler();
  98.             }
  99.         });
  100.     }
  101.  
  102.     uiHandler() {
  103.         this.giftaidAmmount = this.donationAmmount * (this.options.giftaidPercentage / 100);
  104.         this.options.donationAmountTextEl.value = this.donationAmmount;
  105.  
  106.         Array.prototype.forEach.call(this.options.giftaidValueEls, (giftaidValueEl, i) => {
  107.             giftaidValueEl.innerHTML = this.giftaidAmmount;
  108.         });
  109.  
  110.         if (this.options.tagsRadioEls.length) {
  111.             Array.prototype.forEach.call(this.options.tagsRadioEls, (radio, i) => {
  112.                 //debugger;
  113.                 if (radio.checked && radio.value === "wallPlaques" && this.donationAmmount < this.options.wallPlaquesMin) {
  114.                     this.options.tagsErrorMsgEl.classList.remove("is-d-none");
  115.                 } else {
  116.                     this.options.tagsErrorMsgEl.classList.add("is-d-none");
  117.                 }
  118.  
  119.             });
  120.         }
  121.     }
  122.  
  123.     // A function to set amount's manually - Tan
  124.     setDonationAmount(amount) {
  125.         this.DonationAmount.value = amount;
  126.         this.giftaidAmmount = this.DonationAmount.value * (this.options.giftaidPercentage / 100);
  127.         this.options.donationAmountTextEl.value = this.DonationAmount.value;
  128.  
  129.         Array.prototype.forEach.call(this.options.giftaidValueEls, (giftaidValueEl, i) => {
  130.             giftaidValueEl.innerHTML = this.giftaidAmmount;
  131.         });
  132.     }
  133.  
  134.     tagsAndWallPlaquesHandler() {
  135.         // this sets initial amount to 25
  136.         this.setDonationAmount(25);
  137.  
  138.         this.options.donationTributeTag.addEventListener("click", () => {
  139.             this.setDonationAmount(25);
  140.             // this is some wierd bug, needs 2 clicks for the setDonationAmount() function above to work
  141.             //donationTributeTag.click();
  142.             this.options.tributeTags.forEach((item, index) => {
  143.                 this.options.tributeTags[index].style.display = "block";
  144.                 this.options.wallPlaques[index].style.display = "none";
  145.             })
  146.         })
  147.  
  148.         donationWallPlaque.addEventListener("click", () => {
  149.             this.setDonationAmount(250);
  150.             // this is some wierd bug, needs 2 clicks for the setDonationAmount() works
  151.             //donationWallPlaque.click();
  152.             this.options.wallPlaques.forEach((item, index) => {
  153.                 this.options.wallPlaques[index].style.display = "block";
  154.                 this.options.tributeTags[index].style.display = "none";
  155.             })
  156.         })
  157.  
  158.         this.options.tributeTags.forEach((item, index) => {
  159.             this.options.tributeTags[index].style.display = "block";
  160.             this.options.wallPlaques[index].style.display = "none";
  161.         })
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement