index.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Spine = require('spine/core')
  2. require('spine/route')
  3. require('spine/manager')
  4. require('lib/spine-couch-ajax')
  5. require('lib/spine-couch-changes')
  6. require('lib/fastclick')
  7. templates = require('duality/templates')
  8. session = require('session')
  9. MainNav = require('controllers/main-nav')
  10. MainStack = require('controllers/main-stack')
  11. HelpUI = require('controllers/ui/help')
  12. Site = require('models/site')
  13. Author = require('models/author')
  14. Collection = require('models/collection')
  15. Essay = require('models/essay')
  16. Video = require('models/video')
  17. Scene = require('models/scene')
  18. Block = require('models/block')
  19. Contact = require('models/contact')
  20. Sponsor = require('models/sponsor')
  21. Redirect = require('models/redirect')
  22. class App extends Spine.Controller
  23. constructor: ->
  24. super
  25. @appStarted = false
  26. @mainNav = new MainNav
  27. @append @mainNav
  28. @initApp()
  29. initApp: =>
  30. @mainStack = new MainStack
  31. @helpUI = new HelpUI
  32. @append @mainStack, @helpUI
  33. Spine.Route.setup()
  34. @setupSession()
  35. @hookPanelsToNav()
  36. @setupOnlineOffline()
  37. @doOtherStuff()
  38. setupSession: ->
  39. session.on 'change', @checkRole
  40. @getSessionInfo()
  41. getSessionInfo: =>
  42. session.info (err, info) =>
  43. if not info
  44. # try again in 5 seconds
  45. @mainNav.offline.show()
  46. @delay @getSessionInfo, 5000
  47. else
  48. @mainNav.offline.hide()
  49. @checkRole info.userCtx
  50. checkRole: (userCtx) =>
  51. if userCtx?.roles and 'manager' in userCtx.roles
  52. @mainNav.hideLogin()
  53. @mainNav.greetUser(userCtx.name)
  54. @startApp()
  55. @mainStack.el.css('visibility', 'visible')
  56. @helpUI.el.css('visibility', 'visible')
  57. else
  58. @mainNav.showLogin()
  59. @endApp()
  60. @mainStack.el.css('visibility', 'hidden')
  61. @helpUI.el.css('visibility', 'hidden')
  62. startApp: =>
  63. unless @appStarted
  64. @loadData()
  65. @navigate('/')
  66. @appStarted = true
  67. endApp: =>
  68. if @appStarted
  69. @unloadData()
  70. @mainStack.filterBox.reset()
  71. @appStarted = false
  72. loadData: =>
  73. # Load data models
  74. Site.fetch()
  75. Author.fetch()
  76. Collection.fetch()
  77. Essay.fetch()
  78. Video.fetch()
  79. Scene.fetch()
  80. Block.fetch()
  81. Contact.fetch()
  82. Sponsor.fetch()
  83. Redirect.fetch()
  84. @dataLoaded = true
  85. unloadData: =>
  86. # Empty data from client models
  87. Site.deleteAll()
  88. Author.deleteAll()
  89. Collection.deleteAll()
  90. Essay.deleteAll()
  91. Video.deleteAll()
  92. Scene.deleteAll()
  93. Block.deleteAll()
  94. Contact.deleteAll()
  95. Sponsor.deleteAll()
  96. Redirect.deleteAll()
  97. @dataLoaded = false
  98. hookPanelsToNav: ->
  99. cls = @
  100. for k, v of @mainStack.controllers
  101. @mainStack[k].active -> cls.mainNav.selectFromClassName(@className)
  102. setupOnlineOffline: ->
  103. if not navigator.onLine
  104. @mainNav.offline.show()
  105. $(window).on 'offline', =>
  106. @mainNav.offline.show()
  107. $(window).on 'online', =>
  108. @mainNav.offline.hide()
  109. @delay @getSessionInfo, 5000
  110. doOtherStuff: ->
  111. # Use the fastclick module for touch devices.
  112. # Add a class of `needsclick` if the original click is needed.
  113. new FastClick(document.body)
  114. # Alert user when leaving the application.
  115. window.onbeforeunload = (e) ->
  116. msg = 'Bye bye! Leaving Kleks now, but you may have unsaved items. Keep going if you are sure.'
  117. e = window.event if not e?
  118. e.returnValue = msg if e
  119. msg
  120. module.exports = App