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

updated application handler to working without hacks

parent e374a231
...@@ -538,92 +538,6 @@ ...@@ -538,92 +538,6 @@
**/ **/
map.handlers = { 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 * @method software_list
* @param {object} reply Object received from previous chain element * @param {object} reply Object received from previous chain element
...@@ -716,50 +630,7 @@ ...@@ -716,50 +630,7 @@
}).fail(util.error); }).fail(util.error);
} }
return (reply); 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 @@ ...@@ -774,7 +645,17 @@
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"new": function (obj) { "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 @@ ...@@ -783,303 +664,114 @@
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"update": function (obj) { "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 * update server scope - custom action, because my_ not implemented,
* @method get * same for ticket status updates
* @method update
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"get": function (obj) { "update_scope": function (obj) {
storage.fetch(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 * @method install
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"install": function (obj) { "install": function (obj) {
// force new portal type and POST (create new record) // HACK: no good way to refer to the calling server!
obj.gadget.state.force_type = "Service"; obj.state.reference =
obj.gadget.state.create_new = true; window.decodeURIComponent(obj.state.fragment_list[1]);
storage.add(obj); storage.write(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)));
}
});
}
}, },
/** /**
* Start a software instance * Revoke a SSL certificate - custom action, so extra entry
* @method start_instance * @method revoke_ssl
* @param {object} obj Action Object * @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) { "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"); util.loader("", "status_dict.updating");
store.items.get({"_id": window.decodeURIComponent(obj.state.fragment_list[1])},{"_view": "web_view"}) storage.write(obj)
.then(function(response) { .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) {
util.loader("", "status_dict.ssl_revoked", "check"); util.loader("", "status_dict.ssl_revoked", "check");
}) })
.fail(function (error){ .fail(function (error) {
util.error(error);
util.loader("", "status_dict.ssl_no", "ban-circle"); 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 * Start a software instance
* @method start_instance * @method start_instance
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"start_instance": function (obj) { "destroy": function (obj) {
var element = obj.element; 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"); util.loader("", "status_dict.starting", "signal");
jIO.util.ajax({ storage.write(obj)
"url": window.decodeURIComponent(element.href), .then(function (response) {
"type": element.getAttribute("data-method"), util.loader("", "status_dict.success", "check");
"xhrFields": { })
"withCredentials": true .fail(util.error);
}
})
.then(function (response) {
util.loader("", "status_dict.success", "check");
})
.fail(util.error);
}, },
/** /**
...@@ -1087,10 +779,64 @@ ...@@ -1087,10 +779,64 @@
* @method stop_instance * @method stop_instance
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
"stop_instance": function (obj) { "stop": function (obj) {
var element = obj.element;
util.loader("", "status_dict.stopping", "ban-circle"); 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({ jIO.util.ajax({
"url": window.decodeURIComponent(element.href), "url": window.decodeURIComponent(element.href),
"type": element.getAttribute("data-method"), "type": element.getAttribute("data-method"),
...@@ -1098,10 +844,17 @@ ...@@ -1098,10 +844,17 @@
"withCredentials": true "withCredentials": true
} }
}) })
.then(function (response) { .then(function (answer) {
util.loader("", "status_dict.success", "check"); 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 @@ ...@@ -3330,7 +3083,6 @@
// add to DOM if scoped // add to DOM if scoped
if (url_dict.data_url) { if (url_dict.data_url) {
//util.getPanel(url_dict.data_url).appendChild(panel);
return panel; return panel;
} else { } else {
document.body.insertBefore(panel, document.body.children[0]); document.body.insertBefore(panel, document.body.children[0]);
...@@ -3459,202 +3211,204 @@ ...@@ -3459,202 +3211,204 @@
); );
}; };
// loop over form layout sections // secure anyway
for (i = 0; i < spec.scheme.length; i += 1) { // TODO: do properly once we have time
segment = spec.scheme[i]; if (secure === "default") {
area = segment.position === "center" ? 2 : 1;
container = wrap(area);
if (i === 0) {
// secure form
// TODO: do properly once we have time
if (secure === "default") {
stamp = new Date().getTime(); stamp = new Date().getTime();
encode = window.btoa || window.shim.Base64.encode; encode = window.btoa || window.shim.Base64.encode;
// TODO: include client IP in hash // TODO: include client IP in hash
sauce = encode(stamp.toString() + spec.property_dict.secret_hash); sauce = encode(stamp.toString() + spec.property_dict.secret_hash);
fragment.appendChild(factory.formElement(
container.appendChild(factory.formElement( {
{ "type": "input",
"type": "input", "direct": {
"direct": { "type": "text",
"type": "text", "value": "",
"value": "", "className": "secure_form"
"className": "secure_form" },
}, "attributes": {
"attributes": { "placeholder": "Please do not fill out this field",
"placeholder": "Please do not fill out this field", "data-enhanced": "true",
"data-enhanced": "true", "data-i18n": "[placeholder]global.form_helpers.secure_blank"
"data-i18n": "[placeholder]global.form_helpers.secure_blank" },
}, "logic": {
"logic": { "plain_element": true,
"plain_element": true, "label": "Please do not fill out this field",
"label": "Please do not fill out this field", "label_i18n": "global.form_helpers.secure_blank"
"label_i18n": "global.form_helpers.secure_blank" }
} }
} ));
)); fragment.appendChild(factory.element(
container.appendChild(factory.element( "input",
"input", {
{ "type": "hidden",
"type": "hidden", "value": spec.property_dict.secret_hash,
"value": spec.property_dict.secret_hash, "id": spec.set_id + "_not_a_secret"
"id": spec.set_id + "_not_a_secret" },
}, {
{ "data-created": stamp
"data-created": stamp }
} ));
)); fragment.appendChild(factory.formElement(
container.appendChild(factory.formElement( {
{ "type": "input",
"type": "input", "direct": {
"direct": { "type": "text",
"type": "text", "className": "secure_form",
"className": "secure_form", "value": sauce
"value": sauce },
}, "attributes": {
"attributes": { "placeholder": "Please leave this value unchanged",
"placeholder": "Please leave this value unchanged", "data-enhanced": "true",
"data-enhanced": "true", "data-i18n": "[placeholder]global.form_helpers.secure_filled"
"data-i18n": "[placeholder]global.form_helpers.secure_filled" },
}, "logic": {
"logic": { "plain_element": true,
"plain_element": true, "label": "Please leave this value unchanged",
"label": "Please leave this value unchanged", "label_i18n": "global.form_helpers.secure_filled"
"label_i18n": "global.form_helpers.secure_filled" }
}
}
));
} }
));
}
// add hidden storage identifier // add hidden storage identifier
if (item_id) { if (item_id) {
container.appendChild(factory.element( fragment.appendChild(factory.element(
"input", "input",
{ {
"type": "hidden", "type": "hidden",
"value": item_id, "value": item_id,
"name": "identifier" "name": "identifier"
}
));
} }
} ));
}
// loop fieldlist of layout section // loop over form layout sections
for (j = 0; j < segment.fieldlist.length; j += 1) { if (spec.scheme.length > 0) {
field = segment.fieldlist[j]; 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 // loop fieldlist of layout section
if (field.type) { for (j = 0; j < segment.fieldlist.length; j += 1) {
input_config = field; field = segment.fieldlist[j];
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) { // allow non-fieldlist form elements to be generated
setter_list = input_config.logic.setters; // TODO: can we also generate a field from another portal_type here?
if (input_config.logic.skip === undefined) { if (field.type) {
input_config.logic.skip = true; input_config = field;
} prefixed_name = spec.reference + "_" + field.direct.name || util.uuid();
if (setter_list) { field.direct.id = field.direct.name = prefixed_name;
for (l = 0; l < setter_list.length; l += 1) { setter = input_config.logic ? (input_config.logic.setValue || input_config.logic.setFlux) : undefined;
input_config.logic[setter_list[l]] = spec.data[setter];
// 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 (setter_list) {
if (input_config.logic.setFlux) { for (l = 0; l < setter_list.length; l += 1) {
input_config.direct.value = (flux && flux.cert) ? flux.cert[setter] : "Certificate is still active, please revoke existing one."; input_config.logic[setter_list[l]] = spec.data[setter];
}
} else { } else {
input_config.direct.value = spec.data[setter] || if (input_config.logic.setFlux) {
(setter + util.uuid()); 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 { } else {
setter = field.field; config = spec.fields[field.field];
} overrides = field.overrides;
value = doc ? (doc[setter]) : undefined; doc = spec.data || undefined;
// action menu // try to set a value
if (field.action_list && field.action_list.length) { if (doc && field.custom) {
for (m = 0; m < field.action_list.length; m += 1) { setter = field.setValue || undefined;
button = field.action_list[m]; } else {
if (button.setters.length) { setter = field.field;
for (n = 0; n < button.setters.length; n += 1) { }
field.action_list[m][button.setters[n]] = value = doc ? (doc[setter]) : undefined;
util.fetchByArray(doc, button.setValues[n]);
// 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 // links and/or sorting
} else { } else {
// map config to what we need // map config to what we need
input_config = map.utils.mapFormField( input_config = map.utils.mapFormField(
spec.set_id, spec.set_id,
config, config,
overrides, overrides,
value value
); );
}
} }
}
switch (j) { switch (j) {
case 0: case 0:
position = true; position = true;
break; break;
case (segment.fieldlist.length - 1): case (segment.fieldlist.length - 1):
position = false; position = false;
break; break;
case (j === 1 && segment.fieldlist.length === 1): case (j === 1 && segment.fieldlist.length === 1):
position = null; position = null;
break; break;
default: default:
position = undefined; position = undefined;
break; break;
} }
// widget // widget
if (input_config.generate) { if (input_config.generate) {
if (spec.set_id) { if (spec.set_id) {
if (input_config.attributes !== undefined) { if (input_config.attributes !== undefined) {
input_config.attributes["data-reference"] = spec.set_id; input_config.attributes["data-reference"] = spec.set_id;
} else { } else {
input_config.reference = spec.set_id; input_config.reference = spec.set_id;
}
} }
} container.appendChild(app.setContent(input_config));
container.appendChild(app.setContent(input_config));
// html element
// html element } else if (input_config.logic && input_config.logic.skip) {
} else if (input_config.logic && input_config.logic.skip) { container.appendChild(factory.element(
container.appendChild(factory.element( input_config.type,
input_config.type, input_config.direct,
input_config.direct, input_config.attributes || {},
input_config.attributes || {}, input_config.logic || {}
input_config.logic || {} ));
));
// form element // form element
} else { } else {
container.appendChild(factory.formElement( container.appendChild(factory.formElement(
input_config, input_config,
true, true,
true, true,
position position
)); ));
}
} }
fragment.appendChild(container);
} }
fragment.appendChild(container);
} }
// set captcha or anti spam protection // set captcha or anti spam protection
...@@ -5220,73 +4974,49 @@ ...@@ -5220,73 +4974,49 @@
/* STORAGE */ /* STORAGE */
/* ====================================================================== */ /* ====================================================================== */
// generate storage object // generate storage object with placeholders
storage = {}; storage = {
"obj": {},
/* "arr": [],
"span": document.createElement("span")
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 * Fetch data from storage = GET or ALLDOCS
* @method fetch * @method fetch
* @param {object} obj Action object * @param {object} obj Action object
*/ */
// TODO: only use on lookup architecture pass.x or pass.state.x // TODO: merge pass.x or pass.state.x
// TODO: this means state needs to have url_pointer set earilier, so // TODO: set pointer directly inside data-action, remove pointer!
// there is no need to look it up in oass.config.property_dict... // 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) { storage.fetch = function (obj) {
var pass, query, method, map, config, answer, lookup, pointer, action, var pass, query, method, map, config, answer, lookup, pointer, action_id,
quirk; quirk;
pass = obj.pass; pass = obj.pass;
quirk = obj.state || pass.config; quirk = obj.state || pass.config;
lookup = obj.state || quirk.property_dict || {}; lookup = obj.state || quirk.property_dict || storage.obj;
query = obj.query || lookup.query || {}; query = obj.query || lookup.query || storage.obj;
pointer = (lookup["url_pointer"] || {}).jump; pointer = (lookup["url_pointer"] || storage.obj).jump;
// TODO: action handling, should be a form submit not set on element! // TODO: action > id, this should be triggered via form submit not link!
action = ((obj.element || {}).href || "").split("/").pop(); action_id = ((obj.element || storage.obj).href || "").split("/").pop();
query._id = query._id || window.decodeURIComponent(action);
if (action) { if (action_id) {
pointer = undefined; pointer = undefined;
query = {"_id": query._id} query = {"_id": window.decodeURIComponent(action_id)}
} }
switch (true) { 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"; map = "values";
break; break;
case (!!query._id): case (!!query._id):
...@@ -5298,28 +5028,20 @@ ...@@ -5298,28 +5028,20 @@
break; break;
} }
// TODO: remove this - pass custom query to storage // TODO: remove - pass custom query to storage
if (pointer && !config) { if (pointer && !config) {
config = { config = {
"_jump": pointer "_jump": pointer
}; };
} }
return RSVP.resolve( return RSVP.resolve(
app.storage_dict.items[method || "allDocs"](query, config) app.storage_dict.items[method || "allDocs"](query, config)
).then(function (response) { ).then(function (response) {
if (response.status === 200) { if (response.status === 200) {
// TODO: actions should have their own callback set on state! if (action_id) {
if (action) { return response;
util.loader("", "status_dict.rendering");
window.location.href = response.data._links.slapos_jump.href;
} }
answer = storage.mapResponse(response, map); answer = storage.mapResponse(response, map);
if (app.storage_dict.property_dict.force_sync) {
//storage.sync(mapped, parcel.storage);
}
} }
return { return {
"response": answer || response, "response": answer || response,
...@@ -5334,44 +5056,41 @@ ...@@ -5334,44 +5056,41 @@
* @param {object} obj Action object * @param {object} obj Action object
*/ */
storage.write = function (obj) { 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); form = document.getElementById(obj.id);
pointer = (obj.element || storage.span).getAttribute("data-action"),
action = obj.state.url_pointer[pointer];
config = {};
prefix = obj.id + "_"; prefix = obj.id + "_";
valid = storage.validate(obj); valid = storage.validate(obj);
data = storage.parseForm(valid, prefix, true); 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) { if (form.identifier) {
// this must be a PUT, set id, method and view // PUT > set id, method and view
data._id = form.identifier.value; data._id = form.identifier.value;
method = "put"; method = "put";
config = { config._view = obj.state.view;
"_view": obj.state.view
};
} else {
// POSTING, set action set in JSON config
config = {
"action": obj.state.url_pointer[obj.element.getAttribute("data-action")]
};
} }
RSVP.resolve(app.storage_dict.items[method || "post"](data, config)) if (pointer) {
.then(function (response) { config._action = action;
// TODO: if this is strictly a storage operation, do this elsewhere! // in case action is triggerted that needs retrival from a reference
if (response.status === 201) { data._reference = obj.state.reference;
util.loader("", "status_dict.saved", "check"); }
if (obj.state.callback) { console.log("DONE")
answer = util.parse(response); console.log(method)
id = answer.id; console.log(data)
decode = /^[^\/]*%2[^\/]*$/.test(id); console.log(config)
goto = decode ? id : window.encodeURIComponent(id); return RSVP.resolve(
app.storage_dict.items[method || "post"](data, config)
$.mobile.changePage(obj.state.callback.replace("__id__", goto)); );
}
} else {
util.loader("", "status_dict.error", "ban-circle");
}
}).fail(util.error);
}; };
/** /**
...@@ -5777,6 +5496,25 @@ ...@@ -5777,6 +5496,25 @@
*/ */
app.timer = 0; 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 * Handler for search
* @method search * @method search
...@@ -6693,6 +6431,7 @@ ...@@ -6693,6 +6431,7 @@
case "ssl_off": case "ssl_off":
case "ssl": case "ssl":
case "request": case "request":
case "installation":
config.mode = query[i]; config.mode = query[i];
break; break;
} }
...@@ -6941,6 +6680,10 @@ ...@@ -6941,6 +6680,10 @@
if (pass.fields && storage && force_fields) { if (pass.fields && storage && force_fields) {
if (pass.config.scheme) { if (pass.config.scheme) {
pass.field_list = storage.makeSelectList(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: // HACK:
...@@ -7470,68 +7213,6 @@ ...@@ -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 * Load files from disk
* @method loadFile * @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