SavingNotificationController.coffee 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. newStatus = {}
  15. for doc_id, doc of Document.openDocs
  16. saving = doc.pollSavedStatus()
  17. if !saving
  18. if oldStatus[doc_id]?
  19. newStatus[doc_id] = oldStatus[doc_id]
  20. newStatus[doc_id].unsavedSeconds += 1
  21. else
  22. newStatus[doc_id] = {
  23. unsavedSeconds: 0
  24. doc: ide.fileTreeManager.findEntityById(doc_id)
  25. }
  26. # for performance, only update the display if the old or new
  27. # statuses have any unsaved files
  28. if _.size(newStatus) or _.size(oldStatus)
  29. $scope.docSavingStatus = newStatus
  30. $scope.$apply()
  31. warnAboutUnsavedChanges = () ->
  32. if Document.hasUnsavedChanges()
  33. return "You have unsaved changes. If you leave now they will not be saved."
  34. ]