Toggle save button depending on editor changes and return to editor instead of history pane after saving

This commit is contained in:
Andrew Manning 2016-11-23 06:22:50 -05:00
parent f76046b612
commit f14ef10e48

View file

@ -71,7 +71,7 @@
<label for='id_{{$commitMsg.0}}' id='label_{{$commitMsg.0}}'>{{$commitMsg.1}}{{if $commitMsg.4}}<span class="required"> {{$commitMsg.4}}</span>{{/if}}</label> <label for='id_{{$commitMsg.0}}' id='label_{{$commitMsg.0}}'>{{$commitMsg.1}}{{if $commitMsg.4}}<span class="required"> {{$commitMsg.4}}</span>{{/if}}</label>
<span> <span>
<input class="" style="width: 80%;" name='{{$commitMsg.0}}' id='id_{{$commitMsg.0}}' type="text" value="{{$commitMsg.2}}"{{if $commitMsg.5}} {{$commitMsg.5}}{{/if}}> <input class="" style="width: 80%;" name='{{$commitMsg.0}}' id='id_{{$commitMsg.0}}' type="text" value="{{$commitMsg.2}}"{{if $commitMsg.5}} {{$commitMsg.5}}{{/if}}>
<a id="save-page" href="#" class="btn btn-primary btn-md">Save</a> <a id="save-page" href="#" class="btn btn-primary btn-md disabled">Save</a>
</span> </span>
<span id='help_{{$commitMsg.0}}' class='help-block'>{{$commitMsg.3}}</span> <span id='help_{{$commitMsg.0}}' class='help-block'>{{$commitMsg.3}}</span>
@ -231,35 +231,38 @@
} }
$('#save-page').click(function (ev) { $('#save-page').click(function (ev) {
if (window.wiki_resource_id === '' || window.wiki_page_name === '') { if (window.wiki_resource_id === '' || window.wiki_page_name === '') {
window.console.log('You must have a wiki page open in order to edit pages.'); window.console.log('You must have a wiki page open in order to edit pages.');
ev.preventDefault(); ev.preventDefault();
return false; return false;
} }
var currentContent = editor.getValue(); var currentContent = editor.getValue();
if (window.wiki_page_content === currentContent) { if (window.wiki_page_content === currentContent) {
window.console.log('No edits to save.'); window.console.log('No edits to save.');
ev.preventDefault(); ev.preventDefault();
return false; return false;
} }
$.post("wiki/{{$channel}}/save/page", $.post("wiki/{{$channel}}/save/page",
{ content: currentContent, { content: currentContent,
commitMsg: $('#id_commitMsg').val(), commitMsg: $('#id_commitMsg').val(),
name: window.wiki_page_name, name: window.wiki_page_name,
resource_id: window.wiki_resource_id resource_id: window.wiki_resource_id
}, },
function (data) { function (data) {
if (data.success) { if (data.success) {
window.console.log('Page saved successfully.'); window.console.log('Page saved successfully.');
window.wiki_page_content = currentContent; window.wiki_page_content = currentContent;
$('#id_commitMsg').val(''); // Clear the commit message box $('#id_commitMsg').val(''); // Clear the commit message box
$('#wiki-get-history').click(); $('#save-page').addClass('disabled'); // Disable the save button
} else { window.editor.getSession().getUndoManager().markClean(); // Reset the undo history for the editor
alert('Error saving page.'); // TODO: Replace alerts with auto-timeout popups window.editor.focus(); // Return focus to the editor for continued editing
window.console.log('Error saving page.'); // $('#wiki-get-history').click();
} } else {
}, 'json'); alert('Error saving page.'); // TODO: Replace alerts with auto-timeout popups
ev.preventDefault(); window.console.log('Error saving page.');
}
}, 'json');
ev.preventDefault();
}); });
$('#delete-page').click(function (ev) { $('#delete-page').click(function (ev) {
@ -460,6 +463,14 @@
{{else}} {{else}}
$('#new-wiki-button').hide(); $('#new-wiki-button').hide();
{{/if}} {{/if}}
// using input event instead of change since it's called with some timeout
window.editor.on("input", function() {
if(window.editor.getSession().getUndoManager().isClean()) {
$('#save-page').addClass('disabled');
} else {
$('#save-page').removeClass('disabled');
}
});
}); });
</script> </script>