From 12cf4675b75fa8c7cc8527dfc51b2bd5f6a166a7 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Wed, 1 Feb 2012 18:02:19 -0600 Subject: [PATCH] added widget init function & updated saveSort widget --- README.markdown | 24 ++++++++++++++++++++++++ changelog.txt | 25 +++++++++++++++++++++++++ docs/example-widget-savesort.html | 2 +- js/jquery.tablesorter.js | 18 +++++++++++++++--- js/jquery.tablesorter.min.js | 4 ++-- js/jquery.tablesorter.widgets.js | 20 ++++++++++++++------ js/jquery.tablesorter.widgets.min.js | 14 +++++++------- package.json | 2 +- 8 files changed, 89 insertions(+), 20 deletions(-) diff --git a/README.markdown b/README.markdown index 1ad0cbac..ffa97042 100644 --- a/README.markdown +++ b/README.markdown @@ -34,6 +34,30 @@ Included all original [document pages](http://mottie.github.com/tablesorter/docs View the [complete listing here](http://mottie.github.com/tablesorter/changelog.txt). +####Version 2.0.28 (2012-2-1) + +* Added a new function to widgets called "init" which is called upon initialization, before any of the widgets are applied. + * I added it to allow the "saveSort" widget to get the saved sort data (localStorage or cookie) before the initial sort was applied. + * The "saveSort" widget is still compatible with the original tablesorter, but the original version will call all of the widgets twice on initialization, if using the "saveSort" widget. + * New add widget format is as follows: + + ```javascript + $.tablesorter.addWidget({ + id: 'myWidget', + init: function(table, allWidgets, thisWidget){ + // widget initialization code - this is only run ONCE + // but in this example I call the format function because + // I want to keep it backwards compatible with the original tablesorter + thisWidget.format(table, true); + }, + format: function(table, initFlag) { + // widget code to apply to the table AFTER EACH SORT + // the initFlag is true when format is called for the first time, but + // only if it is called from the init function + } + }); + ``` + ####Version 2.0.27 (2012-1-31) * Added `sortReset` option diff --git a/changelog.txt b/changelog.txt index fe808c98..f7229c49 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,30 @@ TableSorter Change Log +Version 2.0.28 (2012-2-1) +============================ + +* Added a new function to widgets called "init" which is called upon initialization, before any of the widgets are applied. + * I added it to allow the "saveSort" widget to get the saved sort data (localStorage or cookie) before the initial sort was applied. + * The "saveSort" widget is still compatible with the original tablesorter, but the original version will call all of the widgets twice on initialization, if using the "saveSort" widget. + * New add widget format is as follows: + + ```javascript + $.tablesorter.addWidget({ + id: 'myWidget', + init: function(table, allWidgets, thisWidget){ + // widget initialization code - this is only run ONCE + // but in this example I call the format function because + // I want to keep it backwards compatible with the original tablesorter + thisWidget.format(table, true); + }, + format: function(table, initFlag) { + // widget code to apply to the table AFTER EACH SORT + // the initFlag is true when format is called for the first time, but + // only if it is called from the init function + } + }); + ``` + Version 2.0.27 (2012-1-31) ============================ diff --git a/docs/example-widget-savesort.html b/docs/example-widget-savesort.html index 8f73c3e1..59459e97 100644 --- a/docs/example-widget-savesort.html +++ b/docs/example-widget-savesort.html @@ -24,7 +24,7 @@ $("table").tablesorter({ // use save sort widget - widgets: ["saveSort"] + widgets: ["saveSort", "zebra"] }); diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index a9009f48..6cf3b9e9 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -1,6 +1,6 @@ /* * TableSorter 2.0 - Client-side table sorting with ease! -* Version 2.0.27 +* Version 2.0.28 * @requires jQuery v1.2.3 * * Copyright (c) 2007 Christian Bach @@ -76,7 +76,7 @@ * @author Christian Bach/christian.bach@polyester.se * @contributor Rob Garrison/https://github.com/Mottie/tablesorter */ -(function($){ +!(function($){ $.extend({ tablesorter: new function(){ @@ -109,7 +109,7 @@ /* debuging utils */ function log(s) { - if (typeof console !== "undefined" && typeof console.debug !== "undefined") { + if (typeof console !== "undefined" && typeof console.log !== "undefined") { console.log(s); } else { alert(s); @@ -266,6 +266,16 @@ return cache; } + function initWidgets(table){ + var i, w, l = widgets.length; + for (i = 0; i < l; i++) { + w = widgets[i]; + if (w && w.hasOwnProperty('init')) { + w.init(table, widgets, w); + } + } + } + function getWidgetById(name) { var i, w, l = widgets.length; for (i = 0; i < l; i++) { @@ -774,6 +784,8 @@ if ($.metadata && ($(this).metadata() && $(this).metadata().sortlist)) { config.sortList = $(this).metadata().sortlist; } + // initialize widgets + initWidgets(this); // if user has supplied a sort list to constructor. if (config.sortList.length > 0) { $this.trigger("sorton", [config.sortList]); diff --git a/js/jquery.tablesorter.min.js b/js/jquery.tablesorter.min.js index a07bb3c0..b2664e57 100644 --- a/js/jquery.tablesorter.min.js +++ b/js/jquery.tablesorter.min.js @@ -1,7 +1,7 @@ /* * TableSorter 2.0 - Client-side table sorting with ease! -* Version 2.0.27 Minified using http://dean.edwards.name/packer/ +* Version 2.0.28 Minified using http://dean.edwards.name/packer/ * Copyright (c) 2007 Christian Bach */ -(function($){$.extend({tablesorter:new function(){var g=[],widgets=[],tbl;this.defaults={cssHeader:"header",cssAsc:"headerSortUp",cssDesc:"headerSortDown",cssChildRow:"expand-child",sortInitialOrder:"asc",sortMultiSortKey:"shiftKey",sortForce:null,sortAppend:null,sortLocaleCompare:false,textExtraction:"simple",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"mmddyyyy",onRenderHeader:null,selectorHeaders:'thead th',tableClass:'tablesorter',debug:false};function log(s){if(typeof console!=="undefined"&&typeof console.debug!=="undefined"){console.log(s)}else{alert(s)}}function benchmark(s,d){log(s+","+(new Date().getTime()-d.getTime())+"ms")}this.benchmark=benchmark;function getElementText(a,b,c){var d="",te=a.textExtraction;if(!b){return""}if(!a.supportsTextContent){a.supportsTextContent=b.textContent||false}if(te==="simple"){if(a.supportsTextContent){d=b.textContent}else{if(b.childNodes[0]&&b.childNodes[0].hasChildNodes()){d=b.childNodes[0].innerHTML}else{d=b.innerHTML}}}else{if(typeof(te)==="function"){d=te(b)}else if(typeof(te)==="object"&&te.hasOwnProperty(c)){d=te[c](b)}else{d=$(b).text()}}return d}function getParserById(a){var i,l=g.length;for(i=0;i").each(function(a){this.column=header_index[this.parentNode.rowIndex+"-"+this.cellIndex];this.order=formatSortingOrder(checkHeaderOrder(b,a));this.count=this.order;if(checkHeaderMetadata(this)||checkHeaderOptions(b,a)||$(this).is('.sorter-false')){this.sortDisabled=true}this.lockedOrder=false;lock=checkHeaderLocked(b,a);if(typeof(lock)!=='undefined'&&lock!==false){this.order=this.lockedOrder=formatSortingOrder(lock)}if(!this.sortDisabled){$th=$(this).addClass(c.cssHeader);if(c.onRenderHeader){c.onRenderHeader.apply($th,[a])}}c.headerList[a]=this});if(c.debug){benchmark("Built headers:",time);log($tableHeaders)}return $tableHeaders}function checkCellColSpan(a,b,d){var i,cell,arr=[],r=a.tHead.rows,c=r[d].cells;for(i=0;i1){arr=arr.concat(checkCellColSpan(a,b,d++))}else{if(a.tHead.length===1||(cell.rowSpan>1||!r[d+1])){arr.push(cell)}}}return arr}function isValueInArray(v,a){var i,l=a.length;for(i=0;i');$("tr:first td",a.tBodies[0]).each(function(){c.append($('').css('width',$(this).width()))});$(a).prepend(c)}}function updateHeaderSortCount(a,b){var i,s,o,c=a.config,l=b.length;for(i=0;ib)?1:-1}catch(er){return 0}}function sortTextDesc(a,b){if(a===''){return 1}if(b===''){return-1}if(a===b){return 0}if($.data(tbl[0],"tablesorter").sortLocaleCompare){return b.localeCompare(a)}return-sortText(a,b)}function getTextValue(a,b,d){if(b){var i,l=a.length,n=b+d;for(i=0;i=2){config.sortList.splice(j,1);o.count=0}}}}else{if(this.order<2){config.sortList.push([i,this.order])}}}if(config.sortAppend!==null){a=config.sortAppend;for(j=0;j0){c.trigger("sorton",[config.sortList])}else{applyWidget(this)}})};this.addParser=function(b){var i,l=g.length,a=true;for(i=0;i").each(function(a){this.column=header_index[this.parentNode.rowIndex+"-"+this.cellIndex];this.order=formatSortingOrder(checkHeaderOrder(b,a));this.count=this.order;if(checkHeaderMetadata(this)||checkHeaderOptions(b,a)||$(this).is('.sorter-false')){this.sortDisabled=true}this.lockedOrder=false;lock=checkHeaderLocked(b,a);if(typeof(lock)!=='undefined'&&lock!==false){this.order=this.lockedOrder=formatSortingOrder(lock)}if(!this.sortDisabled){$th=$(this).addClass(c.cssHeader);if(c.onRenderHeader){c.onRenderHeader.apply($th,[a])}}c.headerList[a]=this});if(c.debug){benchmark("Built headers:",time);log($tableHeaders)}return $tableHeaders}function checkCellColSpan(a,b,d){var i,cell,arr=[],r=a.tHead.rows,c=r[d].cells;for(i=0;i1){arr=arr.concat(checkCellColSpan(a,b,d++))}else{if(a.tHead.length===1||(cell.rowSpan>1||!r[d+1])){arr.push(cell)}}}return arr}function isValueInArray(v,a){var i,l=a.length;for(i=0;i');$("tr:first td",a.tBodies[0]).each(function(){c.append($('').css('width',$(this).width()))});$(a).prepend(c)}}function updateHeaderSortCount(a,b){var i,s,o,c=a.config,l=b.length;for(i=0;ib)?1:-1}catch(er){return 0}}function sortTextDesc(a,b){if(a===''){return 1}if(b===''){return-1}if(a===b){return 0}if($.data(tbl[0],"tablesorter").sortLocaleCompare){return b.localeCompare(a)}return-sortText(a,b)}function getTextValue(a,b,d){if(b){var i,l=a.length,n=b+d;for(i=0;i=2){config.sortList.splice(j,1);o.count=0}}}}else{if(this.order<2){config.sortList.push([i,this.order])}}}if(config.sortAppend!==null){a=config.sortAppend;for(j=0;j0){c.trigger("sorton",[config.sortList])}else{applyWidget(this)}})};this.addParser=function(b){var i,l=g.length,a=true;for(i=0;i 0) { - $(table).trigger('sorton', sortList); + // 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; + } else if (sortList && sortList.length > 0) { + // update sort change + $(table).trigger('sorton', [sortList]); } } } diff --git a/js/jquery.tablesorter.widgets.min.js b/js/jquery.tablesorter.widgets.min.js index 6fc42d34..706b4fc7 100644 --- a/js/jquery.tablesorter.widgets.min.js +++ b/js/jquery.tablesorter.widgets.min.js @@ -1,10 +1,10 @@ -/* TableSorter 2.0 Widgets - updated 1/31/2012 */ +/* TableSorter 2.0 Widgets - updated 2/1/2012 */ (function(b){ -b.tablesorter.addWidget({id:"uitheme",format:function(f){var c,a,d,g,e,h=f.config,i=b(f),j=["ui-icon-arrowthick-2-n-s","ui-icon-arrowthick-1-s","ui-icon-arrowthick-1-n"];h.widgetUitheme&&h.widgetUitheme.hasOwnProperty("css")&&(j=h.widgetUitheme.css||j);d=j.join(" ");h.debug&&(c=new Date);i.is(".ui-theme")||(i.addClass("ui-widget ui-widget-content ui-corner-all ui-theme"),b.each(h.headerList,function(){b(this).addClass("ui-widget-header ui-corner-all").append('').wrapInner('
').hover(function(){b(this).addClass("ui-state-hover")}, function(){b(this).removeClass("ui-state-hover")})}));b.each(h.headerList,function(c){g=b(this);this.sortDisabled?g.find("span.ui-icon").removeClass(d+" ui-icon"):(a=g.hasClass(h.cssAsc)?j[1]:g.hasClass(h.cssDesc)?j[2]:g.hasClass(h.cssHeader)?j[0]:"",e=i.hasClass("hasStickyHeaders")?i.find("tr.stickyHeader").find("th").eq(c).add(g):g,e[a===j[0]?"removeClass":"addClass"]("ui-state-active").find("span.ui-icon").removeClass(d).addClass(a))});h.debug&&b.tablesorter.benchmark("Applying uitheme widget", c)}}); -b.tablesorter.addWidget({id:"columns",format:function(f){var c,a,d,g,e=f.config,h=e.sortList,i=h.length,j=["primary","secondary","tertiary"];e.widgetColumns&&e.widgetColumns.hasOwnProperty("css")&&(j=e.widgetColumns.css||j);d=j.length-1;g=j.join(" ");e.debug&&(a=new Date);h&&h[0]&&b("tr:visible",f.tBodies[0]).each(function(a){c=b(this).children().removeClass(g);c.eq(h[0][0]).addClass(j[0]);if(1',l;i.debug&&(l=new Date);for(c=0;c';k.find("thead").append(f+="").find(".filter").bind("keyup",function(){a=k.find(".filter").map(function(){return(b(this).val()|| "").toLowerCase()}).get();""===a.join("")?k.find("tr").show():k.find("tbody").find("tr:not(.expand-child)").each(function(){d=!0;e=b(this).nextUntil("tr:not(.expand-child)");g=e.length&&("undefined"!==typeof i.widgetFilterChildRows?i.widgetFilterChildRows:1)?e.text():"";h=b(this).find("td");for(c=0;cb.top&&d
').wrapInner('
').find(".resizer").bind("mousedown",function(c){a.resizable_target=b(c.target).closest("th");a.resizable_position=c.pageX}).end().bind("mousemove",function(c){if(!(0===a.resizable_position||null===typeof a.resizable_target)){var b=c.pageX-a.resizable_position, d=a.resizable_target.closest("th").prev();a.resizable_target.width()<-b||d&&d.width()<=b||(d.width(d.width()+b),a.resizable_position=c.pageX)}}).bind("mouseup",function(){e();return!1});b(f).find("thead").bind("mouseup mouseleave",function(){e()});a.resizable=!0}}}); -b.tablesorter.addWidget({id:"saveSort",format:function(f){var c,a,d,g,e=f.config;d='{"sortList":'+JSON.stringify(e.sortList)+"}";e.debug&&(g=new Date);if(e.widgetsavesort)c="tablesorter"+(e.tableIndex||0)+f.id,e.hasLocalStorage?localStorage[c]= d:(a=new Date,a.setTime(a.getTime()+31536E6),document.cookie=c+"="+d+"; expires="+a.toGMTString()+"; path=/"),e.debug&&b.tablesorter.benchmark('saveSort: Saving sort to "'+c+'" in '+(e.hasLocalStorage?"local storage":"a cookie"),g);else{e.widgetsavesort=!0;e.hasLocalStorage=!1;try{localStorage.getItem&&(e.hasLocalStorage=!0)}catch(h){}e.tableIndex=b(".tablesorter").index(b(f));c="tablesorter"+(e.tableIndex||0)+f.id;e.hasLocalStorage?a=localStorage[c]:(d=document.cookie.split(/[;\s|=]/),a=b.inArray(c, d)+1,a=0!==a?d[a]:"");try{a=b.parseJSON(a)}catch(i){a=""}d=a&&a.hasOwnProperty("sortList")&&b.isArray(a.sortList)?[a.sortList]:"";e.debug&&b.tablesorter.benchmark('saveSort: Last sort for "'+c+'" obtained from '+(e.hasLocalStorage?"local storage":"a cookie"),g);d&&0').wrapInner('
').hover(function(){b(this).addClass("ui-state-hover")}, function(){b(this).removeClass("ui-state-hover")})}));b.each(c.headerList,function(e){f=b(this);this.sortDisabled?f.find("span.ui-icon").removeClass(d+" ui-icon"):(a=f.hasClass(c.cssAsc)?j[1]:f.hasClass(c.cssDesc)?j[2]:f.hasClass(c.cssHeader)?j[0]:"",h=i.hasClass("hasStickyHeaders")?i.find("tr.stickyHeader").find("th").eq(e).add(f):f,h[a===j[0]?"removeClass":"addClass"]("ui-state-active").find("span.ui-icon").removeClass(d).addClass(a))});c.debug&&b.tablesorter.benchmark("Applying uitheme widget", g)}}); +b.tablesorter.addWidget({id:"columns",format:function(e){var g,a,d,f,h=e.config,c=h.sortList,i=c.length,j=["primary","secondary","tertiary"];h.widgetColumns&&h.widgetColumns.hasOwnProperty("css")&&(j=h.widgetColumns.css||j);d=j.length-1;f=j.join(" ");h.debug&&(a=new Date);c&&c[0]&&b("tr:visible",e.tBodies[0]).each(function(a){g=b(this).children().removeClass(f);g.eq(c[0][0]).addClass(j[0]);if(1',l;i.debug&&(l=new Date);for(g=0;g';k.find("thead").append(e+="").find(".filter").bind("keyup",function(){a=k.find(".filter").map(function(){return(b(this).val()|| "").toLowerCase()}).get();""===a.join("")?k.find("tr").show():k.find("tbody").find("tr:not(.expand-child)").each(function(){d=!0;h=b(this).nextUntil("tr:not(.expand-child)");f=h.length&&("undefined"!==typeof i.widgetFilterChildRows?i.widgetFilterChildRows:1)?h.text():"";c=b(this).find("td");for(g=0;gc.top&&b
').wrapInner('
').find(".resizer").bind("mousedown",function(c){a.resizable_target=b(c.target).closest("th");a.resizable_position=c.pageX}).end().bind("mousemove",function(c){if(!(0===a.resizable_position||null===typeof a.resizable_target)){var b=c.pageX-a.resizable_position, d=a.resizable_target.closest("th").prev();a.resizable_target.width()<-b||d&&d.width()<=b||(d.width(d.width()+b),a.resizable_position=c.pageX)}}).bind("mouseup",function(){h();return!1});b(e).find("thead").bind("mouseup mouseleave",function(){h()});a.resizable=!0}}}); +b.tablesorter.addWidget({id:"saveSort",init:function(b,g,a){a.format(b,!0)},format:function(e,g){var a,d,f,h,c=e.config;f='{"sortList":'+JSON.stringify(c.sortList)+"}";c.debug&&(h=new Date);if(c.widgetsavesort)a="tablesorter"+(c.tableIndex|| 0)+e.id,c.hasLocalStorage?localStorage[a]=f:(d=new Date,d.setTime(d.getTime()+31536E6),document.cookie=a+"="+f+"; expires="+d.toGMTString()+"; path=/"),c.debug&&b.tablesorter.benchmark('saveSort: Saving sort to "'+a+'" in '+(c.hasLocalStorage?"local storage":"a cookie"),h);else{c.widgetsavesort=!0;c.hasLocalStorage=!1;try{localStorage.getItem&&(c.hasLocalStorage=!0)}catch(i){}c.tableIndex=b(".tablesorter").index(b(e));a="tablesorter"+(c.tableIndex||0)+e.id;c.hasLocalStorage?d=localStorage[a]:(f=document.cookie.split(/[;\s|=]/), d=b.inArray(a,f)+1,d=0!==d?f[d]:"");try{d=b.parseJSON(d)}catch(j){d=""}f=d&&d.hasOwnProperty("sortList")&&b.isArray(d.sortList)?d.sortList:"";c.debug&&b.tablesorter.benchmark('saveSort: Last sort for "'+a+'" obtained from '+(c.hasLocalStorage?"local storage":"a cookie"),h);g&&f&&0