mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
71 lines
9.0 KiB
JavaScript
71 lines
9.0 KiB
JavaScript
/*! Widget: resizable - updated 6/26/2015 (v2.22.2) */
|
|
/*jshint browser:true, jquery:true, unused:false */
|
|
!function(a,b){"use strict";var c=a.tablesorter||{};a.extend(c.css,{resizableContainer:"tablesorter-resizable-container",resizableHandle:"tablesorter-resizable-handle",resizableNoSelect:"tablesorter-disableSelection",resizableStorage:"tablesorter-resizable"}),
|
|
// Add extra scroller css
|
|
a(function(){var b="<style>body."+c.css.resizableNoSelect+" { -ms-user-select: none; -moz-user-select: -moz-none;-khtml-user-select: none; -webkit-user-select: none; user-select: none; }."+c.css.resizableContainer+" { position: relative; height: 1px; }."+c.css.resizableHandle+" { position: absolute; display: inline-block; width: 8px;top: 1px; cursor: ew-resize; z-index: 3; user-select: none; -moz-user-select: none; }</style>";a(b).appendTo("body")}),c.resizable={init:function(b,d){if(!b.$table.hasClass("hasResizable")){b.$table.addClass("hasResizable");var e,f,g,h,i,j=b.$table,k=j.parent(),l=parseInt(j.css("margin-top"),10),
|
|
// internal variables
|
|
m=d.resizable_vars={useStorage:c.storage&&d.resizable!==!1,$wrap:k,mouseXPosition:0,$target:null,$next:null,overflow:"auto"===k.css("overflow")||"scroll"===k.css("overflow")||"auto"===k.css("overflow-x")||"scroll"===k.css("overflow-x"),storedSizes:[]};
|
|
// add container
|
|
for(
|
|
// set default widths
|
|
c.resizableReset(b.table,!0),
|
|
// now get measurements!
|
|
m.tableWidth=j.width(),
|
|
// attempt to autodetect
|
|
m.fullWidth=Math.abs(k.width()-m.tableWidth)<20,/*
|
|
// Hacky method to determine if table width is set to 'auto'
|
|
// http://stackoverflow.com/a/20892048/145346
|
|
if ( !vars.fullWidth ) {
|
|
tmp = $table.width();
|
|
$header = $table.wrap('<span>').parent(); // temp variable
|
|
storedSizes = parseInt( $table.css( 'margin-left' ), 10 ) || 0;
|
|
$table.css( 'margin-left', storedSizes + 50 );
|
|
vars.tableWidth = $header.width() > tmp ? 'auto' : tmp;
|
|
$table.css( 'margin-left', storedSizes ? storedSizes : '' );
|
|
$header = null;
|
|
$table.unwrap('<span>');
|
|
}
|
|
*/
|
|
m.useStorage&&m.overflow&&(
|
|
// save table width
|
|
c.storage(b.table,"tablesorter-table-original-css-width",m.tableWidth),i=c.storage(b.table,"tablesorter-table-resized-width")||"auto",c.resizable.setWidth(j,i,!0)),d.resizable_vars.storedSizes=h=(m.useStorage?c.storage(b.table,c.css.resizableStorage):[])||[],c.resizable.setWidths(b,d,h),c.resizable.updateStoredSizes(b,d),d.$resizable_container=a('<div class="'+c.css.resizableContainer+'">').css({top:l}).insertBefore(j),g=0;g<b.columns;g++)f=b.$headerIndexed[g],i=c.getColumnData(b.table,b.headers,g),e="false"===c.getData(f,i,"resizable"),e||a('<div class="'+c.css.resizableHandle+'">').appendTo(d.$resizable_container).attr({"data-column":g,unselectable:"on"}).data("header",f).bind("selectstart",!1);j.one("tablesorter-initialized",function(){c.resizable.setHandlePosition(b,d),c.resizable.bindings(this.config,this.config.widgetOptions)})}},updateStoredSizes:function(a,b){var c,d,e=a.columns,f=b.resizable_vars;for(f.storedSizes=[],c=0;e>c;c++)d=a.$headerIndexed[c],f.storedSizes[c]=d.is(":visible")?d.width():0},setWidth:function(a,b,c){
|
|
// overflow tables need min & max width set as well
|
|
a.css({width:b,"min-width":c?b:"","max-width":c?b:""})},setWidths:function(b,d,e){var f,g,h=d.resizable_vars,i=a(b.namespace+"_extra_headers"),j=b.$table.children("colgroup").children("col");
|
|
// process only if table ID or url match
|
|
if(e=e||h.storedSizes||[],e.length){for(f=0;f<b.columns;f++)
|
|
// set saved resizable widths
|
|
c.resizable.setWidth(b.$headerIndexed[f],e[f],h.overflow),i.length&&(g=i.eq(f).add(j.eq(f)),c.resizable.setWidth(g,e[f],h.overflow));g=a(b.namespace+"_extra_table"),g.length&&!c.hasWidget(b.table,"scroller")&&c.resizable.setWidth(g,b.$table.outerWidth(),h.overflow)}},setHandlePosition:function(b,d){var e,f=c.hasWidget(b.table,"scroller"),g=b.$table.height(),h=d.$resizable_container.children(),i=Math.floor(h.width()/2);f&&(g=0,b.$table.closest("."+c.css.scrollerWrap).children().each(function(){var b=a(this);g+=b.filter('[style*="height"]').length?b.height():b.children("table").height()})),e=b.$table.position().left,h.each(function(){var c=a(this),f=parseInt(c.attr("data-column"),10),h=b.columns-1,j=c.data("header");j&&(j.is(":visible")?(h>f||f===h&&d.resizable_addLastColumn)&&c.css({display:"inline-block",height:g,left:j.position().left-e+j.outerWidth()-i}):c.hide())})},
|
|
// prevent text selection while dragging resize bar
|
|
toggleTextSelection:function(b,d){var e=b.namespace+"tsresize";b.widgetOptions.resizable_vars.disabled=d,a("body").toggleClass(c.css.resizableNoSelect,d),d?a("body").attr("unselectable","on").bind("selectstart"+e,!1):a("body").removeAttr("unselectable").unbind("selectstart"+e)},bindings:function(d,e){var f=d.namespace+"tsresize";e.$resizable_container.children().bind("mousedown",function(b){
|
|
// save header cell and mouse position
|
|
var f,g=e.resizable_vars,h=a(d.namespace+"_extra_headers"),i=a(b.target).data("header");f=parseInt(i.attr("data-column"),10),g.$target=i=i.add(h.filter('[data-column="'+f+'"]')),g.target=f,g.$next=b.shiftKey||e.resizable_targetLast?i.parent().children().not(".resizable-false").filter(":last"):i.nextAll(":not(.resizable-false)").eq(0),f=parseInt(g.$next.attr("data-column"),10),g.$next=g.$next.add(h.filter('[data-column="'+f+'"]')),g.next=f,g.mouseXPosition=b.pageX,c.resizable.updateStoredSizes(d,e),c.resizable.toggleTextSelection(d,!0)}),a(document).bind("mousemove"+f,function(a){var b=e.resizable_vars;
|
|
// ignore mousemove if no mousedown
|
|
b.disabled&&0!==b.mouseXPosition&&b.$target&&(e.resizable_throttle?(clearTimeout(b.timer),b.timer=setTimeout(function(){c.resizable.mouseMove(d,e,a)},isNaN(e.resizable_throttle)?5:e.resizable_throttle)):c.resizable.mouseMove(d,e,a))}).bind("mouseup"+f,function(){e.resizable_vars.disabled&&(c.resizable.toggleTextSelection(d,!1),c.resizable.stopResize(d,e),c.resizable.setHandlePosition(d,e))}),
|
|
// resizeEnd event triggered by scroller widget
|
|
a(b).bind("resize"+f+" resizeEnd"+f,function(){c.resizable.setHandlePosition(d,e)}),
|
|
// right click to reset columns to default widths
|
|
d.$table.bind("columnUpdate"+f,function(){c.resizable.setHandlePosition(d,e)}).find("thead:first").add(a(d.namespace+"_extra_table").find("thead:first")).bind("contextmenu"+f,function(){
|
|
// $.isEmptyObject() needs jQuery 1.4+; allow right click if already reset
|
|
var a=0===e.resizable_vars.storedSizes.length;return c.resizableReset(d.table),c.resizable.setHandlePosition(d,e),e.resizable_vars.storedSizes=[],a})},mouseMove:function(b,d,e){if(0!==d.resizable_vars.mouseXPosition&&d.resizable_vars.$target){
|
|
// resize columns
|
|
var f,g=0,h=d.resizable_vars,i=h.$next,j=h.storedSizes[h.target],k=e.pageX-h.mouseXPosition;if(h.overflow){if(j+k>0){
|
|
// update the entire table width
|
|
for(h.storedSizes[h.target]+=k,c.resizable.setWidth(h.$target,h.storedSizes[h.target],!0),f=0;f<b.columns;f++)g+=h.storedSizes[f];c.resizable.setWidth(b.$table.add(a(b.namespace+"_extra_table")),g)}i.length||(
|
|
// if expanding right-most column, scroll the wrapper
|
|
h.$wrap[0].scrollLeft=b.$table.width())}else h.fullWidth?(h.storedSizes[h.target]+=k,h.storedSizes[h.next]-=k,c.resizable.setWidths(b,d)):(h.storedSizes[h.target]+=k,c.resizable.setWidths(b,d));h.mouseXPosition=e.pageX,
|
|
// dynamically update sticky header widths
|
|
b.$table.trigger("stickyHeadersUpdate")}},stopResize:function(a,b){var d=b.resizable_vars;c.resizable.updateStoredSizes(a,b),d.useStorage&&(
|
|
// save all column widths
|
|
c.storage(a.table,c.css.resizableStorage,d.storedSizes),c.storage(a.table,"tablesorter-table-resized-width",a.$table.width())),d.mouseXPosition=0,d.$target=d.$next=null,
|
|
// will update stickyHeaders, just in case, see #912
|
|
a.$table.trigger("stickyHeadersUpdate")}},
|
|
// this widget saves the column widths if
|
|
// $.tablesorter.storage function is included
|
|
// **************************
|
|
c.addWidget({id:"resizable",priority:40,options:{resizable:!0,// save column widths to storage
|
|
resizable_addLastColumn:!1,resizable_widths:[],resizable_throttle:!1,// set to true (5ms) or any number 0-10 range
|
|
resizable_targetLast:!1,resizable_fullWidth:null},init:function(a,b,d,e){c.resizable.init(d,e)},remove:function(b,d,e,f){if(e.$resizable_container){var g=d.namespace+"tsresize";d.$table.add(a(d.namespace+"_extra_table")).removeClass("hasResizable").children("thead").unbind("contextmenu"+g),e.$resizable_container.remove(),c.resizable.toggleTextSelection(d,!1),c.resizableReset(b,f),a(document).unbind("mousemove"+g+" mouseup"+g)}}}),c.resizableReset=function(b,d){a(b).each(function(){var a,e,f=this.config,g=f&&f.widgetOptions,h=g.resizable_vars;if(b&&f&&f.$headerIndexed.length){for(
|
|
// restore the initial table width
|
|
h.overflow&&h.tableWidth&&(c.resizable.setWidth(f.$table,h.tableWidth,!0),h.useStorage&&c.storage(b,"tablesorter-table-resized-width","auto")),a=0;a<f.columns;a++)e=f.$headerIndexed[a],g.resizable_widths&&g.resizable_widths[a]?c.resizable.setWidth(e,g.resizable_widths[a],h.overflow):e.hasClass("resizable-false")||c.resizable.setWidth(e,"",h.overflow);
|
|
// reset stickyHeader widths
|
|
f.$table.trigger("stickyHeadersUpdate"),c.storage&&!d&&c.storage(this,c.css.resizableStorage,{})}})}}(jQuery,window); |