/*! tableSorter 2.3 widgets - updated 6/21/2012
*
* jQuery UI Theme
* Column Styles
* Column Filters
* Sticky Header
* Column Resizing
* Save Sort
*
*/
;(function($){
// *** Store data in local storage, with a cookie fallback ***
/* IE7 needs JSON library for JSON.stringify - (http://caniuse.com/#search=json)
if you need it, then include https://github.com/douglascrockford/JSON-js
$.parseJSON is not available is jQuery versions older than 1.4.1, using older
versions will only allow storing information for one page at a time
// *** Save data (JSON format only) ***
// val must be valid JSON... use http://jsonlint.com/ to ensure it is valid
var val = { "mywidget" : "data1" }; // valid JSON uses double quotes
// $.tablesorter.storage(table, key, val);
$.tablesorter.storage(table, 'tablesorter-mywidget', val);
// *** Get data: $.tablesorter.storage(table, key); ***
v = $.tablesorter.storage(table, 'tablesorter-mywidget');
// val may be empty, so also check for your data
val = (v && v.hasOwnProperty('mywidget')) ? v.mywidget : '';
alert(val); // "data1" if saved, or "" if not
*/
$.tablesorter.storage = function(table, key, val){
var d, k, ls = false, v = {},
id = table.id || $('.tablesorter').index( $(table) ),
url = window.location.pathname;
try { ls = !!(localStorage.getItem); } catch(e) {}
// *** get val ***
if ($.parseJSON) {
if (ls) {
v = $.parseJSON(localStorage[key]) || {};
} else {
k = document.cookie.split(/[;\s|=]/); // cookie
d = $.inArray(key, k) + 1; // add one to get from the key to the value
v = (d !== 0) ? $.parseJSON(k[d]) || {} : {};
}
}
if (val && JSON && JSON.hasOwnProperty('stringify')) {
// add unique identifiers = url pathname > table ID/index on page > data
if (v[url] && v[url][id]) {
v[url][id] = val;
} else {
if (v[url]) {
v[url][id] = val;
} else {
v[url] = {};
v[url][id] = val;
}
}
// *** set val ***
if (ls) {
localStorage[key] = JSON.stringify(v);
} else {
d = new Date();
d.setTime(d.getTime()+(31536e+6)); // 365 days
document.cookie = key + '=' + (JSON.stringify(v)).replace(/\"/g,'\"') + '; expires=' + d.toGMTString() + '; path=/';
}
} else {
return ( v && v.hasOwnProperty(url) && v[url].hasOwnProperty(id) ) ? v[url][id] : {};
}
};
// Widget: jQuery UI theme
// "uitheme" option in "widgetOptions"
// **************************
$.tablesorter.addWidget({
id: "uitheme",
format: function(table) {
var time, klass, rmv, $t, t, $table = $(table),
c = table.config, wo = c.widgetOptions,
// ["up/down arrow (cssHeaders, unsorted)", "down arrow (cssDesc, descending)", "up arrow (cssAsc, ascending)" ]
icons = ["ui-icon-arrowthick-2-n-s", "ui-icon-arrowthick-1-s", "ui-icon-arrowthick-1-n"];
// keep backwards compatibility, for now
icons = (c.widgetUitheme && c.widgetUitheme.hasOwnProperty('css')) ? c.widgetUitheme.css || icons :
(wo && wo.hasOwnProperty('uitheme')) ? wo.uitheme : icons;
rmv = icons.join(' ');
if (c.debug) {
time = new Date();
}
if (!$table.hasClass('ui-theme')) {
$table.addClass('ui-widget ui-widget-content ui-corner-all ui-theme');
$.each(c.headerList, function(){
$(this)
// using "ui-theme" class in case the user adds their own ui-icon using onRenderHeader
.addClass('ui-widget-header ui-corner-all ui-state-default')
.append('')
.wrapInner('
')
.hover(function(){
$(this).addClass('ui-state-hover');
}, function(){
$(this).removeClass('ui-state-hover');
});
});
}
$.each(c.headerList, function(i){
$t = $(this);
if (this.sortDisabled) {
// no sort arrows for disabled columns!
$t.find('span.ui-icon').removeClass(rmv + ' ui-icon');
} else {
klass = ($t.hasClass(c.cssAsc)) ? icons[1] : ($t.hasClass(c.cssDesc)) ? icons[2] : $t.hasClass(c.cssHeader) ? icons[0] : '';
t = ($table.hasClass('hasStickyHeaders')) ? $table.find('tr.' + (wo.stickyHeaders || 'tablesorter-stickyHeader')).find('th').eq(i).add($t) : $t;
t[klass === icons[0] ? 'removeClass' : 'addClass']('ui-state-active')
.find('span.ui-icon').removeClass(rmv).addClass(klass);
}
});
if (c.debug) {
$.tablesorter.benchmark("Applying uitheme widget", time);
}
}
});
// Widget: Column styles
// "columns" option in "widgetOptions"
// **************************
$.tablesorter.addWidget({
id: "columns",
format: function(table) {
var $tb, $tr, $td, $t, time, last, rmv, i, k, l,
c = table.config,
b = $(table).children('tbody:not(.' + c.cssInfoBlock + ')'),
list = c.sortList,
len = list.length,
css = [ "primary", "secondary", "tertiary" ]; // default options
// keep backwards compatibility, for now
css = (c.widgetColumns && c.widgetColumns.hasOwnProperty('css')) ? c.widgetColumns.css || css :
(c.widgetOptions && c.widgetOptions.hasOwnProperty('columns')) ? c.widgetOptions.columns || css : css;
last = css.length-1;
rmv = css.join(' ');
if (c.debug) {
time = new Date();
}
// check if there is a sort (on initialization there may not be one)
for (k = 0; k < b.length; k++ ) {
$tb = $(b[k]);
$tr = $tb.addClass('tablesorter-hidden').children('tr');
l = $tr.length;
// loop through the visible rows
$tr.each(function(){
$t = $(this);
if (this.style.display !== 'none') {
// remove all columns class names
$td = $t.children().removeClass(rmv);
// add appropriate column class names
if (list && list[0]) {
// primary sort column class
$td.eq(list[0][0]).addClass(css[0]);
if (len > 1) {
for (i = 1; i < len; i++){
// secondary, tertiary, etc sort column classes
$td.eq(list[i][0]).addClass( css[i] || css[last] );
}
}
}
}
});
$tb.removeClass('tablesorter-hidden');
}
if (c.debug) {
$.tablesorter.benchmark("Applying Columns widget", time);
}
}
});
// Widget: Filter
// "filter_startsWith", "filter_childRows", "filter_ignoreCase",
// "filter_searchDelay" & "filter_functions" options in "widgetOptions"
// **************************
$.tablesorter.addWidget({
id: "filter",
format: function(table) {
if (table.config.parsers && !$(table).hasClass('hasFilters')) {
var i, j, k, l, cv, v, val, r, ff, t, x, xi, cr,
sel, $tb, $th, $tr, $td, reg2,
c = table.config,
$ths = $(c.headerList),
wo = c.widgetOptions,
css = wo.filter_cssFilter || 'tablesorter-filter',
$t = $(table).addClass('hasFilters'),
b = $t.children('tbody:not(.' + c.cssInfoBlock + ')'),
cols = c.parsers.length,
fr = '
',
regexp = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})?$/,
reg1 = new RegExp(c.cssChildRow),
time, timer,
findRows = function(){
if (c.debug) { time = new Date(); }
v = $t.find('thead').eq(0).children('tr').find('select.' + css + ', input.' + css).map(function(){
return $(this).val() || '';
}).get();
cv = v.join('');
for (k = 0; k < b.length; k++ ) {
$tb = $(b[k]);
$tr = $tb.addClass('tablesorter-hidden').children('tr');
l = $tr.length;
// loop through the rows
for (j = 0; j < l; j++) {
if (cv === '') {
$tr[j].style.display = '';
} else {
// skip child rows
if (reg1.test($tr[j].className)) { continue; }
r = true;
cr = $tr.eq(j).nextUntil('tr:not(.' + c.cssChildRow + ')');
// so, if "table.config.widgetOptions.filter_childRows" is true and there is
// a match anywhere in the child row, then it will make the row visible
// checked here so the option can be changed dynamically
t = (cr.length && (wo && wo.hasOwnProperty('filter_childRows') &&
typeof wo.filter_childRows !== 'undefined' ? wo.filter_childRows : true)) ? cr.text() : '';
t = wo.filter_ignoreCase ? t.toLocaleLowerCase() : t;
$td = $tr.eq(j).children('td');
for (i = 0; i < cols; i++) {
x = $.trim($td.eq(i).text());
xi = wo.filter_ignoreCase ? x.toLocaleLowerCase() : x;
// ignore if filter is empty
if (v[i] !== '') {
ff = r; // if r is true, show that row
// val = case insensitive, v[i] = case sensitive
val = wo.filter_ignoreCase ? v[i].toLocaleLowerCase() : v[i];
if (wo.filter_functions && wo.filter_functions[i]) {
if (wo.filter_functions[i] === true) {
// default selector; no "filter-select" class
ff = wo.filter_ignoreCase ? val === xi : v[i] === x;
} else if (typeof wo.filter_functions[i] === 'function') {
// filter callback( exact cell content, parser normalized content, filter input value, column index )
ff = wo.filter_functions[i](x, c.cache[k].normalized[j][i], v[i], i);
} else if (typeof wo.filter_functions[i][v[i]] === 'function'){
// selector option function
ff = wo.filter_functions[i][v[i]](x, c.cache[k].normalized[j][i], v[i], i);
}
// Look for regex
} else if (regexp.test(val)) {
reg2 = regexp.exec(val);
try {
ff = new RegExp(reg2[1], reg2[2]).test(xi);
} catch (err) {
ff = false;
}
// Look for quotes to get an exact match
} else if (/[\"|\']$/.test(val) && xi === val.replace(/(\"|\')/g,'')) {
ff = true;
// Look for wild card: ? = single, or * = multiple
} else if (/[\?|\*]/.test(val)) {
ff = new RegExp( val.replace(/\?/g, '\\S{1}').replace(/\*/g, '\\S*') ).test(xi);
// Look for match, and add child row data for matching
} else {
x = (xi + t).indexOf(val);
ff = ( (!wo.filter_startsWith && x >= 0) || (wo.filter_startsWith && x === 0) );
}
r = (ff) ? (r ? true : false) : false;
}
}
$tr[j].style.display = (r ? '' : 'none');
if (cr.length) { cr[r ? 'show' : 'hide'](); }
}
}
$tb.removeClass('tablesorter-hidden');
}
if (c.debug) {
$.tablesorter.benchmark("Completed filter widget search", time);
}
$t.trigger('applyWidgets'); // make sure zebra widget is applied
},
buildSelect = function(i, updating){
var o, arry = [];
i = parseInt(i, 10);
o = '';
for (k = 0; k < b.length; k++ ) {
l = c.cache[k].row.length;
// loop through the rows
for (j = 0; j < l; j++) {
// get non-normalized cell content
t = c.cache[k].row[j][0].cells[i];
if (t) {
arry.push( c.supportsTextContent ? t.textContent : $(t).text() );
}
}
}
// get unique elements and sort the list
arry = arry.getUnique(true);
// build option list
for (k = 0; k < arry.length; k++) {
o += '';
}
$t.find('thead').find('select.' + css + '[data-column="' + i + '"]')[ updating ? 'html' : 'append' ](o);
},
buildDefault = function(updating){
// build default select dropdown
for (i = 0; i < cols; i++) {
t = $ths.filter('[data-column="' + i + '"]:last');
// look for the filter-select class, but don't build it twice.
if (t.hasClass('filter-select') && !t.hasClass('filter-false') && !(wo.filter_functions && wo.filter_functions[i] === true)){
buildSelect(i, updating);
}
}
};
if (c.debug) {
time = new Date();
}
wo.filter_ignoreCase = wo.filter_ignoreCase !== false; // set default filter_ignoreCase to true
for (i=0; i < cols; i++){
$th = $ths.filter('[data-column="' + i + '"]:last'); // assuming last cell of a column is the main column
sel = (wo.filter_functions && wo.filter_functions[i] && typeof wo.filter_functions[i] !== 'function') || $th.hasClass('filter-select');
fr += '