router.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const Cls = (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 (doc = app.docs.findBySlug(context.params.doc) || app.disabledDocs.findBySlug(context.params.doc)) {
  60. context.doc = doc;
  61. context.entry = doc.toEntry();
  62. this.triggerRoute('entry');
  63. return;
  64. } else {
  65. return next();
  66. }
  67. }
  68. type(context, next) {
  69. let type;
  70. const doc = app.docs.findBySlug(context.params.doc);
  71. if (type = doc != null ? doc.types.findBy('slug', context.params.type) : undefined) {
  72. context.doc = doc;
  73. context.type = type;
  74. this.triggerRoute('type');
  75. return;
  76. } else {
  77. return next();
  78. }
  79. }
  80. entry(context, next) {
  81. let entry;
  82. const doc = app.docs.findBySlug(context.params.doc);
  83. if (!doc) { return next(); }
  84. let {
  85. path
  86. } = context.params;
  87. const {
  88. hash
  89. } = context;
  90. if (entry = doc.findEntryByPathAndHash(path, hash)) {
  91. context.doc = doc;
  92. context.entry = entry;
  93. this.triggerRoute('entry');
  94. return;
  95. } else if (path.slice(-6) === '/index') {
  96. path = path.substr(0, path.length - 6);
  97. if (entry = doc.findEntryByPathAndHash(path, hash)) { return entry.fullPath(); }
  98. } else {
  99. path = `${path}/index`;
  100. if (entry = doc.findEntryByPathAndHash(path, hash)) { return entry.fullPath(); }
  101. }
  102. return next();
  103. }
  104. root() {
  105. if (app.isSingleDoc()) { return '/'; }
  106. this.triggerRoute('root');
  107. }
  108. settings(context) {
  109. if (app.isSingleDoc()) { return `/#/${context.path}`; }
  110. this.triggerRoute('settings');
  111. }
  112. offline(context){
  113. if (app.isSingleDoc()) { return `/#/${context.path}`; }
  114. this.triggerRoute('offline');
  115. }
  116. about(context) {
  117. if (app.isSingleDoc()) { return `/#/${context.path}`; }
  118. context.page = 'about';
  119. this.triggerRoute('page');
  120. }
  121. news(context) {
  122. if (app.isSingleDoc()) { return `/#/${context.path}`; }
  123. context.page = 'news';
  124. this.triggerRoute('page');
  125. }
  126. help(context) {
  127. if (app.isSingleDoc()) { return `/#/${context.path}`; }
  128. context.page = 'help';
  129. this.triggerRoute('page');
  130. }
  131. notFound(context) {
  132. this.triggerRoute('notFound');
  133. }
  134. isIndex() {
  135. return ((this.context != null ? this.context.path : undefined) === '/') || (app.isSingleDoc() && __guard__(this.context != null ? this.context.entry : undefined, x => x.isIndex()));
  136. }
  137. isSettings() {
  138. return (this.context != null ? this.context.path : undefined) === '/settings';
  139. }
  140. setInitialPath() {
  141. // Remove superfluous forward slashes at the beginning of the path
  142. let path;
  143. if ((path = location.pathname.replace(/^\/{2,}/g, '/')) !== location.pathname) {
  144. page.replace(path + location.search + location.hash, null, true);
  145. }
  146. if (location.pathname === '/') {
  147. if (path = this.getInitialPathFromHash()) {
  148. page.replace(path + location.search, null, true);
  149. } else if (path = this.getInitialPathFromCookie()) {
  150. page.replace(path + location.search + location.hash, null, true);
  151. }
  152. }
  153. }
  154. getInitialPathFromHash() {
  155. try {
  156. return __guard__((new RegExp("#/(.+)")).exec(decodeURIComponent(location.hash)), x => x[1]);
  157. } catch (error) {}
  158. }
  159. getInitialPathFromCookie() {
  160. let path;
  161. if (path = Cookies.get('initial_path')) {
  162. Cookies.expire('initial_path');
  163. return path;
  164. }
  165. }
  166. replaceHash(hash) {
  167. page.replace(location.pathname + location.search + (hash || ''), null, true);
  168. }
  169. });
  170. Cls.initClass();
  171. function __guard__(value, transform) {
  172. return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
  173. }