app.coffee 8.3 KB

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