SavingNotificationController.js 3.3 KB

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