| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- class CrmMap {
- constructor() {
- this.element = document.getElementById("map");
- this.markers = [];
- this.infoWindow = new google.maps.InfoWindow();
- this.map = new google.maps.Map(this.element, {
- center: this.latLng,
- zoom: 5
- });
- this.map.addListener("click", () => {
- this.infoWindow.close();
- });
- this.$table = $("#customers-table");
- const $icons = $(".lat-lng-icon");
- $icons.on("click", event => {
- const { latitude, longitude } = event.currentTarget.dataset;
- this.map.setCenter({
- lat: parseFloat(latitude),
- lng: parseFloat(longitude)
- });
- this.map.setZoom(15);
- });
- const $checkboxes = this.$table.find("tbody input[type=checkbox]");
- const $rows = this.$table.find("tbody tr");
- $rows.on("click", event => {
- const $row = $(event.currentTarget);
- const customerId = $row.data("id");
- const $checkbox = $row.find("input[type=checkbox]");
- const checked = $checkbox.prop("checked");
- if(checked) {
- this.removeForCustomerId(customerId)
- }
- else {
- this.plotForCustomerId(customerId);
- }
- $checkbox.prop("checked", !checked);
- });
- 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")) {
- this.plotForCustomerId(customerId).then(() => {
- $item.find("i").attr("class", "fa fa-fw fa-check text-success");
- $item.attr("data-selected", "true");
- });
- }
- else {
- this.removeForCustomerId(customerId);
- $item.find("i").attr("class", "fa fa-fw");
- $item.attr("data-selected", "false");
- }
- });
- const $map = $("#map");
- const data = $map.data();
- const { customerId, locationsPage } = data;
- if(customerId) {
- this.plotForCustomerId(customerId);
- }
- if(locationsPage) {
- const $customerIdDropdown = $("#customer-id");
- const customerId = $customerIdDropdown.val();
- if(customerId) {
- this.plotForCustomerId(customerId);
- }
- else {
- $customerIdDropdown.find("option[value!='']").each((index, element) => {
- this.plotForCustomerId(element.value);
- });
- }
- }
- }
- plotForCustomerId(customerId) {
- const data = {
- customer_id: customerId
- };
- const locationId = $("#location-id").val();
- if(locationId) {
- data.location_id = locationId;
- }
- const name = $("#name").val();
- if(name) {
- data.name = name;
- }
- return $.post("/api/get-markers", data).then(response => {
- if(typeof this.markers[customerId] === "undefined") {
- this.markers[customerId] = [];
- }
- response.markers.forEach(markerData => {
- const { latLng, name, addressLinesHtml, url, customer } = markerData;
- const marker = new google.maps.Marker({
- map: this.map,
- position: latLng,
- });
- marker.addListener("click", () => {
- const $header = $("<h6>").text(name);
- const $address = $("<address>").html(addressLinesHtml);
- const $button = $("<a>").attr("class", "btn btn-sm btn-primary").attr("href", url).text("View");
- const $customerLink = $(`<a class="d-block my-3">`).attr("href", customer.url).text(customer.name);
- const $div = $("<div>").append([$header, $address, $customerLink, $button]);
- this.infoWindow.setContent($div.html());
- this.infoWindow.open({
- anchor: marker,
- map: this.map,
- })
- });
- this.markers[customerId].push(marker);
- });
- });
- }
- removeForCustomerId(customerId) {
- this.markers[customerId].forEach(marker => {
- marker.setMap(null);
- });
- this.markers[customerId] = [];
- }
- get latLng() {
- return {
- lat: 54,
- lng: -4
- };
- }
- }
- $(() => new CrmMap());
|