Commit d54d7277 authored by Sven Franck's avatar Sven Franck

removed ram_cache, set state on gadget instead

parent cf2c1649
(function(window, document, $) {
// fragment cache
var ram_cache = {};
// methods for initialization
var init = {};
......@@ -1828,16 +1825,17 @@
* @return {object} action object
*/
init.generateActionObject = function (e) {
var element, id;
var element, id, gadget;
element = e.target || e;
id = element.getAttribute("data-reference");
gadget = document.getElementById(id);
return {
"element": element,
"id": id,
"gadget": document.getElementById(id),
"state": ram_cache[util.getActivePageModule()].gadgets[id]
"state": $(gadget).data("state")
};
};
......@@ -1910,9 +1908,12 @@
}
// update gadget state
init.cacheGadgetState(
config.id, state.query, state.total, state.method, state.selected
);
$(config.gadget).data("state", {
"query": state.query,
"total": state.total,
"method": state.method,
"selected": state.selected
});
// update info fields
init.updateInfoFields(
......@@ -2168,56 +2169,6 @@
return undefined;
};
/**
* Sets up gadget cache
* @method setupCache
* @param {string} type Type of cache to setup and/or return
* @param {string} page Page id, needed for dynamic page creation
* @return {object} cache
*/
// NOTE: if generating pages dynamic getActivePageModule will not find
// the new page because it hasn't been added, thus storing gadgets on
// wrong page.
init.cacheSetup = function (type, page) {
var active = page || util.getActivePageModule();
// no cache for this page?
if (ram_cache[active] === undefined) {
ram_cache[active] = {};
}
// no cache for gadgets on this page?
if (ram_cache[active][type] === undefined) {
ram_cache[active][type] = {};
}
// always return shortcut to cache of active page
return ram_cache[active][type];
};
/**
* Store a gadget's state in cache
* @method cacheGadgetState
* @param {string} gadget_id Gadget id
* @param {object} query JIO query parameters to get above item(s)
* @param {string} method Method to generate gadget
* @param {integer} total Total records for this query
* @param {array} selected Array of records currently selected
* @param {string} page Page id
*/
init.cacheGadgetState = function (id, query, total, method, selected, page) {
var cache = init.cacheSetup("gadgets", page);
// keep status of gadget in cache
cache[id] = {
"query": query,
"total": total,
"method": method,
"selected": selected
}
};
/* ====================================================================== */
/* UTILS */
/* ====================================================================== */
......@@ -2654,8 +2605,7 @@
.then(function(stored_data) {
// updates require gadget_state, which also includes constructor!
if (create === false) {
gadget_state = ram_cache[util.getActivePageModule()]
.gadgets[gadget_id];
gadget_state = $(document.getElementById(gadget_id)).data("state");
constructor = gadget_state.method;
// save
actual = gadget_state.query.limit;
......@@ -2680,16 +2630,6 @@
return util.fetchData("items", query);
})
.then(function(query_results) {
// cache, page_id needed in case dynamic page is generated!
init.cacheGadgetState(
gadget_id,
query,
total,
constructor,
(create === false ? gadget_state.selected : undefined),
config.id
);
// append gadget to wrapping div
element = factory.map_gadgets[constructor](
gadget_config,
......@@ -2698,6 +2638,14 @@
(create === false ? true : null)
);
// store state on gadget
$(element.querySelector("#"+gadget_id)).data("state", {
"query": query,
"total": total,
"method": constructor,
"selected": create === false ? gadget_state.selected : undefined
});
if (create === false) {
// update gadget
$(element).replaceAll(
......@@ -3000,11 +2948,6 @@
$(this).page("destroy").remove()
};
})
// clear cache when removing a page
// TODO: remove once cache is set on gadgets!
.on("pageremove", "div.ui-page", function (e) {
delete ram_cache[e.target.id];
})
// global popup
// TODO: integrate in action
......@@ -3069,13 +3012,7 @@
reference = pop.getAttribute("data-reference"),
state = pop.getAttribute("data-state");
// TODO: cache or not?
if (ram_cache.page_snippets === undefined) {
ram_cache.page_snippets = {};
}
if (ram_cache.page_snippets[reference] === undefined) {
store = true;
}
// TODO: cache generated content on popup?
// don't reload if same popup is opened
if (state !== reference) {
......@@ -3087,22 +3024,16 @@
"p",{},{},{"text":"No handler for popup"}
);
} else {
if (store || rebuild === undefined) {
if (factory.map_actions[reference]) {
// generate and cache
fragment = factory.map_actions[reference]();
ram_cache.page_snippets[reference] = fragment;
} else {
util.errorHandler(
{"Error":"Global Bindings: No content for popup"}
);
fragment = factory.generateElement(
"p",{},{},{"text":"No conten for popup"}
);
}
if (factory.map_actions[reference]) {
// generate
fragment = factory.map_actions[reference]();
} else {
// pull from cache
fragment = ram_cache.page_snippets[reference];
util.errorHandler(
{"Error":"Global Bindings: No content for popup"}
);
fragment = factory.generateElement(
"p",{},{},{"text":"No conten for popup"}
);
}
pop.setAttribute("data-state", reference);
pop.setAttribute("data-reference", reference);
......
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