$(function() { refreshLocationAlerts(); $(document).on("click", "#not-selected-locations-list-group button", handleSelectButton); $(document).on("click", "#selected-locations-list-group button", handleDeselectButton); }); function handleSelectButton() { let lgi = $(this).closest(".list-group-item"); let button = this; lgi.slideUp("fast", function() { $(button).text("Deselect"); $(button).removeClass("btn-outline-success"); $(button).addClass("btn-outline-danger"); 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).text("Select"); $(button).removeClass("btn-outline-danger"); $(button).addClass("btn-outline-success"); 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(); } }