Commit bd9625c6 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_web_renderjs_ui] Dialog: move code outside rjs method

parent 77a88301
...@@ -10,6 +10,215 @@ ...@@ -10,6 +10,215 @@
return [obj]; return [obj];
} }
function submitDialog(gadget) {
var form_gadget = gadget,
action = form_gadget.state.erp5_document._embedded._view._actions.put,
form_id = form_gadget.state.erp5_document._embedded._view.form_id,
redirect_to_parent;
return form_gadget.notifySubmitting()
.push(function () {
return form_gadget.getDeclaredGadget("erp5_form");
})
.push(function (erp5_form) {
return erp5_form.getContent();
})
.push(function (content_dict) {
var data = {},
key;
data[form_id.key] = form_id['default'];
// XXX Hardcoded
data.dialog_id = form_id['default'];
data.dialog_method = action.action;
//XXX hack for redirect, difined in form
redirect_to_parent = content_dict.field_your_redirect_to_parent;
for (key in content_dict) {
if (content_dict.hasOwnProperty(key)) {
data[key] = content_dict[key];
}
}
return form_gadget.jio_putAttachment(
form_gadget.state.jio_key,
action.href,
data
);
})
.push(function (attachment) {
if (attachment.target.response.type === "application/json") {
// successful form save returns simple redirect and answer as JSON
// validation errors are handled in failure branch on bottom
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(attachment.target.response);
})
.push(function (response_text) {
var response = JSON.parse(response_text.target.result);
return form_gadget.notifySubmitted({
"message": response.portal_status_message,
"status": "success"
});
})
.push(function () {
// here we figure out where to go after form submit - indicated
// by X-Location HTTP header placed by Base_redirect script
var jio_key = new URI(
attachment.target.getResponseHeader("X-Location")
).segment(2),
splitted_jio_key_list,
splitted_current_jio_key_list,
command,
i;
if (redirect_to_parent) {
return form_gadget.redirect({command: 'history_previous'});
}
if (form_gadget.state.jio_key === jio_key) {
// don't update navigation history when not really redirecting
return form_gadget.redirect({
command: 'change',
options: {
"jio_key": jio_key,
"view": "view",
"page": undefined
// do not mingle with editable because it isn't necessary
}
});
}
// Check if the redirection goes to a same parent's subdocument.
// In this case, do not add current document to the history
// example: when cloning, do not keep the original document in history
splitted_jio_key_list = jio_key.split('/');
splitted_current_jio_key_list = form_gadget.state.jio_key.split('/');
command = 'display_with_history';
if (splitted_jio_key_list.length === splitted_current_jio_key_list.length) {
for (i = 0; i < splitted_jio_key_list.length - 1; i += 1) {
if (splitted_jio_key_list[i] !== splitted_current_jio_key_list[i]) {
command = 'push_history';
}
}
} else {
command = 'push_history';
}
// forced document change thus we update history
return form_gadget.redirect({
command: command,
options: {
"jio_key": jio_key
// do not mingle with editable because it isn't necessary
}
});
});
}
if (attachment.target.response.type === "application/hal+json") {
// we have received a view definition thus we need to redirect
// this will happen only in report/export when "Format" is unspecified
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Data received",
"status": "success"
});
})
.push(function () {
return jIO.util.readBlobAsText(attachment.target.response);
})
.push(function (response_text) {
return form_gadget.updateForm(JSON.parse(response_text.target.result));
});
}
// response status > 200 (e.g. 202 "Accepted" or 204 "No Content")
// mean sucessful execution of an action but does not carry any data
// XMLHttpRequest automatically inserts Content-Type="text/xml" thus
// we cannot test based on that
if (attachment.target.response.size === 0 &&
attachment.target.status > 200 &&
attachment.target.status < 400) {
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Action succeeded",
"status": "success"
});
})
.push(function () {
if (redirect_to_parent) {
return form_gadget.redirect({command: 'history_previous'});
}
return form_gadget.redirect({
command: 'change',
options: {
"jio_key": form_gadget.state.jio_key,
"view": "view",
"page": undefined
// do not mingle with editable because it isn't necessary
}
});
});
}
// any other attachment type we force to download because it is most
// likely product of export/report (thus PDF, ODT ...)
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Data received",
"status": "success"
});
})
.push(function () {
return form_gadget.forceDownload(attachment);
});
})
.push(undefined, function (error) {
if (error.target !== undefined) {
var error_text = 'Encountered an unknown error. Try to resubmit',
promise;
// if we know what the error was, try to precise it for the user
if (error.target.status === 400) {
error_text = 'Input data has errors';
} else if (error.target.status === 403) {
error_text = 'You do not have the permissions to edit the object';
} else if (error.target.status === 0) {
error_text = 'Document was not saved! Resubmit when you are online or the document accessible';
}
// display translated error_text to user
promise = form_gadget.notifySubmitted()
.push(function () {
return form_gadget.translate(error_text);
})
.push(function (message) {
return form_gadget.notifyChange({
"message": message + '.',
"status": "error"
});
});
// if server validation of form data failed (indicated by response code 400)
// we parse out field errors and display them to the user
if (error.target.status === 400) {
promise
.push(function () {
// when the server-side validation returns the error description
if (error.target.responseType === "blob") {
return jIO.util.readBlobAsText(error.target.response);
}
// otherwise return (most-likely) textual response of the server
return {target: {result: error.target.response}};
})
.push(function (event) {
return form_gadget.displayFormulatorValidationError(JSON.parse(event.target.result));
});
}
return promise;
}
throw error;
});
}
rJS(window) rJS(window)
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -174,226 +383,7 @@ ...@@ -174,226 +383,7 @@
}) })
.onEvent('submit', function () { .onEvent('submit', function () {
var form_gadget = this, return submitDialog(this);
action = this.state.erp5_document._embedded._view._actions.put,
form_id = this.state.erp5_document._embedded._view.form_id,
redirect_to_parent;
return form_gadget.notifySubmitting()
.push(function () {
return form_gadget.getDeclaredGadget("erp5_form");
})
.push(function (erp5_form) {
return erp5_form.getContent();
})
.push(function (content_dict) {
var data = {},
key;
data[form_id.key] = form_id['default'];
// XXX Hardcoded
data.dialog_id = form_id['default'];
data.dialog_method = action.action;
//XXX hack for redirect, difined in form
redirect_to_parent = content_dict.field_your_redirect_to_parent;
for (key in content_dict) {
if (content_dict.hasOwnProperty(key)) {
data[key] = content_dict[key];
}
}
return form_gadget.jio_putAttachment(
form_gadget.state.jio_key,
action.href,
data
);
})
.push(function (attachment) {
if (attachment.target.response.type === "application/json") {
// successful form save returns simple redirect and answer as JSON
// validation errors are handled in failure branch on bottom
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(attachment.target.response);
})
.push(function (response_text) {
var response = JSON.parse(response_text.target.result);
return form_gadget.notifySubmitted({
"message": response.portal_status_message,
"status": "success"
});
})
.push(function () {
// here we figure out where to go after form submit - indicated
// by X-Location HTTP header placed by Base_redirect script
var jio_key = new URI(
attachment.target.getResponseHeader("X-Location")
).segment(2),
splitted_jio_key_list,
splitted_current_jio_key_list,
command,
i;
if (redirect_to_parent) {
return form_gadget.redirect({command: 'history_previous'});
}
if (form_gadget.state.jio_key === jio_key) {
// don't update navigation history when not really redirecting
return form_gadget.redirect({
command: 'change',
options: {
"jio_key": jio_key,
"view": "view",
"page": undefined
// do not mingle with editable because it isn't necessary
}
});
}
// Check if the redirection goes to a same parent's subdocument.
// In this case, do not add current document to the history
// example: when cloning, do not keep the original document in history
splitted_jio_key_list = jio_key.split('/');
splitted_current_jio_key_list = form_gadget.state.jio_key.split('/');
command = 'display_with_history';
if (splitted_jio_key_list.length === splitted_current_jio_key_list.length) {
for (i = 0; i < splitted_jio_key_list.length - 1; i += 1) {
if (splitted_jio_key_list[i] !== splitted_current_jio_key_list[i]) {
command = 'push_history';
}
}
} else {
command = 'push_history';
}
// forced document change thus we update history
return form_gadget.redirect({
command: command,
options: {
"jio_key": jio_key
// do not mingle with editable because it isn't necessary
}
});
});
}
if (attachment.target.response.type === "application/hal+json") {
// we have received a view definition thus we need to redirect
// this will happen only in report/export when "Format" is unspecified
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Data received",
"status": "success"
});
})
.push(function () {
return jIO.util.readBlobAsText(attachment.target.response);
})
.push(function (response_text) {
return form_gadget.updateForm(JSON.parse(response_text.target.result));
});
}
// response status > 200 (e.g. 202 "Accepted" or 204 "No Content")
// mean sucessful execution of an action but does not carry any data
// XMLHttpRequest automatically inserts Content-Type="text/xml" thus
// we cannot test based on that
if (attachment.target.response.size === 0 &&
attachment.target.status > 200 &&
attachment.target.status < 400) {
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Action succeeded",
"status": "success"
});
})
.push(function () {
if (redirect_to_parent) {
return form_gadget.redirect({command: 'history_previous'});
}
return form_gadget.redirect({
command: 'change',
options: {
"jio_key": form_gadget.state.jio_key,
"view": "view",
"page": undefined
// do not mingle with editable because it isn't necessary
}
});
});
}
// any other attachment type we force to download because it is most
// likely product of export/report (thus PDF, ODT ...)
return new RSVP.Queue()
.push(function () {
return form_gadget.notifySubmitted({
"message": "Data received",
"status": "success"
});
})
.push(function () {
return form_gadget.forceDownload(attachment);
});
})
.push(undefined, function (error) {
if (error.target !== undefined) {
var error_text = 'Encountered an unknown error. Try to resubmit',
promise_queue = new RSVP.Queue();
// if we know what the error was, try to precise it for the user
if (error.target.status === 400) {
error_text = 'Input data has errors';
} else if (error.target.status === 403) {
error_text = 'You do not have the permissions to edit the object';
} else if (error.target.status === 0) {
error_text = 'Document was not saved! Resubmit when you are online or the document accessible';
} else if (error.target.status === 500 && error.target.response.type === "application/json") {
promise_queue
.push(function () {
return jIO.util.readBlobAsText(error.target.response);
})
.push(function (response_text) {
var response = JSON.parse(response_text.target.result);
error_text = response.portal_status_message;
});
}
// display translated error_text to user
promise_queue
.push(function () {
return form_gadget.notifySubmitted();
})
.push(function () {
return form_gadget.translate(error_text);
})
.push(function (message) {
return form_gadget.notifyChange({
"message": message + '.',
"status": "error"
});
});
// if server validation of form data failed (indicated by response code 400)
// we parse out field errors and display them to the user
if (error.target.status === 400) {
promise_queue
.push(function () {
// when the server-side validation returns the error description
if (error.target.responseType === "blob") {
return jIO.util.readBlobAsText(error.target.response);
}
// otherwise return (most-likely) textual response of the server
return {target: {result: error.target.response}};
})
.push(function (event) {
return form_gadget.displayFormulatorValidationError(JSON.parse(event.target.result));
});
}
return promise_queue;
}
throw error;
});
}, false, true); }, false, true);
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>964.3910.33744.24644</string> </value> <value> <string>965.670.53338.41130</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1516350278.01</float> <float>1516725149.52</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment