DropboxSettingsManager.coffee 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. define [
  2. "utils/Modal"
  3. "account/AccountManager"
  4. "underscore"
  5. ], (Modal, accountManager, _) ->
  6. _.templateSettings = interpolate : /\{\{(.+?)\}\}/g
  7. POLLING_INTERVAL = 15
  8. ONE_MIN_MILI = 1000*60
  9. USER_LINKED_TEMPLATE = _.template($('#userLinkedToDropboxTemplate').html())
  10. class DropboxSettingsManger
  11. constructor: (@ide)->
  12. setup = _.once =>
  13. @tab = $("#dropboxProjectSettings")
  14. tabLink = $('#manageDropboxSettiingsTabLink').on 'click', (event)=>
  15. @tab.empty()
  16. if !@ide.isAllowedToDoIt "owner"
  17. else if !@project.get('features').dropbox
  18. ga('send', 'event', 'subscription-funnel', 'askToUpgrade', "dropbox")
  19. accountManager.askToUpgrade @ide,
  20. onUpgrade: =>
  21. @checkIfUserIsLinkedToDropbox()
  22. ga('send', 'event', 'subscription-funnel', 'upgraded-free-trial', "dropbox")
  23. else
  24. @checkIfUserIsLinkedToDropbox()
  25. @ide.on "afterJoinProject", (project) =>
  26. @project = project
  27. setup()
  28. checkIfUserIsLinkedToDropbox : =>
  29. @ide.socket.emit "getUserDropboxLinkStatus", @project.get("owner").id, (err, status)=>
  30. if status.registered
  31. @buildLinkedView()
  32. else
  33. @buildNonLinkedView()
  34. buildLinkedView: ->
  35. self = @
  36. run = ->
  37. @ide.socket.emit "getLastTimePollHappned", (err, lastTimePollHappened)=>
  38. milisecondsSinceLastPoll = new Date().getTime() - lastTimePollHappened
  39. roundedMinsSinceLastPoll = Math.round(milisecondsSinceLastPoll / ONE_MIN_MILI)
  40. minsTillNextPoll = POLLING_INTERVAL - roundedMinsSinceLastPoll
  41. percentageLeftTillNextPoll = 100 - ((roundedMinsSinceLastPoll / POLLING_INTERVAL) * 100)
  42. html = $(USER_LINKED_TEMPLATE(minsTillNextPoll:minsTillNextPoll, percentageLeftTillNextPoll:percentageLeftTillNextPoll))
  43. self.tab.empty()
  44. self.tab.append html
  45. setTimeout run, ONE_MIN_MILI
  46. run()
  47. buildNonLinkedView: ->
  48. @tab.append $('#userNotLinkedToDropboxTemplate').html()