app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*jshint esversion: 6 */
  2. (function() {
  3. 'use strict';
  4. class KeyboardHandler {
  5. constructor() {
  6. this.queue = [];
  7. this.shortcuts = {};
  8. }
  9. on(combination, callback) {
  10. this.shortcuts[combination] = callback;
  11. }
  12. listen() {
  13. document.onkeydown = (event) => {
  14. if (this.isEventIgnored(event)) {
  15. return;
  16. }
  17. let key = this.getKey(event);
  18. this.queue.push(key);
  19. for (let combination in this.shortcuts) {
  20. let keys = combination.split(" ");
  21. if (keys.every((value, index) => value === this.queue[index])) {
  22. this.queue = [];
  23. this.shortcuts[combination]();
  24. return;
  25. }
  26. if (keys.length === 1 && key === keys[0]) {
  27. this.queue = [];
  28. this.shortcuts[combination]();
  29. return;
  30. }
  31. }
  32. if (this.queue.length >= 2) {
  33. this.queue = [];
  34. }
  35. };
  36. }
  37. isEventIgnored(event) {
  38. return event.target.tagName === "INPUT" || event.target.tagName === "TEXTAREA";
  39. }
  40. getKey(event) {
  41. const mapping = {
  42. 'Esc': 'Escape',
  43. 'Up': 'ArrowUp',
  44. 'Down': 'ArrowDown',
  45. 'Left': 'ArrowLeft',
  46. 'Right': 'ArrowRight'
  47. };
  48. for (let key in mapping) {
  49. if (mapping.hasOwnProperty(key) && key === event.key) {
  50. return mapping[key];
  51. }
  52. }
  53. return event.key;
  54. }
  55. }
  56. class FormHandler {
  57. static handleSubmitButtons() {
  58. let elements = document.querySelectorAll("form");
  59. elements.forEach(function (element) {
  60. element.onsubmit = function () {
  61. let button = document.querySelector("button");
  62. if (button) {
  63. button.innerHTML = button.dataset.labelLoading;
  64. button.disabled = true;
  65. }
  66. };
  67. });
  68. }
  69. }
  70. class MouseHandler {
  71. onClick(selector, callback) {
  72. let elements = document.querySelectorAll(selector);
  73. elements.forEach((element) => {
  74. element.onclick = (event) => {
  75. event.preventDefault();
  76. callback(event);
  77. };
  78. });
  79. }
  80. }
  81. class App {
  82. run() {
  83. FormHandler.handleSubmitButtons();
  84. let keyboardHandler = new KeyboardHandler();
  85. keyboardHandler.on("g u", () => this.goToPage("unread"));
  86. keyboardHandler.on("g h", () => this.goToPage("history"));
  87. keyboardHandler.on("g f", () => this.goToPage("feeds"));
  88. keyboardHandler.on("g c", () => this.goToPage("categories"));
  89. keyboardHandler.on("g s", () => this.goToPage("settings"));
  90. keyboardHandler.on("ArrowLeft", () => this.goToPrevious());
  91. keyboardHandler.on("ArrowRight", () => this.goToNext());
  92. keyboardHandler.on("j", () => this.goToPrevious());
  93. keyboardHandler.on("p", () => this.goToPrevious());
  94. keyboardHandler.on("k", () => this.goToNext());
  95. keyboardHandler.on("n", () => this.goToNext());
  96. keyboardHandler.on("h", () => this.goToPage("previous"));
  97. keyboardHandler.on("l", () => this.goToPage("next"));
  98. keyboardHandler.on("o", () => this.openSelectedItem());
  99. keyboardHandler.on("v", () => this.openOriginalLink());
  100. keyboardHandler.on("m", () => this.toggleEntryStatus());
  101. keyboardHandler.on("A", () => this.markPageAsRead());
  102. keyboardHandler.listen();
  103. let mouseHandler = new MouseHandler();
  104. mouseHandler.onClick("a[data-on-click=markPageAsRead]", () => this.markPageAsRead());
  105. mouseHandler.onClick("a[data-confirm]", (event) => this.confirm(event));
  106. if (document.documentElement.clientWidth < 600) {
  107. mouseHandler.onClick(".logo", () => this.toggleMainMenu());
  108. mouseHandler.onClick(".header nav li", (event) => this.clickMenuListItem(event));
  109. }
  110. }
  111. remove(url) {
  112. let request = new Request(url, {
  113. method: "POST",
  114. cache: "no-cache",
  115. credentials: "include",
  116. headers: new Headers({
  117. "X-Csrf-Token": this.getCsrfToken()
  118. })
  119. });
  120. fetch(request).then(() => {
  121. window.location.reload();
  122. });
  123. }
  124. confirm(event) {
  125. let questionElement = document.createElement("span");
  126. let linkElement = event.target;
  127. let containerElement = linkElement.parentNode;
  128. linkElement.style.display = "none";
  129. let yesElement = document.createElement("a");
  130. yesElement.href = "#";
  131. yesElement.appendChild(document.createTextNode(linkElement.dataset.labelYes));
  132. yesElement.onclick = (event) => {
  133. event.preventDefault();
  134. let loadingElement = document.createElement("span");
  135. loadingElement.className = "loading";
  136. loadingElement.appendChild(document.createTextNode(linkElement.dataset.labelLoading));
  137. questionElement.remove();
  138. containerElement.appendChild(loadingElement);
  139. this.remove(linkElement.dataset.url);
  140. };
  141. let noElement = document.createElement("a");
  142. noElement.href = "#";
  143. noElement.appendChild(document.createTextNode(linkElement.dataset.labelNo));
  144. noElement.onclick = (event) => {
  145. event.preventDefault();
  146. linkElement.style.display = "inline";
  147. questionElement.remove();
  148. };
  149. questionElement.className = "confirm";
  150. questionElement.appendChild(document.createTextNode(linkElement.dataset.labelQuestion + " "));
  151. questionElement.appendChild(yesElement);
  152. questionElement.appendChild(document.createTextNode(", "));
  153. questionElement.appendChild(noElement);
  154. containerElement.appendChild(questionElement);
  155. }
  156. clickMenuListItem(event) {
  157. let element = event.target;console.log(element);
  158. if (element.tagName === "A") {
  159. window.location.href = element.getAttribute("href");
  160. } else {
  161. window.location.href = element.querySelector("a").getAttribute("href");
  162. }
  163. }
  164. toggleMainMenu() {
  165. let menu = document.querySelector(".header nav ul");
  166. if (this.isVisible(menu)) {
  167. menu.style.display = "none";
  168. } else {
  169. menu.style.display = "block";
  170. }
  171. }
  172. updateEntriesStatus(entryIDs, status) {
  173. let url = document.body.dataset.entriesStatusUrl;
  174. let request = new Request(url, {
  175. method: "POST",
  176. cache: "no-cache",
  177. credentials: "include",
  178. body: JSON.stringify({entry_ids: entryIDs, status: status}),
  179. headers: new Headers({
  180. "Content-Type": "application/json",
  181. "X-Csrf-Token": this.getCsrfToken()
  182. })
  183. });
  184. fetch(request);
  185. }
  186. markPageAsRead() {
  187. let items = this.getVisibleElements(".items .item");
  188. let entryIDs = [];
  189. items.forEach((element) => {
  190. element.classList.add("item-status-read");
  191. entryIDs.push(parseInt(element.dataset.id, 10));
  192. });
  193. if (entryIDs.length > 0) {
  194. this.updateEntriesStatus(entryIDs, "read");
  195. }
  196. this.goToPage("next");
  197. }
  198. toggleEntryStatus() {
  199. let currentItem = document.querySelector(".current-item");
  200. if (currentItem !== null) {
  201. let entryID = parseInt(currentItem.dataset.id, 10);
  202. let statuses = {read: "unread", unread: "read"};
  203. for (let currentStatus in statuses) {
  204. let newStatus = statuses[currentStatus];
  205. if (currentItem.classList.contains("item-status-" + currentStatus)) {
  206. this.goToNextListItem();
  207. currentItem.classList.remove("item-status-" + currentStatus);
  208. currentItem.classList.add("item-status-" + newStatus);
  209. this.updateEntriesStatus([entryID], newStatus);
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. openOriginalLink() {
  216. let entryLink = document.querySelector(".entry h1 a");
  217. if (entryLink !== null) {
  218. this.openNewTab(entryLink.getAttribute("href"));
  219. return;
  220. }
  221. let currentItemOriginalLink = document.querySelector(".current-item a[data-original-link]");
  222. if (currentItemOriginalLink !== null) {
  223. this.openNewTab(currentItemOriginalLink.getAttribute("href"));
  224. }
  225. }
  226. openSelectedItem() {
  227. let currentItemLink = document.querySelector(".current-item .item-title a");
  228. if (currentItemLink !== null) {
  229. window.location.href = currentItemLink.getAttribute("href");
  230. }
  231. }
  232. goToPage(page) {
  233. let element = document.querySelector("a[data-page=" + page + "]");
  234. if (element) {
  235. document.location.href = element.href;
  236. }
  237. }
  238. goToPrevious() {
  239. if (this.isListView()) {
  240. this.goToPreviousListItem();
  241. } else {
  242. this.goToPage("previous");
  243. }
  244. }
  245. goToNext() {
  246. if (this.isListView()) {
  247. this.goToNextListItem();
  248. } else {
  249. this.goToPage("next");
  250. }
  251. }
  252. goToPreviousListItem() {
  253. let items = this.getVisibleElements(".items .item");
  254. if (items.length === 0) {
  255. return;
  256. }
  257. if (document.querySelector(".current-item") === null) {
  258. items[0].classList.add("current-item");
  259. return;
  260. }
  261. for (let i = 0; i < items.length; i++) {
  262. if (items[i].classList.contains("current-item")) {
  263. items[i].classList.remove("current-item");
  264. if (i - 1 >= 0) {
  265. items[i - 1].classList.add("current-item");
  266. this.scrollPageTo(items[i - 1]);
  267. }
  268. break;
  269. }
  270. }
  271. }
  272. goToNextListItem() {
  273. let items = this.getVisibleElements(".items .item");
  274. if (items.length === 0) {
  275. return;
  276. }
  277. if (document.querySelector(".current-item") === null) {
  278. items[0].classList.add("current-item");
  279. return;
  280. }
  281. for (let i = 0; i < items.length; i++) {
  282. if (items[i].classList.contains("current-item")) {
  283. items[i].classList.remove("current-item");
  284. if (i + 1 < items.length) {
  285. items[i + 1].classList.add("current-item");
  286. this.scrollPageTo(items[i + 1]);
  287. }
  288. break;
  289. }
  290. }
  291. }
  292. getVisibleElements(selector) {
  293. let elements = document.querySelectorAll(selector);
  294. let result = [];
  295. for (let i = 0; i < elements.length; i++) {
  296. if (this.isVisible(elements[i])) {
  297. result.push(elements[i]);
  298. }
  299. }
  300. return result;
  301. }
  302. isListView() {
  303. return document.querySelector(".items") !== null;
  304. }
  305. scrollPageTo(item) {
  306. let windowScrollPosition = window.pageYOffset;
  307. let windowHeight = document.documentElement.clientHeight;
  308. let viewportPosition = windowScrollPosition + windowHeight;
  309. let itemBottomPosition = item.offsetTop + item.offsetHeight;
  310. if (viewportPosition - itemBottomPosition < 0 || viewportPosition - item.offsetTop > windowHeight) {
  311. window.scrollTo(0, item.offsetTop - 10);
  312. }
  313. }
  314. openNewTab(url) {
  315. let win = window.open(url, "_blank");
  316. win.focus();
  317. }
  318. isVisible(element) {
  319. return element.offsetParent !== null;
  320. }
  321. getCsrfToken() {
  322. let element = document.querySelector("meta[name=X-CSRF-Token]");
  323. if (element !== null) {
  324. return element.getAttribute("value");
  325. }
  326. return "";
  327. }
  328. }
  329. document.addEventListener("DOMContentLoaded", function() {
  330. (new App()).run();
  331. });
  332. })();