doc.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  8. */
  9. app.models.Doc = class Doc extends app.Model {
  10. // Attributes: name, slug, type, version, release, db_size, mtime, links
  11. constructor() {
  12. super(...arguments);
  13. this.reset(this);
  14. this.slug_without_version = this.slug.split('~')[0];
  15. this.fullName = `${this.name}` + (this.version ? ` ${this.version}` : '');
  16. this.icon = this.slug_without_version;
  17. if (this.version) { this.short_version = this.version.split(' ')[0]; }
  18. this.text = this.toEntry().text;
  19. }
  20. reset(data) {
  21. this.resetEntries(data.entries);
  22. this.resetTypes(data.types);
  23. }
  24. resetEntries(entries) {
  25. this.entries = new app.collections.Entries(entries);
  26. this.entries.each(entry => { return entry.doc = this; });
  27. }
  28. resetTypes(types) {
  29. this.types = new app.collections.Types(types);
  30. this.types.each(type => { return type.doc = this; });
  31. }
  32. fullPath(path) {
  33. if (path == null) { path = ''; }
  34. if (path[0] !== '/') { path = `/${path}`; }
  35. return `/${this.slug}${path}`;
  36. }
  37. fileUrl(path) {
  38. return `${app.config.docs_origin}${this.fullPath(path)}?${this.mtime}`;
  39. }
  40. dbUrl() {
  41. return `${app.config.docs_origin}/${this.slug}/${app.config.db_filename}?${this.mtime}`;
  42. }
  43. indexUrl() {
  44. return `${app.indexHost()}/${this.slug}/${app.config.index_filename}?${this.mtime}`;
  45. }
  46. toEntry() {
  47. if (this.entry) { return this.entry; }
  48. this.entry = new app.models.Entry({
  49. doc: this,
  50. name: this.fullName,
  51. path: 'index'
  52. });
  53. if (this.version) { this.entry.addAlias(this.name); }
  54. return this.entry;
  55. }
  56. findEntryByPathAndHash(path, hash) {
  57. let entry;
  58. if (hash && (entry = this.entries.findBy('path', `${path}#${hash}`))) {
  59. return entry;
  60. } else if (path === 'index') {
  61. return this.toEntry();
  62. } else {
  63. return this.entries.findBy('path', path);
  64. }
  65. }
  66. load(onSuccess, onError, options) {
  67. if (options == null) { options = {}; }
  68. if (options.readCache && this._loadFromCache(onSuccess)) { return; }
  69. const callback = data => {
  70. this.reset(data);
  71. onSuccess();
  72. if (options.writeCache) { this._setCache(data); }
  73. };
  74. return ajax({
  75. url: this.indexUrl(),
  76. success: callback,
  77. error: onError
  78. });
  79. }
  80. clearCache() {
  81. app.localStorage.del(this.slug);
  82. }
  83. _loadFromCache(onSuccess) {
  84. let data;
  85. if (!(data = this._getCache())) { return; }
  86. const callback = () => {
  87. this.reset(data);
  88. onSuccess();
  89. };
  90. setTimeout(callback, 0);
  91. return true;
  92. }
  93. _getCache() {
  94. let data;
  95. if (!(data = app.localStorage.get(this.slug))) { return; }
  96. if (data[0] === this.mtime) {
  97. return data[1];
  98. } else {
  99. this.clearCache();
  100. return;
  101. }
  102. }
  103. _setCache(data) {
  104. app.localStorage.set(this.slug, [this.mtime, data]);
  105. }
  106. install(onSuccess, onError, onProgress) {
  107. if (this.installing) { return; }
  108. this.installing = true;
  109. const error = () => {
  110. this.installing = null;
  111. onError();
  112. };
  113. const success = data => {
  114. this.installing = null;
  115. app.db.store(this, data, onSuccess, error);
  116. };
  117. ajax({
  118. url: this.dbUrl(),
  119. success,
  120. error,
  121. progress: onProgress,
  122. timeout: 3600
  123. });
  124. }
  125. uninstall(onSuccess, onError) {
  126. if (this.installing) { return; }
  127. this.installing = true;
  128. const success = () => {
  129. this.installing = null;
  130. onSuccess();
  131. };
  132. const error = () => {
  133. this.installing = null;
  134. onError();
  135. };
  136. app.db.unstore(this, success, error);
  137. }
  138. getInstallStatus(callback) {
  139. app.db.version(this, value => callback({installed: !!value, mtime: value}));
  140. }
  141. isOutdated(status) {
  142. if (!status) { return false; }
  143. const isInstalled = status.installed || app.settings.get('autoInstall');
  144. return isInstalled && (this.mtime !== status.mtime);
  145. }
  146. };