Commit 3ed54eae authored by Roque's avatar Roque

erp5_officejs: general refactoring on page rendering (document and module) and...

erp5_officejs: general refactoring on page rendering (document and module) and form definition handling
parent b4f77247
/*global window, rJS */
/*global window, rJS, RSVP */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS) {
(function (window, rJS, RSVP) {
"use strict";
// TODO: check if there are other categories that are 'views' and find a less hardcoded way to get this
var view_categories = ["object_view", "object_jio_view", "object_web_view", "object_list"];
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
......@@ -43,13 +46,46 @@
return gadget.jio_post(doc);
})
.declareMethod("checkMoreActions", function (portal_type, action_category) {
var gadget = this,
has_more_dict = {views: {}, actions: {}},
query;
//if target action is a type of view, get all actions/views for the portal_type
if (view_categories.includes(action_category)) {
query = 'portal_type: "Action Information" AND parent_relative_url: "portal_types/' + portal_type + '"';
return gadget.jio_allDocs({query: query})
.push(function (action_list) {
var path_for_jio_get_list = [], row;
for (row in action_list.data.rows) {
if (action_list.data.rows.hasOwnProperty(row)) {
path_for_jio_get_list.push(gadget.jio_get(action_list.data.rows[row].id));
}
}
return RSVP.all(path_for_jio_get_list);
})
.push(function (action_document_list) {
var get_action_settings_list = [], page, action_key, action_doc;
for (action_key in action_document_list) {
if (action_document_list.hasOwnProperty(action_key)) {
action_doc = action_document_list[action_key];
if (view_categories.includes(action_doc.action_type)) {
has_more_dict.has_more_views = true;
} else {
has_more_dict.has_more_actions = true;
}
}
}
return has_more_dict;
});
}
return has_more_dict;
})
.declareMethod("getFormDefinition", function (portal_type, action_reference, extra_params) {
var gadget = this,
parent = "portal_types/" + portal_type,
query = 'portal_type: "Action Information" AND reference: "' + action_reference + '" AND parent_relative_url: "' + parent + '"',
action_type, action_title;
//TODO: check all actions to set has_more_views and has_more_actions flags in form def
//also check if there is a "new" action for object_list.action_info
action_type, action_title, form_definition;
return gadget.jio_allDocs({query: query})
.push(function (data) {
if (data.data.rows.length === 0) {
......@@ -63,22 +99,31 @@
return gadget.jio_get(action_result.action);
})
.push(function (form_result) {
form_result.form_definition.action_type = action_type;
form_result.form_definition.title = action_title;
if (action_type === "object_list") {
//TODO: get child_portal_type from form_definition (listbox->portal_type).
var child_portal_type = "HTML Post",
action_info = {
form_definition = form_result.form_definition;
form_definition.action_type = action_type;
form_definition.title = action_title;
return gadget.checkMoreActions(portal_type, action_type);
})
.push(function (has_more_dict) {
//view and actions are managed by same actions-gadget-page
form_definition.has_more_views = false;
form_definition.has_more_actions = has_more_dict.has_more_actions;
//for backward compatibility (header add button - '+' icon)
if (extra_params && extra_params.source_reference) {
form_definition._links.action_object_new_content_action = {
page: "handle_action",
title: "New Post",
action: "new",
portal_type: child_portal_type,
parent_portal_type: portal_type,
my_source_reference: extra_params.source_reference
reference: "new",
action_type: "object_jio_js_script",
parent_portal_type: "Post Module",
portal_type: "HTML Post",
source_reference: extra_params.source_reference
};
form_result.form_definition._links.action_object_new_content_action = action_info;
form_definition.has_more_actions = false;
}
return form_result.form_definition;
return form_definition;
});
});
}(window, rJS));
}(window, rJS, RSVP));
......@@ -269,7 +269,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.35539.1795.20002</string> </value>
<value> <string>975.38080.43465.46097</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -287,7 +287,7 @@
</tuple>
<state>
<tuple>
<float>1557188582.39</float>
<float>1557340907.82</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -48,14 +48,14 @@
return gadget.changeState({
jio_key: options.jio_key,
doc: jio_document,
//TODO child_gadget_url should be decided in utils.getFormDefinition based on form type
child_gadget_url: child_gadget_url,
form_definition: form_definition,
form_type: 'page',
editable: false,
view: options.view,
//HARDCODED: following fields should be indicated by the configuration
has_more_views: false,
has_more_actions: options.view === "view"
has_more_views: form_definition.has_more_views,
has_more_actions: form_definition.has_more_actions
});
});
})
......
......@@ -225,7 +225,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.29197.45196.34952</string> </value>
<value> <string>975.36709.247.14950</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -243,7 +243,7 @@
</tuple>
<state>
<tuple>
<float>1556807970.11</float>
<float>1557340899.6</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -3,6 +3,9 @@
(function (window, document, rJS, RSVP) {
"use strict";
//TODO rename this gadget as "front or main" gadget
//check if this can be done by the controller and use that gadget instead
rJS(window)
/////////////////////////////////////////////////////////////////
// Acquired methods
......@@ -22,6 +25,7 @@
default_view = "jio_view",
common_utils_gadget_url = "gadget_officejs_common_utils.html",
child_gadget_url = 'gadget_erp5_pt_form_list.html',
portal_type,
//In a future, the post list will be within a thread. For now:
fake_thread_uid = "thread-" + ("0000" + ((Math.random() * Math.pow(36, 4)) | 0).toString(36)).slice(-4);
return RSVP.Queue()
......@@ -32,21 +36,20 @@
]);
})
.push(function (result) {
portal_type = result[0];
return result[1].getFormDefinition(result[0], default_view, {source_reference: fake_thread_uid});
})
.push(function (form_definition) {
return gadget.changeState({
jio_key: options.jio_key,
portal_type: portal_type,
//TODO child_gadget_url should be decided in utils.getFormDefinition based on form type
child_gadget_url: child_gadget_url,
form_definition: form_definition,
form_type: 'list',
editable: false,
view: default_view,
front_page: true,
//TODO: get following 2 values from form_def in utils.getFormDefinition
has_more_views: false,
has_more_actions: false
front_page: true
});
});
})
......
......@@ -269,7 +269,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.35544.63874.25224</string> </value>
<value> <string>975.38075.2975.32938</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -287,7 +287,7 @@
</tuple>
<state>
<tuple>
<float>1557188776.18</float>
<float>1557340510.94</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -128,7 +128,7 @@
.push(function () {
var url_for_parameter_list = [
{command: 'change', options: {page: "tab"}},
{command: 'change', options: {page: "action_officejs", jio_key: options.jio_key}},
{command: 'change', options: {page: "action_officejs", jio_key: options.jio_key, portal_type: options.portal_type}},
{command: 'history_previous'},
{command: 'selection_previous'},
{command: 'selection_next'},
......@@ -165,6 +165,7 @@
page_title: result_list[2]
};
if (!options.front_page) {
header_dict.selection_url = url_list[2];
header_dict.front_url = url_list[6];
}
} else {
......@@ -174,14 +175,14 @@
next_url: url_list[4],
page_title: options.doc.title
};
if (options.has_more_views) {
if (options.form_definition.has_more_views) {
header_dict.tab_url = url_list[0];
}
if (options.editable) {
header_dict.save_action = true;
}
}
if (options.has_more_actions) {
if (options.form_definition.has_more_actions) {
header_dict.actions_url = url_list[1];
}
if (add_url) {
......
......@@ -269,7 +269,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.35050.19962.53333</string> </value>
<value> <string>975.36690.25762.27101</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -287,7 +287,7 @@
</tuple>
<state>
<tuple>
<float>1557237930.34</float>
<float>1557340270.78</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -10,7 +10,9 @@
var gadget_klass = rJS(window),
table_template = Handlebars.compile(gadget_klass.__template_element
.getElementById("table-template")
.innerHTML);
.innerHTML),
// TODO: check if there are other categories that are 'views' and find a less hardcoded way to get this
view_categories = ["object_view", "object_jio_view", "object_web_view", "object_list"];
/** Render translated HTML of title + links
*
......@@ -36,7 +38,6 @@
);
}
gadget_klass
/////////////////////////////////////////////////////////////////
// Acquired methods
......@@ -48,23 +49,42 @@
.declareAcquiredMethod("getUrlForList", "getUrlForList")
.declareAcquiredMethod("getUrlParameter", "getUrlParameter")
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getSetting", "getSetting")
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod("render", function (options) {
.declareMethod("getActionSettings", function (action_doc, portal_type) {
var gadget = this, page = "handle_action", action_settings = {};
if (view_categories.includes(action_doc.action_type)) {
page = "ojs_controller";
}
action_settings = {
page: page,
title: action_doc.title,
action: action_doc.reference,
reference: action_doc.reference,
action_type: action_doc.action_type,
parent_portal_type: portal_type,
portal_type: portal_type
};
//TODO find a better way to handle "add" actions (how to get child portal type?)
if (action_doc.reference === "new") {
return gadget.getSetting('portal_type')
.push(function (child_portal_type) {
action_settings.portal_type = child_portal_type;
return action_settings;
});
}
return action_settings;
})
.declareMethod("getAllActions", function (portal_type, action_category) {
var gadget = this,
action_info_list = [],
document_title,
portal_type;
return gadget.jio_get(options.jio_key)
.push(function (document) {
var parent = "portal_types/" + document.portal_type,
query = 'portal_type: "Action Information" AND parent_relative_url: "' + parent + '"';
portal_type = document.portal_type;
document_title = document.title;
return gadget.jio_allDocs({query: query});
})
action_info_dict = {views: {}, actions: {}},
query = 'portal_type: "Action Information" AND parent_relative_url: "portal_types/' + portal_type + '"';
return gadget.jio_allDocs({query: query})
.push(function (action_list) {
var path_for_jio_get_list = [], row;
for (row in action_list.data.rows) {
......@@ -75,23 +95,65 @@
return RSVP.all(path_for_jio_get_list);
})
.push(function (action_document_list) {
var url_for_parameter_list = [], i = 0,
page, action_key, action_doc, action_settings;
var get_action_settings_list = [], page, action_key, action_doc;
for (action_key in action_document_list) {
if (action_document_list.hasOwnProperty(action_key)) {
page = "handle_action";
action_doc = action_document_list[action_key];
if (action_doc.reference === "view" || action_doc.reference === "jio_view") {
page = "ojs_controller";
get_action_settings_list.push(gadget.getActionSettings(action_document_list[action_key], portal_type));
}
}
return RSVP.all(get_action_settings_list);
})
.push(function (action_settings_list) {
for (var key in action_settings_list) {
if (action_settings_list.hasOwnProperty(key)) {
var action_settings = action_settings_list[key];
if (view_categories.includes(action_settings.action_type)) {
action_info_dict.views[action_settings.action] = action_settings;
} else {
action_info_dict.actions[action_settings.action] = action_settings;
}
action_settings = {
page: page,
action: action_doc.reference,
parent_portal_type: portal_type,
portal_type: portal_type
};
url_for_parameter_list.push({ command: 'change', options: action_settings });
action_info_list[i] = { reference: action_doc.reference, title: action_doc.title};
}
}
//if portal_type has both view and jio_view, remove classic 'view'
//TODO use action type instead of reference
if (action_info_dict.views.hasOwnProperty("view") && action_info_dict.views.hasOwnProperty("jio_view")) {
delete action_info_dict.views.view;
}
return action_info_dict;
});
})
.declareMethod("render", function (options) {
var gadget = this,
url_for_parameter_list = [],
action_info_list = [],
action_info_dict = {},
document_title;
return gadget.jio_get(options.jio_key)
.push(function (document) {
document_title = document.title;
return gadget.getAllActions(document.portal_type, view_categories[0]);
}, function (error) {
document_title = options.portal_type;
return gadget.getAllActions(options.portal_type, view_categories[0]);
})
.push(function (all_actions) {
var action_info, i = 0;
action_info_dict = all_actions;
//TODO refactor this (actions and views)
for (var action_key in action_info_dict.actions) {
if (action_info_dict.actions.hasOwnProperty(action_key)) {
action_info = action_info_dict.actions[action_key];
url_for_parameter_list.push({ command: 'change', options: action_info });
action_info_list[i] = { reference: action_info.reference, title: action_info.title};
i += 1;
}
}
for (var view_key in action_info_dict.views) {
if (action_info_dict.views.hasOwnProperty(view_key)) {
action_info = action_info_dict.views[view_key];
url_for_parameter_list.push({ command: 'change', options: action_info });
action_info_list[i] = { reference: action_info.reference, title: action_info.title};
i += 1;
}
}
......@@ -104,13 +166,10 @@
icon: null,
name: action_info_list[i].reference,
title: action_info_list[i].title };
// TODO: maybe both view and jio_view should be ignored here
if (element.name !== "view") {
if (element.name === "jio_view") {
view_list.push(element);
} else {
action_list.push(element);
}
if (action_info_dict.views.hasOwnProperty(element.name)) {
view_list.push(element);
} else {
action_list.push(element);
}
}
// TODO: check other lists like clone or delete?
......@@ -130,6 +189,7 @@
});
});
})
.declareMethod("triggerSubmit", function () {
return;
});
......
......@@ -228,7 +228,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.36364.51513.30993</string> </value>
<value> <string>975.36707.15008.32256</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -246,7 +246,7 @@
</tuple>
<state>
<tuple>
<float>1557237896.05</float>
<float>1557340236.58</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -287,7 +287,7 @@
</tuple>
<state>
<tuple>
<float>1556807973.08</float>
<float>1557237549.7</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