Commit ba9d8e80 authored by Sven Franck's avatar Sven Franck

app: added generic subordinate and random dynamic value injection

parent 1c9486b4
......@@ -3340,7 +3340,13 @@
"count": i
}));
}
// NOTE: when loading content via href, element is set to undefined
// TODO: Why? ^^^^^^
// NOTE: fetch subordinate fields
if (element && element.needs_subordination) {
return app.util.setSubordinate(element, wrapper);
}
return RSVP.resolve(element || wrapper.inherit);
};
......@@ -3356,6 +3362,7 @@
// TODO: multiple header rows!
// TODO: action_menu input + form?
// TODO: how to handle translations of record texts?
// TODO: subordinate handling
map.tableItem = function (spec) {
var j, k, segment, row, cell, id, quirk_dict, record, new_item, item;
......@@ -3504,9 +3511,6 @@
obj[setter] = response.value;
new_item[pos].push(util.mergeObject(response.field, obj));
}
// clean up on success and error!
delete flux.active_record[response.relation];
return new_item;
})
.fail(app.util.error);
......@@ -3523,6 +3527,7 @@
**/
// TODO: should a span still have full structure (label/container)?
// TODO: should we wrap in span_12 here. why do we wrap at all, do with CSS
// TODO: subordinate value
map.formItem = function (spec) {
var helper, getHelp, j, k, segment, section, field, setValue, root, veto,
textarea_value, override_value, value, field_value, validation_list,
......@@ -3958,11 +3963,9 @@
storage.subordinate = function (spec) {
var output;
// cached promise
// fetch from cache or query
if (flux.active_record[spec.relation]) {
output = flux.active_record[spec.relation];
// query and cache
} else {
output = flux.active_record[spec.relation] = storage.fetch({
"pass": spec,
......@@ -3971,15 +3974,15 @@
}
return output.then(function (answer) {
var data, doc;
data = answer.response.data;
var data = answer.response.data;
// set subordinate value
if (data.total_rows > 0) {
spec.value = data.rows[0].doc[spec.subordinate]
}
// NOTE: delete here, otherwise impossible to track. This will still
// work cause subsequent requests will fire async and be set from flux
delete flux.active_record[spec.relation];
return spec;
}).fail(app.util.error);
......@@ -6143,9 +6146,10 @@
// TODO: refactor... appocalyptic
app.content.make = function (reply) {
var pass, method, type, kids, promise_list, route,
search, search_id, kid, is_html, is_dynamic, is_id, i, j, last,
search, search_id, kid, is_html, is_dynamic, is_id, i, j, k, last,
encoded, wrapper, active, selector, update_target, target, widget,
grant_child, quirk_dict, generator, pointer_results, data_total_rows;
quirk_dict, generator, pointer_results, data_total_rows, is_parameter,
param_len, parameter;
pass = reply.pass;
pass.config_dict = pass.config_dict || {};
......@@ -6347,6 +6351,7 @@
// we don't need is_dynamic!
is_dynamic = kid.property_dict && kid.property_dict.dynamic;
is_html = kid.direct;
is_parameter = (kid.logic || {}).setParam;
// inherit item id to child elements (see above)
......@@ -6368,26 +6373,23 @@
kid.property_dict.input = "#" + search_id;
}
// inherit properties of parent (> dynamic!) and add data to kid
kid.property_dict = util.mergeObject(kid.property_dict, quirk_dict);
kid.children = util.inherit(
pointer_results.data.rows.concat(kid.children || []),
{"scheme": quirk_dict.scheme, "field_dict": quirk_dict.field_dict}
);
}
// map on kid directly and inherit data down (only single items)
if (quirk_dict.direct_map) {
kid.property_dict.data = pointer_results.data.rows[0];
// HACK for setParam in login window
if (kid.logic && kid.logic.setParam) {
kid.direct.href += kid.direct.href.indexOf("?") > 0 ? "&" : "?";
switch (kid.logic.setParam[1]) {
case "location":
target = window.location.origin +
window.location.pathname +
window.location.hash.split("?")[0];
break;
// set "grant-children", which will be mapped into ?
} else {
kid.property_dict = util.mergeObject(kid.property_dict, quirk_dict);
kid.children = util.inherit(
pointer_results.data.rows.concat(kid.children || []),
{"scheme": quirk_dict.scheme, "field_dict": quirk_dict.field_dict}
);
}
kid.direct.href += window.encodeURIComponent(kid.logic.setParam[0]) +
"=" + window.encodeURIComponent(target);
}
// setParam(s)
if (is_parameter) {
kid = app.util.setParam(kid, wrapper);
}
if (quirk_dict.update !== true || is_dynamic || quirk_dict.dynamic) {
......@@ -6589,6 +6591,91 @@
/* ********************************************************************** */
app.util = {};
/** Fetch a subordinate record to set values of an object
* @method setSubordinate
* @method setParam
* @param {object} kid Element configuration object
* @param {object} wrapper Wrapping document
* @returns {object} kid
*/
// TODO: change setParam into scheme like structure to use same handler?
app.util.setSubordinate = function (element, wrapper) {
var i, len, param_list, param, promise_list, field, pass;
param_list = element.logic.setParam;
promise_list = [];
for (i = 0, len = param_list.length; i < len; i += 1) {
param = param_list[i];
// NOTE: all regular params should be deleted by now!
field = param[1];
pass = {
"field": field,
"relation": wrapper.property_dict.data.doc[field],
"subordinate": field.split("subordinate_")[1]
};
promise_list[i] = storage.subordinate(pass);
}
return RSVP.all(promise_list)
.then(function (response_list) {
var j, k, res_len, response, param_len, out_param;
// TODO: CUSTOM CODE, wrap in callback and make generic handler
for (j = 0, res_len = response_list.length; j < res_len; j += 1) {
response = response_list[j];
for (k = 0, param_len = param_list.length; k < param_len; k += 1) {
out_param = param_list[k];
if (response.field === out_param[1]) {
element.logic[out_param[0]] = response.value;
}
}
}
return element;
})
.fail(app.util.error);
};
/** Set parameters on an object or flag object for subordination =
* fetch related record to get value
* @method setParam
* @param {object} kid Element configuration object
* @param {object} wrapper Wrapping document
* @returns {object} kid
*/
app.util.setParam = function (kid, wrapper) {
var i, len, param_list, param, href, loc, sep;
param_list = kid.logic.setParam;
for (i = 0, len = param_list.length; i < len; i += 1) {
param = param_list[i];
switch (param[1]) {
// NOTE: hacked for oauth redirect url TODO: remove, not generic!
case "location":
href = kid.direct.href;
sep = href.indexOf("?") > 0 ? "&" : "?";
loc = window.location;
kid.direct.href += sep + window.encodeURIComponent(param[0]) + "=" +
window.encodeURIComponent(
loc.origin + loc.pathname + loc.hash.split("?")[0]
);
break;
// set values or flag for subordination inside map.element
default:
if (param[1].split("subordinate_").length > 1) {
kid.needs_subordination = true;
} else {
kid.logic[param[0]] = wrapper.property_dict.data.doc[param[1]];
delete kid.logic.setParam[i];
}
}
}
return kid;
};
/**
* Parse a clicked link to determine which page to load
* @method parseLink
......
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