map.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class Map {
  2. constructor() {
  3. this.element = document.getElementById("map");
  4. this.markers = [];
  5. this.map = new google.maps.Map(this.element, {
  6. center: this.latLng,
  7. zoom: 5
  8. });
  9. this.$table = $("#customers-table");
  10. const $checkboxes = this.$table.find("tbody input[type=checkbox]");
  11. const $allCheckbox = $("#all-items-checkbox");
  12. $allCheckbox.on("change", () => {
  13. const checked = $allCheckbox.prop("checked");
  14. $checkboxes.prop("checked", checked).trigger("change");
  15. });
  16. this.$table.on("change", "tbody input[type=checkbox]", event => {
  17. const $item = $(event.currentTarget);
  18. const customerId = $item.val();
  19. if(!customerId) {
  20. return;
  21. }
  22. if($item.prop("checked")) {
  23. this.plotForCustomerId(customerId).then(() => {
  24. $item.find("i").attr("class", "fa fa-fw fa-check text-success");
  25. $item.attr("data-selected", "true");
  26. });
  27. }
  28. else {
  29. this.removeForCustomerId(customerId);
  30. $item.find("i").attr("class", "fa fa-fw");
  31. $item.attr("data-selected", "false");
  32. }
  33. });
  34. const $map = $("#map");
  35. const data = $map.data();
  36. const { customerId, locationsPage } = data;
  37. if(customerId) {
  38. this.plotForCustomerId(customerId);
  39. }
  40. if(locationsPage) {
  41. const $customerIdDropdown = $("#customer-id");
  42. const customerId = $customerIdDropdown.val();
  43. if(customerId) {
  44. this.plotForCustomerId(customerId);
  45. }
  46. else {
  47. $customerIdDropdown.find("option[value!='']").each((index, element) => {
  48. this.plotForCustomerId(element.value);
  49. });
  50. }
  51. }
  52. }
  53. plotForCustomerId(customerId) {
  54. const data = {
  55. customer_id: customerId
  56. };
  57. const locationId = $("#location-id").val();
  58. if(locationId) {
  59. data.location_id = locationId;
  60. }
  61. const name = $("#name").val();
  62. if(name) {
  63. data.name = name;
  64. }
  65. return $.post("/api/get-markers", data).then(response => {
  66. if(typeof this.markers[customerId] === "undefined") {
  67. this.markers[customerId] = [];
  68. }
  69. response.markers.forEach(markerData => {
  70. const marker = new google.maps.Marker({
  71. map: this.map,
  72. position: markerData.latLng,
  73. });
  74. marker.addListener("click", () => {
  75. window.location = markerData.url;
  76. });
  77. this.markers[customerId].push(marker);
  78. });
  79. });
  80. }
  81. removeForCustomerId(customerId) {
  82. this.markers[customerId].forEach(marker => {
  83. marker.setMap(null);
  84. });
  85. this.markers[customerId] = [];
  86. }
  87. get latLng() {
  88. return {
  89. lat: 54,
  90. lng: -4
  91. };
  92. }
  93. }
  94. $(() => new Map());