app.coffee 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. @app =
  2. _$: $
  3. _$$: $$
  4. _page: page
  5. collections: {}
  6. models: {}
  7. templates: {}
  8. views: {}
  9. init: ->
  10. try @initErrorTracking() catch
  11. return unless @browserCheck()
  12. @el = $('._app')
  13. @localStorage = new LocalStorageStore
  14. @appCache = new app.AppCache if app.AppCache.isEnabled()
  15. @settings = new app.Settings
  16. @db = new app.DB()
  17. @docs = new app.collections.Docs
  18. @disabledDocs = new app.collections.Docs
  19. @entries = new app.collections.Entries
  20. @router = new app.Router
  21. @shortcuts = new app.Shortcuts
  22. @document = new app.views.Document
  23. @mobile = new app.views.Mobile if @isMobile()
  24. if document.body.hasAttribute('data-doc')
  25. @DOC = JSON.parse(document.body.getAttribute('data-doc'))
  26. @bootOne()
  27. else if @DOCS
  28. @bootAll()
  29. else
  30. @onBootError()
  31. return
  32. browserCheck: ->
  33. return true if @isSupportedBrowser()
  34. document.body.innerHTML = app.templates.unsupportedBrowser
  35. @hideLoadingScreen()
  36. false
  37. initErrorTracking: ->
  38. # Show a warning message and don't track errors when the app is loaded
  39. # from a domain other than our own, because things are likely to break.
  40. # (e.g. cross-domain requests)
  41. if @isInvalidLocation()
  42. new app.views.Notif 'InvalidLocation'
  43. else
  44. if @config.sentry_dsn
  45. Raven.config @config.sentry_dsn,
  46. release: @config.release
  47. whitelistUrls: [/devdocs/]
  48. includePaths: [/devdocs/]
  49. ignoreErrors: [/NPObject/, /NS_ERROR/, /^null$/, /EvalError/]
  50. tags:
  51. mode: if @isSingleDoc() then 'single' else 'full'
  52. iframe: (window.top isnt window).toString()
  53. electron: (!!window.process?.versions?.electron).toString()
  54. shouldSendCallback: =>
  55. try
  56. if @isInjectionError()
  57. @onInjectionError()
  58. return false
  59. if @isAndroidWebview()
  60. return false
  61. true
  62. dataCallback: (data) ->
  63. try
  64. $.extend(data.user ||= {}, app.settings.dump())
  65. data.user.docs = data.user.docs.split('/') if data.user.docs
  66. data.user.lastIDBTransaction = app.lastIDBTransaction if app.lastIDBTransaction
  67. data.tags.scriptCount = document.scripts.length
  68. data
  69. .install()
  70. @previousErrorHandler = onerror
  71. window.onerror = @onWindowError.bind(@)
  72. CookieStore.onBlocked = @onCookieBlocked
  73. return
  74. bootOne: ->
  75. @doc = new app.models.Doc @DOC
  76. @docs.reset [@doc]
  77. @doc.load @start.bind(@), @onBootError.bind(@), readCache: true
  78. new app.views.Notice 'singleDoc', @doc
  79. delete @DOC
  80. return
  81. bootAll: ->
  82. docs = @settings.getDocs()
  83. for doc in @DOCS
  84. (if docs.indexOf(doc.slug) >= 0 then @docs else @disabledDocs).add(doc)
  85. @migrateDocs()
  86. @docs.load @start.bind(@), @onBootError.bind(@), readCache: true, writeCache: true
  87. delete @DOCS
  88. return
  89. start: ->
  90. @entries.add doc.toEntry() for doc in @docs.all()
  91. @entries.add doc.toEntry() for doc in @disabledDocs.all()
  92. @initDoc(doc) for doc in @docs.all()
  93. @trigger 'ready'
  94. @router.start()
  95. @hideLoadingScreen()
  96. setTimeout =>
  97. @welcomeBack() unless @doc
  98. @removeEvent 'ready bootError'
  99. , 50
  100. return
  101. initDoc: (doc) ->
  102. doc.entries.add type.toEntry() for type in doc.types.all()
  103. @entries.add doc.entries.all()
  104. return
  105. migrateDocs: ->
  106. for slug in @settings.getDocs() when not @docs.findBy('slug', slug)
  107. needsSaving = true
  108. doc = @disabledDocs.findBy('slug', 'webpack') if slug == 'webpack~2'
  109. doc = @disabledDocs.findBy('slug', 'angular') if slug == 'angular~4_typescript'
  110. doc = @disabledDocs.findBy('slug', 'angular~2') if slug == 'angular~2_typescript'
  111. doc ||= @disabledDocs.findBy('slug_without_version', slug)
  112. if doc
  113. @disabledDocs.remove(doc)
  114. @docs.add(doc)
  115. @saveDocs() if needsSaving
  116. return
  117. enableDoc: (doc, _onSuccess, onError) ->
  118. return if @docs.contains(doc)
  119. onSuccess = =>
  120. return if @docs.contains(doc)
  121. @disabledDocs.remove(doc)
  122. @docs.add(doc)
  123. @docs.sort()
  124. @initDoc(doc)
  125. @saveDocs()
  126. _onSuccess()
  127. return
  128. doc.load onSuccess, onError, writeCache: true
  129. return
  130. saveDocs: ->
  131. @settings.setDocs(doc.slug for doc in @docs.all())
  132. @db.migrate()
  133. @appCache?.updateInBackground()
  134. welcomeBack: ->
  135. visitCount = @settings.get('count')
  136. @settings.set 'count', ++visitCount
  137. new app.views.Notif 'Share', autoHide: null if visitCount is 5
  138. new app.views.News()
  139. new app.views.Updates()
  140. @updateChecker = new app.UpdateChecker()
  141. reload: ->
  142. @docs.clearCache()
  143. @disabledDocs.clearCache()
  144. if @appCache then @appCache.reload() else window.location = '/'
  145. return
  146. reset: ->
  147. @localStorage.reset()
  148. @settings.reset()
  149. @db?.reset()
  150. @appCache?.update()
  151. window.location = '/'
  152. return
  153. showTip: (tip) ->
  154. return if @isSingleDoc()
  155. tips = @settings.getTips()
  156. if tips.indexOf(tip) is -1
  157. tips.push(tip)
  158. @settings.setTips(tips)
  159. new app.views.Tip(tip)
  160. return
  161. hideLoadingScreen: ->
  162. document.body.classList.add '_overlay-scrollbars' if $.overlayScrollbarsEnabled()
  163. document.documentElement.classList.remove '_booting'
  164. return
  165. indexHost: ->
  166. # Can't load the index files from the host/CDN when applicationCache is
  167. # enabled because it doesn't support caching URLs that use CORS.
  168. @config[if @appCache and @settings.hasDocs() then 'index_path' else 'docs_origin']
  169. onBootError: (args...) ->
  170. @trigger 'bootError'
  171. @hideLoadingScreen()
  172. return
  173. onQuotaExceeded: ->
  174. return if @quotaExceeded
  175. @quotaExceeded = true
  176. new app.views.Notif 'QuotaExceeded', autoHide: null
  177. return
  178. onCookieBlocked: (key, value, actual) ->
  179. return if @cookieBlocked
  180. @cookieBlocked = true
  181. new app.views.Notif 'CookieBlocked', autoHide: null
  182. Raven.captureMessage "CookieBlocked/#{key}", level: 'warning', extra: {value, actual}
  183. return
  184. onWindowError: (args...) ->
  185. return if @cookieBlocked
  186. if @isInjectionError args...
  187. @onInjectionError()
  188. else if @isAppError args...
  189. @previousErrorHandler? args...
  190. @hideLoadingScreen()
  191. @errorNotif or= new app.views.Notif 'Error'
  192. @errorNotif.show()
  193. return
  194. onInjectionError: ->
  195. unless @injectionError
  196. @injectionError = true
  197. alert """
  198. JavaScript code has been injected in the page which prevents DevDocs from running correctly.
  199. Please check your browser extensions/addons. """
  200. Raven.captureMessage 'injection error', level: 'info'
  201. return
  202. isInjectionError: ->
  203. # Some browser extensions expect the entire web to use jQuery.
  204. # I gave up trying to fight back.
  205. window.$ isnt app._$ or window.$$ isnt app._$$ or window.page isnt app._page or typeof $.empty isnt 'function' or typeof page.show isnt 'function'
  206. isAppError: (error, file) ->
  207. # Ignore errors from external scripts.
  208. file and file.indexOf('devdocs') isnt -1 and file.indexOf('.js') is file.length - 3
  209. isSupportedBrowser: ->
  210. try
  211. features =
  212. bind: !!Function::bind
  213. pushState: !!history.pushState
  214. matchMedia: !!window.matchMedia
  215. insertAdjacentHTML: !!document.body.insertAdjacentHTML
  216. defaultPrevented: document.createEvent('CustomEvent').defaultPrevented is false
  217. cssVariables: CSS.supports and CSS.supports('(--t: 0)')
  218. for key, value of features when not value
  219. Raven.captureMessage "unsupported/#{key}", level: 'info'
  220. return false
  221. true
  222. catch error
  223. Raven.captureMessage 'unsupported/exception', level: 'info', extra: { error: error }
  224. false
  225. isSingleDoc: ->
  226. document.body.hasAttribute('data-doc')
  227. isMobile: ->
  228. @_isMobile ?= app.views.Mobile.detect()
  229. isAndroidWebview: ->
  230. @_isAndroidWebview ?= app.views.Mobile.detectAndroidWebview()
  231. isInvalidLocation: ->
  232. @config.env is 'production' and location.host.indexOf(app.config.production_host) isnt 0
  233. $.extend app, Events