$(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]").keyup(function(event) { let value = $(this).val().toLowerCase(); let target = $(this).data("filter-target"); if(value.length == 0) { $(target).children().show(); return true; } $(target).children().each(function() { let number = parseInt(value); var show; if(isNaN(number)) { show = $(this).data("filter-value").indexOf(value) != -1; } else { let test1 = (number == $(this).data("location-id")); let test2 = (number == $(this).data("internal-id")); show = test1 || test2; } if(show) { $(this).show(); } else { $(this).hide(); } }); return true; }); }); 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 = 0; 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(); } }