Skip to content

Commit

Permalink
Item14288: fixed submit process
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Mar 24, 2017
1 parent afc509a commit 8586ff9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 111 deletions.
Expand Up @@ -157,14 +157,20 @@ CodemirrorEngine.prototype.init = function() {
}
});

// listen to beforeSubmit event
self.shell.form.on("beforeSubmit.natedit", function() {
self.cm.save(); // copy to textarea
});

return dfd.promise();
};

/*************************************************************************
* intercept save process
*/
CodemirrorEngine.prototype.beforeSubmit = function(action) {
var self = this;

self.cm.save(); // copy to textarea

return $.Deferred().resolve().promise();
};

/*************************************************************************
* register events to editor engine
*/
Expand Down
Expand Up @@ -29,12 +29,6 @@ function TinyMCEEngine(shell, opts) {

self.shell = shell;
self.opts = $.extend({}, TinyMCEEngine.defaults, self.shell.opts.tinymce, opts);

// prefix them with pubUrlPath
$.each(self.opts.tinymce.content_css, function(i, val) {
self.opts.tinymce.content_css[i] = pubUrlPath + val;
});

self.opts.tinymce.selector = "#"+self.shell.id+" textarea";
self.opts.natedit.signatureMarkup = ['-- ', '<a href="'+wikiUserNameUrl+'">'+foswiki.getPreference("WIKINAME")+'</a>', ' - '+foswiki.getPreference("SERVERTIME")];

Expand Down Expand Up @@ -78,15 +72,6 @@ TinyMCEEngine.prototype.init = function() {
});

self.shell.form.on("beforeSubmit.natedit", function(e, params) {
if (params.action !== 'cancel') {
self.html2tml(self.editor.getContent())
.done(function(data) {
$(self.shell.txtarea).val(data);
})
.fail(function() {
alert("Error calling html2tml"); // SMELL: better error handling
});
}
});

};
Expand All @@ -101,39 +86,46 @@ TinyMCEEngine.prototype.init = function() {
return dfd.promise();
};

/*************************************************************************
* intercept save process
*/
TinyMCEEngine.prototype.beforeSubmit = function(action) {
var self = this, dfd;

if (action === 'cancel') {
return;
}

return self.html2tml(self.editor.getContent())
.done(function(data) {
$(self.shell.txtarea).val(data);
})
.fail(function() {
alert("Error calling html2tml"); // SMELL: better error handling
});
};


/*************************************************************************
* init gui
*/
TinyMCEEngine.prototype.initGui = function() {
var self = this,
formatNames = [];
var self = this;

// flag to container ... smell: is this really needed?
self.shell.container.addClass("ui-natedit-wysiwyg-enabled");

$.each(self.opts.tinymce.formats, function(key) {
formatNames.push(key);
});

// highlight buttons on toolbar when the cursor moves into a format
self.editor.on("NodeChange", function(/*ev*/) {
var /*elem = ev.element,*/
formatter = self.editor.formatter,
activeFormats;

self.shell.toolbar.find(".ui-button, .ui-menu-item").removeClass("ui-natedit-active");

activeFormats = formatter.matchAll(formatNames);

$.each(activeFormats, function(index, name) {
$.each(formatter.get(name), function(index, format) {
var selector = format.toolbar;
if (selector) {
self.shell.toolbar.find(selector).addClass("ui-natedit-active");
$.each(self.opts.tinymce.formats, function(formatName) {
self.editor.formatter.formatChanged(formatName, function(state, args) {
$.each(self.editor.formatter.get(formatName), function(i, format) {
if (state) {
self.shell.toolbar.find(format.toolbar).addClass("ui-natedit-active");
} else {
self.shell.toolbar.find(format.toolbar).removeClass("ui-natedit-active");
}
});
});

});

// listen to change events and update stuff
Expand Down Expand Up @@ -431,15 +423,17 @@ TinyMCEEngine.defaults = {
statusbar: false,
plugins: 'contextmenu table searchreplace paste lists link anchor hr legacyoutput image', // save autosave fullscreen anchor charmap code textcolor colorpicker
paste_data_images: true,
content_css: ["/System/TinyMCEPlugin/wysiwyg.css", "/System/SkinTemplates/base.css"], // todo custom onces: ,/pub/System/NatSkin/BaseStyle.css,/pub/System/CustomatoTheme/customato.css"
content_css: ["/pub/System/TinyMCEPlugin/wysiwyg.css", "/pub/System/SkinTemplates/base.css"],
formats: {
h1Markup: { block: "h1", toolbar: ".ui-natedit-h1" },
h2Markup: { block: "h2", toolbar: ".ui-natedit-h2" },
h3Markup: { block: "h3", toolbar: ".ui-natedit-h3" },
h4Markup: { block: "h4", toolbar: ".ui-natedit-h4" },
h5Markup: { block: "h5", toolbar: ".ui-natedit-h5" },
h6Markup: { block: "h6", toolbar: ".ui-natedit-h6" },
boldMarkup: { inline: "strong", toolbar: ".ui-natedit-bold" },
normalMarkup: { block: "p", toolbar: ".ui-natedit-normal"},
quoteMarkup: { block: "blockquote", toolbar: ".ui-natedit-quoted"},
boldMarkup: { inline: "b", toolbar: ".ui-natedit-bold" },
italicMarkup: { inline: "em", toolbar: ".ui-natedit-italic" },
monoMarkup: { inline: "code", toolbar: ".ui-natedit-mono" },
underlineMarkup: { inline: "span", styles: { "text-decoration": "underline" }, toolbar: ".ui-natedit-underline" },
Expand All @@ -450,7 +444,7 @@ TinyMCEEngine.defaults = {
rightMarkup: { block: "p", styles: { "text-align": "right" }, toolbar: ".ui-natedit-right" },
centerMarkup: { block: "p", styles: { "text-align": "center" }, toolbar: ".ui-natedit-center" },
justifyMarkup: { block: "p", styles: { "text-align": "justify" }, toolbar: ".ui-natedit-justify" },
verbatimMarkup: { block: "pre", toolbar: ".ui-natedit-h1" }
verbatimMarkup: { block: "pre", classes: "TMLverbatim", toolbar: ".ui-natedit-verbatim" }
}
}
};
Expand Down
141 changes: 74 additions & 67 deletions NatEditPlugin/pub/System/NatEditPlugin/jquery.natedit.uncompressed.js
Expand Up @@ -574,10 +574,13 @@ $.NatEditor.prototype.beforeSubmit = function(editAction) {
StrikeOne.submit(self.form[0]);
}

// WARNING: handlers are not guaranteed to be called or have finished before the content has been submitted
self.form.trigger("beforeSubmit.natedit", {
editor: self,
action: editAction
});

return self.engine.beforeSubmit(editAction);
};

/*************************************************************************
Expand All @@ -604,12 +607,13 @@ $.NatEditor.prototype.initForm = function() {
buttons,
doIt = function() {
if (self.form.validate().form()) {
self.beforeSubmit("save");
document.title = $.i18n("Saving ...");
$.blockUI({
message: '<h1> '+ $.i18n("Saving ...") + '</h1>'
self.beforeSubmit("save").then(function() {
document.title = $.i18n("Saving ...");
$.blockUI({
message: '<h1> '+ $.i18n("Saving ...") + '</h1>'
});
self.form.submit();
});
self.form.submit();
}
};

Expand Down Expand Up @@ -638,38 +642,38 @@ $.NatEditor.prototype.initForm = function() {
var editAction = $(ev.currentTarget).attr("href").replace(/^#/, "");

if (self.form.validate().form()) {
self.beforeSubmit(editAction);

if (topicName.match(/AUTOINC|XXXXXXXXXX/)) {
// don't ajax when we don't know the resultant URL (can change this if the server tells it to us..)
self.form.submit();
} else {
self.form.ajaxSubmit({
url: self.opts.scriptUrl + '/rest/NatEditPlugin/save', // SMELL: use this one for REST as long as the normal save can't cope with REST
beforeSubmit: function() {
self.hideMessages();
document.title = $.i18n("Saving ...");
$.blockUI({
message: '<h1>'+ $.i18n("Saving ...") + '</h1>'
});
},
error: function(xhr, textStatus) {
var message = self.extractErrorMessage(xhr.responseText || textStatus);
self.showMessage("error", message);
},
complete: function(xhr) {
var nonce = xhr.getResponseHeader('X-Foswiki-Validation');
if (nonce) {
// patch in new nonce
$("input[name='validation_key']").each(function() {
$(this).val("?" + nonce);
self.beforeSubmit(editAction).then(function() {
if (topicName.match(/AUTOINC|XXXXXXXXXX/)) {
// don't ajax when we don't know the resultant URL (can change this if the server tells it to us..)
self.form.submit();
} else {
self.form.ajaxSubmit({
url: self.opts.scriptUrl + '/rest/NatEditPlugin/save', // SMELL: use this one for REST as long as the normal save can't cope with REST
beforeSubmit: function() {
self.hideMessages();
document.title = $.i18n("Saving ...");
$.blockUI({
message: '<h1>'+ $.i18n("Saving ...") + '</h1>'
});
},
error: function(xhr, textStatus) {
var message = self.extractErrorMessage(xhr.responseText || textStatus);
self.showMessage("error", message);
},
complete: function(xhr) {
var nonce = xhr.getResponseHeader('X-Foswiki-Validation');
if (nonce) {
// patch in new nonce
$("input[name='validation_key']").each(function() {
$(this).val("?" + nonce);
});
}
document.title = origTitle;
$.unblockUI();
}
document.title = origTitle;
$.unblockUI();
}
});
}
});
}
});
}
};

Expand All @@ -693,35 +697,35 @@ $.NatEditor.prototype.initForm = function() {
self.form.find(".ui-natedit-preview").on("click", function() {

if (self.form.validate().form()) {
self.beforeSubmit("preview");

self.form.ajaxSubmit({
url: self.opts.scriptUrl + '/rest/NatEditPlugin/save', // SMELL: use this one for REST as long as the normal save can't cope with REST
beforeSubmit: function() {
self.hideMessages();
$.blockUI({
message: '<h1>'+$.i18n("Loading preview ...")+'</h1>'
});
},
error: function(xhr, textStatus) {
var message = self.extractErrorMessage(xhr.responseText || textStatus);
$.unblockUI();
self.showMessage("error", message);
},
success: function(data) {
var $window = $(window),
height = Math.round(parseInt($window.height() * 0.6, 10)),
width = Math.round(parseInt($window.width() * 0.6, 10));
self.beforeSubmit("preview").then(function() {
self.form.ajaxSubmit({
url: self.opts.scriptUrl + '/rest/NatEditPlugin/save', // SMELL: use this one for REST as long as the normal save can't cope with REST
beforeSubmit: function() {
self.hideMessages();
$.blockUI({
message: '<h1>'+$.i18n("Loading preview ...")+'</h1>'
});
},
error: function(xhr, textStatus) {
var message = self.extractErrorMessage(xhr.responseText || textStatus);
$.unblockUI();
self.showMessage("error", message);
},
success: function(data) {
var $window = $(window),
height = Math.round(parseInt($window.height() * 0.6, 10)),
width = Math.round(parseInt($window.width() * 0.6, 10));

$.unblockUI();
$.unblockUI();

if (width < 640) {
width = 640;
}
if (width < 640) {
width = 640;
}

data = data.replace(/%width%/g, width).replace(/%height%/g, height);
$("body").append(data);
}
data = data.replace(/%width%/g, width).replace(/%height%/g, height);
$("body").append(data);
}
});
});
}
return false;
Expand All @@ -734,20 +738,23 @@ $.NatEditor.prototype.initForm = function() {
$("label.error").hide();
$("input.error").removeClass("error");
$(".jqTabGroup a.error").removeClass("error");
self.beforeSubmit("cancel");
self.form.submit();
self.beforeSubmit("cancel").then(function() {
self.form.submit();
});
return false;
});

self.form.find(".ui-natedit-replaceform").on("click", function() {
self.beforeSubmit("replaceform");
self.form.submit();
self.beforeSubmit("replaceform").then(function() {
self.form.submit();
});
return false;
});

self.form.find(".ui-natedit-addform").on("click", function() {
self.beforeSubmit("addform");
self.form.submit();
self.beforeSubmit("addform").then(function() {
self.form.submit();
});
return false;
});

Expand Down
1 change: 1 addition & 0 deletions NatEditPlugin/templates/edittoolbar.natedit.tmpl
Expand Up @@ -25,6 +25,7 @@
%{ ################################################################################ }%
%TMPL:DEF{"edittoolbar::paragraphmenu"}%<!-- -->
<ul class="ui-natedit-menu ui-natedit-paragraph-menu">
<li class="ui-natedit-normal"><a href="#" data-markup="normalMarkup" data-type="line">%MAKETEXT{"Normal"}%</a></li>
<li class="ui-natedit-h1"><a href="#" data-markup="h1Markup" data-type="line">%MAKETEXT{"Heading 1"}%</a></li>
<li class="ui-natedit-h2"><a href="#" data-markup="h2Markup" data-type="line">%MAKETEXT{"Heading 2"}%</a></li>
<li class="ui-natedit-h3"><a href="#" data-markup="h3Markup" data-type="line">%MAKETEXT{"Heading 3"}%</a></li>
Expand Down

0 comments on commit 8586ff9

Please sign in to comment.