blob: 54df9946b08f9a9be99f9ac2c1837f380255936d [file] [log] [blame]
/**
* @module Stacktable
* @author John Polacek
* @see http://johnpolacek.github.com/stacktable.js
* @license MIT & GPL license
*/
(function(a){a.fn.cardtable=function(b){var c=a.extend({},{id:"stacktable small-only",hideOriginal:!0,headIndex:0},b);headIndex=b&&b.headIndex?b.headIndex:0;return this.each(function(){$table=a(this);if(!$table.hasClass("stacktable")){var k=a(this).prop("class"),d=a("<div></div>");"undefined"!==typeof c.myClass&&d.addClass(c.myClass);var b="";$table.addClass("stacktable large-only");$caption=$table.find("caption").clone();$topRow=$table.find("tr").eq(0);$table.find("tbody tr").each(function(d,h){bodyMarkup=
headMarkup="";tr_class=a(this).prop("class");a(this).find("td,th").each(function(b,c){""!==a(this).html()&&(bodyMarkup+='<tr class="'+tr_class+'">',bodyMarkup=$topRow.find("td,th").eq(b).html()?bodyMarkup+('<td class="st-key">'+$topRow.find("td,th").eq(b).html()+"</td>"):bodyMarkup+'<td class="st-key"></td>',bodyMarkup+='<td class="st-val '+a(this).prop("class")+'">'+a(this).html()+"</td>",bodyMarkup+="</tr>")});b+='<table class=" '+k+" "+c.id+'"><tbody>'+headMarkup+bodyMarkup+"</tbody></table>"});
$table.find("tfoot tr td").each(function(d,h){""!==a.trim(a(h).text())&&(b+='<table class="'+k+" "+c.id+'"><tbody><tr><td>'+a(h).html()+"</td></tr></tbody></table>")});d.prepend($caption);d.append(a(b));$table.before(d);c.hideOriginal||$table.show()}})};a.fn.stacktable=function(b){var c=a.extend({},{id:"stacktable small-only",hideOriginal:!0,headIndex:0},b);headIndex=b&&b.headIndex?b.headIndex:0;return this.each(function(){var b=a(this).prop("class"),b=a('<table class="'+b+" "+c.id+'"><tbody></tbody></table>');
"undefined"!==typeof c.myClass&&b.addClass(c.myClass);var d="";$table=a(this);$table.addClass("stacktable large-only");$caption=$table.find("caption").clone();$topRow=$table.find("tr").eq(0);$table.find("tr").each(function(b,c){bodyMarkup=headMarkup="";tr_class=a(this).prop("class");0===b?d+='<tr class=" '+tr_class+' "><th class="st-head-row st-head-row-main" colspan="2">'+a(this).find("th,td").eq(headIndex).html()+"</th></tr>":(a(this).find("td,th").each(function(b,c){b===headIndex?headMarkup='<tr class="'+
tr_class+'"><th class="st-head-row" colspan="2">'+a(this).html()+"</th></tr>":""!==a(this).html()&&(bodyMarkup+='<tr class="'+tr_class+'">',bodyMarkup=$topRow.find("td,th").eq(b).html()?bodyMarkup+('<td class="st-key">'+$topRow.find("td,th").eq(b).html()+"</td>"):bodyMarkup+'<td class="st-key"></td>',bodyMarkup+='<td class="st-val '+a(this).prop("class")+'">'+a(this).html()+"</td>",bodyMarkup+="</tr>")}),d+=headMarkup+bodyMarkup)});b.prepend($caption);b.append(a(d));$table.before(b);c.hideOriginal||
$table.show()})};a.fn.stackcolumns=function(b){var c=a.extend({},{id:"stacktable small-only",hideOriginal:!0},b);return this.each(function(){$table=a(this);var b=$table.find("tr").eq(0).find("td,th").length;if(!(3>b)){var d=a('<table class="'+c.id+'"></table>');"undefined"!==typeof c.myClass&&d.addClass(c.myClass);$table.addClass("stacktable large-only");for(var l=a("<tbody></tbody>"),e=1;e<b;)$table.find("tr").each(function(b,c){var d=a("<tr></tr>");0===b&&d.addClass("st-head-row st-head-row-main");
first=a(this).find("td,th").eq(0).clone().addClass("st-key");var g=e;if(a(this).find("*[colspan]").length){var f=0;a(this).find("td,th").each(function(b,d){var c=a(this).attr("colspan");c?(c=parseInt(c,10),g-=c-1,f+c>e&&(g+=f+c-e-1),f+=c):f++;if(f>e)return!1})}second=a(this).find("td,th").eq(g).clone().addClass("st-val").removeAttr("colspan");d.append(first,second);l.append(d)}),++e;d.append(a(l));$table.before(d);c.hideOriginal||$table.show()}})}})(jQuery);