]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | $(document).ready(() => { | |
11 | const cards = $('#content').find('.card'); | |
12 | const cardNumber = cards.length; | |
13 | let cardIndex = 0; | |
14 | /* If we come from next page */ | |
15 | if (window.location.hash === '#prev') { | |
16 | cardIndex = cardNumber - 1; | |
17 | } | |
18 | let card = cards[cardIndex]; | |
19 | const pagination = $('.pagination'); | |
20 | ||
21 | /* Show nothing on quickstart */ | |
22 | if ($('#content > div.quickstart').length > 0) { | |
23 | return; | |
24 | } | |
25 | ||
26 | /* Focus current card */ | |
27 | toggleFocus(card); | |
28 | ||
29 | /* Actions */ | |
30 | Mousetrap.bind('g n', () => { | |
31 | $('#nav-btn-add').trigger('click'); | |
32 | return false; | |
33 | }); | |
34 | ||
35 | Mousetrap.bind('s', () => { | |
36 | $('#nav-btn-search').trigger('click'); | |
37 | return false; | |
38 | }); | |
39 | ||
40 | Mousetrap.bind('esc', () => { | |
41 | $('.close').trigger('click'); | |
42 | }); | |
43 | ||
44 | /* Select right card. If there's a next page, go to next page */ | |
45 | Mousetrap.bind('right', () => { | |
46 | if (cardIndex >= 0 && cardIndex < cardNumber - 1) { | |
47 | toggleFocus(card); | |
48 | cardIndex += 1; | |
49 | card = cards[cardIndex]; | |
50 | toggleFocus(card); | |
51 | return; | |
52 | } | |
53 | if (pagination.length > 0 && pagination.find('li.next:not(.disabled)').length > 0 && cardIndex === cardNumber - 1) { | |
54 | window.location.href = window.location.origin + $(pagination).find('li.next a').attr('href'); | |
55 | } | |
56 | }); | |
57 | ||
58 | /* Select previous card. If there's a previous page, go to next page */ | |
59 | Mousetrap.bind('left', () => { | |
60 | if (cardIndex > 0 && cardIndex < cardNumber) { | |
61 | toggleFocus(card); | |
62 | cardIndex -= 1; | |
63 | card = cards[cardIndex]; | |
64 | toggleFocus(card); | |
65 | return; | |
66 | } | |
67 | if (pagination.length > 0 && $(pagination).find('li.prev:not(.disabled)').length > 0 && cardIndex === 0) { | |
68 | window.location.href = `${window.location.origin + $(pagination).find('li.prev a').attr('href')}#prev`; | |
69 | } | |
70 | }); | |
71 | ||
72 | Mousetrap.bind('enter', () => { | |
73 | window.location.href = window.location.origin + $(card).find('span.card-title a').attr('href'); | |
74 | }); | |
75 | }); |