2010-07-13 00:46:17 +00:00
|
|
|
// Save -> Command S (need a form)
|
|
|
|
jQuery.fn.saveWithShortcut = function() {
|
|
|
|
|
|
|
|
var resetFormErrors = function(form) {
|
|
|
|
jQuery('div.form-errors').remove();
|
|
|
|
jQuery('div.formError').remove();
|
|
|
|
jQuery('p.inline-errors').remove();
|
|
|
|
form.find('li.error').removeClass('error');
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateFromCodeMirror = function() {
|
2010-07-13 20:01:40 +00:00
|
|
|
if (typeof CodeMirror == undefined)
|
2010-07-13 00:46:17 +00:00
|
|
|
return;
|
|
|
|
jQuery.each(CodeMirrorEditors, function() {
|
|
|
|
this.el.val(this.editor.getCode());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var save = function(form) {
|
|
|
|
$.post(form.attr('action'), form.serializeArray(), function(data) {
|
|
|
|
onSaveCallback(form, data)
|
|
|
|
}, 'json');
|
|
|
|
};
|
|
|
|
|
|
|
|
var onSaveCallback = function(form, data) {
|
|
|
|
resetFormErrors(form);
|
|
|
|
|
|
|
|
if (data.alert != undefined) {
|
|
|
|
$.growl('error', data.alert);
|
2010-07-13 20:01:40 +00:00
|
|
|
for (var field in data.errors) {
|
|
|
|
var error = data.errors[field];
|
|
|
|
var node = form.find('li:has(#' + data.model + '_' + field + ')');
|
2010-07-13 00:46:17 +00:00
|
|
|
node.addClass('error');
|
2010-07-13 20:01:40 +00:00
|
|
|
node.append("<p class='inline-errors'>" + error + "</p>");
|
2010-07-13 00:46:17 +00:00
|
|
|
}
|
|
|
|
form.find('li.error input').eq(0).focus();
|
|
|
|
} else {
|
|
|
|
$.growl('success', data.notice);
|
2010-07-15 20:48:02 +00:00
|
|
|
$.publish('form.saved.success', [data]);
|
2010-07-13 00:46:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.each(function() {
|
|
|
|
var form = jQuery(this);
|
|
|
|
|
|
|
|
jQuery(document).bind('keypress.shortcut', function(event) {
|
|
|
|
if (!(event.which == 115 && (event.ctrlKey || event.metaKey))) return true;
|
|
|
|
updateFromCodeMirror();
|
|
|
|
save(form);
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|