nav_handler.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. class NavHandler {
  2. setFocusToSearchInput(event) {
  3. event.preventDefault();
  4. event.stopPropagation();
  5. let toggleSwitchElement = document.querySelector(".search-toggle-switch");
  6. if (toggleSwitchElement) {
  7. toggleSwitchElement.style.display = "none";
  8. }
  9. let searchFormElement = document.querySelector(".search-form");
  10. if (searchFormElement) {
  11. searchFormElement.style.display = "block";
  12. }
  13. let searchInputElement = document.getElementById("search-input");
  14. if (searchInputElement) {
  15. searchInputElement.focus();
  16. searchInputElement.value = "";
  17. }
  18. }
  19. showKeyboardShortcuts() {
  20. let template = document.getElementById("keyboard-shortcuts");
  21. if (template !== null) {
  22. ModalHandler.open(template.content);
  23. }
  24. }
  25. markPageAsRead() {
  26. let items = DomHelper.getVisibleElements(".items .item");
  27. let entryIDs = [];
  28. items.forEach((element) => {
  29. element.classList.add("item-status-read");
  30. entryIDs.push(parseInt(element.dataset.id, 10));
  31. });
  32. if (entryIDs.length > 0) {
  33. EntryHandler.updateEntriesStatus(entryIDs, "read", () => {
  34. // This callback make sure the Ajax request reach the server before we reload the page.
  35. this.goToPage("next", true);
  36. });
  37. }
  38. }
  39. saveEntry() {
  40. if (this.isListView()) {
  41. let currentItem = document.querySelector(".current-item");
  42. if (currentItem !== null) {
  43. let saveLink = currentItem.querySelector("a[data-save-entry]");
  44. if (saveLink) {
  45. EntryHandler.saveEntry(saveLink);
  46. }
  47. }
  48. } else {
  49. let saveLink = document.querySelector("a[data-save-entry]");
  50. if (saveLink) {
  51. EntryHandler.saveEntry(saveLink);
  52. }
  53. }
  54. }
  55. fetchOriginalContent() {
  56. if (! this.isListView()){
  57. let link = document.querySelector("a[data-fetch-content-entry]");
  58. if (link) {
  59. EntryHandler.fetchOriginalContent(link);
  60. }
  61. }
  62. }
  63. toggleEntryStatus() {
  64. if (! this.isListView()) {
  65. EntryHandler.toggleEntryStatus(document.querySelector(".entry"));
  66. return;
  67. }
  68. let currentItem = document.querySelector(".current-item");
  69. if (currentItem !== null) {
  70. // The order is important here,
  71. // On the unread page, the read item will be hidden.
  72. this.goToNextListItem();
  73. EntryHandler.toggleEntryStatus(currentItem);
  74. }
  75. }
  76. toggleBookmark() {
  77. if (! this.isListView()) {
  78. this.toggleBookmarkLink(document.querySelector(".entry"));
  79. return;
  80. }
  81. let currentItem = document.querySelector(".current-item");
  82. if (currentItem !== null) {
  83. this.toggleBookmarkLink(currentItem);
  84. }
  85. }
  86. toggleBookmarkLink(parent) {
  87. let bookmarkLink = parent.querySelector("a[data-toggle-bookmark]");
  88. if (bookmarkLink) {
  89. EntryHandler.toggleBookmark(bookmarkLink);
  90. }
  91. }
  92. openOriginalLink() {
  93. let entryLink = document.querySelector(".entry h1 a");
  94. if (entryLink !== null) {
  95. DomHelper.openNewTab(entryLink.getAttribute("href"));
  96. return;
  97. }
  98. let currentItemOriginalLink = document.querySelector(".current-item a[data-original-link]");
  99. if (currentItemOriginalLink !== null) {
  100. DomHelper.openNewTab(currentItemOriginalLink.getAttribute("href"));
  101. // Move to the next item and if we are on the unread page mark this item as read.
  102. let currentItem = document.querySelector(".current-item");
  103. this.goToNextListItem();
  104. EntryHandler.markEntryAsRead(currentItem);
  105. }
  106. }
  107. openSelectedItem() {
  108. let currentItemLink = document.querySelector(".current-item .item-title a");
  109. if (currentItemLink !== null) {
  110. window.location.href = currentItemLink.getAttribute("href");
  111. }
  112. }
  113. /**
  114. * @param {string} page Page to redirect to.
  115. * @param {boolean} fallbackSelf Refresh actual page if the page is not found.
  116. */
  117. goToPage(page, fallbackSelf) {
  118. let element = document.querySelector("a[data-page=" + page + "]");
  119. if (element) {
  120. document.location.href = element.href;
  121. } else if (fallbackSelf) {
  122. window.location.reload();
  123. }
  124. }
  125. goToPrevious() {
  126. if (this.isListView()) {
  127. this.goToPreviousListItem();
  128. } else {
  129. this.goToPage("previous");
  130. }
  131. }
  132. goToNext() {
  133. if (this.isListView()) {
  134. this.goToNextListItem();
  135. } else {
  136. this.goToPage("next");
  137. }
  138. }
  139. goToPreviousListItem() {
  140. let items = DomHelper.getVisibleElements(".items .item");
  141. if (items.length === 0) {
  142. return;
  143. }
  144. if (document.querySelector(".current-item") === null) {
  145. items[0].classList.add("current-item");
  146. return;
  147. }
  148. for (let i = 0; i < items.length; i++) {
  149. if (items[i].classList.contains("current-item")) {
  150. items[i].classList.remove("current-item");
  151. if (i - 1 >= 0) {
  152. items[i - 1].classList.add("current-item");
  153. DomHelper.scrollPageTo(items[i - 1]);
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. goToNextListItem() {
  160. let currentItem = document.querySelector(".current-item");
  161. let items = DomHelper.getVisibleElements(".items .item");
  162. if (items.length === 0) {
  163. return;
  164. }
  165. if (currentItem === null) {
  166. items[0].classList.add("current-item");
  167. return;
  168. }
  169. for (let i = 0; i < items.length; i++) {
  170. if (items[i].classList.contains("current-item")) {
  171. items[i].classList.remove("current-item");
  172. if (i + 1 < items.length) {
  173. items[i + 1].classList.add("current-item");
  174. DomHelper.scrollPageTo(items[i + 1]);
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. isListView() {
  181. return document.querySelector(".items") !== null;
  182. }
  183. }