SavingNotificationController.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. define [
  2. "base"
  3. "ide/editor/Document"
  4. ], (App, Document) ->
  5. App.controller "SavingNotificationController", ["$scope", "$interval", "ide", ($scope, $interval, ide) ->
  6. setInterval () ->
  7. pollSavedStatus()
  8. , 1000
  9. $(window).bind 'beforeunload', () =>
  10. warnAboutUnsavedChanges()
  11. lockEditorModal = null # modal showing "connection lost"
  12. MAX_UNSAVED_SECONDS = 15 # lock the editor after this time if unsaved
  13. $scope.docSavingStatus = {}
  14. pollSavedStatus = () ->
  15. oldStatus = $scope.docSavingStatus
  16. oldUnsavedCount = $scope.docSavingStatusCount
  17. newStatus = {}
  18. newUnsavedCount = 0
  19. maxUnsavedSeconds = 0
  20. for doc_id, doc of Document.openDocs
  21. saving = doc.pollSavedStatus()
  22. if !saving
  23. newUnsavedCount++
  24. if oldStatus[doc_id]?
  25. newStatus[doc_id] = oldStatus[doc_id]
  26. t = newStatus[doc_id].unsavedSeconds += 1
  27. if t > maxUnsavedSeconds
  28. maxUnsavedSeconds = t
  29. else
  30. newStatus[doc_id] = {
  31. unsavedSeconds: 0
  32. doc: ide.fileTreeManager.findEntityById(doc_id)
  33. }
  34. if newUnsavedCount > 0 and t > MAX_UNSAVED_SECONDS and not lockEditorModal
  35. lockEditorModal = ide.showLockEditorMessageModal(
  36. "Connection lost"
  37. "Sorry, the connection to the server is down."
  38. )
  39. lockEditorModal.result.finally () ->
  40. lockEditorModal = null # unset the modal if connection comes back
  41. if lockEditorModal and newUnsavedCount is 0
  42. lockEditorModal.dismiss "connection back up"
  43. # for performance, only update the display if the old or new
  44. # counts of unsaved files are nonzeror. If both old and new
  45. # unsaved counts are zero then we know we are in a good state
  46. # and don't need to do anything to the UI.
  47. if newUnsavedCount or oldUnsavedCount
  48. $scope.docSavingStatus = newStatus
  49. $scope.docSavingStatusCount = newUnsavedCount
  50. $scope.$apply()
  51. warnAboutUnsavedChanges = () ->
  52. if Document.hasUnsavedChanges()
  53. return "You have unsaved changes. If you leave now they will not be saved."
  54. ]