nav_handler.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. let currentItem = document.querySelector(".current-item");
  65. if (currentItem !== null) {
  66. // The order is important here,
  67. // On the unread page, the read item will be hidden.
  68. this.goToNextListItem();
  69. EntryHandler.toggleEntryStatus(currentItem);
  70. }
  71. }
  72. toggleBookmark() {
  73. if (! this.isListView()) {
  74. this.toggleBookmarkLink(document.querySelector(".entry"));
  75. return;
  76. }
  77. let currentItem = document.querySelector(".current-item");
  78. if (currentItem !== null) {
  79. this.toggleBookmarkLink(currentItem);
  80. }
  81. }
  82. toggleBookmarkLink(parent) {
  83. let bookmarkLink = parent.querySelector("a[data-toggle-bookmark]");
  84. if (bookmarkLink) {
  85. EntryHandler.toggleBookmark(bookmarkLink);
  86. }
  87. }
  88. openOriginalLink() {
  89. let entryLink = document.querySelector(".entry h1 a");
  90. if (entryLink !== null) {
  91. DomHelper.openNewTab(entryLink.getAttribute("href"));
  92. return;
  93. }
  94. let currentItemOriginalLink = document.querySelector(".current-item a[data-original-link]");
  95. if (currentItemOriginalLink !== null) {
  96. DomHelper.openNewTab(currentItemOriginalLink.getAttribute("href"));
  97. // Move to the next item and if we are on the unread page mark this item as read.
  98. let currentItem = document.querySelector(".current-item");
  99. this.goToNextListItem();
  100. EntryHandler.markEntryAsRead(currentItem);
  101. }
  102. }
  103. openSelectedItem() {
  104. let currentItemLink = document.querySelector(".current-item .item-title a");
  105. if (currentItemLink !== null) {
  106. window.location.href = currentItemLink.getAttribute("href");
  107. }
  108. }
  109. /**
  110. * @param {string} page Page to redirect to.
  111. * @param {boolean} fallbackSelf Refresh actual page if the page is not found.
  112. */
  113. goToPage(page, fallbackSelf) {
  114. let element = document.querySelector("a[data-page=" + page + "]");
  115. if (element) {
  116. document.location.href = element.href;
  117. } else if (fallbackSelf) {
  118. window.location.reload();
  119. }
  120. }
  121. goToPrevious() {
  122. if (this.isListView()) {
  123. this.goToPreviousListItem();
  124. } else {
  125. this.goToPage("previous");
  126. }
  127. }
  128. goToNext() {
  129. if (this.isListView()) {
  130. this.goToNextListItem();
  131. } else {
  132. this.goToPage("next");
  133. }
  134. }
  135. goToPreviousListItem() {
  136. let items = DomHelper.getVisibleElements(".items .item");
  137. if (items.length === 0) {
  138. return;
  139. }
  140. if (document.querySelector(".current-item") === null) {
  141. items[0].classList.add("current-item");
  142. return;
  143. }
  144. for (let i = 0; i < items.length; i++) {
  145. if (items[i].classList.contains("current-item")) {
  146. items[i].classList.remove("current-item");
  147. if (i - 1 >= 0) {
  148. items[i - 1].classList.add("current-item");
  149. DomHelper.scrollPageTo(items[i - 1]);
  150. }
  151. break;
  152. }
  153. }
  154. }
  155. goToNextListItem() {
  156. let currentItem = document.querySelector(".current-item");
  157. let items = DomHelper.getVisibleElements(".items .item");
  158. if (items.length === 0) {
  159. return;
  160. }
  161. if (currentItem === null) {
  162. items[0].classList.add("current-item");
  163. return;
  164. }
  165. for (let i = 0; i < items.length; i++) {
  166. if (items[i].classList.contains("current-item")) {
  167. items[i].classList.remove("current-item");
  168. if (i + 1 < items.length) {
  169. items[i + 1].classList.add("current-item");
  170. DomHelper.scrollPageTo(items[i + 1]);
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. isListView() {
  177. return document.querySelector(".items") !== null;
  178. }
  179. }