| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
;(function($) { |
| 8 |
'use strict'; |
| 9 |
var ts = $.tablesorter; |
| 10 |
|
| 11 |
ts.alignChar = { |
| 12 |
|
| 13 |
init : function(table, c, wo) { |
| 14 |
c.$headers.filter('[' + wo.alignChar_charAttrib + ']').each(function() { |
| 15 |
var $this = $(this), |
| 16 |
vars = { |
| 17 |
column : this.column, |
| 18 |
align : $this.attr(wo.alignChar_charAttrib), |
| 19 |
alignIndex : parseInt( $this.attr(wo.alignChar_indexAttrib) || 0, 10), |
| 20 |
adjust : parseFloat($this.attr(wo.alignChar_adjustAttrib)) || 0 |
| 21 |
}; |
| 22 |
vars.regex = new RegExp('\\' + vars.align, 'g'); |
| 23 |
if (typeof vars.align !== 'undefined') { |
| 24 |
wo.alignChar_savedVars[this.column] = vars; |
| 25 |
ts.alignChar.setup(table, c, wo, vars); |
| 26 |
} |
| 27 |
}); |
| 28 |
}, |
| 29 |
|
| 30 |
setup: function(table, c, wo, v) { |
| 31 |
|
| 32 |
if ($.isEmptyObject(c.cache)) { return; } |
| 33 |
var tbodyIndex, rowIndex, start, end, last, index, rows, val, count, |
| 34 |
len, wLeft, wRight, alignChar, $row, |
| 35 |
left = [], |
| 36 |
right = []; |
| 37 |
for (tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++) { |
| 38 |
rows = c.cache[tbodyIndex]; |
| 39 |
len = rows.normalized.length; |
| 40 |
for (rowIndex = 0; rowIndex < len; rowIndex++) { |
| 41 |
|
| 42 |
$row = rows.row ? rows.row[rowIndex] : rows.normalized[rowIndex][c.columns].$row; |
| 43 |
val = $row.find('td').eq(v.column).text().replace(/[ ]/g, '\u00a0'); |
| 44 |
|
| 45 |
count = (val.match( v.regex ) || []).length; |
| 46 |
|
| 47 |
if (count > 0 && v.alignIndex > 0) { |
| 48 |
end = Math.min(v.alignIndex, count); |
| 49 |
start = 0; |
| 50 |
index = 0; |
| 51 |
last = 0; |
| 52 |
|
| 53 |
while (start++ < end) { |
| 54 |
last = val.indexOf(v.align, last + 1); |
| 55 |
index = last < 0 ? index : last; |
| 56 |
} |
| 57 |
} else { |
| 58 |
index = val.indexOf(v.align); |
| 59 |
} |
| 60 |
if ( index >= 0 ) { |
| 61 |
left.push( val.substring(0, index) || '' ); |
| 62 |
right.push( val.substring(index, val.length) || '' ); |
| 63 |
} else { |
| 64 |
|
| 65 |
|
| 66 |
left.push( (count >= 1 && v.alignIndex >= count) ? '' : val || '' ); |
| 67 |
right.push( (count >= 1 && v.alignIndex >= count) ? val || '' : '' ); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
wLeft = ($.extend([], left)).sort(function(a, b) { return b.length - a.length; })[0]; |
| 74 |
wRight = ($.extend([], right)).sort(function(a, b) { return b.length - a.length; })[0]; |
| 75 |
|
| 76 |
v.width = v.width || ( Math.floor(wLeft.length / (wLeft.length + wRight.length) * 100) + v.adjust ); |
| 77 |
wLeft = 'min-width:' + v.width + '%'; |
| 78 |
wRight = 'min-width:' + (100 - v.width) + '%'; |
| 79 |
|
| 80 |
for (tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++) { |
| 81 |
rows = c.cache[tbodyIndex]; |
| 82 |
len = rows.normalized.length; |
| 83 |
for (rowIndex = 0; rowIndex < len; rowIndex++) { |
| 84 |
alignChar = $(wo.alignChar_wrap).length ? $(wo.alignChar_wrap).html(v.align)[0].outerHTML : v.align; |
| 85 |
$row = rows.row ? rows.row[rowIndex] : rows.normalized[rowIndex][c.columns].$row; |
| 86 |
last = right[rowIndex].slice(v.align.length); |
| 87 |
$row.find('td').eq(v.column).html( |
| 88 |
'<span class="ts-align-wrap"><span class="ts-align-left" style="' + wLeft + '">' + left[rowIndex] + '</span>' + |
| 89 |
'<span class="ts-align-right" style="' + wRight + '">' + ( last.length ? alignChar + last : '' ) + '</span></span>' |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
wo.alignChar_initialized = true; |
| 94 |
|
| 95 |
}, |
| 96 |
|
| 97 |
remove: function(table, c, column) { |
| 98 |
if ($.isEmptyObject(c.cache)) { return; } |
| 99 |
var tbodyIndex, rowIndex, len, rows, $row, $cell; |
| 100 |
for (tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++) { |
| 101 |
rows = c.cache[tbodyIndex]; |
| 102 |
len = rows.normalized.length; |
| 103 |
for (rowIndex = 0; rowIndex < len; rowIndex++) { |
| 104 |
$row = rows.row ? rows.row[rowIndex] : rows.normalized[rowIndex][c.columns].$row; |
| 105 |
$cell = $row.find('td').eq(column); |
| 106 |
$cell.html( $cell.text().replace(/\s/g, ' ') ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
ts.addWidget({ |
| 113 |
id: 'alignChar', |
| 114 |
priority: 100, |
| 115 |
options: { |
| 116 |
alignChar_wrap : '', |
| 117 |
alignChar_charAttrib : 'data-align-char', |
| 118 |
alignChar_indexAttrib : 'data-align-index', |
| 119 |
alignChar_adjustAttrib : 'data-align-adjust' |
| 120 |
}, |
| 121 |
init: function(table, thisWidget, c, wo) { |
| 122 |
wo.alignChar_initialized = false; |
| 123 |
wo.alignChar_savedVars = []; |
| 124 |
ts.alignChar.init(table, c, wo); |
| 125 |
c.$table.on('pagerEnd refreshAlign', function() { |
| 126 |
c.$headers.filter('[' + wo.alignChar_charAttrib + ']').each(function() { |
| 127 |
ts.alignChar.remove(table, c, this.column); |
| 128 |
}); |
| 129 |
ts.alignChar.init(table, c, wo); |
| 130 |
}); |
| 131 |
}, |
| 132 |
format : function(table, c, wo) { |
| 133 |
|
| 134 |
if (!wo.alignChar_initialized) { |
| 135 |
c.$table.triggerHandler('refreshAlign'); |
| 136 |
} |
| 137 |
}, |
| 138 |
remove : function(table, c, wo, refreshing) { |
| 139 |
if (refreshing) { return; } |
| 140 |
c.$headers.filter('[' + wo.alignChar_charAttrib + ']').each(function() { |
| 141 |
ts.alignChar.remove(table, c, this.column); |
| 142 |
}); |
| 143 |
wo.alignChar_initialized = false; |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
})(jQuery); |