1/** 2 * stacktable.js 3 * Author & copyright (c) 2012: John Polacek 4 * CardTable by: Justin McNally (2015) 5 * MIT license 6 * 7 * Page: http://johnpolacek.github.com/stacktable.js 8 * Repo: https://github.com/johnpolacek/stacktable.js/ 9 * 10 * jQuery plugin for stacking tables on small screens 11 * Requires jQuery version 1.7 or above 12 * 13 */ 14;(function($) { 15 $.fn.cardtable = function(options) { 16 var $tables = this, 17 defaults = {headIndex:0}, 18 settings = $.extend({}, defaults, options), 19 headIndex; 20 21 // checking the "headIndex" option presence... or defaults it to 0 22 if(options && options.headIndex) 23 headIndex = options.headIndex; 24 else 25 headIndex = 0; 26 27 return $tables.each(function() { 28 var $table = $(this); 29 if ($table.hasClass('stacktable')) { 30 return; 31 } 32 var table_css = $(this).prop('class'); 33 var $stacktable = $('<div></div>'); 34 if (typeof settings.myClass !== 'undefined') $stacktable.addClass(settings.myClass); 35 var markup = ''; 36 var $caption, $topRow, headMarkup, bodyMarkup, tr_class; 37 38 $table.addClass('stacktable large-only'); 39 40 $caption = $table.find(">caption").clone(); 41 $topRow = $table.find('>thead>tr,>tbody>tr,>tfoot>tr,>tr').eq(0); 42 43 // avoid duplication when paginating 44 $table.siblings().filter('.small-only').remove(); 45 46 // using rowIndex and cellIndex in order to reduce ambiguity 47 $table.find('>tbody>tr').each(function() { 48 49 // declaring headMarkup and bodyMarkup, to be used for separately head and body of single records 50 headMarkup = ''; 51 bodyMarkup = ''; 52 tr_class = $(this).prop('class'); 53 // for the first row, "headIndex" cell is the head of the table 54 // for the other rows, put the "headIndex" cell as the head for that row 55 // then iterate through the key/values 56 $(this).find('>td,>th').each(function(cellIndex) { 57 if ($(this).html() !== ''){ 58 bodyMarkup += '<tr class="' + tr_class +'">'; 59 if ($topRow.find('>td,>th').eq(cellIndex).html()){ 60 bodyMarkup += '<td class="st-key">'+$topRow.find('>td,>th').eq(cellIndex).html()+'</td>'; 61 } else { 62 bodyMarkup += '<td class="st-key"></td>'; 63 } 64 bodyMarkup += '<td class="st-val '+$(this).prop('class') +'">'+$(this).html()+'</td>'; 65 bodyMarkup += '</tr>'; 66 } 67 }); 68 69 markup += '<table class=" '+ table_css +' stacktable small-only"><tbody>' + headMarkup + bodyMarkup + '</tbody></table>'; 70 }); 71 72 $table.find('>tfoot>tr>td').each(function(rowIndex,value) { 73 if ($.trim($(value).text()) !== '') { 74 markup += '<table class="'+ table_css + ' stacktable small-only"><tbody><tr><td>' + $(value).html() + '</td></tr></tbody></table>'; 75 } 76 }); 77 78 $stacktable.prepend($caption); 79 $stacktable.append($(markup)); 80 $table.before($stacktable); 81 }); 82 }; 83 84 $.fn.stacktable = function(options) { 85 var $tables = this, 86 defaults = {headIndex:0,displayHeader:true}, 87 settings = $.extend({}, defaults, options), 88 headIndex; 89 90 // checking the "headIndex" option presence... or defaults it to 0 91 if(options && options.headIndex) 92 headIndex = options.headIndex; 93 else 94 headIndex = 0; 95 96 return $tables.each(function() { 97 var table_css = $(this).prop('class'); 98 var $stacktable = $('<table class="'+ table_css +' stacktable small-only"><tbody></tbody></table>'); 99 if (typeof settings.myClass !== 'undefined') $stacktable.addClass(settings.myClass); 100 var markup = ''; 101 var $table, $caption, $topRow, headMarkup, bodyMarkup, tr_class, displayHeader; 102 103 $table = $(this); 104 $table.addClass('stacktable large-only'); 105 $caption = $table.find(">caption").clone(); 106 $topRow = $table.find('>thead>tr,>tbody>tr,>tfoot>tr').eq(0); 107 108 displayHeader = $table.data('display-header') === undefined ? settings.displayHeader : $table.data('display-header'); 109 110 // using rowIndex and cellIndex in order to reduce ambiguity 111 $table.find('>tbody>tr, >thead>tr').each(function(rowIndex) { 112 113 // declaring headMarkup and bodyMarkup, to be used for separately head and body of single records 114 headMarkup = ''; 115 bodyMarkup = ''; 116 tr_class = $(this).prop('class'); 117 118 // for the first row, "headIndex" cell is the head of the table 119 if (rowIndex === 0) { 120 // the main heading goes into the markup variable 121 if (displayHeader) { 122 markup += '<tr class=" '+tr_class +' "><th class="st-head-row st-head-row-main" colspan="2">'+$(this).find('>th,>
122td').eq(headIndex).html()+'</th></tr>'; 123 } 124 } else { 125 // for the other rows, put the "headIndex" cell as the head for that row 126 // then iterate through the key/values 127 $(this).find('>td,>th').each(function(cellIndex) { 128 if (cellIndex === headIndex) { 129 headMarkup = '<tr class="'+ tr_class+'"><th class="st-head-row" colspan="2">'+$(this).html()+'</th></tr>'; 130 } else { 131 if ($(this).html() !== ''){ 132 bodyMarkup += '<tr class="' + tr_class +'">'; 133 if ($topRow.find('>td,>th').eq(cellIndex).html()){ 134 bodyMarkup += '<td class="st-key">'+$topRow.find('>td,>th').eq(cellIndex).html()+'</td>'; 135 } else { 136 bodyMarkup += '<td class="st-key"></td>'; 137 } 138 bodyMarkup += '<td class="st-val '+$(this).prop('class') +'">'+$(this).html()+'</td>'; 139 bodyMarkup += '</tr>'; 140 } 141 } 142 }); 143 144 markup += headMarkup + bodyMarkup; 145 } 146 }); 147 148 $stacktable.prepend($caption); 149 $stacktable.append($(markup)); 150 $table.before($stacktable); 151 }); 152 }; 153 154 $.fn.stackcolumns = function(options) { 155 var $tables = this, 156 defaults = {}, 157 settings = $.extend({}, defaults, options); 158 159 return $tables.each(function() { 160 var $table = $(this); 161 var $caption = $table.find(">caption").clone(); 162 var num_cols = $table.find('>thead>tr,>tbody>tr,>tfoot>tr').eq(0).find('>td,>th').length; //first table <tr> must not contain colspans, or add sum(colspan-1) here. 163 if(num_cols<3) //stackcolumns has no effect on tables with less than 3 columns 164 return; 165 166 var $stackcolumns = $('<table class="stacktable small-only"></table>'); 167 if (typeof settings.myClass !== 'undefined') $stackcolumns.addClass(settings.myClass); 168 $table.addClass('stacktable large-only'); 169 var tb = $('<tbody></tbody>'); 170 var col_i = 1; //col index starts at 0 -> start copy at second column. 171 172 while (col_i < num_cols) { 173 $table.find('>thead>tr,>tbody>tr,>tfoot>tr').each(function(index) { 174 var tem = $('<tr></tr>'); // todo opt. copy styles of $this; todo check if parent is thead or tfoot to handle accordingly 175 if(index === 0) tem.addClass("st-head-row st-head-row-main"); 176 var first = $(this).find('>td,>th').eq(0).clone().addClass("st-key"); 177 var target = col_i; 178 // if colspan apply, recompute target for second cell. 179 if ($(this).find("*[colspan]").length) { 180 var i =0; 181 $(this).find('>td,>th').each(function() { 182 var cs = $(this).attr("colspan"); 183 if (cs) { 184 cs = parseInt(cs, 10); 185 target -= cs-1; 186 if ((i+cs) > (col_i)) //out of current bounds 187 target += i + cs - col_i -1; 188 i += cs; 189 } else { 190 i++; 191 } 192 193 if (i > col_i) 194 return false; //target is set; break. 195 }); 196 } 197 var second = $(this).find('>td,>th').eq(target).clone().addClass("st-val").removeAttr("colspan"); 198 tem.append(first, second); 199 tb.append(tem); 200 }); 201 ++col_i; 202 } 203 204 $stackcolumns.append($(tb)); 205 $stackcolumns.prepend($caption); 206 $table.before($stackcolumns); 207 }); 208 }; 209 210}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.