app.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. class AddressMap {
  2. constructor(map) {
  3. this.element = map;
  4. this.$latitude = $("input[name=latitude]");
  5. this.$longitude = $("input[name=longitude]");
  6. this.map = new google.maps.Map(this.element, {
  7. center: this.latLng,
  8. zoom: 17
  9. });
  10. this.marker = new google.maps.Marker({
  11. map: this.map,
  12. position: this.latLng,
  13. draggable: true
  14. });
  15. this.marker.addListener("dragend", () => {
  16. const latLng = this.marker.getPosition();
  17. this.$latitude.val(latLng.lat());
  18. this.$longitude.val(latLng.lng());
  19. });
  20. this.geocoder = new google.maps.Geocoder();
  21. $("#plot-button").on("click", () => this.plot());
  22. }
  23. get latLng() {
  24. const latitude = this.$latitude.val() * 1;
  25. const longitude = this.$longitude.val() * 1;
  26. return {
  27. lat: latitude,
  28. lng: longitude
  29. };
  30. }
  31. plot() {
  32. const addressParts = [
  33. $("#address-1").val(),
  34. $("#address-2").val(),
  35. $("#city").val(),
  36. $("#county").val(),
  37. $("#postal-code").val(),
  38. $("#country").val(),
  39. ];
  40. const address = addressParts.filter(value => value.length > 0).join(", ");
  41. this.geocoder.geocode({
  42. address
  43. }, (results, status) => {
  44. if(status === "OK") {
  45. const { location } = results[0].geometry;
  46. this.map.setCenter(location);
  47. this.marker.setPosition(location);
  48. this.$latitude.val(location.lat);
  49. this.$longitude.val(location.lng);
  50. }
  51. else {
  52. alert("Could not plot for address: " + address);
  53. }
  54. });
  55. }
  56. }
  57. const DocumentTypes = Object.freeze({
  58. SalesOrder: 0,
  59. DeliveryNote: 1,
  60. GoodsIn: 2,
  61. AdvancedReplacementUnit: 3,
  62. CreditNote: 4,
  63. });
  64. $(() => {
  65. const addressMap = document.querySelector("#address-map");
  66. if(addressMap) {
  67. new AddressMap(addressMap);
  68. }
  69. toastr.options.positionClass = "toast-bottom-right";
  70. $(window).on("keyup", event => {
  71. if(event.ctrlKey && event.key === "/") {
  72. $("#header-search").trigger("focus");
  73. }
  74. });
  75. $(document).on("select2:open", () => {
  76. document.querySelector(".select2-container--open .select2-search__field").focus()
  77. });
  78. const autoClickTarget = $("#auto-click-target").val();
  79. if(autoClickTarget) {
  80. setTimeout(() => {
  81. $(autoClickTarget).trigger("click");
  82. }, 500);
  83. }
  84. });