diff options
Diffstat (limited to 'themes/_global/js/keyboard.js')
-rw-r--r-- | themes/_global/js/keyboard.js | 131 |
1 files changed, 129 insertions, 2 deletions
diff --git a/themes/_global/js/keyboard.js b/themes/_global/js/keyboard.js index d2a736e4..969a0d6e 100644 --- a/themes/_global/js/keyboard.js +++ b/themes/_global/js/keyboard.js | |||
@@ -1,14 +1,29 @@ | |||
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 | |||
1 | function navigateKeyboard(leftURL, rightURL) { | 7 | function navigateKeyboard(leftURL, rightURL) { |
2 | window.addEventListener("keydown", function (event) { | 8 | window.addEventListener("keydown", function (event) { |
3 | var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions | 9 | var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions |
4 | switch (key) { | 10 | switch (key) { |
5 | case 37: | 11 | case 37: |
6 | goLeft(leftURL); | 12 | goLeft(leftURL); // left arrow |
13 | break; | ||
14 | case 72: | ||
15 | goLeft(leftURL); // h letter (vim style) | ||
7 | break; | 16 | break; |
8 | 17 | ||
9 | case 39: | 18 | case 39: |
10 | goRight(rightURL); | 19 | goRight(rightURL); // right arrow |
11 | break; | 20 | break; |
21 | case 76: | ||
22 | goRight(rightURL); // l letter (vim style) | ||
23 | break; | ||
24 | case 8: | ||
25 | window.history.back(); | ||
26 | |||
12 | } | 27 | } |
13 | 28 | ||
14 | }, false); | 29 | }, false); |
@@ -25,3 +40,115 @@ function goRight(rightURL) { | |||
25 | window.location = window.location.origin + window.location.pathname + rightURL; | 40 | window.location = window.location.origin + window.location.pathname + rightURL; |
26 | } | 41 | } |
27 | } | 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 | } \ No newline at end of file | ||