SavingNotificationController.coffee 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. else
  25. newStatus[doc_id] = {
  26. unsavedSeconds: 0
  27. doc: ide.fileTreeManager.findEntityById(doc_id)
  28. }
  29. # for performance, only update the display if the old or new
  30. # counts of unsaved files are nonzeror. If both old and new
  31. # unsaved counts are zero then we know we are in a good state
  32. # and don't need to do anything to the UI.
  33. if newUnsavedCount or oldUnsavedCount
  34. $scope.docSavingStatus = newStatus
  35. $scope.docSavingStatusCount = newUnsavedCount
  36. $scope.$apply()
  37. warnAboutUnsavedChanges = () ->
  38. if Document.hasUnsavedChanges()
  39. return "You have unsaved changes. If you leave now they will not be saved."
  40. ]