Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit fc4f90f

Browse files
committed
Load and save were not hooked up.
Add warning when going to save an empty program bnmnetp/runestone/#611
1 parent 23c5d7c commit fc4f90f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

runestone/activecode/js/activecode.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ ActiveCode.prototype.createControls = function () {
120120
$(butt).text("Save");
121121
$(butt).css("margin-left", "10px");
122122
this.saveButton = butt;
123+
this.saveButton.onclick = this.saveEditor.bind(this);
123124
ctrlDiv.appendChild(butt);
124125
if (this.hidecode) {
125126
$(butt).css("display", "none")
@@ -132,6 +133,7 @@ ActiveCode.prototype.createControls = function () {
132133
$(butt).text("Load");
133134
$(butt).css("margin-left", "10px");
134135
this.loadButton = butt;
136+
this.loadButton.onclick = this.loadEditor.bind(this);
135137
ctrlDiv.appendChild(butt);
136138
if (this.hidecode) {
137139
$(butt).css("display", "none")
@@ -253,7 +255,7 @@ ActiveCode.prototype.addCaption = function() {
253255
};
254256

255257
ActiveCode.prototype.saveEditor = function () {
256-
258+
var res;
257259
var saveSuccess = function(data, status, whatever) {
258260
if (data.redirect) {
259261
alert("Did not save! It appears you are not logged in properly")
@@ -266,7 +268,7 @@ ActiveCode.prototype.saveEditor = function () {
266268
alert(acid);
267269
} else {
268270
// use a tooltip to provide some success feedback
269-
var save_btn = $("#" + acid + "_saveb");
271+
var save_btn = $(this.saveButton);
270272
save_btn.attr('title', 'Saved your code.');
271273
opts = {
272274
'trigger': 'manual',
@@ -283,19 +285,25 @@ ActiveCode.prototype.saveEditor = function () {
283285
$('#' + acid + ' .CodeMirror').css('border-bottom', '2px solid #aaa');
284286
}
285287
}
286-
};
288+
}.bind(this);
287289

288290
var data = {acid: this.divid, code: this.editor.getValue()};
289291
data.lang = this.language;
292+
if (data.code.match(/^\s$/m)) {
293+
res = confirm("You are about to save an empty program, this will overwrite a previously saved program. Continue?");
294+
if (! res) {
295+
return;
296+
}
297+
}
290298
$(document).ajaxError(function (e, jqhxr, settings, exception) {
291299
alert("Request Failed for" + settings.url)
292300
});
293301
jQuery.post(eBookConfig.ajaxURL + 'saveprog', data, saveSuccess);
294302
if (this.editor.acEditEvent) {
295-
logBookEvent({'event': 'activecode', 'act': 'edit', 'div_id': this.divid}); // Log the run event
303+
this.logBookEvent({'event': 'activecode', 'act': 'edit', 'div_id': this.divid}); // Log the run event
296304
this.editor.acEditEvent = false;
297305
}
298-
logBookEvent({'event': 'activecode', 'act': 'save', 'div_id': this.divid}); // Log the run event
306+
this.logBookEvent({'event': 'activecode', 'act': 'save', 'div_id': this.divid}); // Log the run event
299307

300308
};
301309

@@ -307,27 +315,27 @@ ActiveCode.prototype.loadEditor = function () {
307315

308316
if (res.source) {
309317
this.editor.setValue(res.source);
310-
this.loadButton.tooltip({'placement': 'bottom',
318+
$(this.loadButton).tooltip({'placement': 'bottom',
311319
'title': "Loaded your saved code.",
312320
'trigger': 'manual'
313321
});
314322
} else {
315-
this.loadButton.tooltip({'placement': 'bottom',
323+
$(this.loadButton).tooltip({'placement': 'bottom',
316324
'title': "No saved code.",
317325
'trigger': 'manual'
318326
});
319327
}
320-
this.loadButton.tooltip('show');
328+
$(this.loadButton).tooltip('show');
321329
setTimeout(function () {
322-
this.loadButton.tooltip('destroy')
323-
}, 4000);
330+
$(this.loadButton).tooltip('destroy')
331+
}.bind(this), 4000);
324332
}).bind(this);
325333

326334
var data = {acid: this.divid};
327-
if (sid !== undefined) {
335+
if (this.sid !== undefined) {
328336
data['sid'] = sid;
329337
}
330-
logBookEvent({'event': 'activecode', 'act': 'load', 'div_id': this.divid}); // Log the run event
338+
this.logBookEvent({'event': 'activecode', 'act': 'load', 'div_id': this.divid}); // Log the run event
331339
jQuery.get(eBookConfig.ajaxURL + 'getprog', data, loadEditor);
332340

333341
};

0 commit comments

Comments
 (0)