|
| 1 | +<div class="modal fade" id="advanceSearchModal" tabindex="-1" role="dialog" aria-labelledby="advanceSearchModalLabel" aria-hidden="true"> |
| 2 | + <div class="modal-dialog" role="document"> |
| 3 | + <div class="modal-content"> |
| 4 | + <div class="modal-header"> |
| 5 | + <h5 class="modal-title d-flex align-items-center gap-3" id="advanceSearchModalLabel"> |
| 6 | + <span class="icon-container"> |
| 7 | + <%= image_tag("kaui/modal/search.svg", width: 20, height: 20) %> |
| 8 | + </span> |
| 9 | + Advance Search |
| 10 | + </h5> |
| 11 | + <button type="button" class="close close-button custom-hover" data-bs-dismiss="modal" aria-label="Close"> |
| 12 | + <span aria-hidden="true"> |
| 13 | + <%= image_tag("kaui/modal/close.svg", width: 20, height: 20) %> |
| 14 | + </span> |
| 15 | + </button> |
| 16 | + </div> |
| 17 | + <div class="modal-body"> |
| 18 | + <form id="advanceSearchForm"> |
| 19 | + <div class="form-group d-flex align-items-center"> |
| 20 | + <label for="searchFieldSelect" class="mr-2 field-label" style="width: 30%;">Search Field</label> |
| 21 | + <select id="searchFieldSelect" class="form-control mr-2"> |
| 22 | + <% Kaui::Bundle::SEARCH_FIELDS.each do |value, title| %> |
| 23 | + <option value="<%= value %>" <%= 'selected' if @search_by == value %>><%= title %></option> |
| 24 | + <% end %> |
| 25 | + </select> |
| 26 | + </div> |
| 27 | + <div class="form-group d-flex align-items-center mt-3"> |
| 28 | + <label for="bundleSearchValue" class="mr-2 field-label" style="width: 30%;">Value</label> |
| 29 | + <input type="text" id="bundleSearchValue" class="form-control" placeholder="Enter search value..." value="<%= @search_query %>"> |
| 30 | + </div> |
| 31 | + </form> |
| 32 | + </div> |
| 33 | + <div class="modal-footer"> |
| 34 | + <%= render "kaui/components/button/button", { |
| 35 | + label: 'Clear Search', |
| 36 | + variant: "outline-secondary d-inline-flex align-items-center gap-1", |
| 37 | + type: "button", |
| 38 | + html_class: "kaui-button custom-hover", |
| 39 | + html_options: { id: "clearAdvanceSearch" } |
| 40 | + } %> |
| 41 | + <%= render "kaui/components/button/button", { |
| 42 | + label: 'Apply Search', |
| 43 | + variant: "outline-secondary d-inline-flex align-items-center gap-1", |
| 44 | + type: "button", |
| 45 | + html_class: "kaui-dropdown custom-hover", |
| 46 | + html_options: { id: "applyAdvanceSearch" } |
| 47 | + } %> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | +</div> |
| 52 | + |
| 53 | +<%= javascript_tag do %> |
| 54 | +$(document).ready(function() { |
| 55 | + // Pre-populate modal from current URL params on open |
| 56 | + $('#advanceSearchModal').on('show.bs.modal', function() { |
| 57 | + var params = new URLSearchParams(window.location.search); |
| 58 | + var searchBy = params.get('search_by') || 'bundle_id'; |
| 59 | + var q = params.get('q') || ''; |
| 60 | + $('#searchFieldSelect').val(searchBy); |
| 61 | + $('#bundleSearchValue').val(q); |
| 62 | + }); |
| 63 | + |
| 64 | + // Apply search: navigate to same page with query params |
| 65 | + $('#applyAdvanceSearch').on('click', function() { |
| 66 | + var searchBy = $('#searchFieldSelect').val(); |
| 67 | + var q = $('#bundleSearchValue').val().trim(); |
| 68 | + if (!q) return; |
| 69 | + |
| 70 | + var url = new URL(window.location.href); |
| 71 | + url.searchParams.set('search_by', searchBy); |
| 72 | + url.searchParams.set('q', q); |
| 73 | + url.searchParams.delete('page'); |
| 74 | + window.location.href = url.toString(); |
| 75 | + }); |
| 76 | + |
| 77 | + // Clear search: navigate to same page without query params |
| 78 | + $('#clearAdvanceSearch').on('click', function() { |
| 79 | + var url = new URL(window.location.href); |
| 80 | + url.searchParams.delete('q'); |
| 81 | + url.searchParams.delete('search_by'); |
| 82 | + url.searchParams.delete('page'); |
| 83 | + window.location.href = url.toString(); |
| 84 | + }); |
| 85 | + |
| 86 | + // Handle click on active search label close icon |
| 87 | + $(document).on('click', '.bundle-filter-close-icon', function() { |
| 88 | + var url = new URL(window.location.href); |
| 89 | + url.searchParams.delete('q'); |
| 90 | + url.searchParams.delete('search_by'); |
| 91 | + url.searchParams.delete('page'); |
| 92 | + window.location.href = url.toString(); |
| 93 | + }); |
| 94 | + |
| 95 | + // Show active search label on page load |
| 96 | + updateBundleSearchLabels(); |
| 97 | + |
| 98 | + function updateBundleSearchLabels() { |
| 99 | + var params = new URLSearchParams(window.location.search); |
| 100 | + var q = params.get('q'); |
| 101 | + var searchBy = params.get('search_by'); |
| 102 | + var container = $('#search-labels-container'); |
| 103 | + container.empty(); |
| 104 | + |
| 105 | + if (q && searchBy) { |
| 106 | + var fieldLabel = $('#searchFieldSelect option[value="' + searchBy + '"]').text() || searchBy; |
| 107 | + var label = $('<span>', { |
| 108 | + class: 'label label-info d-inline-flex align-items-center gap-2' |
| 109 | + }); |
| 110 | + label.append($('<span>', { text: fieldLabel + ': ' + q })); |
| 111 | + label.append($('<span>', { |
| 112 | + class: 'bundle-filter-close-icon', |
| 113 | + style: 'cursor: pointer; margin-left: 5px; display: inline-flex; align-items: center;' |
| 114 | + }).html('<svg width="12" height="12" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.8337 4.1665L4.16699 15.8332M4.16699 4.1665L15.8337 15.8332" stroke="#A4A7AE" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>')); |
| 115 | + container.append(label); |
| 116 | + } |
| 117 | + } |
| 118 | +}); |
| 119 | +<% end %> |
0 commit comments