]> git.immae.eu Git - github/wallabag/wallabag.git/blame - app/Resources/static/themes/material/js/shortcuts/main.js
Fix keyboard navigation on quickstart view
[github/wallabag/wallabag.git] / app / Resources / static / themes / material / js / shortcuts / main.js
CommitLineData
5637a26e
TC
1import Mousetrap from 'mousetrap';
2import $ from 'jquery';
3
4function toggleFocus(cardToToogleFocus) {
5 if (cardToToogleFocus) {
6 $(cardToToogleFocus).toggleClass('z-depth-4');
7 }
8}
5637a26e
TC
9
10$(document).ready(() => {
c9309923
TC
11 let cardIndex = 0;
12 const cardNumber = $('#content ul.data > li').length;
13 let card = $('#content ul.data > li')[cardIndex];
14 const pagination = $('.pagination');
5637a26e 15
10a1ffae
TC
16 /* Show nothing on quickstart */
17 if ($('#content > div.quickstart').length > 0) {
18 return;
19 }
20
5637a26e
TC
21 /* If we come from next page */
22 if (window.location.hash === '#prev') {
23 cardIndex = cardNumber - 1;
24 card = $('ul.data > li')[cardIndex];
25 }
26
27 /* Focus current card */
28 toggleFocus(card);
29
30 /* Actions */
31 Mousetrap.bind('g n', () => {
16ef7607 32 $('#nav-btn-add').trigger('click');
5637a26e 33 });
16ef7607 34
5637a26e 35 Mousetrap.bind('esc', () => {
16ef7607 36 $('.close').trigger('click');
5637a26e
TC
37 });
38
39 /* Select right card. If there's a next page, go to next page */
40 Mousetrap.bind('right', () => {
41 if (cardIndex >= 0 && cardIndex < cardNumber - 1) {
42 toggleFocus(card);
43 cardIndex += 1;
44 card = $('ul.data > li')[cardIndex];
45 toggleFocus(card);
46 return;
47 }
c9309923 48 if (pagination.length > 0 && pagination.find('li.next:not(.disabled)').length > 0 && cardIndex === cardNumber - 1) {
5637a26e 49 window.location.href = window.location.origin + $(pagination).find('li.next a').attr('href');
5637a26e
TC
50 }
51 });
52
53 /* Select previous card. If there's a previous page, go to next page */
54 Mousetrap.bind('left', () => {
55 if (cardIndex > 0 && cardIndex < cardNumber) {
56 toggleFocus(card);
57 cardIndex -= 1;
58 card = $('ul.data > li')[cardIndex];
59 toggleFocus(card);
60 return;
61 }
c9309923 62 if (pagination.length > 0 && $(pagination).find('li.prev:not(.disabled)').length > 0 && cardIndex === 0) {
5637a26e 63 window.location.href = `${window.location.origin + $(pagination).find('li.prev a').attr('href')}#prev`;
5637a26e
TC
64 }
65 });
16ef7607 66
5637a26e
TC
67 Mousetrap.bind('enter', () => {
68 window.location.href = window.location.origin + $(card).find('span.card-title a').attr('href');
69 });
16ef7607 70});