SavingNotificationController.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import App from '../../../base'
  15. import Document from '../Document'
  16. export default App.controller(
  17. 'SavingNotificationController',
  18. function ($scope, $interval, ide) {
  19. let warnAboutUnsavedChanges
  20. setInterval(() => pollSavedStatus(), 1000)
  21. $(window).bind('beforeunload', () => {
  22. return warnAboutUnsavedChanges()
  23. })
  24. let lockEditorModal = null // modal showing "connection lost"
  25. let originalPermissionsLevel
  26. const MAX_UNSAVED_SECONDS = 15 // lock the editor after this time if unsaved
  27. $scope.docSavingStatus = {}
  28. var pollSavedStatus = function () {
  29. let t
  30. const oldStatus = $scope.docSavingStatus
  31. const oldUnsavedCount = $scope.docSavingStatusCount
  32. const newStatus = {}
  33. let newUnsavedCount = 0
  34. let maxUnsavedSeconds = 0
  35. for (const doc_id in Document.openDocs) {
  36. const doc = Document.openDocs[doc_id]
  37. const saving = doc.pollSavedStatus()
  38. if (!saving) {
  39. newUnsavedCount++
  40. if (oldStatus[doc_id] != null) {
  41. newStatus[doc_id] = oldStatus[doc_id]
  42. t = newStatus[doc_id].unsavedSeconds += 1
  43. if (t > maxUnsavedSeconds) {
  44. maxUnsavedSeconds = t
  45. }
  46. } else {
  47. newStatus[doc_id] = {
  48. unsavedSeconds: 0,
  49. doc: ide.fileTreeManager.findEntityById(doc_id),
  50. }
  51. }
  52. }
  53. }
  54. if (newUnsavedCount > 0 && t > MAX_UNSAVED_SECONDS && !lockEditorModal) {
  55. lockEditorModal = ide.showLockEditorMessageModal(
  56. 'Connection lost',
  57. 'Sorry, the connection to the server is down.'
  58. )
  59. // put editor in readOnly mode
  60. originalPermissionsLevel = ide.$scope.permissionsLevel
  61. ide.$scope.permissionsLevel = 'readOnly'
  62. lockEditorModal.result.finally(() => {
  63. lockEditorModal = null // unset the modal if connection comes back
  64. // restore original permissions
  65. ide.$scope.permissionsLevel = originalPermissionsLevel
  66. })
  67. }
  68. if (lockEditorModal && newUnsavedCount === 0) {
  69. lockEditorModal.dismiss('connection back up')
  70. // restore original permissions if they were changed
  71. if (originalPermissionsLevel) {
  72. ide.$scope.permissionsLevel = originalPermissionsLevel
  73. }
  74. }
  75. // for performance, only update the display if the old or new
  76. // counts of unsaved files are nonzeror. If both old and new
  77. // unsaved counts are zero then we know we are in a good state
  78. // and don't need to do anything to the UI.
  79. if (newUnsavedCount || oldUnsavedCount) {
  80. $scope.docSavingStatus = newStatus
  81. $scope.docSavingStatusCount = newUnsavedCount
  82. return $scope.$apply()
  83. }
  84. }
  85. return (warnAboutUnsavedChanges = function () {
  86. if (Document.hasUnsavedChanges()) {
  87. return 'You have unsaved changes. If you leave now they will not be saved.'
  88. }
  89. })
  90. }
  91. )