map.js 3.8 KB

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