From 53ce49c1dfe77d9debde9601682456bc2ab2b8e2 Mon Sep 17 00:00:00 2001 From: Ivan Schneider Date: Tue, 7 Nov 2023 21:31:47 +0100 Subject: [PATCH] navigation.js --- app/assets/javascripts/navigation.js | 108 ++++++++++++++++++++ app/assets/javascripts/navigation.js.coffee | 75 -------------- 2 files changed, 108 insertions(+), 75 deletions(-) create mode 100644 app/assets/javascripts/navigation.js delete mode 100644 app/assets/javascripts/navigation.js.coffee diff --git a/app/assets/javascripts/navigation.js b/app/assets/javascripts/navigation.js new file mode 100644 index 00000000..aaef31dd --- /dev/null +++ b/app/assets/javascripts/navigation.js @@ -0,0 +1,108 @@ +class Navigation { + constructor() { + this.helpShown = false + this.itemList() + this.tableSelection() + this.searchBar() + } + + tableSelection() { + this.selector = '#table_select' + $(this.selector) + .select2({ placeholder: 'Jump to table (s to focus)' }) + .removeClass('hidden') + .on('change', event => { + $(this.selector) + .select2('destroy') + .closest('form') + .replaceWith('') + window.location.href = `/resources/${event.target.value}` + }) + $('.modal').on('hide', () => $(this).find('input:focus').blur()) + $(document).keypress(e => { + if ($(e.target).is(':input')) return + // 's' key + if (e.which === 115) { + $(this.selector).select2('open') + e.preventDefault() + } + const actionMap = { create: 99, destroy: 116, edit: 101 } + Object.keys(actionMap).forEach(action => { + if (e.which === actionMap[action] && $(`a.btn.${action}-action`).length) + $(`a.btn.${action}-action`).get(0).click() + }) + // '?' for help + if (e.which === 63) this.toggleKeyboardShortcutsHelp() + }) + } + + searchBar() { + $(document).keypress(e => { + if ($(e.target).is(':input')) return + if (e.which != 47) return + $('form.navbar-form input[type=text]').focus() + e.preventDefault() + }) + } + + toggleKeyboardShortcutsHelp() { + const selector = '#keyboard-shortcuts-help' + if (this.helpShown) { + $(selector).modal('hide') + this.helpShown = false + } else { + if ($(selector).length) $(selector).modal('show') + else { + $('
') + .attr({ id: selector.replace('#', ''), tabindex: '-1' }) + .addClass('modal fade') + .appendTo('body') + .html($('.loading_modal').html()) + .modal('show') + $.get('/docs/keyboard_shortcuts', data => $(selector).html(data)) + $(selector).on('hidden', () => (this.helpShown = false)) + this.helpShown = true + } + } + } + + itemList() { + $(document).keydown(e => { + if ($(e.target).is(':input') || $('.items-list').length == 0) return + const row = $('.items-list tr.success') + if (row.length == 0) { + if ([74, 40].indexOf(e.which) != -1) { + $('.items-list tbody tr').first().addClass('success') + e.preventDefault() + } + return + } + // j or down arrow + if ([74, 40].indexOf(e.which) != -1) { + if (row.next().length) + row.removeClass('success').next().addClass('success') + } + // k or up arrow + else if ([75, 38].indexOf(e.which) != -1) { + if (row.prev().length) + row.removeClass('success').prev().addClass('success') + } + // return or right arrow or o + else if ([13, 39, 79].indexOf(e.which) != -1) + location.href = row.find('td:first-child a:first-child').attr('href') + // x + else if (e.which == 88) + row.find('td:eq(0) input[type=checkbox]').get(0).click() + // t + else if (e.which == 84) + row.find('td:first-child a[data-method="delete"]').trigger('click') + // e + else if (e.which == 69) + location.href = row.find('td:first-child a:eq(1)').attr('href') + else return + e.preventDefault() + }) + } +} + +$(() => new Navigation()) diff --git a/app/assets/javascripts/navigation.js.coffee b/app/assets/javascripts/navigation.js.coffee deleted file mode 100644 index 4cea4aba..00000000 --- a/app/assets/javascripts/navigation.js.coffee +++ /dev/null @@ -1,75 +0,0 @@ -class Navigation - - constructor: -> - @helpShown = false - @itemList() - @tableSelection() - @searchBar() - - tableSelection: -> - @selector = '#table_select' - $(@selector).select2(placeholder: 'Jump to table (s to focus)').removeClass('hidden').on 'change', (event) => - $(@selector).select2('destroy').closest('form').replaceWith('') - window.location.href = "/resources/#{event.target.value}" - $('.modal').on 'hide', -> $(this).find('input:focus').blur() - - $(document).keypress (e) => - return if $(e.target).is(':input') - if e.which is 115 # 's' key - $(@selector).select2('open') - e.preventDefault() - for action, key of {create: 99, destroy: 116, edit: 101} - if e.which is key and $("a.btn.#{action}-action").length - $("a.btn.#{action}-action").get(0).click() - if e.which is 63 # '?' for help - @toggleKeyboardShortcutsHelp() - - toggleKeyboardShortcutsHelp: -> - selector = '#keyboard-shortcuts-help' - if @helpShown - $(selector).modal('hide') - @helpShown = false - else - if $(selector).length - $(selector).modal('show') - else - docs_url = '/docs/keyboard_shortcuts' - $('
').attr(id: selector.replace('#', ''), tabindex: '-1').addClass('modal fade') - .appendTo('body').html($(".loading_modal").html()).modal('show') - $.get docs_url, (data) => $(selector).html(data) - $(selector).on 'hidden', => @helpShown = false - @helpShown = true - - searchBar: -> - $(document).keypress (e) => - return if $(e.target).is(':input') - return unless e.which is 47 - $('form.navbar-form input[type=text]').focus() - e.preventDefault() - - itemList: -> - $(document).keydown (e) => - return if $(e.target).is(':input') or $('.items-list').length is 0 - row = $('.items-list tr.success') - if row.length is 0 - if [74, 40].indexOf(e.which) isnt -1 - $('.items-list tbody tr').first().addClass('success') - e.preventDefault() - return - if [74, 40].indexOf(e.which) isnt -1 # j or down arrow - row.removeClass('success').next().addClass('success') if row.next().length - else if [75, 38].indexOf(e.which) isnt -1 # k or up arrow - row.removeClass('success').prev().addClass('success') if row.prev().length - else if [13, 39, 79].indexOf(e.which) isnt -1 # return or right arrow or o - location.href = row.find('td:first-child a:first-child').attr('href') - else if e.which is 88 # x - checkBox = row.find('td:eq(0) input[type=checkbox]').get(0).click() - else if e.which is 84 # t - row.find('td:first-child a[data-method="delete"]').trigger('click') - else if e.which is 69 # e - location.href = row.find('td:first-child a:eq(1)').attr('href') - else - return - e.preventDefault() - -$ -> new Navigation()