offline_page.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * DS206: Consider reworking classes to avoid initClass
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. app.views.OfflinePage = class OfflinePage extends app.View {
  11. static initClass() {
  12. this.className = "_static";
  13. this.events = {
  14. click: "onClick",
  15. change: "onChange",
  16. };
  17. }
  18. deactivate() {
  19. if (super.deactivate(...arguments)) {
  20. this.empty();
  21. }
  22. }
  23. render() {
  24. if (app.cookieBlocked) {
  25. this.html(this.tmpl("offlineError", "cookie_blocked"));
  26. return;
  27. }
  28. app.docs.getInstallStatuses((statuses) => {
  29. if (!this.activated) {
  30. return;
  31. }
  32. if (statuses === false) {
  33. this.html(this.tmpl("offlineError", app.db.reason, app.db.error));
  34. } else {
  35. let html = "";
  36. for (var doc of Array.from(app.docs.all())) {
  37. html += this.renderDoc(doc, statuses[doc.slug]);
  38. }
  39. this.html(this.tmpl("offlinePage", html));
  40. this.refreshLinks();
  41. }
  42. });
  43. }
  44. renderDoc(doc, status) {
  45. return app.templates.render("offlineDoc", doc, status);
  46. }
  47. getTitle() {
  48. return "Offline";
  49. }
  50. refreshLinks() {
  51. for (var action of ["install", "update", "uninstall"]) {
  52. this.find(`[data-action-all='${action}']`).classList[
  53. this.find(`[data-action='${action}']`) ? "add" : "remove"
  54. ]("_show");
  55. }
  56. }
  57. docByEl(el) {
  58. let slug;
  59. while (!(slug = el.getAttribute("data-slug"))) {
  60. el = el.parentNode;
  61. }
  62. return app.docs.findBy("slug", slug);
  63. }
  64. docEl(doc) {
  65. return this.find(`[data-slug='${doc.slug}']`);
  66. }
  67. onRoute(context) {
  68. this.render();
  69. }
  70. onClick(event) {
  71. let action;
  72. let el = $.eventTarget(event);
  73. if ((action = el.getAttribute("data-action"))) {
  74. const doc = this.docByEl(el);
  75. if (action === "update") {
  76. action = "install";
  77. }
  78. doc[action](
  79. this.onInstallSuccess.bind(this, doc),
  80. this.onInstallError.bind(this, doc),
  81. this.onInstallProgress.bind(this, doc),
  82. );
  83. el.parentNode.innerHTML = `${el.textContent.replace(/e$/, "")}ing…`;
  84. } else if (
  85. (action =
  86. el.getAttribute("data-action-all") ||
  87. el.parentElement.getAttribute("data-action-all"))
  88. ) {
  89. if (action === "uninstall" && !window.confirm("Uninstall all docs?")) {
  90. return;
  91. }
  92. app.db.migrate();
  93. for (el of Array.from(this.findAll(`[data-action='${action}']`))) {
  94. $.click(el);
  95. }
  96. }
  97. }
  98. onInstallSuccess(doc) {
  99. if (!this.activated) {
  100. return;
  101. }
  102. doc.getInstallStatus((status) => {
  103. let el;
  104. if (!this.activated) {
  105. return;
  106. }
  107. if ((el = this.docEl(doc))) {
  108. el.outerHTML = this.renderDoc(doc, status);
  109. $.highlight(el, { className: "_highlight" });
  110. this.refreshLinks();
  111. }
  112. });
  113. }
  114. onInstallError(doc) {
  115. let el;
  116. if (!this.activated) {
  117. return;
  118. }
  119. if ((el = this.docEl(doc))) {
  120. el.lastElementChild.textContent = "Error";
  121. }
  122. }
  123. onInstallProgress(doc, event) {
  124. let el;
  125. if (!this.activated || !event.lengthComputable) {
  126. return;
  127. }
  128. if ((el = this.docEl(doc))) {
  129. const percentage = Math.round((event.loaded * 100) / event.total);
  130. el.lastElementChild.textContent = el.lastElementChild.textContent.replace(
  131. /(\s.+)?$/,
  132. ` (${percentage}%)`,
  133. );
  134. }
  135. }
  136. onChange(event) {
  137. if (event.target.name === "autoUpdate") {
  138. app.settings.set("manualUpdate", !event.target.checked);
  139. }
  140. }
  141. };
  142. app.views.OfflinePage.initClass();