]>
Commit | Line | Data |
---|---|---|
5637a26e TC |
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 | } | |
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 TC |
15 | |
16 | /* If we come from next page */ | |
17 | if (window.location.hash === '#prev') { | |
18 | cardIndex = cardNumber - 1; | |
19 | card = $('ul.data > li')[cardIndex]; | |
20 | } | |
21 | ||
22 | /* Focus current card */ | |
23 | toggleFocus(card); | |
24 | ||
25 | /* Actions */ | |
26 | Mousetrap.bind('g n', () => { | |
16ef7607 | 27 | $('#nav-btn-add').trigger('click'); |
5637a26e | 28 | }); |
16ef7607 | 29 | |
5637a26e | 30 | Mousetrap.bind('esc', () => { |
16ef7607 | 31 | $('.close').trigger('click'); |
5637a26e TC |
32 | }); |
33 | ||
34 | /* Select right card. If there's a next page, go to next page */ | |
35 | Mousetrap.bind('right', () => { | |
36 | if (cardIndex >= 0 && cardIndex < cardNumber - 1) { | |
37 | toggleFocus(card); | |
38 | cardIndex += 1; | |
39 | card = $('ul.data > li')[cardIndex]; | |
40 | toggleFocus(card); | |
41 | return; | |
42 | } | |
c9309923 | 43 | if (pagination.length > 0 && pagination.find('li.next:not(.disabled)').length > 0 && cardIndex === cardNumber - 1) { |
5637a26e | 44 | window.location.href = window.location.origin + $(pagination).find('li.next a').attr('href'); |
5637a26e TC |
45 | } |
46 | }); | |
47 | ||
48 | /* Select previous card. If there's a previous page, go to next page */ | |
49 | Mousetrap.bind('left', () => { | |
50 | if (cardIndex > 0 && cardIndex < cardNumber) { | |
51 | toggleFocus(card); | |
52 | cardIndex -= 1; | |
53 | card = $('ul.data > li')[cardIndex]; | |
54 | toggleFocus(card); | |
55 | return; | |
56 | } | |
c9309923 | 57 | if (pagination.length > 0 && $(pagination).find('li.prev:not(.disabled)').length > 0 && cardIndex === 0) { |
5637a26e | 58 | window.location.href = `${window.location.origin + $(pagination).find('li.prev a').attr('href')}#prev`; |
5637a26e TC |
59 | } |
60 | }); | |
16ef7607 | 61 | |
5637a26e TC |
62 | Mousetrap.bind('enter', () => { |
63 | window.location.href = window.location.origin + $(card).find('span.card-title a').attr('href'); | |
64 | }); | |
16ef7607 | 65 | }); |