router.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. app.Router = class Router extends Events {
  2. static routes = [
  3. ["*", "before"],
  4. ["/", "root"],
  5. ["/settings", "settings"],
  6. ["/offline", "offline"],
  7. ["/about", "about"],
  8. ["/news", "news"],
  9. ["/help", "help"],
  10. ["/:doc-:type/", "type"],
  11. ["/:doc/", "doc"],
  12. ["/:doc/:path(*)", "entry"],
  13. ["*", "notFound"],
  14. ];
  15. constructor() {
  16. super();
  17. for (var [path, method] of this.constructor.routes) {
  18. page(path, this[method].bind(this));
  19. }
  20. this.setInitialPath();
  21. }
  22. start() {
  23. page.start();
  24. }
  25. show(path) {
  26. page.show(path);
  27. }
  28. triggerRoute(name) {
  29. this.trigger(name, this.context);
  30. this.trigger("after", name, this.context);
  31. }
  32. before(context, next) {
  33. const previousContext = this.context;
  34. this.context = context;
  35. this.trigger("before", context);
  36. const res = next();
  37. if (res) {
  38. this.context = previousContext;
  39. return res;
  40. } else {
  41. return;
  42. }
  43. }
  44. doc(context, next) {
  45. let doc;
  46. if (
  47. (doc =
  48. app.docs.findBySlug(context.params.doc) ||
  49. app.disabledDocs.findBySlug(context.params.doc))
  50. ) {
  51. context.doc = doc;
  52. context.entry = doc.toEntry();
  53. this.triggerRoute("entry");
  54. return;
  55. } else {
  56. return next();
  57. }
  58. }
  59. type(context, next) {
  60. const doc = app.docs.findBySlug(context.params.doc);
  61. const type = doc?.types?.findBy("slug", context.params.type);
  62. if (type) {
  63. context.doc = doc;
  64. context.type = type;
  65. this.triggerRoute("type");
  66. return;
  67. } else {
  68. return next();
  69. }
  70. }
  71. entry(context, next) {
  72. const doc = app.docs.findBySlug(context.params.doc);
  73. if (!doc) {
  74. return next();
  75. }
  76. let { path } = context.params;
  77. const { hash } = context;
  78. let entry = doc.findEntryByPathAndHash(path, hash);
  79. if (entry) {
  80. context.doc = doc;
  81. context.entry = entry;
  82. this.triggerRoute("entry");
  83. return;
  84. } else if (path.slice(-6) === "/index") {
  85. path = path.substr(0, path.length - 6);
  86. entry = doc.findEntryByPathAndHash(path, hash);
  87. if (entry) {
  88. return entry.fullPath();
  89. }
  90. } else {
  91. path = `${path}/index`;
  92. entry = doc.findEntryByPathAndHash(path, hash);
  93. if (entry) {
  94. return entry.fullPath();
  95. }
  96. }
  97. return next();
  98. }
  99. root() {
  100. if (app.isSingleDoc()) {
  101. return "/";
  102. }
  103. this.triggerRoute("root");
  104. }
  105. settings(context) {
  106. if (app.isSingleDoc()) {
  107. return `/#/${context.path}`;
  108. }
  109. this.triggerRoute("settings");
  110. }
  111. offline(context) {
  112. if (app.isSingleDoc()) {
  113. return `/#/${context.path}`;
  114. }
  115. this.triggerRoute("offline");
  116. }
  117. about(context) {
  118. if (app.isSingleDoc()) {
  119. return `/#/${context.path}`;
  120. }
  121. context.page = "about";
  122. this.triggerRoute("page");
  123. }
  124. news(context) {
  125. if (app.isSingleDoc()) {
  126. return `/#/${context.path}`;
  127. }
  128. context.page = "news";
  129. this.triggerRoute("page");
  130. }
  131. help(context) {
  132. if (app.isSingleDoc()) {
  133. return `/#/${context.path}`;
  134. }
  135. context.page = "help";
  136. this.triggerRoute("page");
  137. }
  138. notFound(context) {
  139. this.triggerRoute("notFound");
  140. }
  141. isIndex() {
  142. return (
  143. this.context?.path === "/" ||
  144. (app.isSingleDoc() && this.context?.entry?.isIndex())
  145. );
  146. }
  147. isSettings() {
  148. return this.context?.path === "/settings";
  149. }
  150. setInitialPath() {
  151. // Remove superfluous forward slashes at the beginning of the path
  152. let path = location.pathname.replace(/^\/{2,}/g, "/");
  153. if (path !== location.pathname) {
  154. page.replace(path + location.search + location.hash, null, true);
  155. }
  156. if (location.pathname === "/") {
  157. if ((path = this.getInitialPathFromHash())) {
  158. page.replace(path + location.search, null, true);
  159. } else if ((path = this.getInitialPathFromCookie())) {
  160. page.replace(path + location.search + location.hash, null, true);
  161. }
  162. }
  163. }
  164. getInitialPathFromHash() {
  165. try {
  166. return new RegExp("#/(.+)").exec(decodeURIComponent(location.hash))?.[1];
  167. } catch (error) {}
  168. }
  169. getInitialPathFromCookie() {
  170. const path = Cookies.get("initial_path");
  171. if (path) {
  172. Cookies.expire("initial_path");
  173. return path;
  174. }
  175. }
  176. replaceHash(hash) {
  177. page.replace(
  178. location.pathname + location.search + (hash || ""),
  179. null,
  180. true
  181. );
  182. }
  183. };