SavingNotificationController.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. define(['../../../base', '../Document'], (App, Document) =>
  15. App.controller('SavingNotificationController', function(
  16. $scope,
  17. $interval,
  18. ide
  19. ) {
  20. let warnAboutUnsavedChanges
  21. setInterval(() => pollSavedStatus(), 1000)
  22. $(window).bind('beforeunload', () => {
  23. return warnAboutUnsavedChanges()
  24. })
  25. let lockEditorModal = null // modal showing "connection lost"
  26. let originalPermissionsLevel
  27. const MAX_UNSAVED_SECONDS = 15 // lock the editor after this time if unsaved
  28. $scope.docSavingStatus = {}
  29. var pollSavedStatus = function() {
  30. let t
  31. const oldStatus = $scope.docSavingStatus
  32. const oldUnsavedCount = $scope.docSavingStatusCount
  33. const newStatus = {}
  34. let newUnsavedCount = 0
  35. let maxUnsavedSeconds = 0
  36. for (let doc_id in Document.openDocs) {
  37. const doc = Document.openDocs[doc_id]
  38. const saving = doc.pollSavedStatus()
  39. if (!saving) {
  40. newUnsavedCount++
  41. if (oldStatus[doc_id] != null) {
  42. newStatus[doc_id] = oldStatus[doc_id]
  43. t = newStatus[doc_id].unsavedSeconds += 1
  44. if (t > maxUnsavedSeconds) {
  45. maxUnsavedSeconds = t
  46. }
  47. } else {
  48. newStatus[doc_id] = {
  49. unsavedSeconds: 0,
  50. doc: ide.fileTreeManager.findEntityById(doc_id)
  51. }
  52. }
  53. }
  54. }
  55. if (newUnsavedCount > 0 && t > MAX_UNSAVED_SECONDS && !lockEditorModal) {
  56. lockEditorModal = ide.showLockEditorMessageModal(
  57. 'Connection lost',
  58. 'Sorry, the connection to the server is down.'
  59. )
  60. // put editor in readOnly mode
  61. originalPermissionsLevel = ide.$scope.permissionsLevel
  62. ide.$scope.permissionsLevel = 'readOnly'
  63. lockEditorModal.result.finally(() => {
  64. lockEditorModal = null // unset the modal if connection comes back
  65. // restore original permissions
  66. ide.$scope.permissionsLevel = originalPermissionsLevel
  67. })
  68. }
  69. if (lockEditorModal && newUnsavedCount === 0) {
  70. lockEditorModal.dismiss('connection back up')
  71. // restore original permissions if they were changed
  72. if (originalPermissionsLevel) {
  73. ide.$scope.permissionsLevel = originalPermissionsLevel
  74. }
  75. }
  76. // for performance, only update the display if the old or new
  77. // counts of unsaved files are nonzeror. If both old and new
  78. // unsaved counts are zero then we know we are in a good state
  79. // and don't need to do anything to the UI.
  80. if (newUnsavedCount || oldUnsavedCount) {
  81. $scope.docSavingStatus = newStatus
  82. $scope.docSavingStatusCount = newUnsavedCount
  83. return $scope.$apply()
  84. }
  85. }
  86. return (warnAboutUnsavedChanges = function() {
  87. if (Document.hasUnsavedChanges()) {
  88. return 'You have unsaved changes. If you leave now they will not be saved.'
  89. }
  90. })
  91. }))