app.coffee 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_without_version', slug)
  110. if doc
  111. @disabledDocs.remove(doc)
  112. @docs.add(doc)
  113. @saveDocs() if needsSaving
  114. return
  115. enableDoc: (doc, _onSuccess, onError) ->
  116. return if @docs.contains(doc)
  117. onSuccess = =>
  118. return if @docs.contains(doc)
  119. @disabledDocs.remove(doc)
  120. @docs.add(doc)
  121. @docs.sort()
  122. @initDoc(doc)
  123. @saveDocs()
  124. _onSuccess()
  125. return
  126. doc.load onSuccess, onError, writeCache: true
  127. return
  128. saveDocs: ->
  129. @settings.setDocs(doc.slug for doc in @docs.all())
  130. @db.migrate()
  131. @appCache?.updateInBackground()
  132. welcomeBack: ->
  133. visitCount = @settings.get('count')
  134. @settings.set 'count', ++visitCount
  135. new app.views.Notif 'Share', autoHide: null if visitCount is 5
  136. new app.views.News()
  137. new app.views.Updates()
  138. @updateChecker = new app.UpdateChecker()
  139. reload: ->
  140. @docs.clearCache()
  141. @disabledDocs.clearCache()
  142. if @appCache then @appCache.reload() else window.location = '/'
  143. return
  144. reset: ->
  145. @localStorage.reset()
  146. @settings.reset()
  147. @db?.reset()
  148. @appCache?.update()
  149. window.location = '/'
  150. return
  151. showTip: (tip) ->
  152. return if @isSingleDoc()
  153. tips = @settings.getTips()
  154. if tips.indexOf(tip) is -1
  155. tips.push(tip)
  156. @settings.setTips(tips)
  157. new app.views.Tip(tip)
  158. return
  159. showLoading: ->
  160. document.body.classList.remove '_noscript'
  161. document.body.classList.add '_loading'
  162. return
  163. hideLoading: ->
  164. document.body.classList.remove '_booting'
  165. document.body.classList.remove '_loading'
  166. return
  167. indexHost: ->
  168. # Can't load the index files from the host/CDN when applicationCache is
  169. # enabled because it doesn't support caching URLs that use CORS.
  170. @config[if @appCache and @settings.hasDocs() then 'index_path' else 'docs_origin']
  171. onBootError: (args...) ->
  172. @trigger 'bootError'
  173. @hideLoading()
  174. return
  175. onQuotaExceeded: ->
  176. return if @quotaExceeded
  177. @quotaExceeded = true
  178. new app.views.Notif 'QuotaExceeded', autoHide: null
  179. return
  180. onCookieBlocked: (key, value, actual) ->
  181. return if @cookieBlocked
  182. @cookieBlocked = true
  183. new app.views.Notif 'CookieBlocked', autoHide: null
  184. Raven.captureMessage "CookieBlocked/#{key}", level: 'warning', extra: {value, actual}
  185. return
  186. onWindowError: (args...) ->
  187. return if @cookieBlocked
  188. if @isInjectionError args...
  189. @onInjectionError()
  190. else if @isAppError args...
  191. @previousErrorHandler? args...
  192. @hideLoading()
  193. @errorNotif or= new app.views.Notif 'Error'
  194. @errorNotif.show()
  195. return
  196. onInjectionError: ->
  197. unless @injectionError
  198. @injectionError = true
  199. alert """
  200. JavaScript code has been injected in the page which prevents DevDocs from running correctly.
  201. Please check your browser extensions/addons. """
  202. Raven.captureMessage 'injection error', level: 'info'
  203. return
  204. isInjectionError: ->
  205. # Some browser extensions expect the entire web to use jQuery.
  206. # I gave up trying to fight back.
  207. window.$ isnt app._$ or window.$$ isnt app._$$ or window.page isnt app._page or typeof $.empty isnt 'function' or typeof page.show isnt 'function'
  208. isAppError: (error, file) ->
  209. # Ignore errors from external scripts.
  210. file and file.indexOf('devdocs') isnt -1 and file.indexOf('.js') is file.length - 3
  211. isSupportedBrowser: ->
  212. try
  213. features =
  214. bind: !!Function::bind
  215. pushState: !!history.pushState
  216. matchMedia: !!window.matchMedia
  217. classList: !!document.body.classList
  218. insertAdjacentHTML: !!document.body.insertAdjacentHTML
  219. defaultPrevented: document.createEvent('CustomEvent').defaultPrevented is false
  220. cssGradients: supportsCssGradients()
  221. for key, value of features when not value
  222. Raven.captureMessage "unsupported/#{key}", level: 'info'
  223. return false
  224. true
  225. catch error
  226. Raven.captureMessage 'unsupported/exception', level: 'info', extra: { error: error }
  227. false
  228. isSingleDoc: ->
  229. document.body.hasAttribute('data-doc')
  230. isMobile: ->
  231. @_isMobile ?= app.views.Mobile.detect()
  232. isAndroidWebview: ->
  233. @_isAndroidWebview ?= app.views.Mobile.detectAndroidWebview()
  234. isInvalidLocation: ->
  235. @config.env is 'production' and location.host.indexOf(app.config.production_host) isnt 0
  236. supportsCssGradients = ->
  237. el = document.createElement('div')
  238. el.style.cssText = "background-image: -webkit-linear-gradient(top, #000, #fff); background-image: linear-gradient(to top, #000, #fff);"
  239. el.style.backgroundImage.indexOf('gradient') >= 0
  240. $.extend app, Events