document.js 3.2 KB

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