offline_page.js 3.7 KB

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