$(function() {
refreshLocationAlerts();
$(document).on("click", "#not-selected-locations-list-group button", handleSelectButton);
$(document).on("click", "#selected-locations-list-group button", handleDeselectButton);
$("input[data-filter-target]").on("input", event => {
const $input = $(event.currentTarget);
const value = $input.val().toLowerCase();
const target = $input.data("filter-target");
if(value.length == 0) {
$(target).children().show();
return true;
}
$(target).children().each((index, element) => {
let number = parseInt(value);
let show;
if(isNaN(number)) {
show = $(element).data("filter-value").indexOf(value) != -1;
}
else {
let test1 = (number == $(element).data("location-id"));
let test2 = (number == $(element).data("internal-id"));
show = test1 || test2;
}
if(show) {
$(element).show();
}
else {
$(element).hide();
}
});
return true;
});
$("#project-form").on("submit", function() {
$(this).find("button[type=submit]").attr("disabled", "disabled");
});
initialiseTooltips();
const $templateDropdown = $("#template-id");
$("#template-id, #time-of-day, #date-start").on("change", () => autogenProjectName());
$templateDropdown.on("change", () => {
const keys = $templateDropdown.find("option:selected").data("keys");
const keyCount = keys.length;
const $extraFieldsRow = $("#template-extra-fields-row");
const $extraFieldsContainer = $("#template-extra-fields-container");
$extraFieldsContainer.empty();
if(keyCount === 0) {
$extraFieldsRow.collapse("hide");
}
else {
const initialTemplateKeys = JSON.parse($("#initial-template-keys").val());
$.each(keys, (index, value) => {
const id = `template-key-${index}`;
const $element = $(`
`);
const checked = initialTemplateKeys.indexOf(value) !== -1;
$element.find("input").attr("value", value).prop("checked", checked);
$extraFieldsContainer.append($element);
});
$extraFieldsRow.collapse("show");
}
});
$(document).on("change", "#template-extra-fields-container input", () => autogenProjectName());
$("#template-id").trigger("change");
const $autogenSwitch = $("#autogen-name");
$autogenSwitch.on("change", event => {
const checked = $autogenSwitch.prop("checked");
$("#name").prop("readonly", checked);
if(checked) autogenProjectName();
});
});
function getCheckedTemplateKeys() {
const keys = [];
$("#template-extra-fields-container input").each((index, element) => {
const $checkbox = $(element);
const isChecked = $checkbox.prop("checked");
if(isChecked) keys.push($checkbox.val());
});
return keys;
}
function handleSelectButton() {
let lgi = $(this).closest(".list-group-item");
let button = this;
lgi.slideUp("fast", function() {
$(button).find("span").text("Deselect");
$(button).removeClass("btn-outline-success").addClass("btn-outline-danger");
$(button).find("[data-icon]").attr("data-icon", "times");
lgi.detach();
$("#selected-locations-list-group").append(lgi);
let lgis = $("#selected-locations-list-group .list-group-item");
lgis.sort(function(a, b) {
let aName = $(a).data("name").toLowerCase();
let bName = $(b).data("name").toLowerCase();
if(aName == bName) return 0;
return (aName > bName) ? 1 : -1;
}).detach();
$("#selected-locations-list-group").append(lgis);
lgi.slideDown("fast");
refreshLocationAlerts();
refreshLocationIds();
});
}
function handleDeselectButton() {
let lgi = $(this).closest(".list-group-item");
let button = this;
lgi.slideUp("fast", function() {
$(button).find("span").text("Select");
$(button).removeClass("btn-outline-danger").addClass("btn-outline-success");
$(button).find("[data-icon]").attr("data-icon", "check");
lgi.detach();
$("#not-selected-locations-list-group").append(lgi);
let lgis = $("#not-selected-locations-list-group .list-group-item");
lgis.sort(function(a, b) {
let aName = $(a).data("name").toLowerCase();
let bName = $(b).data("name").toLowerCase();
if(aName == bName) return 0;
return (aName > bName) ? 1 : -1;
});
$("#not-selected-locations-list-group").append(lgis);
lgi.slideDown("fast");
refreshLocationAlerts();
refreshLocationIds();
});
}
function refreshLocationIds() {
let lgis = $("#selected-locations-list-group .list-group-item");
let ids = [];
lgis.each(function(index, lgi) {
ids.push($(lgi).data("location-id"));
});
$("#location-ids").val(ids.join(", "));
}
function refreshLocationAlerts() {
let count = $("#not-selected-locations-list-group .list-group-item").length;
if(count > 0) {
$("#not-selected-locations-alert").hide();
}
else {
$("#not-selected-locations-alert").show();
}
count = $("#selected-locations-list-group .list-group-item").length;
if(count > 0) {
$("#selected-locations-alert").hide();
}
else {
$("#selected-locations-alert").show();
}
}
function autogenProjectName() {
const autogenChecked = $("#autogen-name").prop("checked");
if(!autogenChecked) return;
const customerName = $("#customer-name").text();
const templateName = $.trim($("#template-id").find("option:selected").text());
const dateStart = new Date($("#date-start").val());
const timeOfDay = $("#time-of-day").find("option:selected").text();
const templateWords = templateName.split(" ");
const templateInitials = [];
for(const templateWord of templateWords) {
const initial = templateWord.charAt(0);
if(initial === "(") {
break;
}
templateInitials.push(templateWord.charAt(0));
}
const templateKeys = getCheckedTemplateKeys();
const parts = [];
parts.push(customerName);
parts.push(dateStart.toLocaleString("default", {month: "long"}));
parts.push(dateStart.getFullYear());
parts.push(templateInitials.join(""));
if(templateKeys.length > 0) {
parts.push(`(${templateKeys.join(" + ")})`);
}
parts.push(timeOfDay);
const projectName = parts.join(" ");
$("#name").val(projectName);
}