aboutsummaryrefslogtreecommitdiffhomepage
path: root/themes/_global/js/keyboard.js
diff options
context:
space:
mode:
Diffstat (limited to 'themes/_global/js/keyboard.js')
-rw-r--r--themes/_global/js/keyboard.js159
1 files changed, 159 insertions, 0 deletions
diff --git a/themes/_global/js/keyboard.js b/themes/_global/js/keyboard.js
new file mode 100644
index 00000000..04000f47
--- /dev/null
+++ b/themes/_global/js/keyboard.js
@@ -0,0 +1,159 @@
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
7function 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
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 }
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
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 case 78:
67 markReadAndNextArticle(id); // n letter
68 break;
69 }
70
71 }, false);
72}
73
74function deleteArticle(id) {
75 if (id) {
76 window.location = window.location.origin + window.location.pathname + '?action=delete&id=' + id;
77 }
78}
79
80function favoriteArticle(id) {
81 if (id) {
82 window.location = window.location.origin + window.location.pathname + '?action=toggle_fav&id=' + id;
83 }
84}
85
86function markReadArticle(id) {
87 if (id) {
88 window.location = window.location.origin + window.location.pathname + '?action=toggle_archive&id=' + id;
89 }
90}
91
92function markReadAndNextArticle(id) {
93 if (id) {
94 window.location = window.location.origin + window.location.pathname + '?action=archive_and_next&id=' + id;
95 }
96}
97
98function homeNavigation() {
99 selectedArticle = $('.entrie:first');
100 window.addEventListener("keydown", function (event) {
101 var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
102 switch (key) {
103 case 37: // left arrow
104 selectedArticle = goSelectPrev(selectedArticle,1);
105 break;
106 case 72: // h letter (vim style)
107 selectedArticle = goSelectPrev(selectedArticle,1);
108 break;
109
110 case 39: // right arrow
111 selectedArticle = goSelectNext(selectedArticle,1);
112 break;
113 case 76: // l letter (vim style)
114 selectedArticle = goSelectNext(selectedArticle,1);
115 break;
116 case 13: // enter into article
117 enterArticle(selectedArticle);
118 break;
119 case 74: // j letter key
120 selectedArticle = goSelectNext(selectedArticle,3);
121 break;
122 case 40: // down arrow
123 selectedArticle = goSelectNext(selectedArticle,3);
124 break;
125 case 75: // k letter key
126 selectedArticle = goSelectNext(selectedArticle,3);
127 break;
128 case 38: // up arrow
129 selectedArticle = goSelectNext(selectedArticle,3);
130 break;
131 }
132
133 }, false);
134}
135
136function goSelectNext(selectedArticle,number) {
137 if (selectedArticle.next().length) {
138 selectedArticle.removeClass("eselected");
139 selectedArticle = selectedArticle.next();
140 selectedArticle.addClass("eselected");
141 }
142 return selectedArticle;
143}
144
145
146function goSelectPrev(selectedArticle,number) {
147 if (selectedArticle.prev().length) {
148 selectedArticle.removeClass("eselected");
149 selectedArticle = selectedArticle.prev();
150 selectedArticle.addClass("eselected");
151 }
152 return selectedArticle;
153}
154
155function enterArticle(selectedArticle) {
156 if (!$("#bagit").hasClass("current")) {
157 window.location = selectedArticle.find('a:first').attr('href');
158 }
159} \ No newline at end of file