mirror of
https://github.com/kremalicious/krtmn.git
synced 2024-12-22 09:13:25 +01:00
37 lines
14 KiB
JavaScript
37 lines
14 KiB
JavaScript
|
/* jQuery Tablesorter */
|
||
|
(function($){$.extend({tablesorter:new function(){var parsers=[],widgets=[];this.defaults={cssHeader:"header",cssAsc:"headerSortUp",cssDesc:"headerSortDown",sortInitialOrder:"asc",sortMultiSortKey:"shiftKey",sortForce:null,textExtraction:"simple",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"us",debug:false};function benchmark(s,d){log(s+","+(new Date().getTime()-d.getTime())+"ms");}this.benchmark=benchmark;function log(s){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(s);}else{alert(s);}}function buildParserCache(table,$headers){if(table.config.debug){var parsersDebug="";}var rows=table.tBodies[0].rows;if(table.tBodies[0].rows[0]){var list=[],cells=rows[0].cells,l=cells.length;for(var i=0;i<l;i++){var p=false;if($.meta&&($($headers[i]).data()&&$($headers[i]).data().sorter)){p=getParserById($($headers[i]).data().sorter);}else if((table.config.headers[i]&&table.config.headers[i].sorter)){p=getParserById(table.config.headers[i].sorter);}if(!p){p=detectParserForColumn(table.config,cells[i]);}if(table.config.debug){parsersDebug+="column:"+i+" parser:"+p.id+"\n";}list.push(p);}}if(table.config.debug){log(parsersDebug);}return list;};function detectParserForColumn(config,node){var l=parsers.length;for(var i=1;i<l;i++){if(parsers[i].is($.trim(getElementText(config,node)))){return parsers[i];}}return parsers[0];}function getParserById(name){var l=parsers.length;for(var i=0;i<l;i++){if(parsers[i].id.toLowerCase()==name.toLowerCase()){return parsers[i];}}return false;}function buildCache(table){if(table.config.debug){var cacheTime=new Date();}var totalRows=(table.tBodies[0]&&table.tBodies[0].rows.length)||0,totalCells=(table.tBodies[0].rows[0]&&table.tBodies[0].rows[0].cells.length)||0,parsers=table.config.parsers,cache={row:[],normalized:[]};for(var i=0;i<totalRows;++i){var c=table.tBodies[0].rows[i],cols=[];cache.row.push($(c));for(var j=0;j<totalCells;++j){cols.push(parsers[j].format(getElementText(table.config,c.cells[j]),table,c.cells[j]));}cols.push(i);cache.normalized.push(cols);cols=null;};if(table.config.debug){benchmark("Building cache for "+totalRows+" rows:",cacheTime);}return cache;};function getElementText(config,node){if(!node)return"";var t="";if(config.textExtraction=="simple"){if(node.childNodes[0]&&node.childNodes[0].hasChildNodes()){t=node.childNodes[0].innerHTML;}else{t=node.innerHTML;}}else{if(typeof(config.textExtraction)=="function"){t=config.textExtraction(node);}else{t=$(node).text();}}return t;}function appendToTable(table,cache){if(table.config.debug){var appendTime=new Date()}var c=cache,r=c.row,n=c.normalized,totalRows=n.length,checkCell=(n[0].length-1),tableBody=$(table.tBodies[0]),rows=[];for(var i=0;i<totalRows;i++){rows.push(r[n[i][checkCell]]);if(!table.config.appender){var o=r[n[i][checkCell]];var l=o.length;for(var j=0;j<l;j++){tableBody[0].appendChild(o[j]);}}}if(table.config.appender){table.config.appender(table,rows);}rows=null;if(table.config.debug){benchmark("Rebuilt table:",appendTime);}applyWidget(table);};function buildHeaders(table){if(table.config.debug){var time=new Date();}var meta=($.meta)?true:false,tableHeadersRows=[];for(var i=0;i<table.tHead.rows.length;i++){tableHeadersRows[i]=0;};$tableHeaders=$("thead th",table);$tableHeaders.each(function(index){this.count=0;this.column=index;this.order=formatSortingOrder(table.config.sortInitialOrder);if(checkHeaderMetadata(this)||checkHeaderOptions(table,index))this.sortDisabled=true;if(!this.sortDisabled){$(this).addClass(table.config.cssHeader);}table.config.headerList[index]=this;});if(table.config.debug){benchmark("Built headers:",time);log($tableHeaders);}return $tableHeaders;};function checkCellColSpan(table,rows,row){var arr=[],r=table.tHead.rows,c=r[row].cells;for(var i=0;i<c.length;i++){var cell=c[i];if(cell.colSpan>1){arr=arr.concat(checkCellColSpan(table,headerArr,row++));}else{if(table.tHead.length==1||(cell.rowSpan>1||!r[row+1])){arr.push(cell);}}}return arr;};function checkHeaderMetadata(cell){if((
|
||
|
|
||
|
var yourls_defaultsort = 2; // default column to sort on (overwrite this inline in page)
|
||
|
var yourls_defaultorder = 1; // default order ('asc':0, 'desc':1) to sort on (overwrite this inline in page)
|
||
|
|
||
|
// Initialise the table to sort
|
||
|
$(document).ready(function(){
|
||
|
if ($("#main_table").tablesorter && $("#main_table tr#nourl_found").css('display') == 'none') {
|
||
|
var order = {'id':0, 'url':1, 'timestamp':2, 'ip':3, 'clicks':4};
|
||
|
var order_by = {'asc':0, 'desc':1};
|
||
|
var s_by = order[query_string('s_by')];
|
||
|
var s_order = order_by[query_string('s_order')];
|
||
|
if( s_by == undefined ) {
|
||
|
s_by = yourls_defaultsort;
|
||
|
s_order = yourls_defaultorder;
|
||
|
}
|
||
|
|
||
|
$("#main_table").tablesorter({
|
||
|
sortList:[[ s_by, s_order ]], // Sort on column #3 (numbering starts at 0)
|
||
|
headers: { 5: {sorter: false} }, // no sorter on column #6
|
||
|
widgets: ['zebra'] // prettify
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Get query string
|
||
|
function query_string( key ) {
|
||
|
default_="";
|
||
|
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
|
||
|
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
|
||
|
var qs = regex.exec(window.location.href);
|
||
|
if(qs == null)
|
||
|
return yourls_defaultsort;
|
||
|
else
|
||
|
return qs[1];
|
||
|
}
|