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 */ /*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS) { (function (window, rJS, RSVP) {
"use strict"; "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) rJS(window)
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Acquired methods // Acquired methods
...@@ -43,13 +46,46 @@ ...@@ -43,13 +46,46 @@
return gadget.jio_post(doc); 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) { .declareMethod("getFormDefinition", function (portal_type, action_reference, extra_params) {
var gadget = this, var gadget = this,
parent = "portal_types/" + portal_type, parent = "portal_types/" + portal_type,
query = 'portal_type: "Action Information" AND reference: "' + action_reference + '" AND parent_relative_url: "' + parent + '"', query = 'portal_type: "Action Information" AND reference: "' + action_reference + '" AND parent_relative_url: "' + parent + '"',
action_type, action_title; action_type, action_title, form_definition;
//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
return gadget.jio_allDocs({query: query}) return gadget.jio_allDocs({query: query})
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.rows.length === 0) {
...@@ -63,22 +99,31 @@ ...@@ -63,22 +99,31 @@
return gadget.jio_get(action_result.action); return gadget.jio_get(action_result.action);
}) })
.push(function (form_result) { .push(function (form_result) {
form_result.form_definition.action_type = action_type; form_definition = form_result.form_definition;
form_result.form_definition.title = action_title; form_definition.action_type = action_type;
if (action_type === "object_list") { form_definition.title = action_title;
//TODO: get child_portal_type from form_definition (listbox->portal_type). return gadget.checkMoreActions(portal_type, action_type);
var child_portal_type = "HTML Post", })
action_info = { .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", page: "handle_action",
title: "New Post",
action: "new", action: "new",
portal_type: child_portal_type, reference: "new",
parent_portal_type: portal_type, action_type: "object_jio_js_script",
my_source_reference: extra_params.source_reference 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 @@ ...@@ -269,7 +269,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.35539.1795.20002</string> </value> <value> <string>975.38080.43465.46097</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1557188582.39</float> <float>1557340907.82</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -48,14 +48,14 @@ ...@@ -48,14 +48,14 @@
return gadget.changeState({ return gadget.changeState({
jio_key: options.jio_key, jio_key: options.jio_key,
doc: jio_document, doc: jio_document,
//TODO child_gadget_url should be decided in utils.getFormDefinition based on form type
child_gadget_url: child_gadget_url, child_gadget_url: child_gadget_url,
form_definition: form_definition, form_definition: form_definition,
form_type: 'page', form_type: 'page',
editable: false, editable: false,
view: options.view, view: options.view,
//HARDCODED: following fields should be indicated by the configuration has_more_views: form_definition.has_more_views,
has_more_views: false, has_more_actions: form_definition.has_more_actions
has_more_actions: options.view === "view"
}); });
}); });
}) })
......
...@@ -225,7 +225,7 @@ ...@@ -225,7 +225,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.29197.45196.34952</string> </value> <value> <string>975.36709.247.14950</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -243,7 +243,7 @@ ...@@ -243,7 +243,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1556807970.11</float> <float>1557340899.6</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
(function (window, document, rJS, RSVP) { (function (window, document, rJS, RSVP) {
"use strict"; "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) rJS(window)
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Acquired methods // Acquired methods
...@@ -22,6 +25,7 @@ ...@@ -22,6 +25,7 @@
default_view = "jio_view", default_view = "jio_view",
common_utils_gadget_url = "gadget_officejs_common_utils.html", common_utils_gadget_url = "gadget_officejs_common_utils.html",
child_gadget_url = 'gadget_erp5_pt_form_list.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: //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); fake_thread_uid = "thread-" + ("0000" + ((Math.random() * Math.pow(36, 4)) | 0).toString(36)).slice(-4);
return RSVP.Queue() return RSVP.Queue()
...@@ -32,21 +36,20 @@ ...@@ -32,21 +36,20 @@
]); ]);
}) })
.push(function (result) { .push(function (result) {
portal_type = result[0];
return result[1].getFormDefinition(result[0], default_view, {source_reference: fake_thread_uid}); return result[1].getFormDefinition(result[0], default_view, {source_reference: fake_thread_uid});
}) })
.push(function (form_definition) { .push(function (form_definition) {
return gadget.changeState({ return gadget.changeState({
jio_key: options.jio_key, jio_key: options.jio_key,
portal_type: portal_type,
//TODO child_gadget_url should be decided in utils.getFormDefinition based on form type //TODO child_gadget_url should be decided in utils.getFormDefinition based on form type
child_gadget_url: child_gadget_url, child_gadget_url: child_gadget_url,
form_definition: form_definition, form_definition: form_definition,
form_type: 'list', form_type: 'list',
editable: false, editable: false,
view: default_view, view: default_view,
front_page: true, front_page: true
//TODO: get following 2 values from form_def in utils.getFormDefinition
has_more_views: false,
has_more_actions: false
}); });
}); });
}) })
......
...@@ -269,7 +269,7 @@ ...@@ -269,7 +269,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.35544.63874.25224</string> </value> <value> <string>975.38075.2975.32938</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1557188776.18</float> <float>1557340510.94</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -128,7 +128,7 @@ ...@@ -128,7 +128,7 @@
.push(function () { .push(function () {
var url_for_parameter_list = [ var url_for_parameter_list = [
{command: 'change', options: {page: "tab"}}, {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: 'history_previous'},
{command: 'selection_previous'}, {command: 'selection_previous'},
{command: 'selection_next'}, {command: 'selection_next'},
...@@ -165,6 +165,7 @@ ...@@ -165,6 +165,7 @@
page_title: result_list[2] page_title: result_list[2]
}; };
if (!options.front_page) { if (!options.front_page) {
header_dict.selection_url = url_list[2];
header_dict.front_url = url_list[6]; header_dict.front_url = url_list[6];
} }
} else { } else {
...@@ -174,14 +175,14 @@ ...@@ -174,14 +175,14 @@
next_url: url_list[4], next_url: url_list[4],
page_title: options.doc.title page_title: options.doc.title
}; };
if (options.has_more_views) { if (options.form_definition.has_more_views) {
header_dict.tab_url = url_list[0]; header_dict.tab_url = url_list[0];
} }
if (options.editable) { if (options.editable) {
header_dict.save_action = true; header_dict.save_action = true;
} }
} }
if (options.has_more_actions) { if (options.form_definition.has_more_actions) {
header_dict.actions_url = url_list[1]; header_dict.actions_url = url_list[1];
} }
if (add_url) { if (add_url) {
......
...@@ -269,7 +269,7 @@ ...@@ -269,7 +269,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.35050.19962.53333</string> </value> <value> <string>975.36690.25762.27101</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1557237930.34</float> <float>1557340270.78</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
var gadget_klass = rJS(window), var gadget_klass = rJS(window),
table_template = Handlebars.compile(gadget_klass.__template_element table_template = Handlebars.compile(gadget_klass.__template_element
.getElementById("table-template") .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 /** Render translated HTML of title + links
* *
...@@ -36,7 +38,6 @@ ...@@ -36,7 +38,6 @@
); );
} }
gadget_klass gadget_klass
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Acquired methods // Acquired methods
...@@ -48,23 +49,42 @@ ...@@ -48,23 +49,42 @@
.declareAcquiredMethod("getUrlForList", "getUrlForList") .declareAcquiredMethod("getUrlForList", "getUrlForList")
.declareAcquiredMethod("getUrlParameter", "getUrlParameter") .declareAcquiredMethod("getUrlParameter", "getUrlParameter")
.declareAcquiredMethod("updateHeader", "updateHeader") .declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getSetting", "getSetting")
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// declared methods // declared methods
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
.declareMethod("render", function (options) {
var gadget = this, .declareMethod("getActionSettings", function (action_doc, portal_type) {
action_info_list = [], var gadget = this, page = "handle_action", action_settings = {};
document_title, if (view_categories.includes(action_doc.action_type)) {
portal_type; page = "ojs_controller";
return gadget.jio_get(options.jio_key) }
.push(function (document) { action_settings = {
var parent = "portal_types/" + document.portal_type, page: page,
query = 'portal_type: "Action Information" AND parent_relative_url: "' + parent + '"'; title: action_doc.title,
portal_type = document.portal_type; action: action_doc.reference,
document_title = document.title; reference: action_doc.reference,
return gadget.jio_allDocs({query: query}); 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_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) { .push(function (action_list) {
var path_for_jio_get_list = [], row; var path_for_jio_get_list = [], row;
for (row in action_list.data.rows) { for (row in action_list.data.rows) {
...@@ -75,23 +95,65 @@ ...@@ -75,23 +95,65 @@
return RSVP.all(path_for_jio_get_list); return RSVP.all(path_for_jio_get_list);
}) })
.push(function (action_document_list) { .push(function (action_document_list) {
var url_for_parameter_list = [], i = 0, var get_action_settings_list = [], page, action_key, action_doc;
page, action_key, action_doc, action_settings;
for (action_key in action_document_list) { for (action_key in action_document_list) {
if (action_document_list.hasOwnProperty(action_key)) { if (action_document_list.hasOwnProperty(action_key)) {
page = "handle_action"; get_action_settings_list.push(gadget.getActionSettings(action_document_list[action_key], portal_type));
action_doc = action_document_list[action_key];
if (action_doc.reference === "view" || action_doc.reference === "jio_view") {
page = "ojs_controller";
} }
action_settings = { }
page: page, return RSVP.all(get_action_settings_list);
action: action_doc.reference, })
parent_portal_type: portal_type, .push(function (action_settings_list) {
portal_type: portal_type for (var key in action_settings_list) {
}; if (action_settings_list.hasOwnProperty(key)) {
url_for_parameter_list.push({ command: 'change', options: action_settings }); var action_settings = action_settings_list[key];
action_info_list[i] = { reference: action_doc.reference, title: action_doc.title}; 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;
}
}
}
//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; i += 1;
} }
} }
...@@ -104,15 +166,12 @@ ...@@ -104,15 +166,12 @@
icon: null, icon: null,
name: action_info_list[i].reference, name: action_info_list[i].reference,
title: action_info_list[i].title }; title: action_info_list[i].title };
// TODO: maybe both view and jio_view should be ignored here if (action_info_dict.views.hasOwnProperty(element.name)) {
if (element.name !== "view") {
if (element.name === "jio_view") {
view_list.push(element); view_list.push(element);
} else { } else {
action_list.push(element); action_list.push(element);
} }
} }
}
// TODO: check other lists like clone or delete? // TODO: check other lists like clone or delete?
return RSVP.all([ return RSVP.all([
renderLinkList(gadget, "Views", "eye", view_list), renderLinkList(gadget, "Views", "eye", view_list),
...@@ -130,6 +189,7 @@ ...@@ -130,6 +189,7 @@
}); });
}); });
}) })
.declareMethod("triggerSubmit", function () { .declareMethod("triggerSubmit", function () {
return; return;
}); });
......
...@@ -228,7 +228,7 @@ ...@@ -228,7 +228,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.36364.51513.30993</string> </value> <value> <string>975.36707.15008.32256</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1557237896.05</float> <float>1557340236.58</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1556807973.08</float> <float>1557237549.7</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