Commit bd9625c6 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_web_renderjs_ui] Dialog: move code outside rjs method

parent 77a88301
...@@ -10,173 +10,10 @@ ...@@ -10,173 +10,10 @@
return [obj]; return [obj];
} }
function submitDialog(gadget) {
rJS(window) var form_gadget = gadget,
///////////////////////////////////////////////////////////////// action = form_gadget.state.erp5_document._embedded._view._actions.put,
// acquisition form_id = form_gadget.state.erp5_document._embedded._view.form_id,
/////////////////////////////////////////////////////////////////
.declareAcquiredMethod("jio_putAttachment", "jio_putAttachment")
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("notifySubmitting", "notifySubmitting")
.declareAcquiredMethod("notifySubmitted", "notifySubmitted")
.declareAcquiredMethod("translate", "translate")
.declareAcquiredMethod("notifyChange", "notifyChange")
.declareAcquiredMethod("updateForm", "updateForm")
.declareAcquiredMethod("displayFormulatorValidationError", "displayFormulatorValidationError")
/////////////////////////////////////////////////////////////////
// Proxy methods to the child gadget
/////////////////////////////////////////////////////////////////
.declareMethod('checkValidity', function () {
return this.getDeclaredGadget("erp5_form")
.push(function (declared_gadget) {
return declared_gadget.checkValidity();
});
}, {mutex: 'changestate'})
.declareMethod('getContent', function () {
return this.getDeclaredGadget("erp5_form")
.push(function (declared_gadget) {
return declared_gadget.getContent();
});
}, {mutex: 'changestate'})
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod('triggerSubmit', function () {
this.element.querySelector('input[type="submit"]').click();
}, {mutex: 'changestate'})
.declareMethod('render', function (options) {
// copy out wanted items from options and pass it to `changeState`
return this.changeState({
jio_key: options.jio_key,
view: options.view,
// ignore options.editable because dialog is always editable
erp5_document: options.erp5_document,
form_definition: options.form_definition,
erp5_form: options.erp5_form || {}
// ignore global editable state (be always editable)
});
})
.onStateChange(function () {
var form_gadget = this,
icon,
selector = form_gadget.element.querySelector("h3"),
title,
i,
view_list = ensureArray(this.state.erp5_document._links.action_workflow);
title = this.state.form_definition.title;
// XXX hardcoded...
switch (title) {
case "Create User":
icon = " ui-icon-user";
break;
case "Create Document":
icon = " ui-icon-file-o";
break;
case "Validate Workflow Action":
icon = " ui-icon-share-alt";
break;
case "Submit":
icon = " ui-icon-check";
break;
default:
icon = " ui-icon-random";
break;
}
for (i = 0; i < view_list.length; i += 1) {
if (view_list[i].href === this.state.view) {
title = view_list[i].title;
}
}
// Calculate the h3 properties
return new RSVP.Queue()
.push(function () {
return RSVP.all([
form_gadget.translate(form_gadget.state.form_definition.title),
form_gadget.translate(title)
]);
})
.push(function (translated_title_list) {
form_gadget.element.querySelector('input.dialogconfirm').value = translated_title_list[1];
selector.textContent = "\u00A0" + translated_title_list[0];
selector.className = "ui-content-title ui-body-c ui-icon ui-icon-custom" + icon;
// Render the erp5_from
return form_gadget.getDeclaredGadget("erp5_form");
})
.push(function (erp5_form) {
var form_options = form_gadget.state.erp5_form;
// pass own form options to the embedded form
form_options.erp5_document = form_gadget.state.erp5_document;
form_options.form_definition = form_gadget.state.form_definition;
form_options.view = form_gadget.state.view;
form_options.jio_key = form_gadget.state.jio_key;
form_options.editable = true; // dialog is always editable
return erp5_form.render(form_options);
})
.push(function () {
// Render the headers
return RSVP.all([
form_gadget.getUrlFor({command: 'change', options: {page: undefined, view: undefined}}),
calculatePageTitle(form_gadget, form_gadget.state.erp5_document)
]);
})
.push(function (all_result) {
form_gadget.element.querySelector('a.dialogcancel').href = all_result[0];
return form_gadget.updateHeader({
cancel_url: all_result[0],
page_title: all_result[1]
});
});
})
/** The only way how to force download from javascript (working everywhere)
* is unfortunately constructing <a> and clicking on it
*/
.declareJob("forceDownload", function (attachment) {
var attachment_data = attachment.target.response,
filename = /(?:^|;)\s*filename\s*=\s*"?([^";]+)/i.exec(
attachment.target.getResponseHeader("Content-Disposition") || ""
),
a_tag = document.createElement("a");
if (attachment.target.responseType !== "blob") {
attachment_data = new Blob(
[attachment.target.response],
{type: attachment.target.getResponseHeader("Content-Type")}
);
}
a_tag.style = "display: none";
a_tag.href = URL.createObjectURL(attachment_data);
a_tag.download = filename ? filename[1].trim() : "untitled";
document.body.appendChild(a_tag);
a_tag.click();
return new RSVP.Queue()
.push(function () {
return RSVP.delay(10);
})
.push(function () {
URL.revokeObjectURL(a_tag.href);
document.body.removeChild(a_tag);
});
})
.onEvent('submit', function () {
var form_gadget = this,
action = this.state.erp5_document._embedded._view._actions.put,
form_id = this.state.erp5_document._embedded._view.form_id,
redirect_to_parent; redirect_to_parent;
return form_gadget.notifySubmitting() return form_gadget.notifySubmitting()
...@@ -341,7 +178,7 @@ ...@@ -341,7 +178,7 @@
.push(undefined, function (error) { .push(undefined, function (error) {
if (error.target !== undefined) { if (error.target !== undefined) {
var error_text = 'Encountered an unknown error. Try to resubmit', var error_text = 'Encountered an unknown error. Try to resubmit',
promise_queue = new RSVP.Queue(); promise;
// if we know what the error was, try to precise it for the user // if we know what the error was, try to precise it for the user
if (error.target.status === 400) { if (error.target.status === 400) {
error_text = 'Input data has errors'; error_text = 'Input data has errors';
...@@ -349,21 +186,9 @@ ...@@ -349,21 +186,9 @@
error_text = 'You do not have the permissions to edit the object'; error_text = 'You do not have the permissions to edit the object';
} else if (error.target.status === 0) { } else if (error.target.status === 0) {
error_text = 'Document was not saved! Resubmit when you are online or the document accessible'; 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 // display translated error_text to user
promise_queue promise = form_gadget.notifySubmitted()
.push(function () {
return form_gadget.notifySubmitted();
})
.push(function () { .push(function () {
return form_gadget.translate(error_text); return form_gadget.translate(error_text);
}) })
...@@ -376,7 +201,7 @@ ...@@ -376,7 +201,7 @@
// if server validation of form data failed (indicated by response code 400) // if server validation of form data failed (indicated by response code 400)
// we parse out field errors and display them to the user // we parse out field errors and display them to the user
if (error.target.status === 400) { if (error.target.status === 400) {
promise_queue promise
.push(function () { .push(function () {
// when the server-side validation returns the error description // when the server-side validation returns the error description
if (error.target.responseType === "blob") { if (error.target.responseType === "blob") {
...@@ -389,11 +214,176 @@ ...@@ -389,11 +214,176 @@
return form_gadget.displayFormulatorValidationError(JSON.parse(event.target.result)); return form_gadget.displayFormulatorValidationError(JSON.parse(event.target.result));
}); });
} }
return promise_queue; return promise;
} }
throw error; throw error;
}); });
}
rJS(window)
/////////////////////////////////////////////////////////////////
// acquisition
/////////////////////////////////////////////////////////////////
.declareAcquiredMethod("jio_putAttachment", "jio_putAttachment")
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("notifySubmitting", "notifySubmitting")
.declareAcquiredMethod("notifySubmitted", "notifySubmitted")
.declareAcquiredMethod("translate", "translate")
.declareAcquiredMethod("notifyChange", "notifyChange")
.declareAcquiredMethod("updateForm", "updateForm")
.declareAcquiredMethod("displayFormulatorValidationError", "displayFormulatorValidationError")
/////////////////////////////////////////////////////////////////
// Proxy methods to the child gadget
/////////////////////////////////////////////////////////////////
.declareMethod('checkValidity', function () {
return this.getDeclaredGadget("erp5_form")
.push(function (declared_gadget) {
return declared_gadget.checkValidity();
});
}, {mutex: 'changestate'})
.declareMethod('getContent', function () {
return this.getDeclaredGadget("erp5_form")
.push(function (declared_gadget) {
return declared_gadget.getContent();
});
}, {mutex: 'changestate'})
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod('triggerSubmit', function () {
this.element.querySelector('input[type="submit"]').click();
}, {mutex: 'changestate'})
.declareMethod('render', function (options) {
// copy out wanted items from options and pass it to `changeState`
return this.changeState({
jio_key: options.jio_key,
view: options.view,
// ignore options.editable because dialog is always editable
erp5_document: options.erp5_document,
form_definition: options.form_definition,
erp5_form: options.erp5_form || {}
// ignore global editable state (be always editable)
});
})
.onStateChange(function () {
var form_gadget = this,
icon,
selector = form_gadget.element.querySelector("h3"),
title,
i,
view_list = ensureArray(this.state.erp5_document._links.action_workflow);
title = this.state.form_definition.title;
// XXX hardcoded...
switch (title) {
case "Create User":
icon = " ui-icon-user";
break;
case "Create Document":
icon = " ui-icon-file-o";
break;
case "Validate Workflow Action":
icon = " ui-icon-share-alt";
break;
case "Submit":
icon = " ui-icon-check";
break;
default:
icon = " ui-icon-random";
break;
}
for (i = 0; i < view_list.length; i += 1) {
if (view_list[i].href === this.state.view) {
title = view_list[i].title;
}
}
// Calculate the h3 properties
return new RSVP.Queue()
.push(function () {
return RSVP.all([
form_gadget.translate(form_gadget.state.form_definition.title),
form_gadget.translate(title)
]);
})
.push(function (translated_title_list) {
form_gadget.element.querySelector('input.dialogconfirm').value = translated_title_list[1];
selector.textContent = "\u00A0" + translated_title_list[0];
selector.className = "ui-content-title ui-body-c ui-icon ui-icon-custom" + icon;
// Render the erp5_from
return form_gadget.getDeclaredGadget("erp5_form");
})
.push(function (erp5_form) {
var form_options = form_gadget.state.erp5_form;
// pass own form options to the embedded form
form_options.erp5_document = form_gadget.state.erp5_document;
form_options.form_definition = form_gadget.state.form_definition;
form_options.view = form_gadget.state.view;
form_options.jio_key = form_gadget.state.jio_key;
form_options.editable = true; // dialog is always editable
return erp5_form.render(form_options);
})
.push(function () {
// Render the headers
return RSVP.all([
form_gadget.getUrlFor({command: 'change', options: {page: undefined, view: undefined}}),
calculatePageTitle(form_gadget, form_gadget.state.erp5_document)
]);
})
.push(function (all_result) {
form_gadget.element.querySelector('a.dialogcancel').href = all_result[0];
return form_gadget.updateHeader({
cancel_url: all_result[0],
page_title: all_result[1]
});
});
})
/** The only way how to force download from javascript (working everywhere)
* is unfortunately constructing <a> and clicking on it
*/
.declareJob("forceDownload", function (attachment) {
var attachment_data = attachment.target.response,
filename = /(?:^|;)\s*filename\s*=\s*"?([^";]+)/i.exec(
attachment.target.getResponseHeader("Content-Disposition") || ""
),
a_tag = document.createElement("a");
if (attachment.target.responseType !== "blob") {
attachment_data = new Blob(
[attachment.target.response],
{type: attachment.target.getResponseHeader("Content-Type")}
);
}
a_tag.style = "display: none";
a_tag.href = URL.createObjectURL(attachment_data);
a_tag.download = filename ? filename[1].trim() : "untitled";
document.body.appendChild(a_tag);
a_tag.click();
return new RSVP.Queue()
.push(function () {
return RSVP.delay(10);
})
.push(function () {
URL.revokeObjectURL(a_tag.href);
document.body.removeChild(a_tag);
});
})
.onEvent('submit', function () {
return submitDialog(this);
}, 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