]> git.immae.eu Git - github/wallabag/wallabag.git/blob - themes/_global/js/keyboard.js
969a0d6e07354a354efd60aa62c57fd6de0e46c5
[github/wallabag/wallabag.git] / themes / _global / js / keyboard.js
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
7 function navigateKeyboard(leftURL, rightURL) {
8 window.addEventListener("keydown", function (event) {
9 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
10 switch (key) {
11 case 37:
12 goLeft(leftURL); // left arrow
13 break;
14 case 72:
15 goLeft(leftURL); // h letter (vim style)
16 break;
17
18 case 39:
19 goRight(rightURL); // right arrow
20 break;
21 case 76:
22 goRight(rightURL); // l letter (vim style)
23 break;
24 case 8:
25 window.history.back();
26
27 }
28
29 }, false);
30 }
31
32 function goLeft(leftURL) {
33 if (leftURL != "?view=view&id=") {
34 window.location = window.location.origin + window.location.pathname + leftURL;
35 }
36 }
37
38 function goRight(rightURL) {
39 if (rightURL != "?view=view&id=") {
40 window.location = window.location.origin + window.location.pathname + rightURL;
41 }
42 }
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
50 function 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
71 function deleteArticle(id) {
72 if (id) {
73 window.location = window.location.origin + window.location.pathname + '?action=delete&id=' + id;
74 }
75 }
76
77 function favoriteArticle(id) {
78 if (id) {
79 window.location = window.location.origin + window.location.pathname + '?action=toggle_fav&id=' + id;
80 }
81 }
82
83 function markReadArticle(id) {
84 if (id) {
85 window.location = window.location.origin + window.location.pathname + '?action=toggle_archive&id=' + id;
86 }
87 }
88
89 function homeNavigation() {
90 selectedArticle = $('.entrie:first');
91 console.log("selected first article");
92 window.addEventListener("keydown", function (event) {
93 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
94 switch (key) {
95 case 37: // left arrow
96 selectedArticle = goSelectPrev(selectedArticle,1);
97 break;
98 case 72: // h letter (vim style)
99 selectedArticle = goSelectPrev(selectedArticle,1);
100 break;
101
102 case 39: // right arrow
103 selectedArticle = goSelectNext(selectedArticle,1);
104 break;
105 case 76: // l letter (vim style)
106 selectedArticle = goSelectNext(selectedArticle,1);
107 break;
108 case 13: // enter into article
109 enterArticle(selectedArticle);
110 break;
111 case 74: // j letter key
112 selectedArticle = goSelectNext(selectedArticle,3);
113 break;
114 case 40: // down arrow
115 selectedArticle = goSelectNext(selectedArticle,3);
116 break;
117 case 75: // k letter key
118 selectedArticle = goSelectNext(selectedArticle,3);
119 break;
120 case 38: // up arrow
121 selectedArticle = goSelectNext(selectedArticle,3);
122 break;
123 }
124
125 }, false);
126 }
127
128 function goSelectNext(selectedArticle,number) {
129 if (selectedArticle.next().length) {
130 selectedArticle.removeClass("eselected");
131 selectedArticle = selectedArticle.next();
132 selectedArticle.addClass("eselected");
133 console.log("Changed selected to next");
134 console.log("selectedArticle is now " + selectedArticle.attr("id"));
135 }
136 return selectedArticle;
137 }
138
139
140 function goSelectPrev(selectedArticle,number) {
141 if (selectedArticle.prev().length) {
142 selectedArticle.removeClass("eselected");
143 selectedArticle = selectedArticle.prev();
144 selectedArticle.addClass("eselected");
145 console.log("Changed selected to previous");
146 console.log("selectedArticle is now " + selectedArticle.attr("id"));
147
148 }
149 return selectedArticle;
150 }
151
152 function enterArticle(selectedArticle) {
153 window.location = selectedArticle.find('a:first').attr('href');
154 }