2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
|
|
|
import Document from '../Document'
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default App.controller(
|
|
|
|
'SavingNotificationController',
|
|
|
|
function ($scope, $interval, ide) {
|
|
|
|
let warnAboutUnsavedChanges
|
|
|
|
setInterval(() => pollSavedStatus(), 1000)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$(window).bind('beforeunload', () => {
|
|
|
|
return warnAboutUnsavedChanges()
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
let lockEditorModal = null // modal showing "connection lost"
|
|
|
|
let originalPermissionsLevel
|
|
|
|
const MAX_UNSAVED_SECONDS = 15 // lock the editor after this time if unsaved
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.docSavingStatus = {}
|
2021-10-26 04:08:56 -04:00
|
|
|
function pollSavedStatus() {
|
2021-04-14 09:17:21 -04:00
|
|
|
let t
|
|
|
|
const oldStatus = $scope.docSavingStatus
|
|
|
|
const oldUnsavedCount = $scope.docSavingStatusCount
|
|
|
|
const newStatus = {}
|
|
|
|
let newUnsavedCount = 0
|
|
|
|
let maxUnsavedSeconds = 0
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const doc_id in Document.openDocs) {
|
2021-04-14 09:17:21 -04:00
|
|
|
const doc = Document.openDocs[doc_id]
|
|
|
|
const saving = doc.pollSavedStatus()
|
|
|
|
if (!saving) {
|
|
|
|
newUnsavedCount++
|
|
|
|
if (oldStatus[doc_id] != null) {
|
|
|
|
newStatus[doc_id] = oldStatus[doc_id]
|
|
|
|
t = newStatus[doc_id].unsavedSeconds += 1
|
|
|
|
if (t > maxUnsavedSeconds) {
|
|
|
|
maxUnsavedSeconds = t
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newStatus[doc_id] = {
|
|
|
|
unsavedSeconds: 0,
|
2021-04-27 03:52:58 -04:00
|
|
|
doc: ide.fileTreeManager.findEntityById(doc_id),
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
if (newUnsavedCount > 0 && t > MAX_UNSAVED_SECONDS && !lockEditorModal) {
|
|
|
|
lockEditorModal = ide.showLockEditorMessageModal(
|
|
|
|
'Connection lost',
|
|
|
|
'Sorry, the connection to the server is down.'
|
|
|
|
)
|
2020-05-01 09:59:55 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
// put editor in readOnly mode
|
|
|
|
originalPermissionsLevel = ide.$scope.permissionsLevel
|
|
|
|
ide.$scope.permissionsLevel = 'readOnly'
|
2020-05-01 09:59:55 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
lockEditorModal.result.finally(() => {
|
|
|
|
lockEditorModal = null // unset the modal if connection comes back
|
|
|
|
// restore original permissions
|
|
|
|
ide.$scope.permissionsLevel = originalPermissionsLevel
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
if (lockEditorModal && newUnsavedCount === 0) {
|
|
|
|
lockEditorModal.dismiss('connection back up')
|
|
|
|
// restore original permissions if they were changed
|
|
|
|
if (originalPermissionsLevel) {
|
|
|
|
ide.$scope.permissionsLevel = originalPermissionsLevel
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
// for performance, only update the display if the old or new
|
|
|
|
// counts of unsaved files are nonzeror. If both old and new
|
|
|
|
// unsaved counts are zero then we know we are in a good state
|
|
|
|
// and don't need to do anything to the UI.
|
|
|
|
if (newUnsavedCount || oldUnsavedCount) {
|
|
|
|
$scope.docSavingStatus = newStatus
|
|
|
|
$scope.docSavingStatusCount = newUnsavedCount
|
|
|
|
return $scope.$apply()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return (warnAboutUnsavedChanges = function () {
|
|
|
|
if (Document.hasUnsavedChanges()) {
|
|
|
|
return 'You have unsaved changes. If you leave now they will not be saved.'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|