Edit style: warn before losing changes in code areas

This commit is contained in:
9adefaf01e5bf6426d838cd20eae582d2b6ba647 2015-03-03 03:58:04 +03:00
parent b4173d68f6
commit 0fe98e374c

39
edit.js
View File

@ -25,6 +25,8 @@ function setupCodeMirror(textarea) {
lint: CodeMirror.lint.css, lint: CodeMirror.lint.css,
smartIndent: (typeof localStorage["smart-indent"] == "undefined") || localStorage["smart-indent"] == "true" smartIndent: (typeof localStorage["smart-indent"] == "undefined") || localStorage["smart-indent"] == "true"
}); });
cm.lastChange = cm.changeGeneration();
cm.on("change", indicateCodeChange);
editors.push(cm); editors.push(cm);
} }
@ -33,9 +35,29 @@ function makeDirty() {
} }
window.onbeforeunload = function() { window.onbeforeunload = function() {
return dirty ? t('styleChangesNotSaved') : null; return dirty || isCodeDirty() ? t('styleChangesNotSaved') : null;
} }
function isCodeDirty() {
for(var i=0; i < editors.length; i++) {
if (!editors[i].isClean(editors[i].lastChange)) {
return true;
}
}
return false;
}
function indicateCodeChange(cm) {
var clean = cm.isClean(cm.lastChange);
if (clean != cm.lastClean) {
cm.lastClean = clean;
var label = cm.getTextArea().previousElementSibling;
if (label) {
label.textContent = label.textContent.replace(/\*?$/, clean ? '' : '*');
}
}
};
function addAppliesTo(list, name, value) { function addAppliesTo(list, name, value) {
var showingEverything = list.querySelector(".applies-to-everything") != null; var showingEverything = list.querySelector(".applies-to-everything") != null;
// blow away "Everything" if it's there // blow away "Everything" if it's there
@ -203,10 +225,10 @@ function validate() {
} }
function save() { function save() {
// save the contents of the CodeMirror editors back into the textareas // save the contents of the CodeMirror editors back into the textareas
for(var i=0; i < editors.length; i++) { for (var i=0; i < editors.length; i++) {
editors[i].save(); editors[i].save();
} }
var error = validate(); var error = validate();
if (error) { if (error) {
@ -269,6 +291,13 @@ function getMeta(e) {
function saveComplete(style) { function saveComplete(style) {
dirty = false; dirty = false;
for(var i=0; i < editors.length; i++) {
var cm = editors[i];
cm.lastChange = cm.changeGeneration(true);
indicateCodeChange(cm);
}
// Go from new style URL to edit style URL // Go from new style URL to edit style URL
if (location.href.indexOf("id=") == -1) { if (location.href.indexOf("id=") == -1) {
// give the code above a moment before we kill the page // give the code above a moment before we kill the page