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