Переглянути джерело

Add info window to map; plot and remove on row click

Alan Lam 3 роки тому
батько
коміт
013c1da6f9
4 змінених файлів з 53 додано та 12 видалено
  1. 10 6
      controllers/api.php
  2. 6 2
      engine/Address.php
  3. 35 2
      public/js/map.js
  4. 2 2
      views/map.php

+ 10 - 6
controllers/api.php

@@ -3,6 +3,7 @@
 namespace App;
 
 use App\Constraint\HasTextConstraint;
+use Exception;
 use RuntimeException;
 
 /**
@@ -39,10 +40,17 @@ try {
 
 			foreach($locations as $location) {
 				$address = $location->getAddress();
+				$customer = $location->getCustomer();
 				if($address->hasGeolocation()) {
 					$markers[] = [
 						"id" => (int) $location->id,
+						"customer" => [
+							"id" => (int) $customer->id,
+							"name" => $customer->name,
+							"url" => $customer->getUrl()
+						],
 						"name" => $location->name,
+						"addressLinesHtml" => $address->getLinesHtml(),
 						"latLng" => [
 							"lat" => (float) $address->latitude,
 							"lng" => (float) $address->longitude,
@@ -64,11 +72,7 @@ try {
 
 	}
 }
-catch(\Exception $e) {
-	http_response_code(400);
-	echo json_encode([
-		"success" => false,
-		"message" => $e->getMessage()
-	]);
+catch(Exception $e) {
+	Json::fail($e->getMessage());
 }
 

+ 6 - 2
engine/Address.php

@@ -86,13 +86,17 @@ class Address extends Model {
 	public function getLines(): array {
 		$county_parts = array_filter([$this->county, $this->state, $this->postal_code]);
 
-		return array_filter([
+		return array_values(array_filter([
 			$this->address_1,
 			$this->address_2,
 			$this->city,
 			implode(", ", $county_parts),
 			$this->getDisplayCountry()
-		]);
+		]));
+	}
+
+	public function getLinesHtml(): string {
+		return nl2br(htmlspecialchars(implode("\n", $this->getLines())));
 	}
 
 }

+ 35 - 2
public/js/map.js

@@ -2,15 +2,34 @@ class Map {
 	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 $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", () => {
@@ -83,12 +102,26 @@ class Map {
 			}
 
 			response.markers.forEach(markerData => {
+				const { latLng, name, addressLinesHtml, url, customer } = markerData;
+
 				const marker = new google.maps.Marker({
 					map: this.map,
-					position: markerData.latLng,
+					position: latLng,
 				});
+
 				marker.addListener("click", () => {
-					window.location = markerData.url;
+					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);

+ 2 - 2
views/map.php

@@ -15,7 +15,7 @@ namespace App;
 
 <div class="row">
 	<div class="col-xl-3 col-lg-4">
-		<table id="customers-table" class="table table-bordered">
+		<table id="customers-table" class="table table-bordered table-hover">
 			<colgroup>
 				<col style="width: 1%"/>
 				<col style="width: 100%"/>
@@ -35,7 +35,7 @@ namespace App;
 			<?php
 			foreach($customers as $customer) {
 				?>
-				<tr>
+				<tr data-id="<?=$customer->id?>">
 					<td>
 						<!--suppress HtmlFormInputWithoutLabel -->
 						<input class="form-check-input" type="checkbox" name="customer_ids[]" value="<?=$customer->id?>" />