| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- $(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 = $(`<div class="custom-control custom-switch">
- <input type="checkbox" class="custom-control-input" id="${id}" name="template_keys[]" />
- <label class="custom-control-label" for="${id}">Enable ${value} fields</label>
- </div>`);
- 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);
- }
|