document.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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 = { visibilitychange: "onVisibilityChange" };
  20. this.shortcuts = {
  21. help: "onHelp",
  22. preferences: "onPreferences",
  23. escape: "onEscape",
  24. superLeft: "onBack",
  25. superRight: "onForward",
  26. };
  27. this.routes = { after: "afterRoute" };
  28. }
  29. init() {
  30. this.addSubview(
  31. (this.menu = new app.views.Menu()),
  32. this.addSubview((this.sidebar = new app.views.Sidebar())),
  33. );
  34. if (app.views.Resizer.isSupported()) {
  35. this.addSubview((this.resizer = new app.views.Resizer()));
  36. }
  37. this.addSubview((this.content = new app.views.Content()));
  38. if (!app.isSingleDoc() && !app.isMobile()) {
  39. this.addSubview((this.path = new app.views.Path()));
  40. }
  41. if (!app.isSingleDoc()) {
  42. this.settings = new app.views.Settings();
  43. }
  44. $.on(document.body, "click", this.onClick);
  45. this.activate();
  46. }
  47. setTitle(title) {
  48. return (this.el.title = title
  49. ? `${title} — DevDocs`
  50. : "DevDocs API Documentation");
  51. }
  52. afterRoute(route) {
  53. if (route === "settings") {
  54. if (this.settings != null) {
  55. this.settings.activate();
  56. }
  57. } else {
  58. if (this.settings != null) {
  59. this.settings.deactivate();
  60. }
  61. }
  62. }
  63. onVisibilityChange() {
  64. if (this.el.visibilityState !== "visible") {
  65. return;
  66. }
  67. this.delay(function () {
  68. if (app.isMobile() !== app.views.Mobile.detect()) {
  69. location.reload();
  70. }
  71. }, 300);
  72. }
  73. onHelp() {
  74. app.router.show("/help#shortcuts");
  75. }
  76. onPreferences() {
  77. app.router.show("/settings");
  78. }
  79. onEscape() {
  80. const path =
  81. !app.isSingleDoc() || location.pathname === app.doc.fullPath()
  82. ? "/"
  83. : app.doc.fullPath();
  84. app.router.show(path);
  85. }
  86. onBack() {
  87. history.back();
  88. }
  89. onForward() {
  90. history.forward();
  91. }
  92. onClick(event) {
  93. const target = $.eventTarget(event);
  94. if (!target.hasAttribute("data-behavior")) {
  95. return;
  96. }
  97. $.stopEvent(event);
  98. switch (target.getAttribute("data-behavior")) {
  99. case "back":
  100. history.back();
  101. break;
  102. case "reload":
  103. window.location.reload();
  104. break;
  105. case "reboot":
  106. app.reboot();
  107. break;
  108. case "hard-reload":
  109. app.reload();
  110. break;
  111. case "reset":
  112. if (confirm("Are you sure you want to reset DevDocs?")) {
  113. app.reset();
  114. }
  115. break;
  116. case "accept-analytics":
  117. Cookies.set("analyticsConsent", "1", { expires: 1e8 }) && app.reboot();
  118. break;
  119. case "decline-analytics":
  120. Cookies.set("analyticsConsent", "0", { expires: 1e8 }) && app.reboot();
  121. break;
  122. }
  123. }
  124. };
  125. app.views.Document.initClass();