Skip to content

Commit a50b812

Browse files
committed
Set timeout for new beforeunload
Some browsers, such as Safari, require some timeout. Otherwise, removing and readding the handler doesn't have effect. We should still refactor this behavior in the future. Amends 4a14d43
1 parent 4a14d43 commit a50b812

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

app/assets/javascripts/editor/editor.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,11 @@ var CodeOceanEditor = {
856856
selected.instance.deselect_all();
857857
const downloadPath = selected.node.original.download_path;
858858
if (downloadPath) {
859-
$(window).off("beforeunload");
859+
$(window).off('beforeunload');
860860
window.location = downloadPath;
861-
$(window).one("beforeunload", this.unloadEverything.bind(this, App.synchronized_editor));
861+
setTimeout(() => {
862+
$(window).one('beforeunload', this.unloadEverything.bind(this, App.synchronized_editor));
863+
}, 250);
862864
}
863865
}.bind(this));
864866
$('#download-files').removeClass('d-none');

app/assets/javascripts/editor/submissions.js

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

105-
$(window).off("beforeunload");
105+
$(window).off('beforeunload');
106106
// to download just a single file, use the following url
107107
// window.location = Routes.download_file_submission_url(submission.id, CodeOceanEditor.active_file.filename);
108108
window.location = Routes.download_submission_url(submission.id);
109-
$(window).one("beforeunload", this.unloadEverything.bind(this, App.synchronized_editor));
109+
setTimeout(() => {
110+
$(window).one("beforeunload", this.unloadEverything.bind(this, App.synchronized_editor));
111+
}, 250);
110112
},
111113

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

app/assets/javascripts/shell.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,11 @@ $(document).on('turbo-migration:load', function () {
132132
} else {
133133
$(window).off('beforeunload');
134134
window.location = downloadPath;
135-
$(window).one('beforeunload', function () {
136-
CodeOceanEditor.removeFileTreeEventHandlers(fileTree);
137-
})
135+
setTimeout(() => {
136+
$(window).one('beforeunload', function () {
137+
CodeOceanEditor.removeFileTreeEventHandlers(fileTree);
138+
})
139+
}, 250);
138140
}
139141
}.bind(this));
140142
CodeOceanEditor.installFileTreeEventHandlers(fileTree);

0 commit comments

Comments
 (0)