Commit 9b71276e authored by Sven Franck's avatar Sven Franck

updated application handler to working without hacks

parent e374a231
......@@ -538,92 +538,6 @@
**/
map.handlers = {
/**
* @method release_list
* @param {object} reply Object received from previous chain element
* @param {object} response Object object passed on to next element
**/
"installed_services": function (reply) {
var config, property, query, fetch, href, store, pass = reply.pass;
store = app.storage_dict;
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);
},
/**
* @method release_list
* @param {object} reply Object received from previous chain element
* @param {object} response Object object passed on to next element
**/
"release_list": function (reply) {
var config, property, query, fetch, href, store, pass = reply.pass;
store = app.storage_dict;
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);
},
/**
* @method software_list
* @param {object} reply Object received from previous chain element
......@@ -716,50 +630,7 @@
}).fail(util.error);
}
return (reply);
},
/**
* @method ticket_status
* @param {object} reply Object received from previous chain element
* @param {object} response Object object passed on to next element
**/
"ticket_status": function (reply) {
var fetch, config, property, query, href, i, store, reply, pass = reply.pass;
store = app.storage_dict;
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);
},
}
};
/**
......@@ -774,7 +645,17 @@
* @param {object} obj Action Object
**/
"new": function (obj) {
storage.write(obj);
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.saved", "check");
app.navigate(obj, response);
})
.fail(function (error) {
switch (error.status) {
case 408: util.loader("", "status_dict.timeout", "time"); break;
default: util.loader("", "status_dict.error", "ban-circle"); break;
}
});
},
/**
......@@ -783,303 +664,114 @@
* @param {object} obj Action Object
**/
"update": function (obj) {
storage.write(obj);
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.saved", "check");
app.navigate(obj, response);
})
.fail(function (error) {
switch (error.status) {
case 408: util.loader("", "status_dict.timeout", "time"); break;
default: util.loader("", "status_dict.error", "ban-circle"); break;
}
});
},
/**
* GET an object
* @method get
* update server scope - custom action, because my_ not implemented,
* same for ticket status updates
* @method update
* @param {object} obj Action Object
**/
"get": function (obj) {
storage.fetch(obj);
"update_scope": function (obj) {
obj.force_data = true;
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.saved", "check");
app.navigate(obj, response);
})
.fail(function (error) {
switch (error.status) {
case 408: util.loader("", "status_dict.timeout", "time"); break;
default: util.loader("", "status_dict.error", "ban-circle"); break;
}
});
},
/**
* Create an installation = new service object
* Install a service on a machine
* @method install
* @param {object} obj Action Object
**/
"install": function (obj) {
// force new portal type and POST (create new record)
obj.gadget.state.force_type = "Service";
obj.gadget.state.create_new = true;
storage.add(obj);
},
/**
* Destroy an installation = service object
* @method destroy
* @param {object} obj Action Object
**/
"destroy": function (obj) {
storage.remove(obj);
},
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"update_ticket": function (obj) {
var element = obj.element, formData, valid, replace, property, value, id,
decode, store;
id = obj.state.fragment_list[1];
formData = new FormData();
store = app.storage_dict;
valid = storage.validate(obj.gadget);
decode = /^[^\/]*%2[^\/]*$/.test(id);
if (valid === undefined) {
util.loader("", "validation_dict.general", "ban-circle");
// form is valid and not "spam"
} else {
replace = obj.gadget.id + "_";
for (property in valid) {
if (valid.hasOwnProperty(property)) {
value = valid[property];
// prepare to store
// TODO: add id to captcha fields missing it...
if (property !== "undefined") {
if (property !== "identifier") {
formData.append(property.replace(replace, ""), value);
}
}
}
}
util.loader("", "status_dict.updating");
store.items.get({"_id": window.decodeURIComponent(obj.state.fragment_list[1])},{"_view": "web_view"})
.then(function(response) {
return jIO.util.ajax({
"url": util.parse(response).data._actions.update.href,
"type": "POST",
"data": formData,
"xhrFields": {
"withCredentials": true
}
});
})
.then(function (answer) {
util.loader("", "status_dict.success", "check");
$.mobile.changePage("#services");
})
.fail(function(error) {
util.error(error);
if (util.parse(error.target).responseText === "" && id && obj.state.callback) {
$.mobile.changePage(obj.state.callback.replace("__id__", decode ? id : window.encodeURIComponent(id)));
}
});
}
},
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"destroy_installation": function (obj) {
var element = obj.element,
id = obj.state.fragment_list[1],
decode = /^[^\/]*%2[^\/]*$/.test(id);
util.loader("", "status_dict.destroying", "trash");
jIO.util.ajax({
"url": window.decodeURIComponent(element.href),
"type": element.getAttribute("data-method"),
"xhrFields": {
"withCredentials": true
}
})
.then(function (answer) {
util.loader("", "status_dict.success", "check");
$.mobile.changePage("#servers");
})
.fail(function(error) {
util.loader("", "status_dict.success", "check");
util.error(error);
if (util.parse(error.target).responseText === "" && id && obj.state.callback) {
$.mobile.changePage(obj.state.callback.replace("__id__", decode ? id : window.encodeURIComponent(id)));
}
});
},
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"destroy_instance": function (obj) {
var element = obj.element;
util.loader("", "status_dict.destroying", "trash");
jIO.util.ajax({
"url": window.decodeURIComponent(element.href),
"type": element.getAttribute("data-method"),
"xhrFields": {
"withCredentials": true
}
})
.then(function (response) {
util.loader("", "status_dict.success", "check");
$.mobile.changePage("#services");
})
.fail(util.error);
},
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"update_scope": function (obj) {
var element = obj.element, formData, valid, replace, property, value, id,
decode, store;
id = obj.state.fragment_list[1];
formData = new FormData();
store = app.storage_dict;
valid = storage.validate(obj.gadget);
decode = /^[^\/]*%2[^\/]*$/.test(id);
if (valid === undefined) {
util.loader("", "validation_dict.general", "ban-circle");
// form is valid and not "spam"
} else {
replace = obj.gadget.id + "_";
for (property in valid) {
if (valid.hasOwnProperty(property)) {
value = valid[property];
// prepare to store
// TODO: add id to captcha fields missing it...
if (property !== "undefined") {
if (property !== "identifier") {
formData.append(property.replace(replace, ""), value);
}
}
}
}
util.loader("", "status_dict.updating");
store.items.get({"_id": window.decodeURIComponent(obj.state.fragment_list[1])},{"_view": "web_view"})
.then(function(response) {
return jIO.util.ajax({
"url": util.parse(response).data._actions.update_allocation_scope.href,
"type": "POST",
"data": formData,
"xhrFields": {
"withCredentials": true
}
});
})
.then(function (answer) {
util.loader("", "status_dict.success", "check");
$.mobile.changePage("#services");
})
.fail(function(error) {
util.error(error);
if (util.parse(error.target).responseText === "" && id && obj.state.callback) {
$.mobile.changePage(obj.state.callback.replace("__id__", decode ? id : window.encodeURIComponent(id)));
}
});
}
// HACK: no good way to refer to the calling server!
obj.state.reference =
window.decodeURIComponent(obj.state.fragment_list[1]);
storage.write(obj);
},
/**
* Start a software instance
* @method start_instance
* Revoke a SSL certificate - custom action, so extra entry
* @method revoke_ssl
* @param {object} obj Action Object
**/
"request_ssl": function (obj) {
var element, formData, decode, id, store;
id = obj.state.fragment_list[1];
element = obj.element;
store = app.storage_dict;
formData = new FormData();
decode = /^[^\/]*%2[^\/]*$/.test(id);
util.loader("", "status_dict.updating");
store.items.get({"_id": window.decodeURIComponent(obj.state.fragment_list[1])},{"_view": "web_view"})
.then(function(response) {
return jIO.util.ajax({
"url": util.parse(response).data._actions.generate_certificate.href,
"type": "POST",
"data": formData,
"xhrFields": {
"withCredentials": true
}
});
})
.then(function (answer) {
// store answer in flux
flux = flux || {};
flux.cert = util.parse(answer.target.responseText);
if (id && obj.state.callback) {
$.mobile.changePage(obj.state.callback.replace("__id__", decode ? id : window.encodeURIComponent(id)));
}
})
.fail(function (error){
util.error(error);
util.loader("", "status_dict.ssl_error", "ban-circle");
});
},
"revoke_ssl": function (obj) {
var element, formData, decode, id, store;
id = obj.state.fragment_list[1];
element = obj.element;
store = app.storage_dict;
formData = new FormData();
decode = /^[^\/]*%2[^\/]*$/.test(id);
util.loader("", "status_dict.updating");
store.items.get({"_id": window.decodeURIComponent(obj.state.fragment_list[1])},{"_view": "web_view"})
.then(function(response) {
return jIO.util.ajax({
"url": util.parse(response).data._actions.revoke_certificate.href,
"type": "POST",
"data": formData,
"xhrFields": {
"withCredentials": true
}
});
})
.then(function (answer) {
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.ssl_revoked", "check");
})
.fail(function (error){
util.error(error);
.fail(function (error) {
util.loader("", "status_dict.ssl_no", "ban-circle");
});
},
/**
* Request SSL certificate
* @method request_ssl
* @param {object} obj Action Object
**/
"request_ssl": function (obj) {
util.loader("", "status_dict.requesting");
storage.write(obj)
.then(function (response) {
// get certificate!
flux = flux || {};
flux.cert = util.parse(response._temp);
util.loader("", "status_dict.ssl_generated", "check");
app.navigate(obj, response);
})
.fail(function (error) {
util.loader("", "status_dict.ssl_error", "ban-circle");
});
},
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"start_instance": function (obj) {
var element = obj.element;
"destroy": function (obj) {
util.loader("", "status_dict.destroying", "trash");
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.success", "check");
})
.fail(util.error);
},
/**
* Start a software instance
* @method start
* @param {object} obj Action Object
**/
"start": function (obj) {
util.loader("", "status_dict.starting", "signal");
jIO.util.ajax({
"url": window.decodeURIComponent(element.href),
"type": element.getAttribute("data-method"),
"xhrFields": {
"withCredentials": true
}
})
.then(function (response) {
util.loader("", "status_dict.success", "check");
})
.fail(util.error);
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.success", "check");
})
.fail(util.error);
},
/**
......@@ -1087,10 +779,64 @@
* @method stop_instance
* @param {object} obj Action Object
**/
"stop_instance": function (obj) {
var element = obj.element;
"stop": function (obj) {
util.loader("", "status_dict.stopping", "ban-circle");
storage.write(obj)
.then(function (response) {
util.loader("", "status_dict.success", "check");
})
.fail(util.error);
},
/**
* GET an object
* @method get
* @param {object} obj Action Object
**/
"get": function (obj) {
storage.fetch(obj);
},
/**
* Download a file (as pdf)
* @method download
* @param {object} obj Action Object
**/
"download": function (obj) {
util.loader("", "status_dict.downloading");
storage.fetch(obj)
.then(function (response) {
util.loader("", "status_dict.rendering");
window.location.href = response.data._links.slapos_jump.href;
})
.fail(util.error);
},
// ==============================================
// /**
// * Destroy an installation = service object
// * @method destroy
// * @param {object} obj Action Object
// **/
// "destroy": function (obj) {
// storage.remove(obj);
// },
/**
* Start a software instance
* @method start_instance
* @param {object} obj Action Object
**/
"destroy_installation": function (obj) {
var element = obj.element,
id = obj.state.fragment_list[1],
decode = /^[^\/]*%2[^\/]*$/.test(id);
util.loader("", "status_dict.destroying", "trash");
jIO.util.ajax({
"url": window.decodeURIComponent(element.href),
"type": element.getAttribute("data-method"),
......@@ -1098,10 +844,17 @@
"withCredentials": true
}
})
.then(function (response) {
.then(function (answer) {
util.loader("", "status_dict.success", "check");
$.mobile.changePage("#servers");
})
.fail(util.error);
.fail(function(error) {
util.loader("", "status_dict.success", "check");
util.error(error);
if (util.parse(error.target).responseText === "" && id && obj.state.callback) {
$.mobile.changePage(obj.state.callback.replace("__id__", decode ? id : window.encodeURIComponent(id)));
}
});
},
// ==============================================================
......@@ -3330,7 +3083,6 @@
// add to DOM if scoped
if (url_dict.data_url) {
//util.getPanel(url_dict.data_url).appendChild(panel);
return panel;
} else {
document.body.insertBefore(panel, document.body.children[0]);
......@@ -3459,202 +3211,204 @@
);
};
// loop over form layout sections
for (i = 0; i < spec.scheme.length; i += 1) {
segment = spec.scheme[i];
area = segment.position === "center" ? 2 : 1;
container = wrap(area);
if (i === 0) {
// secure form
// TODO: do properly once we have time
if (secure === "default") {
// secure anyway
// TODO: do properly once we have time
if (secure === "default") {
stamp = new Date().getTime();
encode = window.btoa || window.shim.Base64.encode;
// TODO: include client IP in hash
sauce = encode(stamp.toString() + spec.property_dict.secret_hash);
container.appendChild(factory.formElement(
{
"type": "input",
"direct": {
"type": "text",
"value": "",
"className": "secure_form"
},
"attributes": {
"placeholder": "Please do not fill out this field",
"data-enhanced": "true",
"data-i18n": "[placeholder]global.form_helpers.secure_blank"
},
"logic": {
"plain_element": true,
"label": "Please do not fill out this field",
"label_i18n": "global.form_helpers.secure_blank"
}
}
));
container.appendChild(factory.element(
"input",
{
"type": "hidden",
"value": spec.property_dict.secret_hash,
"id": spec.set_id + "_not_a_secret"
},
{
"data-created": stamp
}
));
container.appendChild(factory.formElement(
{
"type": "input",
"direct": {
"type": "text",
"className": "secure_form",
"value": sauce
},
"attributes": {
"placeholder": "Please leave this value unchanged",
"data-enhanced": "true",
"data-i18n": "[placeholder]global.form_helpers.secure_filled"
},
"logic": {
"plain_element": true,
"label": "Please leave this value unchanged",
"label_i18n": "global.form_helpers.secure_filled"
}
}
));
stamp = new Date().getTime();
encode = window.btoa || window.shim.Base64.encode;
// TODO: include client IP in hash
sauce = encode(stamp.toString() + spec.property_dict.secret_hash);
fragment.appendChild(factory.formElement(
{
"type": "input",
"direct": {
"type": "text",
"value": "",
"className": "secure_form"
},
"attributes": {
"placeholder": "Please do not fill out this field",
"data-enhanced": "true",
"data-i18n": "[placeholder]global.form_helpers.secure_blank"
},
"logic": {
"plain_element": true,
"label": "Please do not fill out this field",
"label_i18n": "global.form_helpers.secure_blank"
}
}
));
fragment.appendChild(factory.element(
"input",
{
"type": "hidden",
"value": spec.property_dict.secret_hash,
"id": spec.set_id + "_not_a_secret"
},
{
"data-created": stamp
}
));
fragment.appendChild(factory.formElement(
{
"type": "input",
"direct": {
"type": "text",
"className": "secure_form",
"value": sauce
},
"attributes": {
"placeholder": "Please leave this value unchanged",
"data-enhanced": "true",
"data-i18n": "[placeholder]global.form_helpers.secure_filled"
},
"logic": {
"plain_element": true,
"label": "Please leave this value unchanged",
"label_i18n": "global.form_helpers.secure_filled"
}
}
));
}
// add hidden storage identifier
if (item_id) {
container.appendChild(factory.element(
"input",
{
"type": "hidden",
"value": item_id,
"name": "identifier"
}
));
// add hidden storage identifier
if (item_id) {
fragment.appendChild(factory.element(
"input",
{
"type": "hidden",
"value": item_id,
"name": "identifier"
}
}
));
}
// loop fieldlist of layout section
for (j = 0; j < segment.fieldlist.length; j += 1) {
field = segment.fieldlist[j];
// loop over form layout sections
if (spec.scheme.length > 0) {
for (i = 0; i < spec.scheme.length; i += 1) {
segment = spec.scheme[i];
area = segment.position === "center" ? 2 : 1;
container = wrap(area);
// allow non-fieldlist form elements to be generated
if (field.type) {
input_config = field;
prefixed_name = spec.reference + "_" + field.direct.name || util.uuid();
field.direct.id = field.direct.name = prefixed_name;
setter = input_config.logic ? (input_config.logic.setValue || input_config.logic.setFlux) : undefined;
// loop fieldlist of layout section
for (j = 0; j < segment.fieldlist.length; j += 1) {
field = segment.fieldlist[j];
// make sure value is set correctly
if (input_config.logic && setter) {
setter_list = input_config.logic.setters;
if (input_config.logic.skip === undefined) {
input_config.logic.skip = true;
}
if (setter_list) {
for (l = 0; l < setter_list.length; l += 1) {
input_config.logic[setter_list[l]] = spec.data[setter];
// allow non-fieldlist form elements to be generated
// TODO: can we also generate a field from another portal_type here?
if (field.type) {
input_config = field;
prefixed_name = spec.reference + "_" + field.direct.name || util.uuid();
field.direct.id = field.direct.name = prefixed_name;
setter = input_config.logic ? (input_config.logic.setValue || input_config.logic.setFlux) : undefined;
// make sure value is set correctly
if (input_config.logic && setter) {
setter_list = input_config.logic.setters;
if (input_config.logic.skip === undefined) {
input_config.logic.skip = true;
}
} else {
if (input_config.logic.setFlux) {
input_config.direct.value = (flux && flux.cert) ? flux.cert[setter] : "Certificate is still active, please revoke existing one.";
if (setter_list) {
for (l = 0; l < setter_list.length; l += 1) {
input_config.logic[setter_list[l]] = spec.data[setter];
}
} else {
input_config.direct.value = spec.data[setter] ||
(setter + util.uuid());
if (input_config.logic.setFlux) {
input_config.direct.value = (flux && flux.cert) ? flux.cert[setter] : "Certificate is still active, please revoke existing one.";
} else {
input_config.direct.value = spec.data[setter] ||
(setter + util.uuid());
}
}
}
}
} else {
config = spec.fields[field.field];
overrides = field.overrides;
doc = spec.data || undefined;
// try to set a value
if (doc && field.custom) {
setter = field.setValue || undefined;
} else {
setter = field.field;
}
value = doc ? (doc[setter]) : undefined;
// action menu
if (field.action_list && field.action_list.length) {
for (m = 0; m < field.action_list.length; m += 1) {
button = field.action_list[m];
if (button.setters.length) {
for (n = 0; n < button.setters.length; n += 1) {
field.action_list[m][button.setters[n]] =
util.fetchByArray(doc, button.setValues[n]);
config = spec.fields[field.field];
overrides = field.overrides;
doc = spec.data || undefined;
// try to set a value
if (doc && field.custom) {
setter = field.setValue || undefined;
} else {
setter = field.field;
}
value = doc ? (doc[setter]) : undefined;
// action menu
if (field.action_list && field.action_list.length) {
for (m = 0; m < field.action_list.length; m += 1) {
button = field.action_list[m];
if (button.setters.length) {
for (n = 0; n < button.setters.length; n += 1) {
field.action_list[m][button.setters[n]] =
util.fetchByArray(doc, button.setValues[n]);
}
}
}
}
input_config = factory.util.actionMenu(field.action_list);
input_config = factory.util.actionMenu(field.action_list);
// links and/or sorting
} else {
// map config to what we need
input_config = map.utils.mapFormField(
spec.set_id,
config,
overrides,
value
);
// links and/or sorting
} else {
// map config to what we need
input_config = map.utils.mapFormField(
spec.set_id,
config,
overrides,
value
);
}
}
}
switch (j) {
case 0:
position = true;
break;
case (segment.fieldlist.length - 1):
position = false;
break;
case (j === 1 && segment.fieldlist.length === 1):
position = null;
break;
default:
position = undefined;
break;
}
switch (j) {
case 0:
position = true;
break;
case (segment.fieldlist.length - 1):
position = false;
break;
case (j === 1 && segment.fieldlist.length === 1):
position = null;
break;
default:
position = undefined;
break;
}
// widget
if (input_config.generate) {
if (spec.set_id) {
if (input_config.attributes !== undefined) {
input_config.attributes["data-reference"] = spec.set_id;
} else {
input_config.reference = spec.set_id;
// widget
if (input_config.generate) {
if (spec.set_id) {
if (input_config.attributes !== undefined) {
input_config.attributes["data-reference"] = spec.set_id;
} else {
input_config.reference = spec.set_id;
}
}
}
container.appendChild(app.setContent(input_config));
// html element
} else if (input_config.logic && input_config.logic.skip) {
container.appendChild(factory.element(
input_config.type,
input_config.direct,
input_config.attributes || {},
input_config.logic || {}
));
container.appendChild(app.setContent(input_config));
// html element
} else if (input_config.logic && input_config.logic.skip) {
container.appendChild(factory.element(
input_config.type,
input_config.direct,
input_config.attributes || {},
input_config.logic || {}
));
// form element
} else {
container.appendChild(factory.formElement(
input_config,
true,
true,
position
));
// form element
} else {
container.appendChild(factory.formElement(
input_config,
true,
true,
position
));
}
}
fragment.appendChild(container);
}
fragment.appendChild(container);
}
// set captcha or anti spam protection
......@@ -5220,73 +4974,49 @@
/* STORAGE */
/* ====================================================================== */
// generate storage object
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);
// generate storage object with placeholders
storage = {
"obj": {},
"arr": [],
"span": document.createElement("span")
};
*/
/**
* 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...
// TODO: merge pass.x or pass.state.x
// TODO: set pointer directly inside data-action, remove pointer!
// TODO: state needs to have url_pointer set earlier, so
// there is no need to look it up in pass.config.property_dict...
// TODO: jump is bad, it is the lookup for the data to display. rename!
storage.fetch = function (obj) {
var pass, query, method, map, config, answer, lookup, pointer, action,
quirk;
var pass, query, method, map, config, answer, lookup, pointer, action_id,
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;
lookup = obj.state || quirk.property_dict || storage.obj;
query = obj.query || lookup.query || storage.obj;
pointer = (lookup["url_pointer"] || storage.obj).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);
// TODO: action > id, this should be triggered via form submit not link!
action_id = ((obj.element || storage.obj).href || "").split("/").pop();
if (action) {
if (action_id) {
pointer = undefined;
query = {"_id": query._id}
query = {"_id": window.decodeURIComponent(action_id)}
}
switch (true) {
case ((query.select_list || []).length > 0 && !query.include_docs):
// TODO: "hack" for forcing get into allDocs, don't overwrite criteria!
case !!pointer:
map = "values";
query.select_list = query.select_list || pass.field_list;
query.limit = query.limit || pass.config.initial_query.limit;
break;
case ((query.select_list || storage.arr).length > 0 && !query.include_docs):
map = "values";
break;
case (!!query._id):
......@@ -5298,28 +5028,20 @@
break;
}
// TODO: remove this - pass custom query to storage
// TODO: remove - 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;
if (action_id) {
return response;
}
answer = storage.mapResponse(response, map);
if (app.storage_dict.property_dict.force_sync) {
//storage.sync(mapped, parcel.storage);
}
}
return {
"response": answer || response,
......@@ -5334,44 +5056,41 @@
* @param {object} obj Action object
*/
storage.write = function (obj) {
var form, data, valid, prefix, decode, id, answer, goto, config, method;
var form, data, valid, prefix, config, method, action, pointer, id;
form = document.getElementById(obj.id);
pointer = (obj.element || storage.span).getAttribute("data-action"),
action = obj.state.url_pointer[pointer];
config = {};
prefix = obj.id + "_";
valid = storage.validate(obj);
data = storage.parseForm(valid, prefix, true);
// TODO: remove, used for update scope, which does not honor form definition
if (obj.force_data) {
config._force_data = true;
}
if (form.identifier) {
// this must be a PUT, set id, method and view
// PUT > set id, method and view
data._id = form.identifier.value;
method = "put";
config = {
"_view": obj.state.view
};
} else {
// POSTING, set action set in JSON config
config = {
"action": obj.state.url_pointer[obj.element.getAttribute("data-action")]
};
config._view = obj.state.view;
}
RSVP.resolve(app.storage_dict.items[method || "post"](data, config))
.then(function (response) {
// TODO: if this is strictly a storage operation, do this elsewhere!
if (response.status === 201) {
util.loader("", "status_dict.saved", "check");
if (obj.state.callback) {
answer = util.parse(response);
id = answer.id;
decode = /^[^\/]*%2[^\/]*$/.test(id);
goto = decode ? id : window.encodeURIComponent(id);
$.mobile.changePage(obj.state.callback.replace("__id__", goto));
}
} else {
util.loader("", "status_dict.error", "ban-circle");
}
}).fail(util.error);
if (pointer) {
config._action = action;
// in case action is triggerted that needs retrival from a reference
data._reference = obj.state.reference;
}
console.log("DONE")
console.log(method)
console.log(data)
console.log(config)
return RSVP.resolve(
app.storage_dict.items[method || "post"](data, config)
);
};
/**
......@@ -5777,6 +5496,25 @@
*/
app.timer = 0;
/**
* Navigate to a new page after performing some action
* @method navigate
* @param {Object} obj The action object
* @param {Object} response The action response
*/
app.navigate = function (obj, response) {
var answer, id, goto, decode;
if (obj.state.callback) {
answer = util.parse(response);
id = answer.id;
decode = /^[^\/]*%2[^\/]*$/.test(id);
goto = decode ? id : window.encodeURIComponent(id);
$.mobile.changePage(obj.state.callback.replace("__id__", goto));
}
};
/**
* Handler for search
* @method search
......@@ -6693,6 +6431,7 @@
case "ssl_off":
case "ssl":
case "request":
case "installation":
config.mode = query[i];
break;
}
......@@ -6941,6 +6680,10 @@
if (pass.fields && storage && force_fields) {
if (pass.config.scheme) {
pass.field_list = storage.makeSelectList(pass.config.scheme);
// make sure at least the id is returned
if (pass.field_list.indexOf("_id") === -1) {
pass.field_list.push("_id");
}
}
}
// HACK:
......@@ -7470,68 +7213,6 @@
);
};
// /**
// * Try fetching data from JIO. Fallback to loading fake data until accessible
// * @method fetchData
// * @param {object} parcel Storage, query options and pass to return
// * @return {object} promise object/pass
// */
// storage.fetch = function (parcel) {
// var method, convert, select_list, hacked_view, pass, skip, query, mapped;
//
// pass = parcel.pass;
// query = parcel.query;
// skip = query && query.limit && query.limit.length === 0 &&
// app.storage_dict.property_dict.skip_total_records;
//
//
// // return here, if skipping total query
// if (skip) {
// return {
// "pass": parcel.pass
// };
// }
//
// select_list = parcel.query && parcel.query.select_list && parcel.query.select_list.length;
//
// // single item query GET
// if (parcel.query._id) {
// // TODO: don't set if don't need...
// delete parcel.query.limit;
// hacked_view = {"_view": parcel.pass.config.view}
// method = "get";
// convert = "single_item";
// }
//
// // query that will return value {} object vs. doc
// if (select_list && parcel.query.include_docs === undefined) {
// convert = "values";
// }
//
// return app.storage_dict[parcel.storage][method || "allDocs"](parcel.query, hacked_view)
// .then(function (response) {
//
// // TODO: best way?
// if (convert !== undefined && response.status === 200) {
// mapped = storage.mapResponse(response, convert);
//
// // force sync
// if (app.storage_dict.property_dict.force_sync) {
// //storage.sync(mapped, parcel.storage);
// }
//
// return {
// "response": mapped,
// "pass": parcel.pass
// };
// }
// return {
// "response": response,
// "pass": parcel.pass
// };
// });
// };
/**
* Load files from disk
* @method loadFile
......
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