oxjstmp/demos/test/list.js

638 lines
20 KiB
JavaScript
Raw Normal View History

2010-06-25 15:55:25 +00:00
$(function() {
Ox.theme("modern");
var $body = $("body"),
2010-06-30 15:49:49 +00:00
$document = $(document),
2010-06-25 15:55:25 +00:00
app = new Ox.App({
2010-06-30 21:41:27 +00:00
// requestURL: "http://blackbook.local:8000/api/"
requestURL: "http://lion.oil21.org:8000/api/"
2010-06-25 15:55:25 +00:00
}),
2010-07-02 12:33:45 +00:00
findCondition = {},
2010-06-30 15:49:49 +00:00
groups = [
{
2010-07-02 12:33:45 +00:00
conditions: [],
2010-06-30 15:49:49 +00:00
id: "director",
title: "Director"
},
{
2010-07-02 12:33:45 +00:00
conditions: [],
2010-06-30 15:49:49 +00:00
id: "country",
title: "Country"
},
{
2010-07-02 12:33:45 +00:00
conditions: [],
2010-06-30 15:49:49 +00:00
id: "year",
title: "Year"
},
{
2010-07-02 12:33:45 +00:00
conditions: [],
2010-06-30 15:49:49 +00:00
id: "language",
title: "Language"
},
{
2010-07-02 12:33:45 +00:00
conditions: [],
2010-06-30 15:49:49 +00:00
id: "genre",
title: "Genre"
}
],
2010-06-30 18:47:10 +00:00
$group = [],
2010-06-30 15:49:49 +00:00
elements = [],
2010-07-01 12:26:32 +00:00
rightPanelWidth = $document.width() - 256;
2010-07-01 23:51:08 +00:00
2010-06-30 15:49:49 +00:00
$.each(groups, function(i, v) {
2010-07-01 23:51:08 +00:00
var size = rightPanelWidth / 5 + (rightPanelWidth % 5 > i);
2010-06-30 18:47:10 +00:00
$group[i] = new Ox.TextList({
columns: [
{
align: "left",
id: "name",
operator: v.id == "year" ? "-" : "+",
title: v.title,
unique: true,
visible: true,
2010-07-01 23:51:08 +00:00
width: size - 40 - ($.browser.mozilla ? 16 : 12)
2010-06-30 15:49:49 +00:00
},
2010-06-30 18:47:10 +00:00
{
align: "right",
id: "items",
operator: "-",
2010-07-01 23:51:08 +00:00
title: "#",
2010-06-30 18:47:10 +00:00
visible: true,
2010-07-01 23:51:08 +00:00
width: 40
2010-06-30 18:47:10 +00:00
}
],
id: "group_" + v.id,
request: function(options) {
delete options.keys;
app.request("find", $.extend(options, {
group: v.id,
2010-07-02 12:33:45 +00:00
query: constructQuery()
2010-06-30 18:47:10 +00:00
}), options.callback);
},
sort: [
{
2010-07-01 23:51:08 +00:00
key: v.id == "year" ? "name" : "items",
operator: "-"
2010-06-30 18:47:10 +00:00
}
]
});
elements.push({
element: $group[i],
2010-06-30 21:41:27 +00:00
resizable: true,
2010-06-30 15:49:49 +00:00
size: size
});
});
2010-07-01 12:26:32 +00:00
2010-07-01 23:51:08 +00:00
var $groupsInnerPanel = new Ox.SplitPanel({
elements: [
elements[1],
elements[2],
elements[3]
],
2010-06-30 15:49:49 +00:00
orientation: "horizontal"
}),
2010-07-01 12:26:32 +00:00
2010-07-01 23:51:08 +00:00
$groupsOuterPanel = new Ox.SplitPanel({
elements: [
elements[0],
{
element: $groupsInnerPanel
},
elements[4]
],
orientation: "horizontal"
})
2010-07-05 16:52:12 +00:00
$list = constructList("text");
2010-07-05 07:09:34 +00:00
2010-07-01 12:26:32 +00:00
$toolBar = Ox.Bar({
2010-07-01 15:06:04 +00:00
size: 24
}),
$button = Ox.Button({
id: "groupsButton",
value: ["Show Groups", "Hide Groups"]
}).css({
float: "left",
margin: "4px"
}).width(80).appendTo($toolBar),
2010-07-01 12:26:32 +00:00
$select = Ox.Select({
2010-07-01 15:06:04 +00:00
id: "viewSelect",
2010-07-01 12:26:32 +00:00
items: [
{
checked: true,
id: "list",
2010-07-01 15:06:04 +00:00
title: "View as List"
2010-07-01 12:26:32 +00:00
},
{
id: "icons",
2010-07-01 15:06:04 +00:00
title: "View as Icons"
},
{
id: "clips",
title: "View with Clips"
},
{
id: "timelines",
title: "View with Timelines"
},
{
2010-07-05 07:09:34 +00:00
id: "maps",
2010-07-01 15:06:04 +00:00
title: "View with Maps"
},
{
2010-07-05 07:09:34 +00:00
id: "calendars",
2010-07-01 15:06:04 +00:00
title: "View with Calendars"
},
{
2010-07-05 07:09:34 +00:00
id: "clip",
2010-07-01 15:06:04 +00:00
title: "View as Clips"
},
{
2010-07-05 07:09:34 +00:00
id: "map",
2010-07-01 15:06:04 +00:00
title: "View on Map"
},
{
2010-07-05 07:09:34 +00:00
id: "calendar",
2010-07-01 15:06:04 +00:00
title: "View on Calendar"
},
2010-07-01 12:26:32 +00:00
]
}).css({
2010-07-01 15:06:04 +00:00
float: "left",
2010-07-01 12:26:32 +00:00
margin: "4px"
2010-07-01 15:06:04 +00:00
}).width(120).appendTo($toolBar);
$input = new Ox.Input({
2010-07-02 12:33:45 +00:00
autocomplete: function(key, value, callback) {
Ox.print("autocomplete", key, value);
value === "" && Ox.print("Warning: autocomplete function should never be called with empty value");
if (key == "all") {
callback();
} else {
app.request("find", {
keys: [key],
query: {
conditions: [
{
key: key,
value: value,
2010-07-02 17:37:29 +00:00
operator: ""
2010-07-02 12:33:45 +00:00
}
],
operator: ""
},
sort: [
{
key: key,
operator: "+"
}
],
range: [0, 10]
}, function(result) {
2010-07-02 15:20:46 +00:00
Ox.print("calling callback", result.data.items)
2010-07-02 12:33:45 +00:00
callback($.map(result.data.items, function(v) {
return v.title;
}))
});
}
2010-07-01 15:06:04 +00:00
},
clear: true,
highlight: true,
2010-07-01 15:06:04 +00:00
id: "find",
label: [
2010-07-02 12:33:45 +00:00
{ id: "all", title: "Find: All" },
{ id: "title", title: "Find: Title" },
{ id: "director", title: "Find: Director" },
{ id: "country", title: "Find: Country" },
{ id: "year", title: "Find: Year" },
{ id: "language", title: "Find: Language" },
{ id: "writer", title: "Find: Writer" },
{ id: "producer", title: "Find: Producer" },
{ id: "cinematographer", title: "Find: Cinematographer" },
{ id: "editor", title: "Find: Editor" },
{ id: "actor", title: "Find: Actor" },
{ id: "character", title: "Find: Character" },
{ id: "name", title: "Find: Name" },
{ id: "genre", title: "Find: Genre" },
{ id: "keyword", title: "Find: Keyword" },
{ id: "summary", title: "Find: Summary" },
{ id: "dialog", title: "Find: Dialog" }
2010-07-01 15:06:04 +00:00
],
labelWidth: 85
}).css({
float: "right",
margin: "4px"
}).width(300).appendTo($toolBar);
2010-07-01 12:26:32 +00:00
$contentPanel = new Ox.SplitPanel({
2010-06-30 15:49:49 +00:00
elements: [
{
2010-07-01 23:51:08 +00:00
element: $groupsOuterPanel,
2010-06-30 15:49:49 +00:00
size: 128
},
{
2010-07-01 12:26:32 +00:00
element: $list
}
],
orientation: "vertical"
}),
$statusBar = new Ox.Bar({
size: 16
}).attr({
id: "statusBar"
}),
2010-07-05 16:52:12 +00:00
$totals = new Ox.Element()
.attr({
id: "totals"
})
.appendTo($statusBar),
2010-07-01 12:26:32 +00:00
$leftPanel = new Ox.Container(),
$rightPanel = new Ox.SplitPanel({
elements: [
{
element: $toolBar,
2010-06-30 15:49:49 +00:00
size: 24
},
{
2010-07-01 12:26:32 +00:00
element: $contentPanel
},
{
element: $statusBar,
size: 16
2010-06-30 15:49:49 +00:00
}
],
orientation: "vertical"
2010-07-01 12:26:32 +00:00
}),
2010-07-01 23:51:08 +00:00
$loadingIcon = new Ox.LoadingIcon({
size: "medium"
});
2010-07-01 12:26:32 +00:00
$mainMenu = new Ox.MainMenu({
2010-07-01 23:51:08 +00:00
extras: [
$loadingIcon
],
2010-07-01 12:26:32 +00:00
menus: [
{
id: "oxdb",
title: "OxDB",
2010-07-01 12:26:32 +00:00
items: [
{ id: "about", title: "About" }
]
},
{
id: "sort",
title: "Sort",
items: [
{ id: "sort_movies", title: "Sort Movies by", items: [
{ checked: false, group: "sort_movies", id: "title", title: "Title"},
{ checked: false, group: "sort_movies", id: "director", title: "Director" },
{ checked: false, group: "sort_movies", id: "country", title: "Country" },
{ checked: true, group: "sort_movies", id: "year", title: "Year" },
{ checked: false, group: "sort_movies", id: "language", title: "Language" },
2010-07-01 15:18:33 +00:00
{ checked: false, group: "sort_movies", id: "runtime", title: "Runtime" },
2010-07-01 12:26:32 +00:00
{ checked: false, group: "sort_movies", id: "genre", title: "Genre" },
] },
{ id: "order_movies", title: "Order Movies", items: [
{ checked: false, group: "order_movies", id: "ascending", title: "Ascending"},
{ checked: true, group: "order_movies", id: "descending", title: "Descending" },
] }
]
},
2010-07-02 12:33:45 +00:00
{
id: "debug",
title: "Debug",
items: [
{ id: "show_query", title: "Show Query" }
]
}
2010-07-01 12:26:32 +00:00
],
2010-07-01 15:06:04 +00:00
size: "medium"
2010-07-01 12:26:32 +00:00
}),
$mainPanel = new Ox.SplitPanel({
2010-06-29 13:22:39 +00:00
elements: [
{
2010-07-01 12:26:32 +00:00
element: $leftPanel,
size: 256
},
{
element: $rightPanel
}
]
}),
$appPanel = Ox.SplitPanel({
elements: [
{
element: $mainMenu,
2010-07-01 15:06:04 +00:00
size: 20
2010-06-29 13:22:39 +00:00
},
{
2010-07-01 12:26:32 +00:00
element: $mainPanel
2010-06-29 13:22:39 +00:00
}
],
orientation: "vertical"
2010-06-25 15:55:25 +00:00
}).appendTo($body);
2010-07-01 12:26:32 +00:00
2010-07-01 23:51:08 +00:00
Ox.Request.requests() && $loadingIcon.start();
Ox.Event.bind(null, "requestStart", function() {
Ox.print("start")
$loadingIcon.start();
});
Ox.Event.bind(null, "requestStop", function() {
Ox.print("stop")
$loadingIcon.stop();
});
2010-07-05 07:09:34 +00:00
Ox.Event.bind(null, "change_viewSelect", function(event, data) {
2010-07-05 16:52:12 +00:00
$list.replaceWith(constructList(data.id));
2010-07-05 07:09:34 +00:00
});
2010-07-02 16:32:16 +00:00
Ox.Event.bind(null, "submit_find", function(event, data) {
findCondition = {
key: data.key == "all" ? "" : data.key,
value: data.value,
2010-07-02 17:37:29 +00:00
operator: ""
2010-07-02 16:32:16 +00:00
};
$.each(groups, function(i, group) {
2010-07-02 17:37:29 +00:00
groups[i].conditions = [];
2010-07-02 16:32:16 +00:00
$group[i].options({
request: function(options) {
delete options.keys;
return app.request("find", $.extend(options, {
group: group.id,
query: constructQuery(group.id)
}), options.callback);
}
});
});
$list.options({
request: function(options) {
return app.request("find", $.extend(options, {
query: constructQuery()
}), options.callback);
}
})
});
2010-06-30 18:47:10 +00:00
$.each(groups, function(i, group) {
Ox.Event.bind(null, "select_group_" + group.id, function(event, data) {
$list.options({
request: function(options) {
2010-07-02 12:33:45 +00:00
groups[i].conditions = $.map(data.ids, function(v) {
return {
key: group.id,
value: v,
operator: "="
};
});
2010-06-30 21:41:27 +00:00
return app.request("find", $.extend(options, {
2010-07-02 12:33:45 +00:00
query: constructQuery()
2010-06-30 18:47:10 +00:00
}), options.callback);
}
});
$.each(groups, function(i_, group_) {
if (i_ != i) {
$group[i_].options({
request: function(options) {
delete options.keys;
2010-06-30 21:41:27 +00:00
return app.request("find", $.extend(options, {
2010-06-30 18:47:10 +00:00
group: group_.id,
2010-07-02 12:33:45 +00:00
query: constructQuery(group_.id)
2010-06-30 18:47:10 +00:00
}), options.callback);
}
2010-07-02 16:32:16 +00:00
});
2010-06-30 18:47:10 +00:00
}
});
});
});
2010-06-30 21:41:27 +00:00
Ox.Event.bind(null, "change_sort_movies", function(event, data) {
2010-07-01 12:26:32 +00:00
});
Ox.Event.bind(null, "load_list", function(event, data) {
2010-07-05 16:52:12 +00:00
$totals.html(constructStatus({
"total": data,
"selected": {}
}));
2010-07-01 15:06:04 +00:00
var html = [
2010-07-05 16:52:12 +00:00
Ox.formatNumber(data.items) + " movie" + (data.items != 1 ? "s" : ""),
2010-07-01 15:06:04 +00:00
Ox.formatDuration(data.runtime, "medium"),
data.files + " file" + (data.files != 1 ? "s" : ""),
Ox.formatDuration(data.duration, "short"),
Ox.formatValue(data.size, "B"),
2010-07-01 12:26:32 +00:00
Ox.formatValue(data.pixels, "px")
];
2010-07-05 16:52:12 +00:00
$totals.html("Total: " + constructStatus(data) + " — Selected: " + constructStatus({
duration: 0,
files: 0,
items: 0,
pixels: 0,
runtime: 0,
size: 0
}));
2010-06-30 21:41:27 +00:00
});
Ox.Event.bind(null, "sort_list", function(event, data) {
});
2010-07-05 16:52:12 +00:00
Ox.Event.bind(null, "select_list", function(event, data) {
});
2010-07-02 12:33:45 +00:00
Ox.Event.bind(null, "click_show_query", function(event, data) {
var query = constructQuery(),
html = "Conditions<br/><br/>" + $.map(query.conditions, function(v) {
return v.key + " " + v.operator + " " + v.value;
}).join("<br/>") + "<br/><br/>Operator: " + query.operator,
$dialog = new Ox.Dialog({
title: "Show Query",
buttons: [
{
value: "Close",
click: function() {
$dialog.close();
}
}
]
})
.append(html)
.open();
});
2010-07-05 16:52:12 +00:00
function constructList(view) {
var $list;
if (view == "text") {
$list = new Ox.TextList({
columns: [
{
align: "left",
id: "id",
operator: "+",
title: "ID",
unique: true,
visible: true,
width: 80
},
{
align: "left",
id: "title",
operator: "+",
title: "Title",
visible: true,
width: 160
},
{
align: "left",
id: "director",
operator: "+",
title: "Director",
visible: true,
width: 160
},
{
align: "left",
id: "country",
operator: "+",
title: "Country",
visible: true,
width: 120
},
{
align: "right",
id: "year",
operator: "-",
title: "Year",
visible: true,
width: 60
},
{
align: "left",
id: "language",
operator: "+",
title: "Language",
visible: true,
width: 120
},
{
align: "right",
id: "runtime",
operator: "-",
title: "Runtime",
visible: true,
width: 80
},
{
align: "left",
id: "genre",
operator: "+",
title: "Genre",
visible: true,
width: 120
},
{
align: "right",
id: "rating",
operator: "-",
title: "Rating",
width: 80
},
{
align: "right",
id: "votes",
operator: "-",
title: "Votes",
width: 80
}
],
id: "list",
request: function(options) {
app.request("find", $.extend(options, {
query: constructQuery()
}), options.callback);
},
sort: [
{
key: "director",
operator: "+"
}
]
});
} else if (view == "icons") {
$list = new Ox.IconList({
id: "list",
item: function(data, sort, size) {
return {
height: data.posterHeight,
id: data["id"],
info: data[$.inArray(sort[0].key, ["title", "director"]) > -1 ? "year" : sort[0].key],
title: data.title + (data.director ? " (" + data.director + ")" : ""),
url: "http://0xdb.org/" + data.id + "/poster." + size + "." + "jpg",
width: data.posterWidth
};
},
keys: ["director", "id", "posterHeight", "posterWidth", "posterURL", "title"],
request: function(options) {
app.request("find", $.extend(options, {
query: constructQuery()
}), options.callback);
},
size: 128,
sort: [
{
key: "director",
operator: "+"
}
],
unique: "id"
});
}
return $list;
}
2010-07-02 12:33:45 +00:00
function constructQuery(groupId) {
2010-07-02 16:32:16 +00:00
var conditions = $.merge(!Ox.isUndefined(findCondition.key) ? [findCondition] : [], $.map(groups, function(v, i) {
2010-07-02 12:33:45 +00:00
if (v.id != groupId) {
return v.conditions;
}
}));
/*
Ox.print("conditions", conditions, "groups conditions", $.map(groups, function(v) {
return v.conditions;
}));
*/
return {
conditions: conditions,
operator: conditions.length ? "," : ""
};
}
2010-07-05 16:52:12 +00:00
function constructStatus(data) {
var html = [];
$.each(data, function(k, v) {
html.push(Ox.toTitleCase(k) + ": " + [
Ox.formatNumber(data.items) + " movie" + (data.items != 1 ? "s" : ""),
Ox.formatDuration(data.runtime, "medium"),
data.files + " file" + (data.files != 1 ? "s" : ""),
Ox.formatDuration(data.duration, "short"),
Ox.formatValue(data.size, "B"),
Ox.formatValue(data.pixels, "px")
].join(", "));
})
return html.join(" &mdash; ");
}
2010-07-02 12:33:45 +00:00
function getGroupById(id) { // unused
$.each(groups, function(i, v) {
if (v.id == id) {
return i;
}
});
return -1;
}
2010-06-25 15:55:25 +00:00
});