app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. * DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
  8. * DS207: Consider shorter variations of null checks
  9. * DS208: Avoid top-level this
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  11. */
  12. this.app = {
  13. _$: $,
  14. _$$: $$,
  15. _page: page,
  16. collections: {},
  17. models: {},
  18. templates: {},
  19. views: {},
  20. init() {
  21. try { this.initErrorTracking(); } catch (error) {}
  22. if (!this.browserCheck()) { return; }
  23. this.el = $('._app');
  24. this.localStorage = new LocalStorageStore;
  25. if (app.ServiceWorker.isEnabled()) { this.serviceWorker = new app.ServiceWorker; }
  26. this.settings = new app.Settings;
  27. this.db = new app.DB();
  28. this.settings.initLayout();
  29. this.docs = new app.collections.Docs;
  30. this.disabledDocs = new app.collections.Docs;
  31. this.entries = new app.collections.Entries;
  32. this.router = new app.Router;
  33. this.shortcuts = new app.Shortcuts;
  34. this.document = new app.views.Document;
  35. if (this.isMobile()) { this.mobile = new app.views.Mobile; }
  36. if (document.body.hasAttribute('data-doc')) {
  37. this.DOC = JSON.parse(document.body.getAttribute('data-doc'));
  38. this.bootOne();
  39. } else if (this.DOCS) {
  40. this.bootAll();
  41. } else {
  42. this.onBootError();
  43. }
  44. },
  45. browserCheck() {
  46. if (this.isSupportedBrowser()) { return true; }
  47. document.body.innerHTML = app.templates.unsupportedBrowser;
  48. this.hideLoadingScreen();
  49. return false;
  50. },
  51. initErrorTracking() {
  52. // Show a warning message and don't track errors when the app is loaded
  53. // from a domain other than our own, because things are likely to break.
  54. // (e.g. cross-domain requests)
  55. if (this.isInvalidLocation()) {
  56. new app.views.Notif('InvalidLocation');
  57. } else {
  58. if (this.config.sentry_dsn) {
  59. Raven.config(this.config.sentry_dsn, {
  60. release: this.config.release,
  61. whitelistUrls: [/devdocs/],
  62. includePaths: [/devdocs/],
  63. ignoreErrors: [/NPObject/, /NS_ERROR/, /^null$/, /EvalError/],
  64. tags: {
  65. mode: this.isSingleDoc() ? 'single' : 'full',
  66. iframe: (window.top !== window).toString(),
  67. electron: (!!__guard__(window.process != null ? window.process.versions : undefined, x => x.electron)).toString()
  68. },
  69. shouldSendCallback: () => {
  70. try {
  71. if (this.isInjectionError()) {
  72. this.onInjectionError();
  73. return false;
  74. }
  75. if (this.isAndroidWebview()) {
  76. return false;
  77. }
  78. } catch (error) {}
  79. return true;
  80. },
  81. dataCallback(data) {
  82. try {
  83. $.extend(data.user || (data.user = {}), app.settings.dump());
  84. if (data.user.docs) { data.user.docs = data.user.docs.split('/'); }
  85. if (app.lastIDBTransaction) { data.user.lastIDBTransaction = app.lastIDBTransaction; }
  86. data.tags.scriptCount = document.scripts.length;
  87. } catch (error) {}
  88. return data;
  89. }
  90. }).install();
  91. }
  92. this.previousErrorHandler = onerror;
  93. window.onerror = this.onWindowError.bind(this);
  94. CookiesStore.onBlocked = this.onCookieBlocked;
  95. }
  96. },
  97. bootOne() {
  98. this.doc = new app.models.Doc(this.DOC);
  99. this.docs.reset([this.doc]);
  100. this.doc.load(this.start.bind(this), this.onBootError.bind(this), {readCache: true});
  101. new app.views.Notice('singleDoc', this.doc);
  102. delete this.DOC;
  103. },
  104. bootAll() {
  105. const docs = this.settings.getDocs();
  106. for (var doc of Array.from(this.DOCS)) {
  107. (docs.indexOf(doc.slug) >= 0 ? this.docs : this.disabledDocs).add(doc);
  108. }
  109. this.migrateDocs();
  110. this.docs.load(this.start.bind(this), this.onBootError.bind(this), {readCache: true, writeCache: true});
  111. delete this.DOCS;
  112. },
  113. start() {
  114. let doc;
  115. for (doc of Array.from(this.docs.all())) { this.entries.add(doc.toEntry()); }
  116. for (doc of Array.from(this.disabledDocs.all())) { this.entries.add(doc.toEntry()); }
  117. for (doc of Array.from(this.docs.all())) { this.initDoc(doc); }
  118. this.trigger('ready');
  119. this.router.start();
  120. this.hideLoadingScreen();
  121. setTimeout(() => {
  122. if (!this.doc) { this.welcomeBack(); }
  123. return this.removeEvent('ready bootError');
  124. }
  125. , 50);
  126. },
  127. initDoc(doc) {
  128. for (var type of Array.from(doc.types.all())) { doc.entries.add(type.toEntry()); }
  129. this.entries.add(doc.entries.all());
  130. },
  131. migrateDocs() {
  132. let needsSaving;
  133. for (var slug of Array.from(this.settings.getDocs())) {
  134. if (!this.docs.findBy('slug', slug)) {var doc;
  135. needsSaving = true;
  136. if (slug === 'webpack~2') { doc = this.disabledDocs.findBy('slug', 'webpack'); }
  137. if (slug === 'angular~4_typescript') { doc = this.disabledDocs.findBy('slug', 'angular'); }
  138. if (slug === 'angular~2_typescript') { doc = this.disabledDocs.findBy('slug', 'angular~2'); }
  139. if (!doc) { doc = this.disabledDocs.findBy('slug_without_version', slug); }
  140. if (doc) {
  141. this.disabledDocs.remove(doc);
  142. this.docs.add(doc);
  143. }
  144. }
  145. }
  146. if (needsSaving) { this.saveDocs(); }
  147. },
  148. enableDoc(doc, _onSuccess, onError) {
  149. if (this.docs.contains(doc)) { return; }
  150. const onSuccess = () => {
  151. if (this.docs.contains(doc)) { return; }
  152. this.disabledDocs.remove(doc);
  153. this.docs.add(doc);
  154. this.docs.sort();
  155. this.initDoc(doc);
  156. this.saveDocs();
  157. if (app.settings.get('autoInstall')) {
  158. doc.install(_onSuccess, onError);
  159. } else {
  160. _onSuccess();
  161. }
  162. };
  163. doc.load(onSuccess, onError, {writeCache: true});
  164. },
  165. saveDocs() {
  166. this.settings.setDocs(Array.from(this.docs.all()).map((doc) => doc.slug));
  167. this.db.migrate();
  168. return (this.serviceWorker != null ? this.serviceWorker.updateInBackground() : undefined);
  169. },
  170. welcomeBack() {
  171. let visitCount = this.settings.get('count');
  172. this.settings.set('count', ++visitCount);
  173. if (visitCount === 5) { new app.views.Notif('Share', {autoHide: null}); }
  174. new app.views.News();
  175. new app.views.Updates();
  176. return this.updateChecker = new app.UpdateChecker();
  177. },
  178. reboot() {
  179. if ((location.pathname !== '/') && (location.pathname !== '/settings')) {
  180. window.location = `/#${location.pathname}`;
  181. } else {
  182. window.location = '/';
  183. }
  184. },
  185. reload() {
  186. this.docs.clearCache();
  187. this.disabledDocs.clearCache();
  188. if (this.serviceWorker) { this.serviceWorker.reload(); } else { this.reboot(); }
  189. },
  190. reset() {
  191. this.localStorage.reset();
  192. this.settings.reset();
  193. if (this.db != null) {
  194. this.db.reset();
  195. }
  196. if (this.serviceWorker != null) {
  197. this.serviceWorker.update();
  198. }
  199. window.location = '/';
  200. },
  201. showTip(tip) {
  202. if (this.isSingleDoc()) { return; }
  203. const tips = this.settings.getTips();
  204. if (tips.indexOf(tip) === -1) {
  205. tips.push(tip);
  206. this.settings.setTips(tips);
  207. new app.views.Tip(tip);
  208. }
  209. },
  210. hideLoadingScreen() {
  211. if ($.overlayScrollbarsEnabled()) { document.body.classList.add('_overlay-scrollbars'); }
  212. document.documentElement.classList.remove('_booting');
  213. },
  214. indexHost() {
  215. // Can't load the index files from the host/CDN when service worker is
  216. // enabled because it doesn't support caching URLs that use CORS.
  217. return this.config[this.serviceWorker && this.settings.hasDocs() ? 'index_path' : 'docs_origin'];
  218. },
  219. onBootError(...args) {
  220. this.trigger('bootError');
  221. this.hideLoadingScreen();
  222. },
  223. onQuotaExceeded() {
  224. if (this.quotaExceeded) { return; }
  225. this.quotaExceeded = true;
  226. new app.views.Notif('QuotaExceeded', {autoHide: null});
  227. },
  228. onCookieBlocked(key, value, actual) {
  229. if (this.cookieBlocked) { return; }
  230. this.cookieBlocked = true;
  231. new app.views.Notif('CookieBlocked', {autoHide: null});
  232. Raven.captureMessage(`CookieBlocked/${key}`, {level: 'warning', extra: {value, actual}});
  233. },
  234. onWindowError(...args) {
  235. if (this.cookieBlocked) { return; }
  236. if (this.isInjectionError(...Array.from(args || []))) {
  237. this.onInjectionError();
  238. } else if (this.isAppError(...Array.from(args || []))) {
  239. if (typeof this.previousErrorHandler === 'function') {
  240. this.previousErrorHandler(...Array.from(args || []));
  241. }
  242. this.hideLoadingScreen();
  243. if (!this.errorNotif) { this.errorNotif = new app.views.Notif('Error'); }
  244. this.errorNotif.show();
  245. }
  246. },
  247. onInjectionError() {
  248. if (!this.injectionError) {
  249. this.injectionError = true;
  250. alert(`\
  251. JavaScript code has been injected in the page which prevents DevDocs from running correctly.
  252. Please check your browser extensions/addons. `
  253. );
  254. Raven.captureMessage('injection error', {level: 'info'});
  255. }
  256. },
  257. isInjectionError() {
  258. // Some browser extensions expect the entire web to use jQuery.
  259. // I gave up trying to fight back.
  260. return (window.$ !== app._$) || (window.$$ !== app._$$) || (window.page !== app._page) || (typeof $.empty !== 'function') || (typeof page.show !== 'function');
  261. },
  262. isAppError(error, file) {
  263. // Ignore errors from external scripts.
  264. return file && (file.indexOf('devdocs') !== -1) && (file.indexOf('.js') === (file.length - 3));
  265. },
  266. isSupportedBrowser() {
  267. try {
  268. const features = {
  269. bind: !!Function.prototype.bind,
  270. pushState: !!history.pushState,
  271. matchMedia: !!window.matchMedia,
  272. insertAdjacentHTML: !!document.body.insertAdjacentHTML,
  273. defaultPrevented: document.createEvent('CustomEvent').defaultPrevented === false,
  274. cssVariables: !!__guardMethod__(CSS, 'supports', o => o.supports('(--t: 0)'))
  275. };
  276. for (var key in features) {
  277. var value = features[key];
  278. if (!value) {
  279. Raven.captureMessage(`unsupported/${key}`, {level: 'info'});
  280. return false;
  281. }
  282. }
  283. return true;
  284. } catch (error) {
  285. Raven.captureMessage('unsupported/exception', {level: 'info', extra: { error }});
  286. return false;
  287. }
  288. },
  289. isSingleDoc() {
  290. return document.body.hasAttribute('data-doc');
  291. },
  292. isMobile() {
  293. return this._isMobile != null ? this._isMobile : (this._isMobile = app.views.Mobile.detect());
  294. },
  295. isAndroidWebview() {
  296. return this._isAndroidWebview != null ? this._isAndroidWebview : (this._isAndroidWebview = app.views.Mobile.detectAndroidWebview());
  297. },
  298. isInvalidLocation() {
  299. return (this.config.env === 'production') && (location.host.indexOf(app.config.production_host) !== 0);
  300. }
  301. };
  302. $.extend(app, Events);
  303. function __guard__(value, transform) {
  304. return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
  305. }
  306. function __guardMethod__(obj, methodName, transform) {
  307. if (typeof obj !== 'undefined' && obj !== null && typeof obj[methodName] === 'function') {
  308. return transform(obj, methodName);
  309. } else {
  310. return undefined;
  311. }
  312. }