document.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS002: Fix invalid constructor
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * DS207: Consider shorter variations of null checks
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  10. */
  11. const Cls = (app.views.Document = class Document extends app.View {
  12. constructor(...args) {
  13. this.afterRoute = this.afterRoute.bind(this);
  14. this.onVisibilityChange = this.onVisibilityChange.bind(this);
  15. super(...args);
  16. }
  17. static initClass() {
  18. this.el = document;
  19. this.events =
  20. {visibilitychange: 'onVisibilityChange'};
  21. this.shortcuts = {
  22. help: 'onHelp',
  23. preferences: 'onPreferences',
  24. escape: 'onEscape',
  25. superLeft: 'onBack',
  26. superRight: 'onForward'
  27. };
  28. this.routes =
  29. {after: 'afterRoute'};
  30. }
  31. init() {
  32. this.addSubview((this.menu = new app.views.Menu),
  33. this.addSubview(this.sidebar = new app.views.Sidebar));
  34. if (app.views.Resizer.isSupported()) { this.addSubview(this.resizer = new app.views.Resizer); }
  35. this.addSubview(this.content = new app.views.Content);
  36. if (!app.isSingleDoc() && !app.isMobile()) { this.addSubview(this.path = new app.views.Path); }
  37. if (!app.isSingleDoc()) { this.settings = new app.views.Settings; }
  38. $.on(document.body, 'click', this.onClick);
  39. this.activate();
  40. }
  41. setTitle(title) {
  42. return this.el.title = title ? `${title} — DevDocs` : 'DevDocs API Documentation';
  43. }
  44. afterRoute(route) {
  45. if (route === 'settings') {
  46. if (this.settings != null) {
  47. this.settings.activate();
  48. }
  49. } else {
  50. if (this.settings != null) {
  51. this.settings.deactivate();
  52. }
  53. }
  54. }
  55. onVisibilityChange() {
  56. if (this.el.visibilityState !== 'visible') { return; }
  57. this.delay(function() {
  58. if (app.isMobile() !== app.views.Mobile.detect()) { location.reload(); }
  59. }
  60. , 300);
  61. }
  62. onHelp() {
  63. app.router.show('/help#shortcuts');
  64. }
  65. onPreferences() {
  66. app.router.show('/settings');
  67. }
  68. onEscape() {
  69. const path = !app.isSingleDoc() || (location.pathname === app.doc.fullPath()) ?
  70. '/'
  71. :
  72. app.doc.fullPath();
  73. app.router.show(path);
  74. }
  75. onBack() {
  76. history.back();
  77. }
  78. onForward() {
  79. history.forward();
  80. }
  81. onClick(event) {
  82. const target = $.eventTarget(event);
  83. if (!target.hasAttribute('data-behavior')) { return; }
  84. $.stopEvent(event);
  85. switch (target.getAttribute('data-behavior')) {
  86. case 'back': history.back(); break;
  87. case 'reload': window.location.reload(); break;
  88. case 'reboot': app.reboot(); break;
  89. case 'hard-reload': app.reload(); break;
  90. case 'reset': if (confirm('Are you sure you want to reset DevDocs?')) { app.reset(); } break;
  91. case 'accept-analytics': Cookies.set('analyticsConsent', '1', {expires: 1e8}) && app.reboot(); break;
  92. case 'decline-analytics': Cookies.set('analyticsConsent', '0', {expires: 1e8}) && app.reboot(); break;
  93. }
  94. }
  95. });
  96. Cls.initClass();