| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- class Map {
- constructor() {
- this.element = document.getElementById("map");
- this.markers = [];
- this.map = new google.maps.Map(this.element, {
- center: this.latLng,
- zoom: 5
- });
- this.$table = $("#customers-table");
- const $checkboxes = this.$table.find("tbody input[type=checkbox]");
- const $allCheckbox = $("#all-items-checkbox");
- $allCheckbox.on("change", () => {
- const checked = $allCheckbox.prop("checked");
- $checkboxes.prop("checked", checked).trigger("change");
- });
- this.$table.on("change", "tbody input[type=checkbox]", event => {
- const $item = $(event.currentTarget);
- const customerId = $item.val();
- if(!customerId) {
- return;
- }
- if($item.prop("checked")) {
- // Add markers
- $.post("/api/get-markers", {
- customer_id: customerId
- }).then(response => {
- if(typeof this.markers[customerId] === "undefined") {
- this.markers[customerId] = [];
- }
- response.markers.forEach(markerData => {
- const marker = new google.maps.Marker({
- map: this.map,
- position: markerData.latLng,
- });
- marker.addListener("click", () => {
- window.location = markerData.url + "/update";
- });
- this.markers[customerId].push(marker);
- });
- $item.find("i").attr("class", "fa fa-fw fa-check text-success");
- $item.attr("data-selected", "true");
- });
- }
- else {
- // Remove markers
- this.markers[customerId].forEach(marker => {
- marker.setMap(null);
- });
- this.markers[customerId] = [];
- $item.find("i").attr("class", "fa fa-fw");
- $item.attr("data-selected", "false");
- }
- });
- }
- get latLng() {
- return {
- lat: 54,
- lng: -4
- };
- }
- }
- $(() => new Map());
|