doc.js 4.0 KB

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