map.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 } = data;
  37. if(customerId) {
  38. this.plotForCustomerId(customerId);
  39. }
  40. }
  41. plotForCustomerId(customerId) {
  42. return $.post("/api/get-markers", {
  43. customer_id: customerId
  44. }).then(response => {
  45. if(typeof this.markers[customerId] === "undefined") {
  46. this.markers[customerId] = [];
  47. }
  48. response.markers.forEach(markerData => {
  49. const marker = new google.maps.Marker({
  50. map: this.map,
  51. position: markerData.latLng,
  52. });
  53. marker.addListener("click", () => {
  54. window.location = markerData.url;
  55. });
  56. this.markers[customerId].push(marker);
  57. });
  58. });
  59. }
  60. removeForCustomerId(customerId) {
  61. this.markers[customerId].forEach(marker => {
  62. marker.setMap(null);
  63. });
  64. this.markers[customerId] = [];
  65. }
  66. get latLng() {
  67. return {
  68. lat: 54,
  69. lng: -4
  70. };
  71. }
  72. }
  73. $(() => new Map());