|
| 1 | +{{!-- This is a form that will submit data using POST method to the URL specified in the top-level 'action' property coming from the SQL query--}} |
| 2 | +<form method="POST" action="{{action}}"> |
| 3 | + {{!-- Create a row with centered content and spacing between items --}} |
| 4 | + <div class="row justify-content-center align-items-center g-4"> |
| 5 | + {{!-- Left List Box (5 columns wide) --}} |
| 6 | + <div class="col-5"> |
| 7 | + {{!-- Card with no border and subtle shadow --}} |
| 8 | + <div class="card border-0 shadow-sm"> |
| 9 | + <div class="card-header bg-white border-bottom fw-semibold text-secondary py-3"> |
| 10 | + Available Items |
| 11 | + </div> |
| 12 | + <div class="card-body p-3"> |
| 13 | + {{!-- Multiple-select dropdown list, 300px tall --}} |
| 14 | + <select class="form-select" id="leftList" multiple style="height: 300px"> |
| 15 | + {{!-- Loop through each row of data returned by the second SQL query (row-level properties are available as variables) --}} |
| 16 | + {{#each_row}} |
| 17 | + {{!-- Create an option for each item, marking it selected if the 'selected' property is true --}} |
| 18 | + <option class="py-2 px-3 rounded-1 my-1" value="{{id}}" {{#if selected}}selected{{/if}}>{{label}}</option> |
| 19 | + {{/each_row}} |
| 20 | + </select> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | + |
| 25 | + {{!-- Middle section with transfer buttons (auto-sized column) --}} |
| 26 | + <div class="col-auto d-flex flex-column gap-2"> |
| 27 | + {{!-- Right arrow button (→) to move items to selected list --}} |
| 28 | + <button type="button" class="btn btn-outline-primary rounded-circle p-0 d-flex align-items-center justify-content-center" |
| 29 | + id="moveRight" |
| 30 | + title="Move to selected" |
| 31 | + style="width: 40px; height: 40px"> |
| 32 | + → |
| 33 | + </button> |
| 34 | + {{!-- Left arrow button (←) to remove items from selected list --}} |
| 35 | + <button type="button" class="btn btn-outline-primary rounded-circle p-0 d-flex align-items-center justify-content-center" |
| 36 | + id="moveLeft" |
| 37 | + title="Remove from selected" |
| 38 | + style="width: 40px; height: 40px"> |
| 39 | + ← |
| 40 | + </button> |
| 41 | + </div> |
| 42 | + |
| 43 | + {{!-- Right List Box (5 columns wide) --}} |
| 44 | + <div class="col-5"> |
| 45 | + <div class="card border-0 shadow-sm"> |
| 46 | + <div class="card-header bg-white border-bottom fw-semibold text-secondary py-3"> |
| 47 | + Selected Items |
| 48 | + </div> |
| 49 | + <div class="card-body p-3"> |
| 50 | + {{!-- Multiple-select dropdown that will contain selected items. The name attribute makes it submit as an array --}} |
| 51 | + <select class="form-select" id="rightList" name="selected_items[]" multiple style="height: 300px"></select> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + |
| 56 | + {{!-- Submit Button Section (full width) --}} |
| 57 | + <div class="col-12 text-center mt-4"> |
| 58 | + <input type="submit" class="btn btn-primary px-4 py-2 fw-semibold shadow-sm" |
| 59 | + id="submitBtn" |
| 60 | + disabled |
| 61 | + value="Submit Selection"> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | +</form> |
| 65 | + |
| 66 | +{{!-- JavaScript code with CSP (Content Security Policy) nonce for security --}} |
| 67 | +<script nonce="{{@csp_nonce}}"> |
| 68 | + // Get references to both list elements |
| 69 | + const rightList = document.getElementById('rightList'); |
| 70 | + const leftList = document.getElementById('leftList'); |
| 71 | +
|
| 72 | + /** |
| 73 | + * Moves selected items from one list to another while maintaining alphabetical order |
| 74 | + * @param {HTMLSelectElement} fromList - The list to take items from |
| 75 | + * @param {HTMLSelectElement} toList - The list to add items to |
| 76 | + */ |
| 77 | + function transferItems(fromList, toList) { |
| 78 | + // Deselect all items in the destination list |
| 79 | + for (const option of toList.options) option.selected = false; |
| 80 | + |
| 81 | + // Combine existing options with newly selected ones |
| 82 | + const newOptions = [...toList.options, ...fromList.selectedOptions]; |
| 83 | + |
| 84 | + // Sort the combined options alphabetically |
| 85 | + newOptions.sort((a, b) => a.text.localeCompare(b.text)); |
| 86 | + |
| 87 | + // Add all options to the destination list |
| 88 | + toList.append(...newOptions); |
| 89 | +
|
| 90 | + // Focus the destination list and update submit button state |
| 91 | + toList.focus(); |
| 92 | + updateSubmitButton(); |
| 93 | + } |
| 94 | +
|
| 95 | + /** |
| 96 | + * Enable/disable submit button based on whether there are items in the right list |
| 97 | + */ |
| 98 | + function updateSubmitButton() { |
| 99 | + submitBtn.disabled = rightList.options.length === 0; |
| 100 | + } |
| 101 | +
|
| 102 | + /** |
| 103 | + * Ensure all items in right list are selected before form submission |
| 104 | + * This is necessary because unselected options aren't included in form data |
| 105 | + */ |
| 106 | + function handleSubmit(e) { |
| 107 | + for (const option of rightList.options) option.selected = true; |
| 108 | + } |
| 109 | +
|
| 110 | + // Set initial state of submit button |
| 111 | + updateSubmitButton(); |
| 112 | +
|
| 113 | + // Move any pre-selected items to the right list when page loads |
| 114 | + transferItems(leftList, rightList); |
| 115 | +
|
| 116 | + // Set up click handlers for the transfer buttons and form submission |
| 117 | + document.getElementById('moveRight').addEventListener('click', transferItems.bind(null, leftList, rightList)); |
| 118 | + document.getElementById('moveLeft').addEventListener('click', transferItems.bind(null, rightList, leftList)); |
| 119 | + document.querySelector('form').addEventListener('submit', handleSubmit); |
| 120 | +</script> |
0 commit comments