app.coffee 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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$/]
  51. tags:
  52. mode: if @isSingleDoc() then 'single' else 'full'
  53. iframe: (window.top isnt window).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
  68. .install()
  69. @previousErrorHandler = onerror
  70. window.onerror = @onWindowError.bind(@)
  71. CookieStore.onBlocked = @onCookieBlocked
  72. return
  73. bootOne: ->
  74. @doc = new app.models.Doc @DOC
  75. @docs.reset [@doc]
  76. @doc.load @start.bind(@), @onBootError.bind(@), readCache: true
  77. new app.views.Notice 'singleDoc', @doc
  78. delete @DOC
  79. return
  80. bootAll: ->
  81. docs = @settings.getDocs()
  82. for doc in @DOCS
  83. (if docs.indexOf(doc.slug) >= 0 then @docs else @disabledDocs).add(doc)
  84. @migrateDocs()
  85. @docs.sort()
  86. @disabledDocs.sort()
  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. @welcomeBack() unless @doc
  98. @removeEvent 'ready bootError'
  99. return
  100. initDoc: (doc) ->
  101. @entries.add type.toEntry() for type in doc.types.all()
  102. @entries.add doc.entries.all()
  103. return
  104. migrateDocs: ->
  105. for slug in @settings.getDocs() when not @docs.findBy('slug', slug)
  106. needsSaving = true
  107. doc = @disabledDocs.findBy('slug_without_version', slug)
  108. if doc
  109. @disabledDocs.remove(doc)
  110. @docs.add(doc)
  111. @saveDocs() if needsSaving
  112. return
  113. enableDoc: (doc, _onSuccess, onError) ->
  114. return if @docs.contains(doc)
  115. onSuccess = =>
  116. return if @docs.contains(doc)
  117. @disabledDocs.remove(doc)
  118. @docs.add(doc)
  119. @docs.sort()
  120. @initDoc(doc)
  121. @saveDocs()
  122. _onSuccess()
  123. return
  124. doc.load onSuccess, onError, writeCache: true
  125. return
  126. saveDocs: ->
  127. @settings.setDocs(doc.slug for doc in @docs.all())
  128. @db.migrate()
  129. @appCache?.updateInBackground()
  130. welcomeBack: ->
  131. visitCount = @settings.get('count')
  132. @settings.set 'count', ++visitCount
  133. new app.views.Notif 'Share', autoHide: null if visitCount is 5
  134. new app.views.News()
  135. new app.views.Updates()
  136. @updateChecker = new app.UpdateChecker()
  137. reload: ->
  138. @docs.clearCache()
  139. @disabledDocs.clearCache()
  140. if @appCache then @appCache.reload() else window.location = '/'
  141. return
  142. reset: ->
  143. @localStorage.reset()
  144. @settings.reset()
  145. @db?.reset()
  146. @appCache?.update()
  147. window.location = '/'
  148. return
  149. showTip: (tip) ->
  150. return if @isSingleDoc()
  151. tips = @settings.getTips()
  152. if tips.indexOf(tip) is -1
  153. tips.push(tip)
  154. @settings.setTips(tips)
  155. new app.views.Tip(tip)
  156. return
  157. showLoading: ->
  158. document.body.classList.remove '_noscript'
  159. document.body.classList.add '_loading'
  160. return
  161. hideLoading: ->
  162. document.body.classList.remove '_booting'
  163. document.body.classList.remove '_loading'
  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. @hideLoading()
  172. return
  173. onQuotaExceeded: ->
  174. return if @quotaExceeded
  175. @quotaExceeded = true
  176. new app.views.Notif 'QuotaExceeded', autoHide: null
  177. Raven.captureMessage 'QuotaExceededError', level: 'warning'
  178. return
  179. onCookieBlocked: (key, value, actual) ->
  180. return if @cookieBlocked
  181. @cookieBlocked = true
  182. new app.views.Notif 'CookieBlocked', autoHide: null
  183. Raven.captureMessage "CookieBlocked/#{key}", level: 'warning', extra: {value, actual}
  184. return
  185. onWindowError: (args...) ->
  186. return if @cookieBlocked
  187. if @isInjectionError args...
  188. @onInjectionError()
  189. else if @isAppError args...
  190. @previousErrorHandler? args...
  191. @hideLoading()
  192. @errorNotif or= new app.views.Notif 'Error'
  193. @errorNotif.show()
  194. return
  195. onInjectionError: ->
  196. unless @injectionError
  197. @injectionError = true
  198. alert """
  199. JavaScript code has been injected in the page which prevents DevDocs from running correctly.
  200. Please check your browser extensions/addons. """
  201. Raven.captureMessage 'injection error', level: 'info'
  202. return
  203. isInjectionError: ->
  204. # Some browser extensions expect the entire web to use jQuery.
  205. # I gave up trying to fight back.
  206. window.$ isnt app._$ or window.$$ isnt app._$$ or window.page isnt app._page or typeof $.empty isnt 'function' or typeof page.show isnt 'function'
  207. isAppError: (error, file) ->
  208. # Ignore errors from external scripts.
  209. file and file.indexOf('devdocs') isnt -1 and file.indexOf('.js') is file.length - 3
  210. isSupportedBrowser: ->
  211. try
  212. features =
  213. bind: !!Function::bind
  214. pushState: !!history.pushState
  215. matchMedia: !!window.matchMedia
  216. classList: !!document.body.classList
  217. insertAdjacentHTML: !!document.body.insertAdjacentHTML
  218. defaultPrevented: document.createEvent('CustomEvent').defaultPrevented is false
  219. cssGradients: supportsCssGradients()
  220. for key, value of features when not value
  221. Raven.captureMessage "unsupported/#{key}", level: 'info'
  222. return false
  223. true
  224. catch error
  225. Raven.captureMessage 'unsupported/exception', level: 'info', extra: { error: error }
  226. false
  227. isSingleDoc: ->
  228. document.body.hasAttribute('data-doc')
  229. isMobile: ->
  230. @_isMobile ?= app.views.Mobile.detect()
  231. isAndroidWebview: ->
  232. @_isAndroidWebview ?= app.views.Mobile.detectAndroidWebview()
  233. isInvalidLocation: ->
  234. @config.env is 'production' and location.host.indexOf(app.config.production_host) isnt 0
  235. supportsCssGradients = ->
  236. el = document.createElement('div')
  237. el.style.cssText = "background-image: -webkit-linear-gradient(top, #000, #fff); background-image: linear-gradient(to top, #000, #fff);"
  238. el.style.backgroundImage.indexOf('gradient') >= 0
  239. $.extend app, Events