]> git.immae.eu Git - github/wallabag/wallabag.git/blob - app/Resources/static/themes/material/js/shortcuts/main.js
62b7ec6455e8fe71117c65630601e8258c2b948d
[github/wallabag/wallabag.git] / app / Resources / static / themes / material / js / shortcuts / main.js
1 import Mousetrap from 'mousetrap';
2 import $ from 'jquery';
3
4 function toggleFocus(cardToToogleFocus) {
5 if (cardToToogleFocus) {
6 $(cardToToogleFocus).toggleClass('z-depth-4');
7 }
8 }
9 let card;
10 let cardIndex;
11 let cardNumber;
12 let pagination;
13
14 $(document).ready(() => {
15 cardIndex = 0;
16 cardNumber = $('#content ul.data > li').length;
17 card = $('#content ul.data > li')[cardIndex];
18 pagination = $('.pagination');
19
20 /* If we come from next page */
21 if (window.location.hash === '#prev') {
22 cardIndex = cardNumber - 1;
23 card = $('ul.data > li')[cardIndex];
24 }
25
26 /* Focus current card */
27 toggleFocus(card);
28
29 /* Actions */
30 Mousetrap.bind('g n', () => {
31 $('#nav-btn-add').trigger('click');
32 });
33
34 Mousetrap.bind('esc', () => {
35 $('.close').trigger('click');
36 });
37
38 /* Select right card. If there's a next page, go to next page */
39 Mousetrap.bind('right', () => {
40 if (cardIndex >= 0 && cardIndex < cardNumber - 1) {
41 toggleFocus(card);
42 cardIndex += 1;
43 card = $('ul.data > li')[cardIndex];
44 toggleFocus(card);
45 return;
46 }
47 if (pagination != null && pagination.find('li.next') && cardIndex === cardNumber - 1) {
48 window.location.href = window.location.origin + $(pagination).find('li.next a').attr('href');
49 return;
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 }
62 if (pagination !== null && $(pagination).find('li.prev') && cardIndex === 0) {
63 window.location.href = `${window.location.origin + $(pagination).find('li.prev a').attr('href')}#prev`;
64 return;
65 }
66 });
67
68 Mousetrap.bind('enter', () => {
69 window.location.href = window.location.origin + $(card).find('span.card-title a').attr('href');
70 });
71 });