tablesorter/js/jquery.tablesorter.widgets.js

598 lines
21 KiB
JavaScript
Raw Normal View History

2012-06-21 06:10:02 +00:00
/*! tableSorter 2.3 widgets - updated 6/21/2012
2011-10-26 06:50:02 +00:00
*
* jQuery UI Theme
* Column Styles
2012-05-23 17:11:30 +00:00
* Column Filters
2011-10-26 06:50:02 +00:00
* Sticky Header
* Column Resizing
2012-02-01 05:14:28 +00:00
* Save Sort
2011-10-26 06:50:02 +00:00
*
*/
;(function($){
2011-10-26 06:50:02 +00:00
2012-03-07 18:06:35 +00:00
// *** 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
2012-03-27 01:49:48 +00:00
$.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
2012-03-07 18:06:35 +00:00
// *** 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){
2012-04-02 19:19:17 +00:00
var d, k, ls = false, v = {},
2012-03-07 18:06:35 +00:00
id = table.id || $('.tablesorter').index( $(table) ),
url = window.location.pathname;
try { ls = !!(localStorage.getItem); } catch(e) {}
2012-03-27 01:49:48 +00:00
// *** get val ***
if ($.parseJSON) {
if (ls) {
2012-04-02 19:19:17 +00:00
v = $.parseJSON(localStorage[key]) || {};
2012-03-27 01:49:48 +00:00
} else {
k = document.cookie.split(/[;\s|=]/); // cookie
d = $.inArray(key, k) + 1; // add one to get from the key to the value
2012-04-02 19:19:17 +00:00
v = (d !== 0) ? $.parseJSON(k[d]) || {} : {};
2012-03-27 01:49:48 +00:00
}
}
if (val && JSON && JSON.hasOwnProperty('stringify')) {
2012-03-07 18:06:35 +00:00
// add unique identifiers = url pathname > table ID/index on page > data
2012-04-02 19:19:17 +00:00
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;
}
}
2012-03-07 18:06:35 +00:00
// *** 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=/';
}
2012-03-27 01:49:48 +00:00
} else {
2012-04-02 19:19:17 +00:00
return ( v && v.hasOwnProperty(url) && v[url].hasOwnProperty(id) ) ? v[url][id] : {};
2012-03-07 18:06:35 +00:00
}
};
// Widget: jQuery UI theme
// "uitheme" option in "widgetOptions"
2011-09-13 22:55:31 +00:00
// **************************
2011-09-11 17:51:02 +00:00
$.tablesorter.addWidget({
id: "uitheme",
format: function(table) {
2012-03-07 18:06:35 +00:00
var time, klass, rmv, $t, t, $table = $(table),
c = table.config, wo = c.widgetOptions,
2011-09-11 17:51:02 +00:00
// ["up/down arrow (cssHeaders, unsorted)", "down arrow (cssDesc, descending)", "up arrow (cssAsc, ascending)" ]
2011-09-16 15:43:09 +00:00
icons = ["ui-icon-arrowthick-2-n-s", "ui-icon-arrowthick-1-s", "ui-icon-arrowthick-1-n"];
2012-03-07 18:06:35 +00:00
// keep backwards compatibility, for now
icons = (c.widgetUitheme && c.widgetUitheme.hasOwnProperty('css')) ? c.widgetUitheme.css || icons :
(wo && wo.hasOwnProperty('uitheme')) ? wo.uitheme : icons;
2011-09-16 15:43:09 +00:00
rmv = icons.join(' ');
2011-09-11 17:51:02 +00:00
if (c.debug) {
time = new Date();
}
2012-03-07 18:06:35 +00:00
if (!$table.hasClass('ui-theme')) {
2011-12-14 17:37:55 +00:00
$table.addClass('ui-widget ui-widget-content ui-corner-all ui-theme');
2011-09-11 17:51:02 +00:00
$.each(c.headerList, function(){
$(this)
// using "ui-theme" class in case the user adds their own ui-icon using onRenderHeader
2012-03-07 18:06:35 +00:00
.addClass('ui-widget-header ui-corner-all ui-state-default')
2011-12-14 17:37:55 +00:00
.append('<span class="ui-icon"/>')
2012-03-07 18:06:35 +00:00
.wrapInner('<div class="tablesorter-inner"/>')
2011-09-14 14:20:15 +00:00
.hover(function(){
$(this).addClass('ui-state-hover');
}, function(){
$(this).removeClass('ui-state-hover');
2011-12-14 17:37:55 +00:00
});
2011-09-11 17:51:02 +00:00
});
}
$.each(c.headerList, function(i){
2011-12-14 17:37:55 +00:00
$t = $(this);
2011-12-16 00:17:57 +00:00
if (this.sortDisabled) {
2011-09-11 17:51:02 +00:00
// no sort arrows for disabled columns!
2011-12-14 17:37:55 +00:00
$t.find('span.ui-icon').removeClass(rmv + ' ui-icon');
2011-09-11 17:51:02 +00:00
} else {
2011-12-14 17:37:55 +00:00
klass = ($t.hasClass(c.cssAsc)) ? icons[1] : ($t.hasClass(c.cssDesc)) ? icons[2] : $t.hasClass(c.cssHeader) ? icons[0] : '';
2012-03-27 01:49:48 +00:00
t = ($table.hasClass('hasStickyHeaders')) ? $table.find('tr.' + (wo.stickyHeaders || 'tablesorter-stickyHeader')).find('th').eq(i).add($t) : $t;
2011-12-14 17:37:55 +00:00
t[klass === icons[0] ? 'removeClass' : 'addClass']('ui-state-active')
2011-09-14 14:20:15 +00:00
.find('span.ui-icon').removeClass(rmv).addClass(klass);
2011-09-11 17:51:02 +00:00
}
});
if (c.debug) {
$.tablesorter.benchmark("Applying uitheme widget", time);
}
}
});
2012-03-07 18:06:35 +00:00
// Widget: Column styles
// "columns" option in "widgetOptions"
2011-09-13 22:55:31 +00:00
// **************************
2011-09-11 17:51:02 +00:00
$.tablesorter.addWidget({
id: "columns",
format: function(table) {
2012-05-28 15:01:40 +00:00
var $tb, $tr, $td, $t, time, last, rmv, i, k, l,
2011-09-11 17:51:02 +00:00
c = table.config,
2012-05-11 17:46:54 +00:00
b = $(table).children('tbody:not(.' + c.cssInfoBlock + ')'),
2011-09-11 17:51:02 +00:00
list = c.sortList,
len = list.length,
2012-03-07 18:06:35 +00:00
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;
2011-09-16 15:43:09 +00:00
last = css.length-1;
2011-09-11 17:51:02 +00:00
rmv = css.join(' ');
if (c.debug) {
time = new Date();
}
2011-09-13 22:55:31 +00:00
// check if there is a sort (on initialization there may not be one)
2012-05-23 17:11:30 +00:00
for (k = 0; k < b.length; k++ ) {
$tb = $(b[k]);
2012-05-28 15:01:40 +00:00
$tr = $tb.addClass('tablesorter-hidden').children('tr');
2012-05-23 17:11:30 +00:00
l = $tr.length;
// loop through the visible rows
2012-05-28 15:01:40 +00:00
$tr.each(function(){
$t = $(this);
if (this.style.display !== 'none') {
2012-05-23 17:11:30 +00:00
// remove all columns class names
2012-05-28 15:01:40 +00:00
$td = $t.children().removeClass(rmv);
2012-05-23 17:11:30 +00:00
// 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] );
}
2012-05-03 14:46:31 +00:00
}
2011-09-13 22:55:31 +00:00
}
2012-05-19 20:46:14 +00:00
}
2012-05-28 15:01:40 +00:00
});
$tb.removeClass('tablesorter-hidden');
2011-09-13 22:55:31 +00:00
}
2011-09-11 17:51:02 +00:00
if (c.debug) {
$.tablesorter.benchmark("Applying Columns widget", time);
}
}
2011-09-13 22:55:31 +00:00
});
2012-03-07 18:06:35 +00:00
// Widget: Filter
2012-06-01 14:49:46 +00:00
// "filter_startsWith", "filter_childRows", "filter_ignoreCase",
// "filter_searchDelay" & "filter_functions" options in "widgetOptions"
2011-09-13 22:55:31 +00:00
// **************************
$.tablesorter.addWidget({
id: "filter",
format: function(table) {
2012-06-21 14:29:52 +00:00
if (table.config.parsers && !$(table).hasClass('hasFilters')) {
2012-06-01 14:49:46 +00:00
var i, j, k, l, cv, v, val, r, ff, t, x, xi, cr,
sel, $tb, $th, $tr, $td, reg2,
2012-05-23 17:11:30 +00:00
c = table.config,
$ths = $(c.headerList),
2012-05-23 17:11:30 +00:00
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 = '<tr class="' + css + '">',
2012-05-30 04:55:28 +00:00
regexp = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})?$/,
reg1 = new RegExp(c.cssChildRow),
2012-05-23 17:11:30 +00:00
time, timer,
findRows = function(){
if (c.debug) { time = new Date(); }
2012-06-01 14:49:46 +00:00
v = $t.find('thead').eq(0).children('tr').find('select.' + css + ', input.' + css).map(function(){
return $(this).val() || '';
2012-05-23 17:11:30 +00:00
}).get();
cv = v.join('');
for (k = 0; k < b.length; k++ ) {
$tb = $(b[k]);
2012-05-28 15:01:40 +00:00
$tr = $tb.addClass('tablesorter-hidden').children('tr');
2012-05-23 17:11:30 +00:00
l = $tr.length;
// loop through the rows
for (j = 0; j < l; j++) {
if (cv === '') {
$tr[j].style.display = '';
} else {
2012-06-20 12:15:46 +00:00
// skip child rows
if (reg1.test($tr[j].className)) { continue; }
2012-06-01 14:49:46 +00:00
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() : '';
2012-06-20 12:15:46 +00:00
t = wo.filter_ignoreCase ? t.toLocaleLowerCase() : t;
2012-06-01 14:49:46 +00:00
$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;
}
2012-06-01 14:49:46 +00:00
// Look for quotes to get an exact match
} else if (/[\"|\']$/.test(val) && xi === val.replace(/(\"|\')/g,'')) {
ff = true;
2012-06-01 14:49:46 +00:00
// 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) );
2012-05-23 17:11:30 +00:00
}
2012-06-01 14:49:46 +00:00
r = (ff) ? (r ? true : false) : false;
2012-05-23 17:11:30 +00:00
}
}
2012-06-01 14:49:46 +00:00
$tr[j].style.display = (r ? '' : 'none');
if (cr.length) { cr[r ? 'show' : 'hide'](); }
2012-05-23 17:11:30 +00:00
}
}
2012-05-28 15:01:40 +00:00
$tb.removeClass('tablesorter-hidden');
2012-05-23 17:11:30 +00:00
}
if (c.debug) {
$.tablesorter.benchmark("Completed filter widget search", time);
}
$t.trigger('applyWidgets'); // make sure zebra widget is applied
2012-06-01 14:49:46 +00:00
},
buildSelect = function(i, updating){
2012-06-01 14:49:46 +00:00
var o, arry = [];
i = parseInt(i, 10);
o = '<option value="">' + ($ths.filter('[data-column="' + i + '"]:last').attr('data-placeholder') || '') + '</option>';
2012-06-01 14:49:46 +00:00
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() );
}
2012-06-01 14:49:46 +00:00
}
}
// get unique elements and sort the list
arry = arry.getUnique(true);
// build option list
for (k = 0; k < arry.length; k++) {
o += '<option value="' + arry[k] + '">' + arry[k] + '</option>';
}
$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);
}
}
2012-05-23 17:11:30 +00:00
};
2011-09-13 22:55:31 +00:00
if (c.debug) {
time = new Date();
}
2012-06-20 12:15:46 +00:00
wo.filter_ignoreCase = wo.filter_ignoreCase !== false; // set default filter_ignoreCase to true
2011-09-13 22:55:31 +00:00
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');
2012-06-01 14:49:46 +00:00
fr += '<td>';
if (sel){
fr += '<select data-column="' + i + '" class="' + css;
2012-06-01 14:49:46 +00:00
} else {
fr += '<input type="search" placeholder="' + ($th.attr('data-placeholder') || "") + '" data-column="' + i + '" class="' + css;
2012-06-01 14:49:46 +00:00
}
2011-09-16 18:39:03 +00:00
// use header option - headers: { 1: { filter: false } } OR add class="filter-false"
2012-05-19 20:46:14 +00:00
if ($.tablesorter.getData) {
// get data from jQuery data, metadata, headers option or header class name
fr += $.tablesorter.getData($th[0], c.headers[i], 'filter') === 'false' ? ' disabled" disabled' : '"';
2012-05-19 20:46:14 +00:00
} else {
// only class names and header options - keep this for compatibility with tablesorter v2.0.5
fr += ((c.headers[i] && c.headers[i].hasOwnProperty('filter') && c.headers[i].filter === false) || $th.hasClass('filter-false') ) ? ' disabled" disabled' : '"';
2012-05-19 20:46:14 +00:00
}
2012-06-01 14:49:46 +00:00
fr += (sel ? '></select>' : '>') + '</td>';
2011-09-13 22:55:31 +00:00
}
2012-03-07 18:06:35 +00:00
$t
.bind('addRows updateCell update appendCache', function(){
buildDefault(true);
findRows();
})
2012-06-01 14:49:46 +00:00
.find('thead').eq(0).append(fr += '</tr>')
.find('input.' + css).bind('keyup search', function(e, delay){
// ignore arrow and meta keys; allow backspace
if ((e.which < 32 && e.which !== 8) || (e.which >= 37 && e.which <=40)) { return; }
// skip delay
if (delay === false) {
findRows();
return;
}
// delay filtering
clearTimeout(timer);
timer = setTimeout(function(){
findRows();
}, wo.filter_searchDelay || 300);
});
if (wo.filter_functions) {
// i = column # (string)
for (i in wo.filter_functions) {
t = $ths.filter('[data-column="' + i + '"]:last');
2012-06-01 14:49:46 +00:00
fr = '';
if (typeof i === 'string' && wo.filter_functions[i] === true && !t.hasClass('filter-false')) {
buildSelect(i);
} else if (typeof i === 'string' && !t.hasClass('filter-false')) {
// add custom drop down list
for (j in wo.filter_functions[i]) {
if (typeof j === 'string') {
fr += fr === '' ? '<option>' + (t.attr('data-placeholder') || '') + '</option>' : '';
fr += '<option>' + j + '</option>';
}
}
$t.find('thead').find('select.' + css + '[data-column="' + i + '"]').append(fr);
2011-09-13 22:55:31 +00:00
}
2012-06-01 14:49:46 +00:00
}
}
buildDefault();
2012-06-01 14:49:46 +00:00
$t.find('select.' + css).bind('change', function(){
findRows();
});
2011-09-13 22:55:31 +00:00
if (c.debug) {
$.tablesorter.benchmark("Applying Filter widget", time);
}
}
}
});
2011-09-16 15:43:09 +00:00
2012-03-07 18:06:35 +00:00
// Widget: Sticky headers
2011-10-11 05:48:34 +00:00
// based on this awesome article:
// http://css-tricks.com/13465-persistent-headers/
// **************************
$.tablesorter.addWidget({
id: "stickyHeaders",
format: function(table) {
2011-12-14 17:37:55 +00:00
if ($(table).hasClass('hasStickyHeaders')) { return; }
var $table = $(table).addClass('hasStickyHeaders'),
2012-03-07 18:06:35 +00:00
wo = table.config.widgetOptions,
2011-12-06 15:25:51 +00:00
win = $(window),
header = $(table).children('thead'),
hdrCells = header.children('tr:not(.sticky-false)').children(),
2012-03-27 01:49:48 +00:00
css = wo.stickyHeaders || 'tablesorter-stickyHeader',
innr = '.tablesorter-header-inner',
firstCell = hdrCells.eq(0),
2012-05-03 14:46:31 +00:00
tfoot = $table.find('tfoot'),
2012-04-23 19:24:45 +00:00
sticky = header.find('tr.tablesorter-header:not(.sticky-false)').clone()
.removeClass('tablesorter-header')
2012-03-07 18:06:35 +00:00
.addClass(css)
2011-10-11 05:48:34 +00:00
.css({
2012-03-27 01:49:48 +00:00
width : header.outerWidth(true),
2011-10-11 05:48:34 +00:00
position : 'fixed',
left : firstCell.offset().left,
2012-03-22 14:58:43 +00:00
margin : 0,
top : 0,
2011-10-26 06:50:02 +00:00
visibility : 'hidden',
zIndex : 10
2011-10-11 05:48:34 +00:00
}),
stkyCells = sticky.children(),
2012-03-27 01:49:48 +00:00
laststate = '';
// update sticky header class names to match real header after sorting
2011-12-06 15:25:51 +00:00
$table.bind('sortEnd', function(e,t){
2011-10-11 05:48:34 +00:00
var th = $(t).find('thead tr'),
2012-03-07 18:06:35 +00:00
sh = th.filter('.' + css).children();
th.filter(':not(.' + css + ')').children().each(function(i){
2011-10-11 05:48:34 +00:00
sh.eq(i).attr('class', $(this).attr('class'));
});
2012-01-27 20:03:41 +00:00
}).bind('pagerComplete', function(){
win.resize(); // trigger window resize to make sure column widths & position are correct
2011-10-11 05:48:34 +00:00
});
// set sticky header cell width and link clicks to real header
hdrCells.each(function(i){
2012-05-28 15:01:40 +00:00
var t = $(this);
stkyCells.eq(i)
2011-10-11 05:48:34 +00:00
// clicking on sticky will trigger sort
2012-05-28 15:01:40 +00:00
.bind('mouseup', function(e){
t.trigger(e, true); // external mouseup flag (click timer is ignored)
2011-10-11 05:48:34 +00:00
})
// prevent sticky header text selection
.bind('mousedown', function(){
this.onselectstart = function(){ return false; };
return false;
2012-03-27 01:49:48 +00:00
})
// set cell widths
.find(innr).width( t.find(innr).width() );
2011-10-11 05:48:34 +00:00
});
header.prepend( sticky );
// make it sticky!
2011-10-15 14:58:44 +00:00
win
.scroll(function(){
var offset = firstCell.offset(),
2011-10-15 14:58:44 +00:00
sTop = win.scrollTop(),
2012-05-03 14:46:31 +00:00
tableHt = $table.height() - (firstCell.height() + (tfoot.height() || 0)),
vis = (sTop > offset.top) && (sTop < offset.top + tableHt) ? 'visible' : 'hidden';
2011-12-06 15:25:51 +00:00
sticky.css({
left : offset.left - win.scrollLeft(),
visibility : vis
});
if (vis !== laststate) {
// trigger resize to make sure the column widths match
win.resize();
laststate = vis;
}
2011-10-15 14:58:44 +00:00
})
.resize(function(){
var ht = 0;
2011-12-06 15:25:51 +00:00
sticky.css({
left : firstCell.offset().left - win.scrollLeft(),
2012-03-27 01:49:48 +00:00
width: header.outerWidth()
}).each(function(i){
$(this).css('top', ht);
ht += header.find('tr').eq(i).outerHeight();
2011-12-06 15:25:51 +00:00
});
stkyCells.find(innr).each(function(i){
$(this).width( hdrCells.eq(i).find(innr).width() );
2011-10-15 14:58:44 +00:00
});
});
2011-10-11 05:48:34 +00:00
}
});
2011-10-26 06:50:02 +00:00
// Add Column resizing widget
2012-03-07 18:06:35 +00:00
// this widget saves the column widths if
// $.tablesorter.storage function is included
2011-10-26 06:50:02 +00:00
// **************************
$.tablesorter.addWidget({
id: "resizable",
format: function(table) {
2012-03-07 18:06:35 +00:00
if ($(table).hasClass('hasResizable')) { return; }
$(table).addClass('hasResizable');
2012-05-28 15:01:40 +00:00
var j, s, c = table.config,
2012-03-07 18:06:35 +00:00
$cols = $(c.headerList).filter(':gt(0)'),
position = 0,
$target = null,
$prev = null,
stopResize = function(){
position = 0;
$target = $prev = null;
$(window).trigger('resize'); // will update stickyHeaders, just in case
};
s = ($.tablesorter.storage) ? $.tablesorter.storage(table, 'tablesorter-resizable') : '';
// process only if table ID or url match
if (s) {
for (j in s) {
if (!isNaN(j) && j < c.headerList.length) {
$(c.headerList[j]).width(s[j]); // set saved resizable widths
}
2011-10-26 06:50:02 +00:00
}
2012-03-07 18:06:35 +00:00
}
$cols
.each(function(){
$(this)
.append('<div class="tablesorter-resizer" style="cursor:w-resize;position:absolute;height:100%;width:20px;left:-20px;top:0;z-index:1;"></div>')
.wrapInner('<div style="position:relative;height:100%;width:100%"></div>');
})
.bind('mousemove', function(e){
// ignore mousemove if no mousedown
if (position === 0 || !$target) { return; }
2012-05-28 15:01:40 +00:00
var w = e.pageX - position;
2012-03-07 18:06:35 +00:00
// make sure
if ( $target.width() < -w || ( $prev && $prev.width() <= w )) { return; }
// resize current column
$prev.width( $prev.width() + w );
position = e.pageX;
})
.bind('mouseup', function(){
if (s && $.tablesorter.storage && $target) {
s[$prev.index()] = $prev.width();
$.tablesorter.storage(table, 'tablesorter-resizable', s);
}
2011-10-26 06:50:02 +00:00
stopResize();
2012-03-07 18:06:35 +00:00
return false;
})
.find('.tablesorter-resizer')
.bind('mousedown', function(e){
// save header cell and mouse position
$target = $(e.target).closest('th');
$prev = $target.prev();
position = e.pageX;
2012-05-28 15:01:40 +00:00
return false;
2011-10-26 06:50:02 +00:00
});
2012-03-07 18:06:35 +00:00
$(table).find('thead').bind('mouseup mouseleave', function(){
stopResize();
});
2011-10-26 06:50:02 +00:00
}
});
2012-02-01 05:14:28 +00:00
// Save table sort widget
2012-03-07 18:06:35 +00:00
// this widget saves the last sort only if the
// $.tablesorter.storage function is included
2012-02-01 05:14:28 +00:00
// **************************
$.tablesorter.addWidget({
id: 'saveSort',
init: function(table, allWidgets, thisWidget){
// run widget format before all other widgets are applied to the table
thisWidget.format(table, true);
},
format: function(table, init) {
2012-05-28 15:01:40 +00:00
var sl, time, c = table.config, sortList = { "sortList" : c.sortList };
2012-02-01 05:14:28 +00:00
if (c.debug) {
time = new Date();
}
2012-03-07 18:06:35 +00:00
if ($(table).hasClass('hasSaveSort')) {
if (table.hasInitialized && $.tablesorter.storage) {
$.tablesorter.storage( table, 'tablesorter-savesort', sortList );
2012-02-21 00:14:25 +00:00
if (c.debug) {
2012-03-07 18:06:35 +00:00
$.tablesorter.benchmark('saveSort widget: Saving last sort: ' + c.sortList, time);
2012-02-21 00:14:25 +00:00
}
2012-02-01 05:14:28 +00:00
}
} else {
// set table sort on initial run of the widget
2012-03-07 18:06:35 +00:00
$(table).addClass('hasSaveSort');
sortList = '';
2012-02-01 05:14:28 +00:00
// get data
2012-03-07 18:06:35 +00:00
if ($.tablesorter.storage) {
sl = $.tablesorter.storage( table, 'tablesorter-savesort' );
sortList = (sl && sl.hasOwnProperty('sortList') && $.isArray(sl.sortList)) ? sl.sortList : '';
if (c.debug) {
$.tablesorter.benchmark('saveSort: Last sort loaded: ' + sortList, time);
}
2012-02-01 05:14:28 +00:00
}
// init is true when widget init is run, this will run this widget before all other widgets have initialized
// this method allows using this widget in the original tablesorter plugin; but then it will run all widgets twice.
if (init && sortList && sortList.length > 0) {
c.sortList = sortList;
2012-02-21 00:14:25 +00:00
} else if (table.hasInitialized && sortList && sortList.length > 0) {
// update sort change
$(table).trigger('sorton', [sortList]);
2012-02-01 05:14:28 +00:00
}
}
}
});
2012-05-30 04:55:28 +00:00
})(jQuery);
2012-06-01 14:49:46 +00:00
// return an array with unique values https://gist.github.com/461516
Array.prototype.getUnique = function(s){
var c, a = [], o = {}, i, j = 0, l = this.length;
for(i=0; i < l; ++i) {
c = this[i];
if (!o[c]) {
o[c] = {};
a[j++] = c;
}
}
return (s) ? a.sort() : a;
};