Commit a2a5ea79 authored by Sven Franck's avatar Sven Franck

refactor items gadgets (not done)

parent 16d91fb9
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
.items_listview li { .items_listview li {
float: left; float: left;
width: 25%; width: 25%;
list-style: none;
} }
.items_listview li, .items_listview li,
.items_listview li a { .items_listview li a {
...@@ -61,6 +62,7 @@ ...@@ -61,6 +62,7 @@
white-space: normal; white-space: normal;
width: 100%; width: 100%;
padding-bottom: 0; padding-bottom: 0;
text-decoration: none;
} }
/* we can safely use nth-child, because IE8- doesn't support corners anyway */ /* we can safely use nth-child, because IE8- doesn't support corners anyway */
......
...@@ -4,42 +4,63 @@ define([ ...@@ -4,42 +4,63 @@ define([
, 'css!items' , 'css!items'
], ],
function (App, source) { function (App, source) {
var response = {}; var that = {};
var priv = {};
// in the perfect renderjs world, the items gadget should either expose
// an API on how to pass ... query string ... or have default config
// like pointers or a default fallback. This is what we are doing currently.
// Leave it like that!
// Javascript closest()
priv.closest = function (elem, selector) {
var matchesSelector = elem.matches ||
elem.webkitMatchesSelector ||
elem.mozMatchesSelector ||
elem.msMatchesSelector;
while (elem) {
if (matchesSelector.bind(elem)(selector)) {
return elem;
} else {
elem = elem.parentNode;
}
}
return false;
};
response.data = source; // generate items to display
// TODO: the method to query and display items should be set be priv.generateItems = function (params) {
// either the searchbar or the storage?
response.mockup = function (source, callback_mockup) {
var markup, spec, var markup, spec,
gadget = RenderJs.getSelfGadget(), id = params.gadget.dom.attr('id').split("__").splice(-1)[0],
id = gadget.dom.attr('id').split("__").splice(-1)[0], config = params.gadget.state[0][id]._config,
config = gadget.state[0][id]._config,
module = config.datasource.module, module = config.datasource.module,
// settable options // settable options
screen = App.settings.screen_format, screen = App.settings.screen_format,
size = screen === "small" ? "small" : "medium", size = screen === "small" ? "small" : "medium",
shortcut = size.substring(0, 1), shortcut = size.substring(0, 1),
identifier = App.settings.identifier || "item_id"; identifier = App.settings.identifier || "item_id";
spec = {}; spec = {};
spec.pointer = config.datasource.pointer; spec.pointer = config.datasource.pointer;
spec.method = spec.pointer ? undefined : config.datasource.method || "allDocs"; spec.method = spec.pointer ? undefined : config.datasource.method || "allDocs";
spec.storage = spec.pointer ? undefined : config.datasource.storage || "items"; spec.storage = spec.pointer ? undefined : config.datasource.storage || "items";
spec.options = spec.pointer ? undefined : { spec.options = spec.pointer ? undefined : {
"limit": config.datasource.limit || [0, 24], "limit": config.datasource.limit || [0, 24],
"sort_on": config.datasource.sort || [["_id", "descending"]], "sort_on": config.datasource.sort || [["_id", "descending"]],
"select_list": config.datasource.fields || undefined "select_list": config.datasource.fields || undefined
}; };
// if we have a search tag being passed, run a search // if we have a search tag being passed, run a search
if (location.search !== "") { if (location.search !== "") {
spec.query = true; spec.query = true;
spec.query_string = location.search.split("=")[1].replace("+", " "); spec.query_string = location.search.split("=")[1].replace("+", " ");
} }
spec.callback = function(err, response) { spec.callback = function(err, response) {
var item, i, str = "", price; var item, i, str = "", price;
// cleanup // cleanup
...@@ -78,38 +99,51 @@ define([ ...@@ -78,38 +99,51 @@ define([
} }
// add to data // add to data
markup = source.replace(/<!-- items -->/g, str); // TODO: this should only replace list items and not the whole
// gadget content...
markup = params.source.replace(/<!-- items -->/g, str);
// this is not response.callback(!!) // this is not response.callback(!!)
callback_mockup(markup); params.callback_mockup(markup);
} }
// query for items // query for items
App[module].switchboard(spec); App[module].switchboard(spec);
}; };
response.callback = function (self) { // response object
var closest = function (elem, selector) { that.data = source;
var matchesSelector = elem.matches || that.mockup = function (source, callback_mockup) {
elem.webkitMatchesSelector || var spec = {};
elem.mozMatchesSelector ||
elem.msMatchesSelector; spec.gadget = RenderJs.getSelfGadget();
// TODO: this is for page event bindings. Once JQM content replaces
while (elem) { // page, remove this and make the gadget the content section to
if (matchesSelector.bind(elem)(selector)) { // be updated.
return elem; spec.page = priv.closest(spec.gadget.dom[0], "div[data-role='page']");
} else { spec.pageId = "#" + spec.page.attr('id');
elem = elem.parentNode; spec.source = source;
} spec.callback_mockup = callback_mockup;
}
return false; // if unbound, bind and run once, if bound, just run
}; if (spec.gadget.sourceSet === undefined) {
spec.gadget.sourceSet = true;
$(document).on("pagebeforeshow", spec.pageId, function (e) {
priv.generateItems(spec);
});
priv.generateItems(spec);
} else {
priv.generateItems(spec);
}
};
that.callback = function (self) {
// set binding for opening a detailed view // set binding for opening a detailed view
// TODO: this should also be a link-method call // TODO: this should also be a link-method call
self.dom.find("ul").on("click", "a", function (e) { self.dom.find("ul").on("click", "a", function (e) {
var key; var key;
e.preventDefault(); e.preventDefault();
key = closest(e.target, "a").getAttribute("href").split("?")[1]; key = priv.closest(e.target, "a").getAttribute("href").split("?")[1];
// show this item // show this item
$.mobile.changePage("details.html", { $.mobile.changePage("details.html", {
"transition": "slide", "transition": "slide",
...@@ -136,6 +170,6 @@ define([ ...@@ -136,6 +170,6 @@ define([
}; };
// return response object // return response object
return response; return that;
} }
); );
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