Commit d07bb10c authored by Roque Porchetto's avatar Roque Porchetto

New custom JIO gadget to allow local storage.

parent 387e3462
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>Jio Gadget</title>
<!-- renderjs -->
<script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="jiodev.js" type="text/javascript"></script>
<!-- custom script -->
<script src="gadget_erp5_custom_jio.js" type="text/javascript"></script>
</head>
<body>
<div data-gadget-url='gadget_jio.html' data-gadget-scope='jio'></div>
</body>
</html>
\ No newline at end of file
/*global window, rJS, RSVP, UriTemplate, URI, Query, SimpleQuery, ComplexQuery, jIO */
/*jslint indent: 2, maxerr: 3, nomen: true */
(function (window, rJS, RSVP, UriTemplate, URI, Query, SimpleQuery, ComplexQuery, jIO) {
"use strict";
function wrapJioCall(gadget, method_name, argument_list) {
var storage = gadget.state_parameter_dict.jio_storage,
regexp = /^X-Delegate uri="(http[s]*:\/\/[\/\-\[\]{}()*+:?.,\\\^$|#\s\w%]+)"$/,
login_page;
return storage[method_name].apply(storage, argument_list)
.push(undefined, function (error) {
if ((error.target !== undefined) && (error.target.status === 401)) {
login_page = error.target.getResponseHeader('WWW-Authenticate');
// Only connect to https to login
if (regexp.test(login_page)) {
return gadget.getUrlFor({
command: 'login',
absolute_url: true
})
.push(function (came_from) {
return gadget.redirect({
command: 'raw',
options: {
url: UriTemplate.parse(regexp.exec(login_page)[1]).expand({came_from: came_from})
}
});
});
/*
window.location = UriTemplate.parse(
regexp.exec(login_page)[1]
).expand({came_from: window.location.href + "{&me}"});
return RSVP.timeout(5000);
*/
// Redirect to the login view
}
// return gadget.redirect({command: 'display', options: {page: 'login'}});
}
throw error;
});
}
function isSingleLocalRoles(parsed_query) {
if ((parsed_query instanceof SimpleQuery) &&
(parsed_query.key === 'local_roles')) {
// local_roles:"Assignee"
return parsed_query.value;
}
}
function isMultipleLocalRoles(parsed_query) {
var i,
sub_query,
is_multiple = true,
local_role_list = [];
if ((parsed_query instanceof ComplexQuery) &&
(parsed_query.operator === 'OR')) {
for (i = 0; i < parsed_query.query_list.length; i += 1) {
sub_query = parsed_query.query_list[i];
if ((sub_query instanceof SimpleQuery) &&
(sub_query.key === 'local_roles')) {
local_role_list.push(sub_query.value);
} else {
is_multiple = false;
}
}
if (is_multiple) {
// local_roles:"Assignee" OR local_roles:"Assignor"
return local_role_list;
}
}
}
rJS(window)
.ready(function (gadget) {
return gadget.getDeclaredGadget('jio')
.push(function (jio_gadget) {
// Initialize the gadget local parameters
gadget.state_parameter_dict = {jio_storage: jio_gadget};
});
})
.declareAcquiredMethod('getSetting', 'getSetting')
.declareAcquiredMethod('redirect', 'redirect')
.declareAcquiredMethod('getUrlFor', 'getUrlFor')
.declareMethod('createJio', function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.getSetting('hateoas_url'),
gadget.getSetting('default_view_reference')
]);
})
.push(function (setting_list) {
/*return gadget.state_parameter_dict.jio_storage.createJio({
type: "erp5",
url: setting_list[0],
default_view_reference: setting_list[1]
});*/
return gadget.state_parameter_dict.jio_storage.createJio({
type: "replicate",
local_sub_storage: {
type: "query",
"sub_storage": {
type: "indexeddb",
database: "telecom"
}
},
remote_sub_storage: {
type: "erp5",
url: setting_list[0],
default_view_reference: setting_list[1]
},
query: {
query: 'portal_type:"Data Array"',
limit: [0, 10000]
}
});
})
.push(function () {
return gadget.state_parameter_dict.jio_storage.repair();
});
})
.declareMethod('allDocs', function (option_dict) {
// throw new Error('do not use all docs');
if (option_dict.list_method_template === undefined) {
return wrapJioCall(this, 'allDocs', arguments);
}
var query = option_dict.query,
i,
parsed_query,
sub_query,
result_list,
tmp_list = [],
local_roles;
if (option_dict.query) {
parsed_query = jIO.QueryFactory.create(option_dict.query);
result_list = isSingleLocalRoles(parsed_query);
if (result_list) {
query = undefined;
local_roles = result_list;
} else {
result_list = isMultipleLocalRoles(parsed_query);
if (result_list) {
query = undefined;
local_roles = result_list;
} else if ((parsed_query instanceof ComplexQuery) &&
(parsed_query.operator === 'AND')) {
// portal_type:"Person" AND local_roles:"Assignee"
for (i = 0; i < parsed_query.query_list.length; i += 1) {
sub_query = parsed_query.query_list[i];
result_list = isSingleLocalRoles(sub_query);
if (result_list) {
local_roles = result_list;
parsed_query.query_list.splice(i, 1);
query = Query.objectToSearchText(parsed_query);
i = parsed_query.query_list.length;
} else {
result_list = isMultipleLocalRoles(sub_query);
if (result_list) {
local_roles = result_list;
parsed_query.query_list.splice(i, 1);
query = Query.objectToSearchText(parsed_query);
i = parsed_query.query_list.length;
}
}
}
}
}
option_dict.query = query;
option_dict.local_roles = local_roles;
}
if (option_dict.sort_on) {
for (i = 0; i < option_dict.sort_on.length; i += 1) {
tmp_list.push(JSON.stringify(option_dict.sort_on[i]));
}
option_dict.sort_on = tmp_list;
}
return wrapJioCall(
this,
'getAttachment',
[
// XXX Ugly hardcoded meaningless id...
"erp5",
new UriTemplate.parse(option_dict.list_method_template)
.expand(option_dict),
{format: "json"}
]
)
.push(function (catalog_json) {
var data = catalog_json._embedded.contents,
count = data.length,
k,
uri,
item,
result = [];
for (k = 0; k < count; k += 1) {
item = data[k];
uri = new URI(item._links.self.href);
delete item._links;
result.push({
id: uri.segment(2),
doc: {},
value: item
});
}
return {
data: {
rows: result,
total_rows: result.length
}
};
});
})
.declareMethod('get', function (id) {
return wrapJioCall(this, 'get', [id]);
})
.declareMethod('getAttachment', function (id, name) {
return wrapJioCall(this, 'getAttachment', [id, name, {format: "json"}]);
})
.declareMethod('putAttachment', function (id, name, json) {
return wrapJioCall(this, 'putAttachment', [id, name, JSON.stringify(json)]);
})
.declareMethod('put', function (key, doc) {
return wrapJioCall(this, 'put', [key, doc]);
})
.declareMethod('post', function (doc) {
return wrapJioCall(this, 'post', [doc]);
});
}(window, rJS, RSVP, UriTemplate, URI, Query, SimpleQuery, ComplexQuery, jIO));
\ No newline at end of file
......@@ -73,9 +73,10 @@
"editable": 1,
"editable_column_list": [],
"key": "field_listbox",
"lines": 3,
"lines": 15,
"list_method": "portal_catalog",
"query": "urn:jio:allDocs?query=portal_type%3A%22Data+Array%22+AND+validation_state%3A%22validated%22",
//"query": "urn:jio:allDocs?query=portal_type%3A%22Data+Array%22+AND+validation_state%3A%22validated%22",
"query": "urn:jio:allDocs?query=portal_type%3A%22Data+Array%22",
/*
query: 'portal_type:"Data Array"' +
' AND validation_state:"validated"',
......
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>958.41691.17449.16640</string> </value>
<value> <string>958.42869.59925.11741</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1492006762.01</float>
<float>1492077486.56</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -349,7 +349,7 @@
</item>
<item>
<key> <string>configuration_jio_gadget_url</string> </key>
<value> <string>gadget_erp5_jio.html</string> </value>
<value> <string>gadget_erp5_custom_jio.html</string> </value>
</item>
<item>
<key> <string>configuration_panel_gadget_url</string> </key>
......@@ -602,7 +602,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>958.41308.14150.37444</string> </value>
<value> <string>958.42840.36773.38502</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -620,7 +620,7 @@
</tuple>
<state>
<tuple>
<float>1491983934.67</float>
<float>1492075822.42</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