map.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Add markers
  24. $.post("/api/get-markers", {
  25. customer_id: customerId
  26. }).then(response => {
  27. if(typeof this.markers[customerId] === "undefined") {
  28. this.markers[customerId] = [];
  29. }
  30. response.markers.forEach(markerData => {
  31. const marker = new google.maps.Marker({
  32. map: this.map,
  33. position: markerData.latLng,
  34. });
  35. marker.addListener("click", () => {
  36. window.location = markerData.url + "/update";
  37. });
  38. this.markers[customerId].push(marker);
  39. });
  40. $item.find("i").attr("class", "fa fa-fw fa-check text-success");
  41. $item.attr("data-selected", "true");
  42. });
  43. }
  44. else {
  45. // Remove markers
  46. this.markers[customerId].forEach(marker => {
  47. marker.setMap(null);
  48. });
  49. this.markers[customerId] = [];
  50. $item.find("i").attr("class", "fa fa-fw");
  51. $item.attr("data-selected", "false");
  52. }
  53. });
  54. }
  55. get latLng() {
  56. return {
  57. lat: 54,
  58. lng: -4
  59. };
  60. }
  61. }
  62. $(() => new Map());