Skip to content

Commit f09dffd

Browse files
committed
Revert changes to autosave.js
1 parent e46ed74 commit f09dffd

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

exercise/static/exercise/autosave.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
/**
22
* Autosave functionality for exercises.
33
*/
4-
(function(window) {
4+
(function($, window) {
55
"use strict";
66

77
const pluginName = "aplusAutoSave";
8-
const defaults = {
8+
var defaults = {
99
autoSaveIndicatorClasses: "small",
1010
autoSaveInterval: 10000, // ms
1111
};
1212

1313
function AplusAutoSave(element, options) {
1414
this.dom_element = element;
15-
this.settings = Object.assign({}, defaults, options);
15+
this.element = $(element);
16+
this.settings = $.extend({}, defaults, options);
1617
this.autoSaveTimeoutHandle = null;
1718
this.autoSaveRequest = null;
1819
this.autoSaveUrl = null;
1920
this.autoSaveIndicator = null;
2021
this.init();
2122
}
2223

23-
AplusAutoSave.prototype = {
24+
$.extend(AplusAutoSave.prototype, {
2425
/**
2526
* Initializes the autosave functionality for this form.
2627
*/
2728
init: function() {
2829
const self = this;
2930

30-
const submitUrl = new URL(self.dom_element.getAttribute("action"), window.location);
31+
const submitUrl = new URL(self.element.attr("action"), window.location);
3132
self.autoSaveUrl = new URL("draft/", submitUrl).href;
3233

33-
self.autoSaveIndicator = document.createElement("div");
34-
self.autoSaveIndicator.className = self.settings.autoSaveIndicatorClasses;
35-
self.dom_element.appendChild(self.autoSaveIndicator);
34+
self.autoSaveIndicator = $("<div></div>")
35+
.addClass(self.settings.autoSaveIndicatorClasses)
36+
.appendTo(self.element);
3637

37-
const timestamp = self.dom_element.dataset.draftTimestamp;
38+
const timestamp = self.element.data("draft-timestamp");
3839
if (timestamp) {
3940
self.setIndicatorSaveDate(new Date(timestamp));
4041
}
4142

42-
self.dom_element.addEventListener("input", function() {
43+
self.element.on("input", function() {
4344
self.scheduleAutoSave();
4445
});
45-
self.dom_element.addEventListener("submit", function() {
46+
self.element.on("submit", function() {
4647
// Cancel the autosave (if it was scheduled) when an actual submission
4748
// is made.
4849
clearTimeout(self.autoSaveTimeoutHandle);
49-
self.autoSaveIndicator.textContent = "";
50+
self.autoSaveIndicator.text("");
5051
});
5152
},
5253

@@ -71,7 +72,7 @@
7172
// If a save HTTP request is currently active, wait until it's done
7273
// before scheduling the next save.
7374
if (self.autoSaveRequest) {
74-
self.autoSaveRequest.finally(scheduleAutoSaveInternal);
75+
self.autoSaveRequest.always(scheduleAutoSaveInternal);
7576
} else {
7677
scheduleAutoSaveInternal();
7778
}
@@ -83,19 +84,19 @@
8384
*/
8485
doAutoSave: function() {
8586
const self = this;
86-
self.autoSaveIndicator.textContent = "Saving draft...";
87-
88-
self.autoSaveRequest = fetch(self.autoSaveUrl, {
89-
method: "POST",
90-
body: new FormData(self.dom_element),
91-
}).then(function(response) {
92-
if (!response.ok) {
93-
throw new Error("Failed to save draft");
94-
}
87+
self.autoSaveIndicator.text(_("Saving draft..."));
88+
89+
self.autoSaveRequest = $.ajax({
90+
url: self.autoSaveUrl,
91+
type: "POST",
92+
data: new FormData(self.dom_element),
93+
contentType: false,
94+
processData: false,
95+
}).fail(function() {
96+
self.autoSaveIndicator.text(_("Failed to save draft"));
97+
}).done(function() {
9598
self.setIndicatorSaveDate(new Date());
96-
}).catch(function() {
97-
self.autoSaveIndicator.textContent = "Failed to save draft";
98-
}).finally(function() {
99+
}).always(function() {
99100
// Allow new autosave requests.
100101
self.autoSaveRequest = null;
101102
});
@@ -116,13 +117,15 @@
116117
language = "en-GB";
117118
}
118119
const dateString = date.toLocaleString(language);
119-
this.autoSaveIndicator.textContent = "Draft saved " + dateString;
120+
this.autoSaveIndicator.text(_("Draft saved") + " " + dateString);
120121
},
121-
};
122+
});
122123

123-
window[pluginName] = function(element, options) {
124-
if (!element[pluginName]) {
125-
element[pluginName] = new AplusAutoSave(element, options);
126-
}
124+
$.fn[pluginName] = function(options) {
125+
return this.each(function() {
126+
if (!$.data(this, "plugin_" + pluginName)) {
127+
$.data(this, "plugin_" + pluginName, new AplusAutoSave(this, options));
128+
}
129+
});
127130
};
128-
})(window);
131+
})(jQuery, window);

0 commit comments

Comments
 (0)