Commit da105967 authored by Sven Franck's avatar Sven Franck

fetching storages from data-storage tag, cleaned up methods

parent 2a4d45d0
......@@ -46,7 +46,7 @@
<!-- stuff happens here -->
<script type="text/javascript" src="js/shims.js"></script>
<script type="text/javascript" data-storage="data/storage.json" src="js/erp5_loader.js"></script>
<script type="text/javascript" data-storage="data/storages.json" data-config="data/global.json" src="js/erp5_loader.js"></script>
</body>
</html>
......@@ -5,7 +5,7 @@
(function (window, document, $) {
"use strict";
var init, util, erp5, factory;
var init, app, util, erp5, factory;
/* ====================================================================== */
/* MAPPING ERP5 */
......@@ -3061,6 +3061,99 @@
/*
* Object containing application related methods
*/
app = {};
/**
* fetch configuration and set up storages
* @method setupStorages
* @param {string} url URL of storage definition to fetch
* @return {object} promise
*/
app.setupStorages = function (url) {
if (url) {
// The storage definition should be cached not stored, so we skip
// storing - also because the storage will not be initiated as
// we are fetching instructions for initiation here
return app.getFromDisk({"skip": true, "url": url})
.then(app.createStorages)
.fail(util.errorHandler);
}
// no storage requested
return RSVP.all([]);
};
/**
* sets up JIO based on loaded JSON "recipe"
* @method createStorages
* @param {object} response object containing data (jio.util.ajax response)
* @return {object} promise object
*/
app.createStorages = function (reply) {
var i,
store,
promises = [];
app.storages = {};
for (i = 0; i < reply.length; i += 1) {
store = reply[i].definition;
promises[i] =
app.storages[store.application_name] =
jIO.createJIO(store);
}
return RSVP.all(promises);
};
/**
* load files into JIO that are missing
* @method getFromDisk
* @param {object} props parameters & baggage to pass to subsequent then()
* @return {object} object including baggage and reply
*/
// TODO: put/putAttachment should be set somewhere more general!
app.getFromDisk = function (property_dict) {
var response,
put = function (e) {
response = util.parseIfNeeded(e.target.responseText);
if (property_dict.skip) {
return;
}
return property_dict.store.put({
"_id": property_dict.file
});
},
putAttachment = function () {
if (property_dict.skip) {
return;
}
return property_dict.store.putAttachment({
"_id": property_dict.file,
"_attachment": property_dict.attachment,
"_data": JSON.stringify(response),
"_mimetype": "application/json"
});
},
url = property_dict.url || "data/" + property_dict.attachment + ".json";
return util.ajax({"url": url})
.then(put)
.then(putAttachment)
.then(function () {
if (property_dict.baggage) {
return {
"response": response,
"baggage": property_dict.baggage
};
}
return response;
})
.fail(util.errorHandler);
};
init = {};
// TODO: should be inside app/init object
......@@ -3071,9 +3164,10 @@
* @param {object} parcel parameters & baggage to pass to subsequent then()
* @return {object} object including baggage and reply
*/
// TODO should be generic, not only JIO based!
init.fetchPortalTypeConfiguration = function (parcel) {
//storage, file, attachment, baggage
var store = init.storages[parcel.storage];
var reply, store = app.storages[parcel.storage];
return store.getAttachment({
"_id": parcel.file,
......@@ -3084,68 +3178,29 @@
})
.then(
function (answer) {
return {
"response": JSON.parse(answer.target.result),
"baggage": parcel.baggage
};
reply = JSON.parse(answer.target.result);
if (parcel.baggage) {
return {
"response": reply,
"baggage": parcel.baggage
};
}
return reply;
},
function (error) {
if (error.status === 404 && error.id === parcel.file) {
return init.getFromDisk({
return app.getFromDisk({
"store": store,
"file": parcel.file,
"attachment": parcel.attachment,
"baggage": parcel.baggage
})
.then(function (reply) {
return {
"response": util.parseIfNeeded(reply.response),
"baggage": reply.baggage
};
});
}
throw error;
}
);
};
/**
* load files into JIO that are missing
* @method getFromDisk
* @param {object} parcel parameters & baggage to pass to subsequent then()
* @return {object} object including baggage and reply
*/
init.getFromDisk = function (parcel) {
var response,
put = function (e) {
response = util.parseIfNeeded(e.target.responseText);
return parcel.store.put({
"_id": parcel.file
});
},
putAttachment = function () {
return parcel.store.putAttachment({
"_id": parcel.file,
"_attachment": parcel.attachment,
"_data": JSON.stringify(response),
"_mimetype": "application/json"
});
},
url = "data/" + parcel.attachment + ".json";
return util.ajax({"url": url})
.then(put)
.then(putAttachment)
.then(function () {
// stored in JIO, continue
return {
"response": response,
"baggage": parcel.baggage
};
})
.fail(util.errorHandler);
};
/**
* Try fetching data from JIO. Fallback to loading fake data until accessible
* @method fetchData
......@@ -3154,7 +3209,7 @@
*/
// NOTE: until we have real data we load fake data on application init!
init.fetchData = function (parcel) {
return init.storages[parcel.storage].allDocs(parcel.query)
return app.storages[parcel.storage].allDocs(parcel.query)
.then(function (response) {
return {
"response": response,
......@@ -3573,13 +3628,9 @@
// give user half second to pick his state
init.timer = window.setTimeout(function () {
// update gadgets
init.setPageElements({}, {
"response": [{
"section_list": [{
"gadget": config.id
}]
}]
}, false);
init.setPageElements(
{}, [{"section_list": [{"gadget": config.id}]}], false
);
init.timer = 0;
}, 500);
};
......@@ -3634,13 +3685,9 @@
// we only want x records
// update gadget
init.setPageElements({}, {
"response": [{
"section_list": [{
"gadget": config.id
}]
}]
}, false);
init.setPageElements(
{}, [{"section_list": [{"gadget": config.id}]}], false
);
})
.fail(util.errorHandler);
};
......@@ -3771,13 +3818,9 @@
config.state.query.limit = [start, records];
// update gadget
init.setPageElements({}, {
"response": [{
"section_list": [{
"gadget": config.id
}]
}]
}, false);
init.setPageElements(
{}, [{"section_list": [{"gadget": config.id}]}], false
);
} else {
util.errorHandler({
"Error": "No state information stored for gadget"
......@@ -4103,7 +4146,7 @@
promises,
record,
items,
store = init.storages["items"],
store = app.storages["items"],
baggage = reply.baggage;
if (reply.response) {
......@@ -4300,7 +4343,7 @@
gadgets,
baggage,
promises = [],
layout = layouts.response[config.layout_level || 0];
layout = layouts[config.layout_level || 0];
if (create === true) {
page = factory.generatePage(config, layout);
......@@ -4444,52 +4487,6 @@
}
};
/**
* fetch application configuration, store in JIO and setup application
* @method runApplicationSetup
* @param {string} file File needed to setup
* @param {string} attachment Attachement with necessary config
* @return {object} Promise object
*/
init.runApplicationSetup = function (attachment) {
init.storages = {};
// TODO: find a way to not always refetch application
// NOTE: can't fetchConfig because need to setup storages first
if (init.storages[attachment]) {
return;
}
return util.ajax({"url": "data/" + attachment + ".json"})
.then(function(e) {
return e.target.responseText;
})
.fail(util.errorHandler);
};
/**
* sets up JIO based on loaded JSON "recipe"
* @method setupStorages
* @param {object} response object containing data (jio.util.ajax response)
* @return {object} promise object
*/
init.setupStorages = function (reply) {
var i,
store,
promises = [],
config = JSON.parse(reply);
for (i = 0; i < config.length; i += 1) {
store = config[i].definition;
promises[i] =
init.storages[store.application_name] =
jIO.createJIO(store);
}
return RSVP.all(promises);
};
/**
* Loads and runs application setters
* @method loadGlobalElements
......@@ -4514,7 +4511,7 @@
var i,
element,
content,
config = util.parseIfNeeded(reply.response);
config = util.parseIfNeeded(reply);
if (config && config.length) {
for (i = 0; i < config.length; i += 1) {
......@@ -4858,11 +4855,12 @@
/* ====================================================================== */
init.contentLoaded(window, function () {
// TODO: find a way to sync when storage recipe changes!
window.localStorage.clear();
// TODO: modernizr check and shim here
// HACK: don't!
// TODO: sync once JIO is up properly
window.localStorage.clear();
// initalize translations
$.i18n.init({
......@@ -4874,14 +4872,10 @@
});
// TODO: run this to the normal route!
init
// "Application Setup"
.runApplicationSetup("storages")
.then(init.setupStorages)
app.setupStorages(util.getPathFromScriptTag("data-storage"))
.then(init.loadGlobalElements)
.then(init.setupGlobalElements)
.then(init.setGlobalBindings)
// "Page Setup"
.then(init.parsePage)
.fail(util.errorHandler);
......
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