Commit c8c2474f authored by Sven Franck's avatar Sven Franck

offline work-in-progress commit

parent 2a7c34a3
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
{"field": "total_price", "show": true, "persist": true, "sort":false}, {"field": "total_price", "show": true, "persist": true, "sort":false},
{"field": "price_currency", "show": "true", "priority": 4, "merge": "total_price"}, {"field": "price_currency", "show": "true", "priority": 4, "merge": "total_price"},
{"field": "translated_simulation_state_title", "show":"true", "priority": 5}, {"field": "translated_simulation_state_title", "show":"true", "priority": 5},
{"custom": true, "show": true, "persist": true, "sort": false, "action_list": [{"class_list": "action translate", "action": "download", "icon":"file-alt", "text": "Download", "text_i18n":"portable_type_dict.invoice_dict.download"}]} {"custom": true, "show": true, "persist": true, "sort": false, "action_list": [{"class_list": "action translate", "action": "get", "icon":"file-alt", "text": "Download", "text_i18n":"portable_type_dict.invoice_dict.download"}]}
] ]
} }
], ],
......
...@@ -769,8 +769,8 @@ ...@@ -769,8 +769,8 @@
map.actions = { map.actions = {
/** /**
* Generate a new object * POST an object
* @method install * @method new
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"new": function (obj) { "new": function (obj) {
...@@ -778,14 +778,23 @@ ...@@ -778,14 +778,23 @@
}, },
/** /**
* Update an object * PUT an object
* @method submit * @method update
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"update": function (obj) { "update": function (obj) {
storage.write(obj); storage.write(obj);
}, },
/**
* GET an object
* @method get
* @param {object} obj Action Object
**/
"get": function (obj) {
storage.fetch(obj);
},
/** /**
* Create an installation = new service object * Create an installation = new service object
* @method install * @method install
...@@ -1095,31 +1104,6 @@ ...@@ -1095,31 +1104,6 @@
.fail(util.error); .fail(util.error);
}, },
/**
* Download a document as pdf
* @method download
* @param {string} type
* @param {string} portal type
* @param {string} identifier
**/
"download": function (obj) {
var store;
store = app.storage_dict;
util.loader("", "status_dict.downloading");
store.items.get({"_id": window.decodeURIComponent(obj.element.href.split("/").pop())},{"_view": "web_view"})
.then(function(response) {
util.loader("", "status_dict.rendering");
window.location.href = util.parse(response).data._links.slapos_jump.href;
})
.fail(function (error){
util.error(error);
util.loader("", "status_dict.failed", "ban-circle");
});
},
// ============================================================== // ==============================================================
/** /**
...@@ -1644,7 +1628,6 @@ ...@@ -1644,7 +1628,6 @@
// loop data (!) // loop data (!)
for (k = 0; k < items.length; k += 1) { for (k = 0; k < items.length; k += 1) {
item = items[k].doc; item = items[k].doc;
if (item === undefined) { if (item === undefined) {
util.error("mapTableRows: Missing include_docs/data-set"); util.error("mapTableRows: Missing include_docs/data-set");
...@@ -1659,7 +1642,6 @@ ...@@ -1659,7 +1642,6 @@
// set id so it can be retrieved for radio/checkbox // set id so it can be retrieved for radio/checkbox
// TODO: remove this! don't set on every cell... // TODO: remove this! don't set on every cell...
cell.id = item.id || items[k]["_id"]; cell.id = item.id || items[k]["_id"];
if (props.link) { if (props.link) {
cell.href = item[props.link_source] || cell.href = item[props.link_source] ||
("#" + core + "/" + window.encodeURIComponent(item[props.link_identifier || "id"])); ("#" + core + "/" + window.encodeURIComponent(item[props.link_identifier || "id"]));
...@@ -1671,10 +1653,10 @@ ...@@ -1671,10 +1653,10 @@
// action menu // action menu
if (props.action_menu && cell.action_list) { if (props.action_menu && cell.action_list) {
// HACK to add correct HREF and reference // HACK to add correct HREF and reference
if (item._relative_url) { if (item._id) {
for (o = 0; o < cell.action_list.length; o += 1) { for (o = 0; o < cell.action_list.length; o += 1) {
cell.action_list[o].reference = spec.set_id; cell.action_list[o].reference = spec.set_id;
cell.action_list[o].href = window.encodeURIComponent(item._relative_url); cell.action_list[o].href = window.encodeURIComponent(item._id);
} }
} }
cell.action_list = cell.action_list || []; cell.action_list = cell.action_list || [];
...@@ -5231,6 +5213,111 @@ ...@@ -5231,6 +5213,111 @@
// generate storage object // generate storage object
storage = {}; storage = {};
/*
if (store.property_dict.storage) {
// access storage
fetch = reply.pass.value || reply.pass.state.query.force;
return store.items.get({"_id": window.decodeURIComponent(fetch)})
.then(function(answer) {
query = util.parse(answer).data._links.slapos_jump._query;
if (pass.config.initial_query === undefined) {
pass.config.initial_query = {};
}
pass.config.initial_query.query = query;
pass.config.initial_query.skip_type = true;
pass.preserve_lookup = reply.pass.value;
// set a flag to now run allDocs
pass.force_allDocs = true;
// pass hacked query back into chain
if (reply.response) {
return {
"response": util.parse(reply.response),
"pass": pass
};
}
return {
"pass": pass
}
}).fail(util.error);
}
return (reply);
*/
/**
* Fetch data from storage = GET or ALLDOCS
* @method fetch
* @param {object} obj Action object
*/
// TODO: only use on lookup architecture pass.x or pass.state.x
// TODO: this means state needs to have url_pointer set earilier, so
// there is no need to look it up in oass.config.property_dict...
storage.fetch = function (obj) {
var pass, query, method, map, config, answer, lookup, pointer, action,
quirk;
pass = obj.pass;
quirk = obj.state || pass.config;
lookup = obj.state || quirk.property_dict || {};
query = obj.query || lookup.query || {};
pointer = (lookup["url_pointer"] || {}).jump;
// TODO: action handling, should be a form submit not set on element!
action = ((obj.element || {}).href || "").split("/").pop();
query._id = query._id || window.decodeURIComponent(action);
if (action) {
pointer = undefined;
query = {"_id": query._id}
}
switch (true) {
case ((query.select_list || []).length > 0 && !query.include_docs):
map = "values";
break;
case (!!query._id):
map = "single_item";
method = "get";
config = {
"_view": quirk.view
};
break;
}
// TODO: remove this - pass custom query to storage
if (pointer && !config) {
config = {
"_jump": pointer
};
}
return RSVP.resolve(
app.storage_dict.items[method || "allDocs"](query, config)
).then(function (response) {
if (response.status === 200) {
// TODO: actions should have their own callback set on state!
if (action) {
util.loader("", "status_dict.rendering");
window.location.href = response.data._links.slapos_jump.href;
}
answer = storage.mapResponse(response, map);
if (app.storage_dict.property_dict.force_sync) {
//storage.sync(mapped, parcel.storage);
}
}
return {
"response": answer || response,
"pass": pass
};
}).fail(util.error);
},
/** /**
* Write to storage = POST or PUT * Write to storage = POST or PUT
* @method write * @method write
...@@ -5244,23 +5331,23 @@ ...@@ -5244,23 +5331,23 @@
valid = storage.validate(obj); valid = storage.validate(obj);
data = storage.parseForm(valid, prefix, true); data = storage.parseForm(valid, prefix, true);
// post or put
if (form.identifier) { if (form.identifier) {
// this must be a PUT, set id, method and view
data._id = form.identifier.value;
method = "put"; method = "put";
config = { config = {
"_view": obj.state.view "_view": obj.state.view
}; };
data._id = form.identifier.value;
} else { } else {
// POSTING, set action set in JSON config
config = { config = {
"action": obj.state.url_pointer[obj.element.getAttribute("data-action")] "action": obj.state.url_pointer[obj.element.getAttribute("data-action")]
}; };
} }
RSVP.resolve( RSVP.resolve(app.storage_dict.items[method || "post"](data, config))
app.storage_dict.items[method || "post"](data, config)
)
.then(function (response) { .then(function (response) {
// TODO: if this is strictly a storage operation, do this elsewhere!
if (response.status === 201) { if (response.status === 201) {
util.loader("", "status_dict.saved", "check"); util.loader("", "status_dict.saved", "check");
if (obj.state.callback) { if (obj.state.callback) {
...@@ -5411,6 +5498,7 @@ ...@@ -5411,6 +5498,7 @@
storage.mapResponse = function (response, convert) { storage.mapResponse = function (response, convert) {
var answer, i, record, key, data, pushit, value_dict; var answer, i, record, key, data, pushit, value_dict;
if (convert) {
answer = {"data": {"total_rows": null, "rows": []}}; answer = {"data": {"total_rows": null, "rows": []}};
pushit = function (obj, record) { pushit = function (obj, record) {
var new_record = { var new_record = {
...@@ -5447,7 +5535,8 @@ ...@@ -5447,7 +5535,8 @@
answer.data.rows.push(pushit(data, record)); answer.data.rows.push(pushit(data, record));
} }
} }
return answer; }
return answer || response;
}; };
/** /**
...@@ -5746,7 +5835,7 @@ ...@@ -5746,7 +5835,7 @@
// ); // );
} }
return app.fetchData({ return storage.fetch({
"storage": "items", "storage": "items",
"query": answer.query, "query": answer.query,
"pass": {"profile": answer.profile} "pass": {"profile": answer.profile}
...@@ -6815,7 +6904,7 @@ ...@@ -6815,7 +6904,7 @@
* @param {object} reply Response from previous promise and pass-params * @param {object} reply Response from previous promise and pass-params
* @return {object} response object/promise * @return {object} response object/promise
*/ */
app.fetchDataQuery = function (reply) { storage.fetchQuery = function (reply) {
var parsed, pass = reply.pass, key, value, force_fields; var parsed, pass = reply.pass, key, value, force_fields;
force_fields = app.storage_dict.property_dict.force_field_definitions; force_fields = app.storage_dict.property_dict.force_field_definitions;
...@@ -6864,7 +6953,7 @@ ...@@ -6864,7 +6953,7 @@
// get an item? // get an item?
if (pass.mode !== "new" || pass.config.initial_query !== undefined) { if (pass.mode !== "new" || pass.config.initial_query !== undefined) {
return app.fetchData({ return storage.fetch({
"storage": "items", "storage": "items",
"query": pass.state.query, "query": pass.state.query,
"pass": pass "pass": pass
...@@ -6882,7 +6971,7 @@ ...@@ -6882,7 +6971,7 @@
* @param {object} reply Response from previous promise and pass-params * @param {object} reply Response from previous promise and pass-params
* @return {object} response object/promise * @return {object} response object/promise
*/ */
app.fetchDataTotal = function (reply) { storage.fetchTotal = function (reply) {
var pass = reply.pass, last; var pass = reply.pass, last;
if (pass.skip === undefined) { if (pass.skip === undefined) {
...@@ -6932,6 +7021,8 @@ ...@@ -6932,6 +7021,8 @@
} }
} }
if (!app.storage_dict.property_dict.skip_total_records) {
if (pass.skip === undefined && if (pass.skip === undefined &&
(pass.no_auth || ((pass.auth && pass.active_login) || (pass.no_auth || ((pass.auth && pass.active_login) ||
(pass.mode === "new" || pass.purge))) && (pass.mode === "new" || pass.purge))) &&
...@@ -6945,13 +7036,14 @@ ...@@ -6945,13 +7036,14 @@
// store the complete initial query, not only the "query" // store the complete initial query, not only the "query"
pass.state.initial_query = pass.config.initial_query; pass.state.initial_query = pass.config.initial_query;
return app.fetchData({ return storage.fetch({
"pass": pass, "pass": pass,
"storage": "items", "storage": "items",
"query": pass.state.query "query": pass.state.query
}); });
} }
} }
}
return { return {
"pass": pass "pass": pass
}; };
...@@ -7094,7 +7186,7 @@ ...@@ -7094,7 +7186,7 @@
* @param {object} reply Response from previous promise and pass-params * @param {object} reply Response from previous promise and pass-params
* @return {object} response object/promise * @return {object} response object/promise
*/ */
app.fetchDataSample = function (reply) { storage.fetchSample = function (reply) {
var pass = reply.pass; var pass = reply.pass;
if (reply.response) { if (reply.response) {
...@@ -7134,7 +7226,7 @@ ...@@ -7134,7 +7226,7 @@
// try to get 1 record for this portal type // try to get 1 record for this portal type
if ((pass.create !== false || pass.purge) && pass.config.initial_query && if ((pass.create !== false || pass.purge) && pass.config.initial_query &&
(pass.no_auth || (pass.auth && pass.active_login))) { (pass.no_auth || (pass.auth && pass.active_login))) {
return app.fetchData({ return storage.fetch({
"storage": "items", "storage": "items",
"query": app.generateQueryObject({"limit": [0, 1]}, pass.type), "query": app.generateQueryObject({"limit": [0, 1]}, pass.type),
"pass": pass "pass": pass
...@@ -7291,8 +7383,8 @@ ...@@ -7291,8 +7383,8 @@
.then(app.checkAuth) .then(app.checkAuth)
.then(app.fetchFields) .then(app.fetchFields)
.then(app.store) .then(app.store)
.then(app.fetchDataTotal) .then(storage.fetchTotal)
.then(app.fetchDataQuery) .then(storage.fetchQuery)
.then(app.makeContent) .then(app.makeContent)
.fail(util.error); .fail(util.error);
...@@ -7368,67 +7460,67 @@ ...@@ -7368,67 +7460,67 @@
); );
}; };
/** // /**
* Try fetching data from JIO. Fallback to loading fake data until accessible // * Try fetching data from JIO. Fallback to loading fake data until accessible
* @method fetchData // * @method fetchData
* @param {object} parcel Storage, query options and pass to return // * @param {object} parcel Storage, query options and pass to return
* @return {object} promise object/pass // * @return {object} promise object/pass
*/ // */
app.fetchData = function (parcel) { // storage.fetch = function (parcel) {
var method, convert, select_list, hacked_view, pass, skip, query, mapped; // var method, convert, select_list, hacked_view, pass, skip, query, mapped;
//
pass = parcel.pass; // pass = parcel.pass;
query = parcel.query; // query = parcel.query;
skip = query && query.limit && query.limit.length === 0 && // skip = query && query.limit && query.limit.length === 0 &&
app.storage_dict.property_dict.skip_total_records; // app.storage_dict.property_dict.skip_total_records;
//
//
// return here, if skipping total query // // return here, if skipping total query
if (skip) { // if (skip) {
return { // return {
"pass": parcel.pass // "pass": parcel.pass
}; // };
} // }
//
select_list = parcel.query && parcel.query.select_list && parcel.query.select_list.length; // select_list = parcel.query && parcel.query.select_list && parcel.query.select_list.length;
//
// single item query GET // // single item query GET
if (parcel.query._id) { // if (parcel.query._id) {
// TODO: don't set if don't need... // // TODO: don't set if don't need...
delete parcel.query.limit; // delete parcel.query.limit;
hacked_view = {"_view": parcel.pass.config.view} // hacked_view = {"_view": parcel.pass.config.view}
method = "get"; // method = "get";
convert = "single_item"; // convert = "single_item";
} // }
//
// query that will return value {} object vs. doc // // query that will return value {} object vs. doc
if (select_list && parcel.query.include_docs === undefined) { // if (select_list && parcel.query.include_docs === undefined) {
convert = "values"; // convert = "values";
} // }
//
return app.storage_dict[parcel.storage][method || "allDocs"](parcel.query, hacked_view) // return app.storage_dict[parcel.storage][method || "allDocs"](parcel.query, hacked_view)
.then(function (response) { // .then(function (response) {
//
// TODO: best way? // // TODO: best way?
if (convert !== undefined && response.status === 200) { // if (convert !== undefined && response.status === 200) {
mapped = storage.mapResponse(response, convert); // mapped = storage.mapResponse(response, convert);
//
// force sync // // force sync
if (app.storage_dict.property_dict.force_sync) { // if (app.storage_dict.property_dict.force_sync) {
//storage.sync(mapped, parcel.storage); // //storage.sync(mapped, parcel.storage);
} // }
//
return { // return {
"response": mapped, // "response": mapped,
"pass": parcel.pass // "pass": parcel.pass
}; // };
} // }
return { // return {
"response": response, // "response": response,
"pass": parcel.pass // "pass": parcel.pass
}; // };
}); // });
}; // };
/** /**
* Load files from disk * Load files from disk
......
...@@ -23,11 +23,10 @@ ...@@ -23,11 +23,10 @@
this._url = spec.url; this._url = spec.url;
} }
ERP5Storage.prototype._getPrivates = function (site_hal, param, options) { ERP5Storage.prototype._getFile = function (command, site_hal, param, options) {
var fetch, item = (param || {})._id || (site_hal._links.me || {}).href; var fetch, item = (param || {})._id || (site_hal._links.me || {}).href;
if (!item) { if (!item) {
return command.error(401); command.error(401);
} }
fetch = new URI(item); fetch = new URI(item);
return this._getSiteDocument( return this._getSiteDocument(
...@@ -38,7 +37,6 @@ ...@@ -38,7 +37,6 @@
ERP5Storage.prototype._getSiteDocument = function (traverse, expando) { ERP5Storage.prototype._getSiteDocument = function (traverse, expando) {
var url = UriTemplate.parse(traverse || "").expand(expando || {}); var url = UriTemplate.parse(traverse || "").expand(expando || {});
console.log(url);
return jIO.util.ajax({ return jIO.util.ajax({
"type": "GET", "type": "GET",
"url": url || this._url, "url": url || this._url,
...@@ -46,29 +44,24 @@ ...@@ -46,29 +44,24 @@
"withCredentials": true "withCredentials": true
} }
}).then(function (response) { }).then(function (response) {
var result = JSON.parse(response.target.responseText); return JSON.parse(response.target.responseText);
// result._id = param._id;
return result;
}); });
}; };
ERP5Storage.prototype._get = function (param, options) { ERP5Storage.prototype._get = function (command, param, options) {
var that = this; var that = this;
return that._getSiteDocument() return that._getSiteDocument()
.then(function (site_hal) { .then(function (site_hal) {
return that._getPrivates(site_hal, param, options); return that._getFile(command, site_hal, param, options);
}) })
.then(function (response) { .then(function (response) {
console.log("GET, response") response._id = param._id;
console.log(response) return response;
var result = JSON.parse(response.target.responseText);
result._id = param._id;
return result;
}); });
}; };
ERP5Storage.prototype.get = function (command, param, options) { ERP5Storage.prototype.get = function (command, param, options) {
this._get(param, options) this._get(command, param, options)
.then(function (response) { .then(function (response) {
command.success({"data": response}); command.success({"data": response});
}) })
...@@ -87,7 +80,7 @@ ...@@ -87,7 +80,7 @@
var that = this; var that = this;
return that._getSiteDocument() return that._getSiteDocument()
.then(function (site_hal) { .then(function (site_hal) {
return that._getPrivates(site_hal, undefined, options); return that._getFile(command, site_hal, undefined, options);
}) })
.then(function(opts) { .then(function(opts) {
var key, custom_action = opts._actions[options.action], var key, custom_action = opts._actions[options.action],
...@@ -164,28 +157,39 @@ ...@@ -164,28 +157,39 @@
"Unable to call put" "Unable to call put"
); );
}); });
}; };
ERP5Storage.prototype.allDocs = function (command, param, options) { ERP5Storage.prototype.allDocs = function (command, param, options) {
var that = this, search_pointer;
return this._getSiteDocument() return this._getSiteDocument()
.then(function (site_hal) { .then(function (site_hal) {
return jIO.util.ajax({ search_pointer = site_hal._links.raw_search.href;
"type": "GET", return that._getFile(command, site_hal, undefined, options);
"url": UriTemplate.parse(site_hal._links.raw_search.href) })
.expand({ .then(function (opts) {
query: options.query, var i, len, jump_list, jump;
// HACK: add ERP5 custom jump
if (param._jump && options.query) {
jump_list = opts._links["slapos_jump" || []];
len = jump_list.length;
for (i = 0; i < len; i += 1) {
jump = jump_list[i];
if (jump.name === param._jump) {
options.query = jump._query;
}
}
}
return that._getSiteDocument(
search_pointer,
{
"query": options.query,
// XXX Force erp5 to return embedded document // XXX Force erp5 to return embedded document
select_list: options.select_list || ["title", "reference"], "select_list": options.select_list || ["title", "reference"],
limit: options.limit "limit": options.limit
}),
"xhrFields": {
withCredentials: true
} }
}); );
})
.then(function (response) {
return JSON.parse(response.target.responseText);
}) })
.then(function (catalog_json) { .then(function (catalog_json) {
var data = catalog_json._embedded.contents, var data = catalog_json._embedded.contents,
......
...@@ -251,7 +251,6 @@ ...@@ -251,7 +251,6 @@
var length = promise_list.length; var length = promise_list.length;
promise_list = promise_list.slice(); promise_list = promise_list.slice();
return new Promise(function (resolve, reject, notify) { return new Promise(function (resolve, reject, notify) {
var index, trace, conflict_list, count, error_count, tag, doc; var index, trace, conflict_list, count, error_count, tag, doc;
...@@ -344,19 +343,20 @@ ...@@ -344,19 +343,20 @@
sequence([ sequence([
function () { function () {
// master storage
if (master !== undefined) { if (master !== undefined) {
return last(priority_list, master); return last(priority_list, master);
} }
return {}; return {};
}, },
function (response) { function (response) {
// slave storages
var method, subindex = 0; var method, subindex = 0;
// this is the id issued by the master storage
if (response.id) { if (response.id) {
metadata._id = response.id; metadata._id = response.id;
method = "put"; method = "put";
} else { } else {
// TODO: error/offline handling // TODO: master 404/offline handling
} }
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
...@@ -454,7 +454,7 @@ ...@@ -454,7 +454,7 @@
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = promise_list[index] =
success(command.storage(this._storage_list[index]).allDocs(option)); success(command.storage(this._storage_list[index]).allDocs(option, param));
} }
sequence([function () { sequence([function () {
...@@ -463,10 +463,10 @@ ...@@ -463,10 +463,10 @@
var i, j, k, l, record, reply, rows, count, total, base, test, must_add, var i, j, k, l, record, reply, rows, count, total, base, test, must_add,
len = answers.length; len = answers.length;
// loop storage response
// NOTE: every storage may return different records! // NOTE: every storage may return different records!
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
reply = answers[i]; reply = answers[i];
if (reply.result === "success") {
total = reply.data.total_rows; total = reply.data.total_rows;
// loop records returned in response // loop records returned in response
...@@ -509,6 +509,7 @@ ...@@ -509,6 +509,7 @@
} }
} }
} }
}
// if a storage returns rows = 0, make sure all records are labelled // if a storage returns rows = 0, make sure all records are labelled
if (total === 0) { if (total === 0) {
...@@ -517,7 +518,6 @@ ...@@ -517,7 +518,6 @@
rows[l]._missing = true; rows[l]._missing = true;
} }
} }
return {"data": {"total_rows": (rows || []).length, "rows": rows || []}}; return {"data": {"total_rows": (rows || []).length, "rows": rows || []}};
}, [command.success, command.error]]); }, [command.success, command.error]]);
}; };
......
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