map.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. class Map {
  2. constructor() {
  3. this.element = document.getElementById("map");
  4. this.markers = [];
  5. this.infoWindow = new google.maps.InfoWindow();
  6. this.map = new google.maps.Map(this.element, {
  7. center: this.latLng,
  8. zoom: 5
  9. });
  10. this.map.addListener("click", () => {
  11. this.infoWindow.close();
  12. });
  13. this.$table = $("#customers-table");
  14. const $checkboxes = this.$table.find("tbody input[type=checkbox]");
  15. const $rows = this.$table.find("tbody tr");
  16. $rows.on("click", event => {
  17. const $row = $(event.currentTarget);
  18. const customerId = $row.data("id");
  19. const $checkbox = $row.find("input[type=checkbox]");
  20. const checked = $checkbox.prop("checked");
  21. if(checked) {
  22. this.removeForCustomerId(customerId)
  23. }
  24. else {
  25. this.plotForCustomerId(customerId);
  26. }
  27. $checkbox.prop("checked", !checked);
  28. });
  29. const $allCheckbox = $("#all-items-checkbox");
  30. $allCheckbox.on("change", () => {
  31. const checked = $allCheckbox.prop("checked");
  32. $checkboxes.prop("checked", checked).trigger("change");
  33. });
  34. this.$table.on("change", "tbody input[type=checkbox]", event => {
  35. const $item = $(event.currentTarget);
  36. const customerId = $item.val();
  37. if(!customerId) {
  38. return;
  39. }
  40. if($item.prop("checked")) {
  41. this.plotForCustomerId(customerId).then(() => {
  42. $item.find("i").attr("class", "fa fa-fw fa-check text-success");
  43. $item.attr("data-selected", "true");
  44. });
  45. }
  46. else {
  47. this.removeForCustomerId(customerId);
  48. $item.find("i").attr("class", "fa fa-fw");
  49. $item.attr("data-selected", "false");
  50. }
  51. });
  52. const $map = $("#map");
  53. const data = $map.data();
  54. const { customerId, locationsPage } = data;
  55. if(customerId) {
  56. this.plotForCustomerId(customerId);
  57. }
  58. if(locationsPage) {
  59. const $customerIdDropdown = $("#customer-id");
  60. const customerId = $customerIdDropdown.val();
  61. if(customerId) {
  62. this.plotForCustomerId(customerId);
  63. }
  64. else {
  65. $customerIdDropdown.find("option[value!='']").each((index, element) => {
  66. this.plotForCustomerId(element.value);
  67. });
  68. }
  69. }
  70. }
  71. plotForCustomerId(customerId) {
  72. const data = {
  73. customer_id: customerId
  74. };
  75. const locationId = $("#location-id").val();
  76. if(locationId) {
  77. data.location_id = locationId;
  78. }
  79. const name = $("#name").val();
  80. if(name) {
  81. data.name = name;
  82. }
  83. return $.post("/api/get-markers", data).then(response => {
  84. if(typeof this.markers[customerId] === "undefined") {
  85. this.markers[customerId] = [];
  86. }
  87. response.markers.forEach(markerData => {
  88. const { latLng, name, addressLinesHtml, url, customer } = markerData;
  89. const marker = new google.maps.Marker({
  90. map: this.map,
  91. position: latLng,
  92. });
  93. marker.addListener("click", () => {
  94. const $header = $("<h6>").text(name);
  95. const $address = $("<address>").html(addressLinesHtml);
  96. const $button = $("<a>").attr("class", "btn btn-sm btn-primary").attr("href", url).text("View");
  97. const $customerLink = $(`<a class="d-block my-3">`).attr("href", customer.url).text(customer.name);
  98. const $div = $("<div>").append([$header, $address, $customerLink, $button]);
  99. this.infoWindow.setContent($div.html());
  100. this.infoWindow.open({
  101. anchor: marker,
  102. map: this.map,
  103. })
  104. });
  105. this.markers[customerId].push(marker);
  106. });
  107. });
  108. }
  109. removeForCustomerId(customerId) {
  110. this.markers[customerId].forEach(marker => {
  111. marker.setMap(null);
  112. });
  113. this.markers[customerId] = [];
  114. }
  115. get latLng() {
  116. return {
  117. lat: 54,
  118. lng: -4
  119. };
  120. }
  121. }
  122. $(() => new Map());