Skip to content

Commit 4a14d43

Browse files
committed
Prevent beforeunload actions when downloading files
Previously, downloading a file would also trigger the beforeunload action and thereby remove the ACE editor or JsTree objects. Now, we prevent this by removing the `beforeunload` event listener and reinstalling it after "navigation".
1 parent 809c53d commit 4a14d43

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

app/assets/javascripts/editor/editor.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,19 +428,22 @@ var CodeOceanEditor = {
428428
const jsTree = filesInstance?.jstree(true);
429429
$(document).on('theme:change', themeListener);
430430
$(document).one('turbo:visit', function() {
431-
$(document).off('theme:change', themeListener);
432-
if (jsTree && jsTree.element) {
433-
jsTree.destroy(true);
434-
}
431+
CodeOceanEditor.removeFileTreeEventHandlers(filesInstance);
435432
});
436433
$(window).one('beforeunload', function() {
437-
$(document).off('theme:change', themeListener);
438-
if (jsTree && jsTree.element) {
439-
jsTree.destroy(true);
440-
}
434+
CodeOceanEditor.removeFileTreeEventHandlers(filesInstance);
441435
});
442436
},
443437

438+
removeFileTreeEventHandlers: function (filesInstance) {
439+
const themeListener = this.createFileTreeThemeChangeListener(filesInstance);
440+
const jsTree = filesInstance?.jstree(true);
441+
$(document).off('theme:change', themeListener);
442+
if (jsTree && jsTree.element) {
443+
jsTree.destroy(true);
444+
}
445+
},
446+
444447
createFileTreeThemeChangeListener: function (filesInstance) {
445448
return function (event) {
446449
const jsTree = filesInstance?.jstree(true);
@@ -853,7 +856,9 @@ var CodeOceanEditor = {
853856
selected.instance.deselect_all();
854857
const downloadPath = selected.node.original.download_path;
855858
if (downloadPath) {
859+
$(window).off("beforeunload");
856860
window.location = downloadPath;
861+
$(window).one("beforeunload", this.unloadEverything.bind(this, App.synchronized_editor));
857862
}
858863
}.bind(this));
859864
$('#download-files').removeClass('d-none');

app/assets/javascripts/editor/submissions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ CodeOceanEditorSubmissions = {
102102
const submission = await this.createSubmission('#download', null).catch(this.ajaxError.bind(this));
103103
if(!submission) return;
104104

105+
$(window).off("beforeunload");
105106
// to download just a single file, use the following url
106107
// window.location = Routes.download_file_submission_url(submission.id, CodeOceanEditor.active_file.filename);
107108
window.location = Routes.download_submission_url(submission.id);
109+
$(window).one("beforeunload", this.unloadEverything.bind(this, App.synchronized_editor));
108110
},
109111

110112
resetCode: function(initiator, onlyActiveFile = false) {

app/assets/javascripts/shell.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ $(document).on('turbo-migration:load', function () {
130130
text: I18n.t('execution_environments.shell.file_tree.permission_denied')
131131
});
132132
} else {
133+
$(window).off('beforeunload');
133134
window.location = downloadPath;
135+
$(window).one('beforeunload', function () {
136+
CodeOceanEditor.removeFileTreeEventHandlers(fileTree);
137+
})
134138
}
135139
}.bind(this));
136140
CodeOceanEditor.installFileTreeEventHandlers(fileTree);

0 commit comments

Comments
 (0)