diff options
Diffstat (limited to 'themes/_global/js/keyboard.js')
-rw-r--r-- | themes/_global/js/keyboard.js | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/themes/_global/js/keyboard.js b/themes/_global/js/keyboard.js new file mode 100644 index 00000000..4469248d --- /dev/null +++ b/themes/_global/js/keyboard.js | |||
@@ -0,0 +1,148 @@ | |||
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 | 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 | |||
127 | function goSelectNext(selectedArticle,number) { | ||
128 | if (selectedArticle.next().length) { | ||
129 | selectedArticle.removeClass("eselected"); | ||
130 | selectedArticle = selectedArticle.next(); | ||
131 | selectedArticle.addClass("eselected"); | ||
132 | } | ||
133 | return selectedArticle; | ||
134 | } | ||
135 | |||
136 | |||
137 | function goSelectPrev(selectedArticle,number) { | ||
138 | if (selectedArticle.prev().length) { | ||
139 | selectedArticle.removeClass("eselected"); | ||
140 | selectedArticle = selectedArticle.prev(); | ||
141 | selectedArticle.addClass("eselected"); | ||
142 | } | ||
143 | return selectedArticle; | ||
144 | } | ||
145 | |||
146 | function enterArticle(selectedArticle) { | ||
147 | window.location = selectedArticle.find('a:first').attr('href'); | ||
148 | } \ No newline at end of file | ||