cityssm/lottery-licence-manager

View on GitHub
public/javascripts/location-edit.min.js

Summary

Maintainability
A
0 mins
Test Coverage
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),(()=>{const e=document.querySelector("main").dataset.urlPrefix,t=document.querySelector("#form--location"),o=document.querySelector("#container--form-message"),a=document.querySelector("#location--locationID").value;let s=!1;const n=""===a,i="true"===document.querySelector("main").dataset.isAdmin;if(t.addEventListener("submit",a=>{a.preventDefault(),o.innerHTML='Saving... <i class="fas fa-circle-notch fa-spin" aria-hidden="true"></i>',cityssm.postJSON(e+(n?"/locations/doCreate":"/locations/doUpdate"),t,t=>{t.success&&(s=!1,cityssm.disableNavBlocker()),t.success&&n?window.location.href=e+"/locations/"+t.locationID.toString()+"/edit":(o.innerHTML="",cityssm.alertModal(t.message,"","OK",t.success?"success":"danger"))})}),!n){const o=()=>{cityssm.postJSON(e+"/locations/doDelete",{locationID:a},t=>{t.success&&(window.location.href=e+"/locations")})};t.querySelector(".is-delete-button").addEventListener("click",e=>{e.preventDefault(),cityssm.confirmModal("Delete Location?","Are you sure you want to delete this location?<br />Note that any active licences associated with this location will remain active.","Yes, Delete Location","warning",o)})}if(!n&&i){const o=Number.parseInt(a,10);t.querySelector(".is-merge-button").addEventListener("click",t=>{if(t.preventDefault(),s)return void cityssm.alertModal("Unsaved Changes","You must save all unsaved changes before merging this location record.","OK","warning");const n=document.querySelector("#location--locationName").value,i=(""===n?document.querySelector("#location--locationAddress1").value:n)+", #"+a;let c,l,r,d="",u=[];const m=()=>{cityssm.postJSON(e+"/locations/doMerge",{targetLocationID:a,sourceLocationID:d},e=>{e.success?window.location.reload():cityssm.alertModal("Merge Not Completed","Please try again.","OK","danger")})},p=e=>{e.preventDefault();const t=e.currentTarget;d=t.getAttribute("data-location-id");const o=t.getAttribute("data-location-display-name");r(),cityssm.confirmModal("Confirm Merge","Are you sure you want to update all licences associated with <em>"+o+", #"+d+"</em> and associate them with <em>"+i+"</em>?","Yes, Complete Merge","warning",m)},f=()=>{const e=c.value.trim().toLowerCase().split(" "),t=document.createElement("div");t.className="panel";for(const a of u){if(a.locationID===o)continue;let s=!0;for(const t of e)if(!a.locationName.toLowerCase().includes(t)){s=!1;break}if(!s)continue;const n=document.createElement("a");n.className="panel-block is-block",n.dataset.locationId=a.locationID.toString(),n.dataset.locationDisplayName=a.locationDisplayName,n.addEventListener("click",p),n.innerHTML='<div class="level is-marginless"><div class="level-left"><div>'+cityssm.escapeHTML(a.locationDisplayName)+"<br /><small>"+cityssm.escapeHTML(a.locationAddress1)+'</small></div></div><div class="level-right">#'+a.locationID.toString()+'</div></div><div class="has-text-right">'+(a.licences_count>0?' <span class="tag is-info has-tooltip-left" data-tooltip="Licence Count"><span class="icon"><i class="fas fa-certificate" aria-hidden="true"></i></span> <span>'+a.licences_count.toString()+"</span></span>":"")+(a.distributor_count>0?' <span class="tag is-info has-tooltip-left" data-tooltip="Distributor Count"><span class="icon"><i class="fas fa-truck-moving" aria-hidden="true"></i></span> <span>'+a.distributor_count.toString()+"</span></span>":"")+(a.manufacturer_count>0?' <span class="tag is-info has-tooltip-left" data-tooltip="Manufacturer Count"><span class="icon"><i class="fas fa-print" aria-hidden="true"></i></span> <span>'+a:"")+"</div>",t.append(n)}cityssm.clearElement(l),l.append(t)};cityssm.openHtmlModal("locationMerge",{onshow(e){const t=e.querySelectorAll(".mergeLocation--locationDisplayNameAndID_target");for(const e of t)e.textContent=i;l=document.querySelector("#container--sourceLocations"),(c=document.querySelector("#mergeLocation--locationFilter")).addEventListener("keyup",f)},onshown(t,o){r=o,cityssm.postJSON(e+"/locations/doGetLocations",{limit:-1},e=>{u=e.locations,c.removeAttribute("disabled"),c.focus(),f()})}})})}const c=()=>{s=!0,cityssm.enableNavBlocker(),o.innerHTML='<span class="tag is-light is-info is-medium"><span class="icon"><i class="fas fa-exclamation-triangle" aria-hidden="true"></i></span> <span>Unsaved Changes</span></div>'},l=t.querySelectorAll("input");for(const e of l)e.addEventListener("change",c);const r=document.querySelector("#location--locationName");n&&r.focus();const d=document.querySelector("#location--locationIsDistributor"),u=document.querySelector("#location--locationIsManufacturer"),m=()=>{d.checked||u.checked?r.setAttribute("required","required"):r.removeAttribute("required")};d.addEventListener("change",m),u.addEventListener("change",m)})();