SavingNotificationController.coffee 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. $scope.docSavingStatus = {}
  12. pollSavedStatus = () ->
  13. oldStatus = $scope.docSavingStatus
  14. oldUnsavedCount = $scope.docSavingStatusCount
  15. newStatus = {}
  16. newUnsavedCount = 0
  17. for doc_id, doc of Document.openDocs
  18. saving = doc.pollSavedStatus()
  19. if !saving
  20. newUnsavedCount++
  21. if oldStatus[doc_id]?
  22. newStatus[doc_id] = oldStatus[doc_id]
  23. newStatus[doc_id].unsavedSeconds += 1
  24. if newStatus[doc_id].unsavedSeconds == 60
  25. reportUnsavedChanges(doc)
  26. else
  27. newStatus[doc_id] = {
  28. unsavedSeconds: 0
  29. doc: ide.fileTreeManager.findEntityById(doc_id)
  30. }
  31. # for performance, only update the display if the old or new
  32. # counts of unsaved files are nonzeror. If both old and new
  33. # unsaved counts are zero then we know we are in a good state
  34. # and don't need to do anything to the UI.
  35. if newUnsavedCount or oldUnsavedCount
  36. $scope.docSavingStatus = newStatus
  37. $scope.docSavingStatusCount = newUnsavedCount
  38. $scope.$apply()
  39. reportUnsavedChanges = (doc) ->
  40. ide.reportError new Error("unsaved changes"), {
  41. inflightOp: doc?.getInflightOp(),
  42. pendingOp: doc?.getPendingOp()
  43. }
  44. warnAboutUnsavedChanges = () ->
  45. if Document.hasUnsavedChanges()
  46. return "You have unsaved changes. If you leave now they will not be saved."
  47. ]