nav_handler.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. unsubscribeFromFeed() {
  114. let unsubscribeLinks = document.querySelectorAll("[data-action=remove-feed]");
  115. if (unsubscribeLinks.length === 1) {
  116. let unsubscribeLink = unsubscribeLinks[0];
  117. FeedHandler.unsubscribe(unsubscribeLink.dataset.url, () => {
  118. if (unsubscribeLink.dataset.redirectUrl) {
  119. window.location.href = unsubscribeLink.dataset.redirectUrl;
  120. } else {
  121. window.location.reload();
  122. }
  123. });
  124. }
  125. }
  126. /**
  127. * @param {string} page Page to redirect to.
  128. * @param {boolean} fallbackSelf Refresh actual page if the page is not found.
  129. */
  130. goToPage(page, fallbackSelf) {
  131. let element = document.querySelector("a[data-page=" + page + "]");
  132. if (element) {
  133. document.location.href = element.href;
  134. } else if (fallbackSelf) {
  135. window.location.reload();
  136. }
  137. }
  138. goToPrevious() {
  139. if (this.isListView()) {
  140. this.goToPreviousListItem();
  141. } else {
  142. this.goToPage("previous");
  143. }
  144. }
  145. goToNext() {
  146. if (this.isListView()) {
  147. this.goToNextListItem();
  148. } else {
  149. this.goToPage("next");
  150. }
  151. }
  152. goToFeedOrFeeds() {
  153. if (this.isEntry()) {
  154. let feedAnchor = document.querySelector("span.entry-website a");
  155. if (feedAnchor !== null) {
  156. window.location.href = feedAnchor.href;
  157. }
  158. } else {
  159. this.goToPage('feeds');
  160. }
  161. }
  162. goToPreviousListItem() {
  163. let items = DomHelper.getVisibleElements(".items .item");
  164. if (items.length === 0) {
  165. return;
  166. }
  167. if (document.querySelector(".current-item") === null) {
  168. items[0].classList.add("current-item");
  169. items[0].querySelector('.item-header a').focus();
  170. return;
  171. }
  172. for (let i = 0; i < items.length; i++) {
  173. if (items[i].classList.contains("current-item")) {
  174. items[i].classList.remove("current-item");
  175. if (i - 1 >= 0) {
  176. items[i - 1].classList.add("current-item");
  177. DomHelper.scrollPageTo(items[i - 1]);
  178. items[i - 1].querySelector('.item-header a').focus();
  179. }
  180. break;
  181. }
  182. }
  183. }
  184. goToNextListItem() {
  185. let currentItem = document.querySelector(".current-item");
  186. let items = DomHelper.getVisibleElements(".items .item");
  187. if (items.length === 0) {
  188. return;
  189. }
  190. if (currentItem === null) {
  191. items[0].classList.add("current-item");
  192. items[0].querySelector('.item-header a').focus();
  193. return;
  194. }
  195. for (let i = 0; i < items.length; i++) {
  196. if (items[i].classList.contains("current-item")) {
  197. items[i].classList.remove("current-item");
  198. if (i + 1 < items.length) {
  199. items[i + 1].classList.add("current-item");
  200. DomHelper.scrollPageTo(items[i + 1]);
  201. items[i + 1].querySelector('.item-header a').focus();
  202. }
  203. break;
  204. }
  205. }
  206. }
  207. isEntry() {
  208. return document.querySelector("section.entry") !== null;
  209. }
  210. isListView() {
  211. return document.querySelector(".items") !== null;
  212. }
  213. }