/*
* Dynamic pvl.verkko.table frontend.
*/
function html (tag, params, html) {
return $("<" + tag + " />", $.extend({html: html}, params));
};
/*
* item_url - base URL path to /items/0 by id=0
* columns - [ column_name ]
* animate - attempt to animate table..
*/
function Table (table, params) {
var tbody = table.children('tbody');
var animate = params.animate || false;
if (animate)
table.css('display', 'block');
/*
* Render <tr> for item.
*/
function render_tr (item) {
var columns = [
html("th", {},
html("a", { href: params.item_url.replace('0', item.id) }, "#" )
),
];
$.each(params.columns, function (i, column) {
var col = item[column];
var td = html("td", { title: col.title }, col.html);
$.each(col.css, function (i, css) {
td.addClass(css);
});
columns.push(td);
});
return html("tr", { id: item.id }, columns);
}
return {
animate: animate,
/*
* Update given item into our <table>
*/
update: function (item) {
var tr = $('#' + item.id);
if (tr.length) {
if (animate) tr.slideUp();
} else {
tr = render_tr(item)
if (animate) tr.hide();
}
// move to top
tr.prependTo(tbody);
if (animate)
tr.slideDown();
else
tr.effect("highlight", {}, 'slow');
}
}
};