Просмотр исходного кода

Add location map to customer page

Alan Lam 3 лет назад
Родитель
Сommit
29bbcdb761
3 измененных файлов с 44 добавлено и 25 удалено
  1. 1 0
      controllers/customer.php
  2. 38 25
      public/js/map.js
  3. 5 0
      views/customer.php

+ 1 - 0
controllers/customer.php

@@ -22,6 +22,7 @@ $user->requireAdmin();
 
 
 $core->addScript("/js/new-sale-modal.js");
 $core->addScript("/js/new-sale-modal.js");
 $core->addScript("/js/ticket-modal.js");
 $core->addScript("/js/ticket-modal.js");
+$core->addScript("/js/map.js");
 
 
 $location_ids = array_column($customer->getLocations(), "id");
 $location_ids = array_column($customer->getLocations(), "id");
 
 

+ 38 - 25
public/js/map.js

@@ -27,41 +27,54 @@ class Map {
 			}
 			}
 
 
 			if($item.prop("checked")) {
 			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;
-						});
-
-						this.markers[customerId].push(marker);
-					});
-
+				this.plotForCustomerId(customerId).then(() => {
 					$item.find("i").attr("class", "fa fa-fw fa-check text-success");
 					$item.find("i").attr("class", "fa fa-fw fa-check text-success");
 					$item.attr("data-selected", "true");
 					$item.attr("data-selected", "true");
 				});
 				});
 			}
 			}
 			else {
 			else {
-				// Remove markers
-				this.markers[customerId].forEach(marker => {
-					marker.setMap(null);
-				});
-				this.markers[customerId] = [];
+				this.removeForCustomerId(customerId);
 
 
 				$item.find("i").attr("class", "fa fa-fw");
 				$item.find("i").attr("class", "fa fa-fw");
 				$item.attr("data-selected", "false");
 				$item.attr("data-selected", "false");
 			}
 			}
 		});
 		});
+
+		const $map = $("#map");
+		const data = $map.data();
+		const { customerId } = data;
+		if(customerId) {
+			this.plotForCustomerId(customerId);
+		}
+	}
+
+	plotForCustomerId(customerId) {
+		return $.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;
+				});
+
+				this.markers[customerId].push(marker);
+			});
+		});
+	}
+
+	removeForCustomerId(customerId) {
+		this.markers[customerId].forEach(marker => {
+			marker.setMap(null);
+		});
+		this.markers[customerId] = [];
 	}
 	}
 
 
 	get latLng() {
 	get latLng() {

+ 5 - 0
views/customer.php

@@ -46,6 +46,11 @@ if(empty($locations)) {
 	<?php
 	<?php
 }
 }
 else {
 else {
+	?>
+	<div class="mb-3" id="map" style="height: 480px;" data-customer-id="<?=$customer->id?>">
+
+	</div>
+	<?php
 	View::include("locations_table", compact("locations"));
 	View::include("locations_table", compact("locations"));
 }
 }
 ?>
 ?>