SavingNotificationController.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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', 'ide/editor/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. 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. lockEditorModal.result.finally(() => (lockEditorModal = null)) // unset the modal if connection comes back
  61. }
  62. if (lockEditorModal && newUnsavedCount === 0) {
  63. lockEditorModal.dismiss('connection back up')
  64. }
  65. // for performance, only update the display if the old or new
  66. // counts of unsaved files are nonzeror. If both old and new
  67. // unsaved counts are zero then we know we are in a good state
  68. // and don't need to do anything to the UI.
  69. if (newUnsavedCount || oldUnsavedCount) {
  70. $scope.docSavingStatus = newStatus
  71. $scope.docSavingStatusCount = newUnsavedCount
  72. return $scope.$apply()
  73. }
  74. }
  75. return (warnAboutUnsavedChanges = function() {
  76. if (Document.hasUnsavedChanges()) {
  77. return 'You have unsaved changes. If you leave now they will not be saved.'
  78. }
  79. })
  80. }))