]> git.immae.eu Git - github/wallabag/wallabag.git/blame - themes/_global/js/keyboard.js
fix saving URL with enter key
[github/wallabag/wallabag.git] / themes / _global / js / keyboard.js
CommitLineData
22a46267
TC
1/**
2 * @desc Navigate with Keyboard from an article to another on an article's page
3 * @param string leftURL - URL of the article on the left
4 * @param string rightURL - URL of the article on the right
5 */
6
00dc7622 7function navigateKeyboard(leftURL, rightURL) {
8b90a815 8 window.addEventListener("keydown", function (event) {
00dc7622 9 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
00dc7622
TC
10 switch (key) {
11 case 37:
22a46267
TC
12 goLeft(leftURL); // left arrow
13 break;
14 case 72:
15 goLeft(leftURL); // h letter (vim style)
00dc7622 16 break;
c34a8956 17
00dc7622 18 case 39:
22a46267 19 goRight(rightURL); // right arrow
00dc7622 20 break;
22a46267
TC
21 case 76:
22 goRight(rightURL); // l letter (vim style)
23 break;
24 case 8:
25 window.history.back();
26
00dc7622
TC
27 }
28
29 }, false);
c34a8956
TC
30}
31
32function goLeft(leftURL) {
33 if (leftURL != "?view=view&id=") {
34 window.location = window.location.origin + window.location.pathname + leftURL;
35 }
36}
37
38function goRight(rightURL) {
39 if (rightURL != "?view=view&id=") {
40 window.location = window.location.origin + window.location.pathname + rightURL;
41 }
8b90a815 42}
22a46267
TC
43
44
45/**
46 * @desc Do actions with Keyboard on an article's page
47 * @param number id - ID of the current article
48 */
49
50function actionArticle(id) {
51 window.addEventListener("keydown", function (event) {
52 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
53 switch (key) {
54 case 46:
55 deleteArticle(id); // delete key
56 break;
57 case 68:
58 deleteArticle(id); // d key letter
59 break;
60 case 70:
61 favoriteArticle(id); // f key letter
62 break;
63 case 77:
64 markReadArticle(id); // m key letter
65 break;
66 }
67
68 }, false);
69}
70
71function deleteArticle(id) {
72 if (id) {
73 window.location = window.location.origin + window.location.pathname + '?action=delete&id=' + id;
74 }
75}
76
77function favoriteArticle(id) {
78 if (id) {
79 window.location = window.location.origin + window.location.pathname + '?action=toggle_fav&id=' + id;
80 }
81}
82
83function markReadArticle(id) {
84 if (id) {
85 window.location = window.location.origin + window.location.pathname + '?action=toggle_archive&id=' + id;
86 }
87}
88
89function homeNavigation() {
90 selectedArticle = $('.entrie:first');
22a46267
TC
91 window.addEventListener("keydown", function (event) {
92 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
93 switch (key) {
94 case 37: // left arrow
95 selectedArticle = goSelectPrev(selectedArticle,1);
96 break;
97 case 72: // h letter (vim style)
98 selectedArticle = goSelectPrev(selectedArticle,1);
99 break;
100
101 case 39: // right arrow
102 selectedArticle = goSelectNext(selectedArticle,1);
103 break;
104 case 76: // l letter (vim style)
105 selectedArticle = goSelectNext(selectedArticle,1);
106 break;
107 case 13: // enter into article
108 enterArticle(selectedArticle);
109 break;
110 case 74: // j letter key
111 selectedArticle = goSelectNext(selectedArticle,3);
112 break;
113 case 40: // down arrow
114 selectedArticle = goSelectNext(selectedArticle,3);
115 break;
116 case 75: // k letter key
117 selectedArticle = goSelectNext(selectedArticle,3);
118 break;
119 case 38: // up arrow
120 selectedArticle = goSelectNext(selectedArticle,3);
121 break;
122 }
123
124 }, false);
125}
126
127function goSelectNext(selectedArticle,number) {
128 if (selectedArticle.next().length) {
129 selectedArticle.removeClass("eselected");
130 selectedArticle = selectedArticle.next();
131 selectedArticle.addClass("eselected");
22a46267
TC
132 }
133 return selectedArticle;
134}
135
136
137function goSelectPrev(selectedArticle,number) {
138 if (selectedArticle.prev().length) {
139 selectedArticle.removeClass("eselected");
140 selectedArticle = selectedArticle.prev();
141 selectedArticle.addClass("eselected");
22a46267
TC
142 }
143 return selectedArticle;
144}
145
146function enterArticle(selectedArticle) {
48a59f05
TC
147 if (!$("#bagit").hasClass("current")) {
148 window.location = selectedArticle.find('a:first').attr('href');
149 }
22a46267 150}