Commit 35809f62 authored by Roque's avatar Roque

erp5_officejs: refactor handle custom actions gadget

parent c4026c75
......@@ -156,7 +156,7 @@
action_doc = action_document_list[action_key];
if (app_actions.includes(action_doc.reference)) {
action_settings = {
page: undefined,
page: "handle_action",
jio_key: jio_key,
title: action_doc.title,
action: action_doc.reference,
......@@ -165,11 +165,12 @@
parent_portal_type: portal_type
};
if (view_categorie_list.includes(action_settings.action_type)) {
if (app_view === action_settings.action) {
action_settings.page = "ojs_local_controller";
}
action_info_dict.view_list[action_settings.action] =
action_settings;
} else {
action_settings.page = "handle_action";
action_info_dict.action_list[action_settings.action] =
action_settings;
}
......
......@@ -275,7 +275,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>980.16928.56199.10649</string> </value>
<value> <string>987.1958.49484.49100</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -293,7 +293,7 @@
</tuple>
<state>
<tuple>
<float>1575473624.38</float>
<float>1601668696.35</float>
<string>UTC</string>
</tuple>
</state>
......
/*global window, document, rJS, RSVP */
/*global window, document, rJS, RSVP, console */
/*jslint nomen: true, indent: 2, maxerr: 10, maxlen: 80 */
(function (window, document, rJS, RSVP) {
(function (window, document, rJS, RSVP, console) {
"use strict";
function declareActionGadget(gadget, state_options) {
var fragment = document.createElement('div');
gadget.element.appendChild(fragment);
return gadget.declareGadget(state_options.action_gadget_url, {
scope: "action_field",
element: fragment
});
}
function submitAction(gadget, action_gadget, state_options, content_dict) {
var submit_dict;
//handleSubmit() may return dictionary entries with
//notification messages or redirect options
state_options.gadget = gadget;
return action_gadget.handleSubmit(content_dict, state_options)
.push(function (result) {
submit_dict = result;
if (submit_dict.notify) {
return gadget.notifySubmitted(submit_dict.notify);
}
})
.push(function () {
if (submit_dict.redirect) {
return gadget.redirect(submit_dict.redirect);
}
}, function (error) {
if (!(error instanceof RSVP.CancellationError)) {
console.log("Action error:", error);
return gadget.notifySubmitted({
message: "Action Failed",
status: "error"
});
}
throw error;
});
}
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
......@@ -12,6 +49,7 @@
.declareAcquiredMethod("notifySubmitted", 'notifySubmitted')
.declareAcquiredMethod("notifySubmitting", "notifySubmitting")
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("updateHeader", "updateHeader")
/////////////////////////////////////////////////////////////////
// declared methods
......@@ -34,7 +72,11 @@
})
.declareMethod("render", function (options) {
var gadget = this, action_reference;
var gadget = this,
action_reference,
form_definition,
state_options,
action_gadget;
return RSVP.Queue()
.push(function () {
return RSVP.all([
......@@ -44,24 +86,33 @@
]);
})
.push(function (result) {
if (result[0] !== undefined) { options.portal_type = result[0]; }
if (result[0] !== undefined) {
options.portal_type = result[0];
}
if (result[1] !== undefined) {
options.parent_relative_url = result[1];
}
action_reference = result[2];
return gadget.getActionFormDefinition(action_reference);
})
.push(function (form_definition) {
var fragment = document.createElement('div'),
action_gadget_url,
form_type = form_definition.form_type,
.push(function (result) {
form_definition = result;
var form_type = form_definition.form_type,
child_gadget_url = form_definition.child_gadget_url,
//an action form must have a GadgetField called
//"gadget_field_new_action_js_script"
//this gadget will point the custom action gadget
valid_action = form_definition.action_type ===
"object_jio_js_script" && form_definition.fields_raw_properties
.hasOwnProperty("gadget_field_action_js_script"),
//action validity determined by gadget_field_action_js_script field
valid_action = form_definition.fields_raw_properties
.hasOwnProperty("gadget_field_action_js_script");
if (!valid_action) {
return gadget.notifySubmitted({
message: 'Could not perform this action: configuration error',
status: 'fail'
})
.push(function (result) {
return gadget.redirect({
'command': 'display'
});
});
}
state_options = {
doc: {},
action_options: options,
......@@ -69,37 +120,30 @@
form_type: form_type,
form_definition: form_definition,
view: action_reference,
valid_action: valid_action
valid_action: valid_action,
action_gadget_url: form_definition.fields_raw_properties
.gadget_field_action_js_script.values.gadget_url
};
if (valid_action) {
action_gadget_url = form_definition.fields_raw_properties
.gadget_field_action_js_script.values.gadget_url;
// as custom gadget render is being done here
// avoid to child gadget to render it
delete form_definition.fields_raw_properties
.gadget_field_action_js_script;
gadget.element.appendChild(fragment);
return gadget.declareGadget(action_gadget_url, {
scope: "action_field",
element: fragment
return declareActionGadget(gadget, state_options);
})
.push(function (action_gadget) {
.push(function (result) {
action_gadget = result;
options.form_definition = form_definition;
//preRenderDocument() may return a document dict with fields to be
//rendered in the action form, or configurations like skip_action_form
return action_gadget.preRenderDocument(options);
})
.push(function (doc) {
state_options.doc = doc;
state_options.action_gadget_url = action_gadget_url;
return gadget.changeState(state_options);
}, function (error) {
if (error.status === 404) {
return gadget.notifySubmitted({
message: "Error in action",
status: "error"
});
if (doc.header_dict) {
state_options.header_dict = doc.header_dict;
delete doc.header_dict;
}
throw error;
});
state_options.doc = doc;
if (doc.skip_action_form) {
delete state_options.doc.skip_action_form;
return submitAction(gadget, action_gadget, state_options, {});
}
return gadget.changeState(state_options);
});
......@@ -116,6 +160,11 @@
{element: fragment, scope: 'fg'})
.push(function (form_view_gadget) {
return form_view_gadget.render(gadget.state);
})
.push(function () {
if (gadget.state.header_dict) {
return gadget.updateHeader(gadget.state.header_dict);
}
});
})
......@@ -128,40 +177,15 @@
.allowPublicAcquisition('submitContent', function (options) {
var gadget = this,
//target_url = options[1],
content_dict = options[2],
fragment = document.createElement('div'),
submit_dict;
content_dict = options[2];
if (gadget.state.valid_action) {
return gadget.notifySubmitting()
.push(function () {
gadget.element.appendChild(fragment);
return gadget.declareGadget(gadget.state.action_gadget_url, {
scope: "action_field",
element: fragment
});
return declareActionGadget(gadget, gadget.state);
})
.push(function (action_gadget) {
return action_gadget.handleSubmit(content_dict, gadget.state);
})
.push(function (submit_result) {
submit_dict = submit_result;
//submit_dict must contain:
//notify: options_dict for notifySubmitted
//redirect: options_dict for redirect
return gadget.notifySubmitted(submit_dict.notify);
})
.push(function () {
return gadget.redirect(submit_dict.redirect);
}, function (error) {
if (!(error instanceof RSVP.CancellationError)) {
console.log("Action error:", error);
return gadget.notifySubmitted({
message: "Action Failed",
status: "error"
});
}
throw error;
return submitAction(gadget, action_gadget,
gadget.state, content_dict);
});
}
return gadget.notifySubmitted(
......@@ -169,4 +193,4 @@
status: 'fail'});
});
}(window, document, rJS, RSVP));
}(window, document, rJS, RSVP, console));
......@@ -275,7 +275,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>986.35897.25368.15923</string> </value>
<value> <string>987.1942.40980.22510</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -293,7 +293,7 @@
</tuple>
<state>
<tuple>
<float>1599861051.55</float>
<float>1601668958.25</float>
<string>UTC</string>
</tuple>
</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