document.js 2.8 KB

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