Commit 2206f4f8 authored by Roque's avatar Roque

erp5_officejs: code cleanup

parent 982396a4
......@@ -4,32 +4,33 @@
"use strict";
function renderField(field_id, field_definition, document) {
var result = {};
for (var key in field_definition.values) {
// order to get the final value (based on Field.py get_value)
// 1.tales, 2.override, 3.form-def-value, 4.context-default
var raw_value = field_definition.values[key],
tales_expr = field_definition.tales[key],
override = field_definition.overrides[key],
final_value;
final_value = undefined;
if (tales_expr !== undefined && tales_expr !== null && tales_expr !== '') {
try {
throw "error";
//final_value = eval(tales_expr);
} catch (e) {} // TALES expressions are usually python code, so for now ignore
}
if (final_value === undefined) {
if (override !== undefined && override !== null && override !== '') {
final_value = override;
} else if (raw_value !== undefined && raw_value !== null && raw_value !== '') {
final_value = raw_value;
} else if (document && document.hasOwnProperty(key)) {
final_value = document[key];
var key, raw_value, tales_expr, override, final_value, result = {};
for (key in field_definition.values) {
if (field_definition.values.hasOwnProperty(key)) {
// order to get the final value (based on Field.py get_value)
// 1.tales, 2.override, 3.form-def-value, 4.context-default
raw_value = field_definition.values[key];
tales_expr = field_definition.tales[key];
override = field_definition.overrides[key];
final_value = undefined;
if (tales_expr !== undefined && tales_expr !== null && tales_expr !== '') {
try {
throw "error";
//final_value = eval(tales_expr);
} catch (ignore) {} // TALES expressions are usually python code, so for now ignore
}
if (final_value === undefined) {
if (override !== undefined && override !== null && override !== '') {
final_value = override;
} else if (raw_value !== undefined && raw_value !== null && raw_value !== '') {
final_value = raw_value;
} else if (document && document.hasOwnProperty(key)) {
final_value = document[key];
}
}
if (final_value !== undefined && final_value !== null && final_value !== '') {
result[key] = final_value;
}
}
if (final_value !== undefined && final_value !== null && final_value !== '') {
result[key] = final_value;
}
}
result.type = field_definition.type;
......@@ -59,8 +60,8 @@
.declareMethod("getFormDefinition", function (portal_type) {
var gadget = this,
parent = "portal_types/" + portal_type,
query = 'portal_type: "Action Information" AND reference: "jio_view" AND parent_relative_url: "' + parent + '"';
parent = "portal_types/" + portal_type,
query = 'portal_type: "Action Information" AND reference: "jio_view" AND parent_relative_url: "' + parent + '"';
return gadget.jio_allDocs({query: query})
.push(function (data) {
if (data.data.rows.length === 0) {
......@@ -77,14 +78,15 @@
})
.declareMethod("renderForm", function (form_definition, document) {
var raw_properties = form_definition.fields_raw_properties;
var form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
}, i, j, fields, field_info, my_element, element_id;
var i, j, fields, field_info, my_element, element_id, rendered_field,
raw_properties = form_definition.fields_raw_properties,
form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
};
for (i = 0; i < form_definition.group_list.length; i += 1) {
fields = form_definition.group_list[i][1];
for (j = 0; j < fields.length; j += 1) {
......@@ -98,7 +100,7 @@
}
if (raw_properties.hasOwnProperty(my_element)) {
field_info = raw_properties[my_element];
var rendered_field = renderField(element_id, field_info, document);
rendered_field = renderField(element_id, field_info, document);
form_json.erp5_document._embedded._view[my_element] = rendered_field;
}
}
......
......@@ -225,7 +225,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>974.38878.8625.14933</string> </value>
<value> <string>974.39977.38000.44305</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -243,7 +243,7 @@
</tuple>
<state>
<tuple>
<float>1553542945.18</float>
<float>1553613284.01</float>
<string>UTC</string>
</tuple>
</state>
......
/*global window, rJS, jIO, FormData, UriTemplate */
/*jslint indent: 2, maxerr: 3 */
(function (window, rJS, jIO, RSVP, document, URLSearchParams) {
(function (window, rJS, jIO, RSVP, document, URLSearchParams, console) {
"use strict";
// jIO call wrapper for redirection to authentication page if needed
......@@ -61,24 +61,28 @@
}
function processHateoasDict(raw_dict) {
var return_dict = {};
var raw_fields, type, parent, field_key, field_id, return_dict = {};
try {
var raw_fields = raw_dict._embedded._view,
type = raw_dict._links.type.name,
parent = raw_dict._links.parent.name;
/*jslint nomen: true*/
raw_fields = raw_dict._embedded._view;
type = raw_dict._links.type.name;
parent = raw_dict._links.parent.name;
/*jslint nomen: false*/
return_dict.parent_relative_url = "portal_types/" + parent;
return_dict.portal_type = type;
for (var field_key in raw_fields) {
var field_id = "";
if (raw_fields[field_key]["default"] !== undefined && raw_fields[field_key]["default"] !== "") {
if (field_key.startsWith("my_")) {
field_id = field_key.replace("my_", "");
} else if (field_key.startsWith("your_")) {
field_id = field_key.replace("your_", "");
} else {
field_id = field_key;
for (field_key in raw_fields) {
if (raw_fields.hasOwnProperty(field_key)) {
field_id = "";
if (raw_fields[field_key]["default"] !== undefined && raw_fields[field_key]["default"] !== "") {
if (field_key.startsWith("my_")) {
field_id = field_key.replace("my_", "");
} else if (field_key.startsWith("your_")) {
field_id = field_key.replace("your_", "");
} else {
field_id = field_key;
}
return_dict[field_id] = raw_fields[field_key]["default"];
}
return_dict[field_id] = raw_fields[field_key]["default"];
}
}
} catch (e) {
......@@ -102,9 +106,8 @@
.declareAcquiredMethod('getUrlFor', 'getUrlFor')
.declareMethod('createJio', function (jio_options) {
var gadget = this,
var appcache_storage,
origin_url = window.location.href,
prefix = "./",
hateoas_script = "hateoas/ERP5Document_getHateoas",
// TODO manifest should come from gadget.props.cache_file -add script in html body
manifest = "gadget_officejs_discussion_tool.configuration",
......@@ -133,7 +136,7 @@
type: "appcache",
manifest: manifest
}
}, appcache_storage,
},
sync_flag = "appcache-stored",
configuration_ids_dict = {};
if (jio_options === undefined) {
......@@ -160,52 +163,57 @@
// TODO: find a better flag for this?
return appcache_storage.get(sync_flag)
.push(undefined, function (error) {
return appcache_storage.repair()
.push(function () {
return appcache_storage.allAttachments(origin_url)
.push(function (attachment_dict) {
return new RSVP.Queue()
.push(function () {
var promise_list = [], i = 0;
for (var id in attachment_dict) {
if (error && String(error.status_code) !== "404") {
throw error;
}
return appcache_storage.repair()
.push(function () {
return appcache_storage.allAttachments(origin_url)
.push(function (attachment_dict) {
return new RSVP.Queue()
.push(function () {
var id, promise_list = [], i = 0;
for (id in attachment_dict) {
if (attachment_dict.hasOwnProperty(id)) {
if (id.indexOf(hateoas_script) === -1) {
promise_list.push(appcache_storage.getAttachment(origin_url, id));
} else {
promise_list.push(appcache_storage.getAttachment(origin_url, id, {"format": "json"}));
}
configuration_ids_dict["" + i] = id;
configuration_ids_dict[String(i)] = id;
i += 1;
}
return RSVP.all(promise_list);
})
.push(function (content_list) {
var promise_list = [];
for (var i = 0; i < content_list.length; i += 1) {
var id = configuration_ids_dict["" + i],
parser = document.createElement('a');
parser.href = id;
var urlParams = new URLSearchParams(parser.search);
id = urlParams.get("relative_url");
if (id === null) { id = configuration_ids_dict["" + i]; }
var content = processHateoasDict(content_list[i]);
promise_list.push(appcache_storage.put(id, content));
}
return RSVP.all(promise_list);
})
.push(function () {
return appcache_storage.put(sync_flag, {})
.push(undefined);
});
});
}, function (error) {
}
return RSVP.all(promise_list);
})
.push(function (content_list) {
var i, id, parser, urlParams, content, promise_list = [];
for (i = 0; i < content_list.length; i += 1) {
id = configuration_ids_dict[String(i)];
parser = document.createElement('a');
parser.href = id;
urlParams = new URLSearchParams(parser.search);
id = urlParams.get("relative_url");
if (id === null) { id = configuration_ids_dict[String(i)]; }
content = processHateoasDict(content_list[i]);
promise_list.push(appcache_storage.put(id, content));
}
return RSVP.all(promise_list);
})
.push(function () {
return appcache_storage.put(sync_flag, {})
.push(undefined);
});
});
}, function (error) {
console.log("Error while appcache-local storage synchronization");
if (error && error.currentTarget && error.currentTarget.status == "401") {
if (error && error.currentTarget && error.currentTarget.status === "401") {
console.log("Unauthorized access to storage, sync cancelled");
return;
}
throw error;
});
});
});
});
})
.declareMethod('allDocs', function () {
......@@ -239,4 +247,4 @@
return wrapJioCall(this, 'repair', arguments);
});
}(window, rJS, jIO, RSVP, document, URLSearchParams));
\ No newline at end of file
}(window, rJS, jIO, RSVP, document, URLSearchParams, console));
\ No newline at end of file
......@@ -234,7 +234,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>974.38585.37530.16384</string> </value>
<value> <string>974.40019.39794.62054</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -252,7 +252,7 @@
</tuple>
<state>
<tuple>
<float>1553525541.12</float>
<float>1553611478.88</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