Explorar o código

Add ability to select all customers in Map page

Alan Lam %!s(int64=3) %!d(string=hai) anos
pai
achega
3612671a36
Modificáronse 5 ficheiros con 117 adicións e 78 borrados
  1. 2 0
      controllers/map.php
  2. 0 70
      public/js/app.js
  3. 75 0
      public/js/map.js
  4. 10 2
      views/header.php
  5. 30 6
      views/map.php

+ 2 - 0
controllers/map.php

@@ -11,6 +11,8 @@ $core->addBreadcrumb("/map", __("Map"));
 
 $customers = Customer::getAll();
 
+$core->addScript("/js/map.js");
+
 View::include("header");
 View::include("map", compact("customers"));
 View::include("footer");

+ 0 - 70
public/js/app.js

@@ -70,81 +70,11 @@ class AddressMap {
 	}
 }
 
-/* Map Overview */
-
-class GoogleMap {
-	constructor(element) {
-		this.element = element;
-		this.markers = [];
-
-		this.map = new google.maps.Map(this.element, {
-			center: this.latLng,
-			zoom: 5
-		});
-
-		this.$customerList = $("#customer-list");
-		this.$customerList.on("click", ".list-group-item", event => {
-			const $item = $(event.currentTarget);
-			const customerId = $item.attr("data-id");
-			const selected = $item.attr("data-selected");
-
-			if(selected === "false") {
-				// 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
-		};
-	}
-}
-
 $(() => {
 	const addressMap = document.querySelector("#address-map");
 	if(addressMap) {
 		new AddressMap(addressMap);
 	}
 
-	const map = document.getElementById("map");
-	if(map) {
-		new GoogleMap(map);
-	}
-
 	toastr.options.positionClass = "toast-bottom-right";
 });

+ 75 - 0
public/js/map.js

@@ -0,0 +1,75 @@
+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());

+ 10 - 2
views/header.php

@@ -113,14 +113,22 @@ $google_api_key = $_ENV["GOOGLE_API_KEY"] ?? null;
 
 				<div class="card mb-3">
 					<div class="card-header">
-						<?=htmlspecialchars(__("Admin"))?>
+						<?=htmlspecialchars(__("Manage"))?>
 					</div>
 					<div class="list-group list-group-flush">
-						<a class="list-group-item list-group-item-action" href="/companies"><?=htmlspecialchars(__("Companies"))?></a>
 						<a class="list-group-item list-group-item-action" href="/customers"><?=htmlspecialchars(__("Customers"))?></a>
 						<a class="list-group-item list-group-item-action" href="/locations"><?=htmlspecialchars(__("Locations"))?></a>
 						<a class="list-group-item list-group-item-action" href="/products"><?=htmlspecialchars(__("Products"))?></a>
 						<a class="list-group-item list-group-item-action" href="/suppliers"><?=htmlspecialchars(__("Suppliers"))?></a>
+					</div>
+				</div>
+
+				<div class="card mb-3">
+					<div class="card-header">
+						<?=htmlspecialchars(__("Admin"))?>
+					</div>
+					<div class="list-group list-group-flush">
+						<a class="list-group-item list-group-item-action" href="/companies"><?=htmlspecialchars(__("Companies"))?></a>
 						<a class="list-group-item list-group-item-action" href="/users"><?=htmlspecialchars(__("Users"))?></a>
 					</div>
 				</div>

+ 30 - 6
views/map.php

@@ -22,17 +22,41 @@ namespace App;
 	<div class="col-lg-6">
 		<h4><?=htmlspecialchars(__("Customers"))?></h4>
 
-		<div class="list-group" id="customer-list">
+		<table id="customers-table" class="table table-bordered">
+			<colgroup>
+				<col style="width: 1%"/>
+				<col style="width: 100%"/>
+			</colgroup>
+			<thead>
+			<tr>
+				<th>
+					<!--suppress HtmlFormInputWithoutLabel -->
+					<input class="form-check-input" type="checkbox" id="all-items-checkbox" />
+				</th>
+				<th>
+					<?=htmlspecialchars(__("Name"))?>
+				</th>
+			</tr>
+			</thead>
+			<tbody>
 			<?php
 			foreach($customers as $customer) {
 				?>
-				<a class="list-group-item list-group-item-action" data-id="<?=$customer->id?>" data-selected="false">
-					<i class="fa fa-fw"></i>
-					<?=htmlspecialchars($customer->name)?>
-				</a>
+					<tr>
+						<td>
+							<!--suppress HtmlFormInputWithoutLabel -->
+							<input class="form-check-input" type="checkbox" name="customer_ids[]" value="<?=$customer->id?>" />
+						</td>
+						<td>
+							<a href="<?=htmlspecialchars($customer->getUrl())?>">
+								<?=htmlspecialchars($customer->name)?>
+							</a>
+						</td>
+					</tr>
 				<?php
 			}
 			?>
-		</div>
+			</tbody>
+		</table>
 	</div>
 </div>