app.js 12 KB

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