| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- let infoWindow = false;
- let exportIds = [];
- let markerMap = {};
- // noinspection JSUnusedGlobalSymbols
- function initialiseLocations() {
- if(!infoWindow) {
- infoWindow = new google.maps.InfoWindow();
- }
- const includeArchived = $("#include-archived").prop("checked") ? 1 : 0;
- const includeClosed = $("#include-closed").prop("checked") ? 1 : 0;
- $.get(`/api/customers/${customerId}/get-locations`, {
- id: $("#id").val(),
- internal_id: $("#internal-id").val(),
- name: $("#name").val(),
- legal_entity_name: $("#legal-entity-name").val(),
- include_archived: includeArchived,
- include_closed: includeClosed
- }, function(response) {
- if(!response.success) return false;
- if(!response.locations) return false;
- let map = maps["map"];
- let bounds = new google.maps.LatLngBounds;
- response.locations.forEach(location => {
- let position = {
- lat: location.latitude,
- lng: location.longitude
- };
- let options = {
- map: map,
- position: position,
- title: location.name
- };
- if(location.icon) {
- options.icon = "/img/markers/building-red.png";
- }
- bounds.extend(position);
- let key = "marker-" + location.id;
- let marker = new google.maps.Marker(options);
- marker.location = location;
- marker.addListener("click", function() {
- displayInfoWindow(this);
- });
- markerMap[key] = marker;
- });
- map.fitBounds(bounds);
- });
- $(document).on("click", "a[data-export-location-id]", function(event) {
- let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
- let index = exportIds.indexOf(locationId);
- let key = "marker-" + locationId;
- let marker = markerMap[key];
- if(index === -1) {
- exportIds.push(locationId);
- marker.setOptions({
- icon: "/img/markers/building-green.png"
- });
- }
- else {
- exportIds.splice(index, 1);
- marker.setOptions({
- icon: "/img/markers/building-red.png"
- });
- }
- displayInfoWindow(marker);
- $createProjectButton.prop("disabled", (exportIds.length === 0));
- $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
- });
- $("#export-csv-button, #export-kml-button").on("click", function(event) {
- event.preventDefault();
- const $form = $("#locations-export-form");
- const action = $(event.currentTarget).attr("data-action");
- $form.find("input[name='ids[]']").remove();
- $form.find("input[name='action']").val(action);
- for(const i in exportIds) {
- if(!exportIds.hasOwnProperty(i)) continue;
- let locationId = exportIds[i];
- $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
- }
- $form.trigger("submit");
- });
- const $createProjectButton = $("#create-project-button");
- $createProjectButton.on("click", function(event) {
- event.preventDefault();
- if(exportIds.length === 0) return;
- const $form = $("#locations-create-project-form");
- $form.find("input[name='location_ids']").remove();
- $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
- $form.trigger("submit");
- });
- const $addLocationsToProjectButton = $("#add-locations-to-project-button");
- new AddLocationsToProjectModal();
- }
- function displayInfoWindow(marker) {
- const locationId = parseInt(marker.location.id);
- const index = exportIds.indexOf(locationId);
- const archived = marker.location.archived;
- let badge = "";
- if(archived) {
- badge = `<span class="badge badge-secondary">Archived</span>`;
- }
- let exportText = "Select";
- let exportClass = "btn-primary";
- if(index > -1) {
- exportText = "Deselect";
- exportClass = "btn-success";
- }
- const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
- <p>
- ${marker.location.name}
- ${badge}
- </p>
- <div class="form-row">
- <div class="col">
- <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
- </div>
- <div class="col">
- <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
- </div>
- </div>
- </div>`;
- infoWindow.setContent(content);
- infoWindow.open(map, marker);
- }
- class AddLocationsToProjectModal {
- constructor() {
- this.$modal = $("#add-locations-to-project-modal");
- this.$modal.on("show.bs.modal", event => this.onShow(event));
- this.$form = this.$modal.find("form");
- this.$form.on("submit", event => this.onSubmit(event));
- this.$projectDropdown = this.$form.find("[name=project_id]");
- }
- onShow(event) {
- this.$form.find(`input[name="location_ids[]"]`).remove();
- exportIds.forEach(locationId => {
- this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
- });
- }
- onSubmit(event) {
- event.preventDefault();
- const projectId = this.$projectDropdown.val();
- const data = this.$form.serialize();
- $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
- if(response.success) {
- if(response.dupeLocations.length > 0) {
- const locationNames = response.dupeLocations.map(location => location.name).join("\n");
- alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
- }
- else {
- alert("Locations added to project successfully.");
- }
- }
- this.$modal.modal("hide");
- }).catch(error => {
- alert(`An error has occurred: ${error}`);
- });
- }
- }
|