some url parsing

This commit is contained in:
rolux 2010-07-14 17:25:20 +02:00
parent bca6408974
commit 0d03674ac9

View file

@ -11,20 +11,20 @@ $(function() {
appName: "0xDB", appName: "0xDB",
findKeys: [ findKeys: [
{ id: "all", title: "All" }, { id: "all", title: "All" },
{ id: "title", title: "Title" }, { id: "title", title: "Title", autocomplete: true },
{ id: "director", title: "Director" }, { id: "director", title: "Director", autocomplete: true },
{ id: "country", title: "Country" }, { id: "country", title: "Country", autocomplete: true },
{ id: "year", title: "Year" }, { id: "year", title: "Year", autocomplete: true },
{ id: "language", title: "Language" }, { id: "language", title: "Language", autocomplete: true },
{ id: "writer", title: "Writer" }, { id: "writer", title: "Writer", autocomplete: true },
{ id: "producer", title: "Producer" }, { id: "producer", title: "Producer", autocomplete: true },
{ id: "cinematographer", title: "Cinematographer" }, { id: "cinematographer", title: "Cinematographer", autocomplete: true },
{ id: "editor", title: "Editor" }, { id: "editor", title: "Editor", autocomplete: true },
{ id: "actor", title: "Actor" }, { id: "actor", title: "Actor", autocomplete: true },
{ id: "character", title: "Character" }, { id: "character", title: "Character", autocomplete: true },
{ id: "name", title: "Name" }, { id: "name", title: "Name", autocomplete: true },
{ id: "genre", title: "Genre" }, { id: "genre", title: "Genre", autocomplete: true },
{ id: "keyword", title: "Keyword" }, { id: "keyword", title: "Keyword", autocomplete: true },
{ id: "summary", title: "Summary" }, { id: "summary", title: "Summary" },
{ id: "dialog", title: "Dialog" } { id: "dialog", title: "Dialog" }
], ],
@ -110,8 +110,8 @@ $(function() {
userSettings: { userSettings: {
group: "guest", group: "guest",
ui: { ui: {
columns: ["title", "director", "country", "year", "language", "runtime", "genre"], columns: ["id", "title", "director", "country", "year", "language", "runtime", "genre"],
find: { key: "all", value: "", operator: "" }, find: { conditions: [{ key: "", value: "", operator: "" }], operator: "" },
itemView: "info", itemView: "info",
listsSize: 192, listsSize: 192,
listView: "list", listView: "list",
@ -135,10 +135,152 @@ $(function() {
ui = { ui = {
infoRatio: 4 / 3, infoRatio: 4 / 3,
selectedMovies: [] selectedMovies: []
}; },
// Objects
Query = (function() {
function constructFind(query) {
Ox.print("cF", query)
return $.map(query.conditions, function(v, i) {
if (!Ox.isUndefined(v.conditions)) {
return "[" + constructFind(v) + "]";
} else {
return v.value !== "" ? v.key + (v.key ? ":" : "") + constructValue(v.value, v.operator) : null;
}
}).join(query.operator);
}
function constructValue(value, operator) {
operator = operator.replace("=", "^$");
if (operator.indexOf("$") > -1) {
value = operator.substr(0, operator.length - 1) + value + "$"
} else {
value = operator + value;
}
return value;
}
function mergeFind() {
}
function parseFind(str) {
var find = {
conditions: [],
operator: ""
},
subconditions = str.match(/\[.*?\]/) || [];
$.each(subconditions, function(i, v) {
subconditions[i] = subconditions[i].substr(1, -1);
str = str.replace(v, "[" + i + "]");
});
if (str.indexOf(",") > -1) {
find.operator = "&";
} else if (str.indexOf("|") > -1) {
find.operator = "|";
}
Ox.print("pF", str)
find.conditions = $.map(find.operator === "" ? [str] : str.split(find.operator == "&" ? "," : "|"), function(v, i) {
var ret, kv;
if (v[0] == "[") {
Ox.print("recursion")
ret = parseFind(subconditions[parseInt(v[0].substr(1, -1))]);
} else {
kv = ((str.indexOf(":") > - 1 ? "" : ":") + str).split(":");
ret = $.extend({
key: kv[0]
}, parseValue(kv[1]));
}
Ox.print("ret", ret)
return ret;
});
return find;
}
function parseValue(str) {
var value = {
value: str,
operator: ""
};
if (value.value[0] == "!") {
value.operator = "!"
value.value = value.value.substr(1);
}
if ("^<>".indexOf(value.value[0]) > -1) {
value.operator += value.value[0];
value.value = value.value.substr(1);
}
if (value.value.substr(-1) == "$") {
value.operator += "$";
value.value = value.value.substr(0, -1);
}
value.operator.replace("^$", "=");
return value;
}
return {
fromString: function(str) {
var query = Ox.unserialize(str),
sort = [];
if ("find" in query) {
user.ui.find = parseFind(query.find);
Ox.print("user.ui.find", user.ui.find)
}
if ("sort" in query) {
sort = query.sort.split(",")
user.ui.sort = $.map(query.sort.split(","), function(v, i) {
var hasOperator = "+-".indexOf(v[0]) > -1,
key = hasOperator ? query.sort.substr(1) : query.sort,
operator = hasOperator ? v[0].replace("+", "") : Ox.getObjectById(config.sortKeys, key).operator;
return {
key: key,
operator: operator
};
});
}
if ("view" in query) {
user.ui.listView = query.view;
}
},
toObject: function(groupId) {
Ox.print("tO", user.ui.find.conditions)
// the inner $.merge() creates a clone
var conditions = $.merge($.merge([], user.ui.find.conditions), groups ? $.map(groups, function(v, i) {
if (v.id != groupId && v.conditions.length) {
return v.conditions.length == 1 ? v.conditions : {
conditions: v.conditions,
operator: "|"
};
}
}) : []),
operator = conditions.length < 2 ? "" : ",";
Ox.print(groupId, user.ui.find, conditions);
return {
conditions: conditions,
operator: operator
};
},
toString: function() {
Ox.print("tS", user.ui.find)
return Ox.serialize({
find: constructFind(Query.toObject()),
sort: user.ui.sort[0].operator + user.ui.sort[0].key,
view: user.ui.listView
});
}
};
})();
// App // App
Query.fromString(location.hash.substr(1));
Ox.print("user.ui", user.ui)
document.title = config.appName; document.title = config.appName;
Ox.theme(user.ui.theme); Ox.theme(user.ui.theme);
app = new Ox.App({ app = new Ox.App({
@ -350,11 +492,10 @@ $(function() {
.append( .append(
$ui.findInput = new Ox.Input({ $ui.findInput = new Ox.Input({
autocomplete: function(key, value, callback) { autocomplete: function(key, value, callback) {
var findKey = getObjectById(config.findKeys, key)
Ox.print("autocomplete", key, value); Ox.print("autocomplete", key, value);
value === "" && Ox.print("Warning: autocomplete function should never be called with empty value"); value === "" && Ox.print("Warning: autocomplete function should never be called with empty value");
if (key == "all") { if ("autocomplete" in findKey && findKey.autocomplete) {
callback();
} else {
app.request("find", { app.request("find", {
keys: [key], keys: [key],
query: { query: {
@ -379,6 +520,8 @@ $(function() {
return v.title; return v.title;
})); }));
}); });
} else {
callback();
} }
}, },
clear: true, clear: true,
@ -433,7 +576,7 @@ $(function() {
delete options.keys; delete options.keys;
app.request("find", $.extend(options, { app.request("find", $.extend(options, {
group: id, group: id,
query: constructQuery() query: Query.toObject()
}), options.callback); }), options.callback);
}, },
sort: [ sort: [
@ -590,6 +733,22 @@ $ui.statusbar = new Ox.Bar({
// Menu // Menu
Ox.Event.bind("click_about", function(event, data) {
var $dialog = new Ox.Dialog({
buttons: [
{
click: function() {
$dialog.close();
},
id: "close",
title: "Close",
value: "Close"
}
],
id: "about",
title: "About"
}).open();
});
Ox.Event.bind("change_viewmovies", function(event, data) { Ox.Event.bind("change_viewmovies", function(event, data) {
$ui.viewSelect.selectItem(data.id); $ui.viewSelect.selectItem(data.id);
}); });
@ -606,6 +765,22 @@ $ui.statusbar = new Ox.Bar({
Ox.Event.bind("change_find", function(event, data) { Ox.Event.bind("change_find", function(event, data) {
$ui.findInput.changeLabel(data.id); $ui.findInput.changeLabel(data.id);
}); });
Ox.Event.bind("click_query", function(event, data) {
var $dialog = new Ox.Dialog({
buttons: [
{
click: function() {
$dialog.close();
},
id: "close",
title: "Close",
value: "Close"
}
],
id: "query",
title: "Query"
}).append(Query.toString() + "<br/><br/>" + JSON.stringify(Query.toObject())).open();
});
// Toolbar // Toolbar
@ -617,9 +792,15 @@ $ui.statusbar = new Ox.Bar({
}); });
Ox.Event.bind("submit_findInput", function(event, data) { Ox.Event.bind("submit_findInput", function(event, data) {
findCondition = { var query;
key: data.key == "all" ? "" : data.key, user.ui.find = {
value: data.value, conditions: [
{
key: data.key == "all" ? "" : data.key,
value: data.value,
operator: ""
}
],
operator: "" operator: ""
}; };
$.each(groups, function(i, group) { $.each(groups, function(i, group) {
@ -629,7 +810,7 @@ $ui.statusbar = new Ox.Bar({
delete options.keys; delete options.keys;
return app.request("find", $.extend(options, { return app.request("find", $.extend(options, {
group: group.id, group: group.id,
query: constructQuery(group.id) query: Query.toObject(group.id)
}), options.callback); }), options.callback);
} }
}); });
@ -637,27 +818,30 @@ $ui.statusbar = new Ox.Bar({
$ui.list.options({ $ui.list.options({
request: function(options) { request: function(options) {
return app.request("find", $.extend(options, { return app.request("find", $.extend(options, {
query: constructQuery() query: query = Query.toObject()
}), options.callback); }), options.callback);
} }
}) });
location.hash = Query.toString(query);
}); });
// Groups // Groups
$.each(groups, function(i, group) { $.each(groups, function(i, group) {
Ox.Event.bind("select_group_" + group.id, function(event, data) { Ox.Event.bind("select_group_" + group.id, function(event, data) {
var query;
groups[i].conditions = $.map(data.ids, function(v) {
return {
key: group.id,
value: v,
operator: "="
};
});
query = Query.toObject();
$ui.list.options({ $ui.list.options({
request: function(options) { request: function(options) {
groups[i].conditions = $.map(data.ids, function(v) {
return {
key: group.id,
value: v,
operator: "="
};
});
return app.request("find", $.extend(options, { return app.request("find", $.extend(options, {
query: constructQuery() query: query
}), options.callback); }), options.callback);
} }
}); });
@ -668,12 +852,13 @@ $ui.statusbar = new Ox.Bar({
delete options.keys; delete options.keys;
return app.request("find", $.extend(options, { return app.request("find", $.extend(options, {
group: group_.id, group: group_.id,
query: constructQuery(group_.id) query: Query.toObject(group_.id)
}), options.callback); }), options.callback);
} }
}); });
} }
}); });
location.hash = Query.toString(query);
}); });
}); });
@ -848,8 +1033,9 @@ $ui.statusbar = new Ox.Bar({
}), }),
id: "list", id: "list",
request: function(options) { request: function(options) {
Ox.print("options, Query.toObject", options, Query.toObject())
app.request("find", $.extend(options, { app.request("find", $.extend(options, {
query: constructQuery() query: Query.toObject()
}), options.callback); }), options.callback);
}, },
sort: user.ui.sort sort: user.ui.sort
@ -870,7 +1056,7 @@ $ui.statusbar = new Ox.Bar({
keys: ["director", "id", "posterHeight", "posterWidth", "posterURL", "title"], keys: ["director", "id", "posterHeight", "posterWidth", "posterURL", "title"],
request: function(options) { request: function(options) {
app.request("find", $.extend(options, { app.request("find", $.extend(options, {
query: constructQuery() query: Query.toObject()
}), options.callback); }), options.callback);
}, },
size: 128, size: 128,
@ -886,21 +1072,6 @@ $ui.statusbar = new Ox.Bar({
return $list; return $list;
} }
function constructQuery(groupId) {
var conditions = $.merge(!Ox.isUndefined(user.ui.find.key) ? [user.ui.find] : [], groups ? $.map(groups, function(v, i) {
if (v.id != groupId && v.conditions.length) {
return v.conditions.length == 1 ? v.conditions : {
conditions: v.conditions,
operator: "|"
};
}
}) : []);
return {
conditions: conditions,
operator: conditions.length ? "," : ""
};
}
function constructStatus(key, data) { function constructStatus(key, data) {
return Ox.toTitleCase(key) + ": " + [ return Ox.toTitleCase(key) + ": " + [
Ox.formatNumber(data.items) + " movie" + (data.items != 1 ? "s" : ""), Ox.formatNumber(data.items) + " movie" + (data.items != 1 ? "s" : ""),
@ -1399,7 +1570,7 @@ $ui.statusbar = new Ox.Bar({
app.request("find", $.extend(options, { app.request("find", $.extend(options, {
query: { query: {
conditions: [], conditions: [],
operator: "&" operator: "," // fixme: should be &
} }
}), options.callback); }), options.callback);
}, },