| 1 | <h2 class='fst-italic fw-light'><%= t('search') %></h2> |
| 2 | <div class="row g-5"> |
| 3 | <div class="col-8"> |
| 4 | <%= form_with url: search_path, method: :get, html: { :class=> "row g-3" } do |form| %> |
| 5 | <div class="col-12"> |
| 6 | <%= form.text_field :s, class: 'form-control search', autocomplete: 'off', placeholder: t('search_placeholder_title') %> |
| 7 | <div class="list-group result-list" style="display:none"></div> |
| 8 | </div> |
| 9 | <% end %> |
| 10 | </div> |
| 11 | <div class="col-4"> |
| 12 | <%= link_to("<span class='badge bg-warning text-dark'>#{t('nav_chords').downcase}</span>".html_safe, guitar_chords_path).html_safe %> |
| 13 | <%= link_to("<span class='badge bg-warning text-dark'>#{t('nav_javanese_gending_notation').downcase}</span>".html_safe, javanese_gending_gamelan_notation_path).html_safe %> |
| 14 | </div> |
| 15 | </div> |
| 16 | |
| 17 | <script> |
| 18 | $(document).on('turbolinks:load', function() { |
| 19 | $('input.search').on('input',function(e) { |
| 20 | |
| 21 | if ($(this).val().length < 3) |
| 22 | return |
| 23 | |
| 24 | const url = "<%= api_v1_search_path %>?q=" + $(this).val() + "&client=web_beta&locale=<%= I18n.locale %>" |
| 25 | $.ajax({ |
| 26 | type: "GET", |
| 27 | url: url, |
| 28 | success: function(res) { |
| 29 | var ins = "" |
| 30 | for (var i = 0; i < res.results.length; i++) { |
| 31 | const item = res.results[i] |
| 32 | ins += `<div |
| 33 | class="item list-group-item list-group-item-action flex-column align-items-start" |
| 34 | data-path="${item.path}" data-name="${item.name}" |
| 35 | style="cursor: pointer;">` |
| 36 | ins += "<div class='d-flex w-100 justify-content-between'>" |
| 37 | ins += "<h5 class='mb-1'>" + item.name + "</h5>" |
| 38 | ins += "<small class='text-muted fst-italic fw-light'>" + item.type + "</small>" |
| 39 | ins += "</div>" |
| 40 | ins += "</div>" |
| 41 | }; |
| 42 | $('.result-list').html(ins).show(); |
| 43 | } |
| 44 | }) |
| 45 | }) |
| 46 | $('body').on('click', 'div.item', function() { |
| 47 | |
| 48 | const destination = $(this).attr('data-path') |
| 49 | |
| 50 | function redirectToPath(res) { |
| 51 | // console.log(res) |
| 52 | window.location.href = destination |
| 53 | } |
| 54 | |
| 55 | $.ajax({ |
| 56 | type: 'POST', |
| 57 | url: '<%= api_v1_search_path %>', |
| 58 | data: 'term=' + $(this).attr('data-name'), |
| 59 | success: redirectToPath, |
| 60 | error: redirectToPath |
| 61 | }) |
| 62 | }); |
| 63 | }) |
| 64 | </script> |