router.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
  8. * DS206: Consider reworking classes to avoid initClass
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  11. */
  12. app.Router = class Router {
  13. static initClass() {
  14. $.extend(this.prototype, Events);
  15. this.routes = [
  16. ["*", "before"],
  17. ["/", "root"],
  18. ["/settings", "settings"],
  19. ["/offline", "offline"],
  20. ["/about", "about"],
  21. ["/news", "news"],
  22. ["/help", "help"],
  23. ["/:doc-:type/", "type"],
  24. ["/:doc/", "doc"],
  25. ["/:doc/:path(*)", "entry"],
  26. ["*", "notFound"],
  27. ];
  28. }
  29. constructor() {
  30. for (var [path, method] of Array.from(this.constructor.routes)) {
  31. page(path, this[method].bind(this));
  32. }
  33. this.setInitialPath();
  34. }
  35. start() {
  36. page.start();
  37. }
  38. show(path) {
  39. page.show(path);
  40. }
  41. triggerRoute(name) {
  42. this.trigger(name, this.context);
  43. this.trigger("after", name, this.context);
  44. }
  45. before(context, next) {
  46. let res;
  47. const previousContext = this.context;
  48. this.context = context;
  49. this.trigger("before", context);
  50. if ((res = next())) {
  51. this.context = previousContext;
  52. return res;
  53. } else {
  54. return;
  55. }
  56. }
  57. doc(context, next) {
  58. let doc;
  59. if (
  60. (doc =
  61. app.docs.findBySlug(context.params.doc) ||
  62. app.disabledDocs.findBySlug(context.params.doc))
  63. ) {
  64. context.doc = doc;
  65. context.entry = doc.toEntry();
  66. this.triggerRoute("entry");
  67. return;
  68. } else {
  69. return next();
  70. }
  71. }
  72. type(context, next) {
  73. let type;
  74. const doc = app.docs.findBySlug(context.params.doc);
  75. if (
  76. (type =
  77. doc != null ? doc.types.findBy("slug", context.params.type) : undefined)
  78. ) {
  79. context.doc = doc;
  80. context.type = type;
  81. this.triggerRoute("type");
  82. return;
  83. } else {
  84. return next();
  85. }
  86. }
  87. entry(context, next) {
  88. let entry;
  89. const doc = app.docs.findBySlug(context.params.doc);
  90. if (!doc) {
  91. return next();
  92. }
  93. let { path } = context.params;
  94. const { hash } = context;
  95. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  96. context.doc = doc;
  97. context.entry = entry;
  98. this.triggerRoute("entry");
  99. return;
  100. } else if (path.slice(-6) === "/index") {
  101. path = path.substr(0, path.length - 6);
  102. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  103. return entry.fullPath();
  104. }
  105. } else {
  106. path = `${path}/index`;
  107. if ((entry = doc.findEntryByPathAndHash(path, hash))) {
  108. return entry.fullPath();
  109. }
  110. }
  111. return next();
  112. }
  113. root() {
  114. if (app.isSingleDoc()) {
  115. return "/";
  116. }
  117. this.triggerRoute("root");
  118. }
  119. settings(context) {
  120. if (app.isSingleDoc()) {
  121. return `/#/${context.path}`;
  122. }
  123. this.triggerRoute("settings");
  124. }
  125. offline(context) {
  126. if (app.isSingleDoc()) {
  127. return `/#/${context.path}`;
  128. }
  129. this.triggerRoute("offline");
  130. }
  131. about(context) {
  132. if (app.isSingleDoc()) {
  133. return `/#/${context.path}`;
  134. }
  135. context.page = "about";
  136. this.triggerRoute("page");
  137. }
  138. news(context) {
  139. if (app.isSingleDoc()) {
  140. return `/#/${context.path}`;
  141. }
  142. context.page = "news";
  143. this.triggerRoute("page");
  144. }
  145. help(context) {
  146. if (app.isSingleDoc()) {
  147. return `/#/${context.path}`;
  148. }
  149. context.page = "help";
  150. this.triggerRoute("page");
  151. }
  152. notFound(context) {
  153. this.triggerRoute("notFound");
  154. }
  155. isIndex() {
  156. return (
  157. (this.context != null ? this.context.path : undefined) === "/" ||
  158. (app.isSingleDoc() &&
  159. __guard__(this.context != null ? this.context.entry : undefined, (x) =>
  160. x.isIndex(),
  161. ))
  162. );
  163. }
  164. isSettings() {
  165. return (
  166. (this.context != null ? this.context.path : undefined) === "/settings"
  167. );
  168. }
  169. setInitialPath() {
  170. // Remove superfluous forward slashes at the beginning of the path
  171. let path;
  172. if (
  173. (path = location.pathname.replace(/^\/{2,}/g, "/")) !== location.pathname
  174. ) {
  175. page.replace(path + location.search + location.hash, null, true);
  176. }
  177. if (location.pathname === "/") {
  178. if ((path = this.getInitialPathFromHash())) {
  179. page.replace(path + location.search, null, true);
  180. } else if ((path = this.getInitialPathFromCookie())) {
  181. page.replace(path + location.search + location.hash, null, true);
  182. }
  183. }
  184. }
  185. getInitialPathFromHash() {
  186. try {
  187. return __guard__(
  188. new RegExp("#/(.+)").exec(decodeURIComponent(location.hash)),
  189. (x) => x[1],
  190. );
  191. } catch (error) {}
  192. }
  193. getInitialPathFromCookie() {
  194. let path;
  195. if ((path = Cookies.get("initial_path"))) {
  196. Cookies.expire("initial_path");
  197. return path;
  198. }
  199. }
  200. replaceHash(hash) {
  201. page.replace(
  202. location.pathname + location.search + (hash || ""),
  203. null,
  204. true,
  205. );
  206. }
  207. };
  208. app.Router.initClass();
  209. function __guard__(value, transform) {
  210. return typeof value !== "undefined" && value !== null
  211. ? transform(value)
  212. : undefined;
  213. }