|
| 1 | +async { |
| 2 | + # determine base URL and what filters and options are selected |
| 3 | + base = window.location.pathname |
| 4 | + filters = new Set() |
| 5 | + options = {} |
| 6 | + window.location.search.scan(/(\w+)(=([^&]*))?/).each do |match| |
| 7 | + options[match[0]] = match[2] && decodeURIComponent(match[2]) |
| 8 | + end |
| 9 | + if options.filter |
| 10 | + options.filter.split(',').each {|option| filters.add(option)} |
| 11 | + end |
| 12 | + |
| 13 | + updateLocation = -> (force = false) do |
| 14 | + location = URL.new(base, window.location) |
| 15 | + |
| 16 | + options.filter = Array(filters).join(',') |
| 17 | + options.delete(:filter) if filters.empty? |
| 18 | + |
| 19 | + search = [] |
| 20 | + options.each_pair do |key, value| |
| 21 | + search << (value == undefined ? key : "#{key}=#{encodeURIComponent(value)}") |
| 22 | + end |
| 23 | + |
| 24 | + location.search = search.empty? ? "" : "#{search.join('&')}" |
| 25 | + return if !force && window.location.to_s == location.to_s |
| 26 | + |
| 27 | + history.replaceState({}, null, location.to_s) |
| 28 | + |
| 29 | + return if document.getElementById('js').style.display == 'none' |
| 30 | + |
| 31 | + # update JavaScript |
| 32 | + event = MouseEvent.new('click', |
| 33 | + bubbles: true, cancelable: true, view: window) |
| 34 | + document.querySelector('input[type=submit]').dispatchEvent(event) |
| 35 | + end |
| 36 | + |
| 37 | + optionDialog = document.getElementById('option') |
| 38 | + optionInput = optionDialog.querySelector('sl-input') |
| 39 | + optionClose = optionDialog.querySelector('sl-button[slot="footer"]') |
| 40 | + |
| 41 | + optionDialog.addEventListener 'sl-initial-focus' do |
| 42 | + event.preventDefault() |
| 43 | + optionInput.setFocus(preventScroll: true) |
| 44 | + end |
| 45 | + |
| 46 | + optionClose.addEventListener 'click' do |
| 47 | + options[optionDialog.label] = optionInput.value |
| 48 | + optionDialog.hide() |
| 49 | + end |
| 50 | + |
| 51 | + document.getElementById('ast').addEventListener 'sl-change' do |
| 52 | + updateLocation(true) |
| 53 | + end |
| 54 | + |
| 55 | + document.querySelectorAll('sl-dropdown').each do |dropdown| |
| 56 | + menu = dropdown.querySelector('sl-menu') |
| 57 | + dropdown.addEventListener 'sl-show', -> () { |
| 58 | + menu.style.display = 'block' |
| 59 | + }, once: true |
| 60 | + |
| 61 | + menu.addEventListener 'sl-select' do |event| |
| 62 | + item = event.detail.item |
| 63 | + |
| 64 | + if dropdown.id == 'options' |
| 65 | + item.checked = !item.checked |
| 66 | + name = item.textContent |
| 67 | + |
| 68 | + if options.include? name |
| 69 | + options.delete(name) |
| 70 | + elsif item.dataset.args |
| 71 | + event.target.parentNode.hide() |
| 72 | + dialog = document.getElementById('option') |
| 73 | + dialog.label = name |
| 74 | + dialog.querySelector('sl-input').value = options[name] || '' |
| 75 | + dialog.show() |
| 76 | + else |
| 77 | + options[name] = undefined |
| 78 | + end |
| 79 | + |
| 80 | + elsif dropdown.id == 'filters' |
| 81 | + |
| 82 | + item.checked = !item.checked |
| 83 | + name = item.textContent |
| 84 | + filters.add(name) unless filters.delete!(name) |
| 85 | + |
| 86 | + elsif dropdown.id == 'eslevel' |
| 87 | + |
| 88 | + button = event.target.parentNode.querySelector('sl-button') |
| 89 | + value = item.textContent |
| 90 | + options['es' + value] = undefined if value != "default" |
| 91 | + event.target.querySelectorAll('sl-menu-item').each do |option| |
| 92 | + option.checked = (option == item) |
| 93 | + next if option.value == 'default' || option.value == value |
| 94 | + options.delete('es' + option.value) |
| 95 | + end |
| 96 | + button.textContent = value |
| 97 | + |
| 98 | + end |
| 99 | + |
| 100 | + updateLocation() |
| 101 | + end |
| 102 | + end |
| 103 | +}[] |
0 commit comments