Debian Codegraph @

js/widgets/widget-resizable.js

raw symbols SCIP
1 /*! Widget: resizable - updated 2018-03-26 (v2.30.2) */
2 /*jshint browser:true, jquery:true, unused:false */
3 ;(function ($, window) {
4 'use strict';
5 var ts = $.tablesorter || {};
6
7 $.extend(ts.css, {
8 resizableContainer : 'tablesorter-resizable-container',
9 resizableHandle : 'tablesorter-resizable-handle',
10 resizableNoSelect : 'tablesorter-disableSelection',
11 resizableStorage : 'tablesorter-resizable'
12 });
13
14 // Add extra scroller css
15 $(function() {
16 var s = '<style>' +
17 'body.' + ts.css.resizableNoSelect + ' { -ms-user-select: none; -moz-user-select: -moz-none;' +
18 '-khtml-user-select: none; -webkit-user-select: none; user-select: none; }' +
19 '.' + ts.css.resizableContainer + ' { position: relative; height: 1px; }' +
20 // make handle z-index > than stickyHeader z-index, so the handle stays above sticky header
21 '.' + ts.css.resizableHandle + ' { position: absolute; display: inline-block; width: 8px;' +
22 'top: 1px; cursor: ew-resize; z-index: 3; user-select: none; -moz-user-select: none; }' +
23 '</style>';
24 $('head').append(s);
25 });
26
27 ts.resizable = {
28 init : function( c, wo ) {
29 if ( c.$table.hasClass( 'hasResizable' ) ) { return; }
30 c.$table.addClass( 'hasResizable' );
31
32 var noResize, $header, column, storedSizes, tmp,
33 $table = c.$table,
34 $parent = $table.parent(),
35 marginTop = parseInt( $table.css( 'margin-top' ), 10 ),
36
37 // internal variables
38 vars = wo.resizable_vars = {
39 useStorage : ts.storage && wo.resizable !== false,
40 $wrap : $parent,
41 mouseXPosition : 0,
42 $target : null,
43 $next : null,
44 overflow : $parent.css('overflow') === 'auto' ||
45 $parent.css('overflow') === 'scroll' ||
46 $parent.css('overflow-x') === 'auto' ||
47 $parent.css('overflow-x') === 'scroll',
48 storedSizes : []
49 };
50
51 // set default widths
52 ts.resizableReset( c.table, true );
53
54 // now get measurements!
55 vars.tableWidth = $table.width();
56 // attempt to autodetect
57 vars.fullWidth = Math.abs( $parent.width() - vars.tableWidth ) < 20;
58
59 /*
60 // Hacky method to determine if table width is set to 'auto'
61 // http://stackoverflow.com/a/20892048/145346
62 if ( !vars.fullWidth ) {
63 tmp = $table.width();
64 $header = $table.wrap('<span>').parent(); // temp variable
65 storedSizes = parseInt( $table.css( 'margin-left' ), 10 ) || 0;
66 $table.css( 'margin-left', storedSizes + 50 );
67 vars.tableWidth = $header.width() > tmp ? 'auto' : tmp;
68 $table.css( 'margin-left', storedSizes ? storedSizes : '' );
69 $header = null;
70 $table.unwrap('<span>');
71 }
72 */
73
74 if ( vars.useStorage && vars.overflow ) {
75 // save table width
76 ts.storage( c.table, 'tablesorter-table-original-css-width', vars.tableWidth );
77 tmp = ts.storage( c.table, 'tablesorter-table-resized-width' ) || 'auto';
78 ts.resizable.setWidth( $table, tmp, true );
79 }
80 wo.resizable_vars.storedSizes = storedSizes = ( vars.useStorage ?
81 ts.storage( c.table, ts.css.resizableStorage ) :
82 [] ) || [];
83 ts.resizable.setWidths( c, wo, storedSizes );
84 ts.resizable.updateStoredSizes( c, wo );
85
86 wo.$resizable_container = $( '<div class="' + ts.css.resizableContainer + '">' )
87 .css({ top : marginTop })
88 .insertBefore( $table );
89 // add container
90 for ( column = 0; column < c.columns; column++ ) {
91 $header = c.$headerIndexed[ column ];
92 tmp = ts.getColumnData( c.table, c.headers, column );
93 noResize = ts.getData( $header, tmp, 'resizable' ) === 'false';
94 if ( !noResize ) {
95 $( '<div class="' + ts.css.resizableHandle + '">' )
96 .appendTo( wo.$resizable_container )
97 .attr({
98 'data-column' : column,
99 'unselectable' : 'on'
100 })
101 .data( 'header', $header )
102 .bind( 'selectstart', false );
103 }
104 }
105 ts.resizable.bindings( c, wo );
106 },
107
108 updateStoredSizes : function( c, wo ) {
109 var column, $header,
110 len = c.columns,
111 vars = wo.resizable_vars;
112 vars.storedSizes = [];
113 for ( column = 0; column < len; column++ ) {
114 $header = c.$headerIndexed[ column ];
115 vars.storedSizes[ column ] = $header.is(':visible') ? $header.width() : 0;
116 }
117 },
118
119 setWidth : function( $el, width, overflow ) {
120 // overflow tables need min & max width set as well
121 $el.css({
122 'width' : width,
123 'min-width' : overflow ? width : '',
124 'max-width' : overflow ? width : ''
125 });
126 },
127
128 setWidths : function( c, wo, storedSizes ) {
129 var column, $temp,
130 vars = wo.resizable_vars,
131 $extra = $( c.namespace + '_extra_headers' ),
132 $col = c.$table.children( 'colgroup' ).children( 'col' );
133 storedSizes = storedSizes || vars.storedSizes || [];
134 // process only if table ID or url match
135 if ( storedSizes.length ) {
136 for ( column = 0; column < c.columns; column++ ) {
137 // set saved resizable widths
138 ts.resizable.setWidth( c.$headerIndexed[ column ], storedSizes[ column ], vars.overflow );
139 if ( $extra.length ) {
140 // stickyHeaders needs to modify min & max width as well
141 $temp = $extra.eq( column ).add( $col.eq( column ) );
142 ts.resizable.setWidth( $temp, storedSizes[ column ], vars.overflow );
143 }
144 }
145 $temp = $( c.namespace + '_extra_table' );
146 if ( $temp.length && !ts.hasWidget( c.table, 'scroller' ) ) {
147 ts.resizable.setWidth( $temp, c.$table.outerWidth(), vars.overflow );
148 }
149 }
150 },
151
152 setHandlePosition : function( c, wo ) {
153 var startPosition,
154 tableHeight = c.$table.height(),
155 $handles = wo.$resizable_container.children(),
156 handleCenter = Math.floor( $handles.width() / 2 );
157
158 if ( ts.hasWidget( c.table, 'scroller' ) ) {
159 tableHeight = 0;
160 c.$table.closest( '.' + ts.css.scrollerWrap ).children().each(function() {
161 var $this = $(this);
162 // center table has a max-height set
163 tableHeight += $this.filter('[style*="height"]').length ? $this.height() : $this.children('table').height();
164 });
165 }
166
167 if ( !wo.resizable_includeFooter && c.$table.children('tfoot').length ) {
168 tableHeight -= c.$table.children('tfoot').height();
169 }
170 // subtract out table left position from resizable handles. Fixes #864
171 // jQuery v3.3.0+ appears to include the start position with the $header.position().left; see #1544
172 startPosition = parseFloat($.fn.jquery) >= 3.3 ? 0 : c.$table.position().left;
173 $handles.each( function() {
174 var $this = $(this),
175 column = parseInt( $this.attr( 'data-column' ), 10 ),
176 columns = c.columns - 1,
177 $header = $this.data( 'header' );
178 if ( !$header ) { return; } // see #859
179 if (
180 !$header.is(':visible') ||
181 ( !wo.resizable_addLastColumn && ts.resizable.checkVisibleColumns(c, column) )
182 ) {
183 $this.hide();
184 } else if ( column < columns || column === columns && wo.resizable_addLastColumn ) {
185 $this.css({
186 display: 'inline-block',
187 height : tableHeight,
188 left : $header.position().left - startPosition + $header.outerWidth() - handleCenter
189 });
190 }
191 });
192 },
193
194 // Fixes #1485
195 checkVisibleColumns: function( c, column ) {
196 var i,
197 len = 0;
198 for ( i = column + 1; i < c.columns; i++ ) {
199 len += c.$headerIndexed[i].is( ':visible' ) ? 1 : 0;
200 }
201 return len === 0;
202 },
203
204 // prevent text selection while dragging resize bar
205 toggleTextSelection : function( c, wo, toggle ) {
206 var namespace = c.namespace + 'tsresize';
207 wo.resizable_vars.disabled = toggle;
208 $( 'body' ).toggleClass( ts.css.resizableNoSelect, toggle );
209 if ( toggle ) {
210 $( 'body' )
211 .attr( 'unselectable', 'on' )
212 .bind( 'selectstart' + namespace, false );
213 } else {
214 $( 'body' )
215 .removeAttr( 'unselectable' )
216 .unbind( 'selectstart' + namespace );
217 }
218 },
219
220 bindings : function( c, wo ) {
221 var namespace = c.namespace + 'tsresize';
222 wo.$resizable_container.children().bind( 'mousedown', function( event ) {
223 // save header cell and mouse position
224 var column,
225 vars = wo.resizable_vars,
226 $extras = $( c.namespace + '_extra_headers' ),
227 $header = $( event.target ).data( 'header' );
228
229 column = parseInt( $header.attr( 'data-column' ), 10 );
230 vars.$target = $header = $header.add( $extras.filter('[data-column="' + column + '"]') );
231 vars.target = column;
232
233 // if table is not as wide as it's parent, then resize the table
234 vars.$next = event.shiftKey || wo.resizable_targetLast ?
235 $header.parent().children().not( '.resizable-false' ).filter( ':last' ) :
236 $header.nextAll( ':not(.resizable-false)' ).eq( 0 );
237
238 column = parseInt( vars.$next.attr( 'data-column' ), 10 );
239 vars.$next = vars.$next.add( $extras.filter('[data-column="' + column + '"]') );
240 vars.next = column;
241
242 vars.mouseXPosition = event.pageX;
243 ts.resizable.updateStoredSizes( c, wo );
244 ts.resizable.toggleTextSelection(c, wo, true );
245 });
246
247 $( document )
248 .bind( 'mousemove' + namespace, function( event ) {
249 var vars = wo.resizable_vars;
250 // ignore mousemove if no mousedown
251 if ( !vars.disabled || vars.mouseXPosition === 0 || !vars.$target ) { return; }
252 if ( wo.resizable_throttle ) {
253 clearTimeout( vars.timer );
254 vars.timer = setTimeout( function() {
255 ts.resizable.mouseMove( c, wo, event );
256 }, isNaN( wo.resizable_throttle ) ? 5 : wo.resizable_throttle );
257 } else {
258 ts.resizable.mouseMove( c, wo, event );
259 }
260 })
261 .bind( 'mouseup' + namespace, function() {
262 if (!wo.resizable_vars.disabled) { return; }
263 ts.resizable.toggleTextSelection( c, wo, false );
264 ts.resizable.stopResize( c, wo );
265 ts.resizable.setHandlePosition( c, wo );
266 });
267
268 // resizeEnd event triggered by scroller widget
269 $( window ).bind( 'resize' + namespace + ' resizeEnd' + namespace, function() {
270 ts.resizable.setHandlePosition( c, wo );
271 });
272
273 // right click to reset columns to default widths
274 c.$table
275 .bind( 'columnUpdate pagerComplete resizableUpdate '.split( ' ' ).join( namespace + ' ' ), function() {
276 ts.resizable.setHandlePosition( c, wo );
277 })
278 .bind( 'resizableReset' + namespace, function() {
279 ts.resizableReset( c.table );
280 })
281 .find( 'thead:first' )
282 .add( $( c.namespace + '_extra_table' ).find( 'thead:first' ) )
283 .bind( 'contextmenu' + namespace, function() {
284 // $.isEmptyObject() needs jQuery 1.4+; allow right click if already reset
285 var allowClick = wo.resizable_vars.storedSizes.length === 0;
286 ts.resizableReset( c.table );
287 ts.resizable.setHandlePosition( c, wo );
288 wo.resizable_vars.storedSizes = [];
289 return allowClick;
290 });
291
292 },
293
294 mouseMove : function( c, wo, event ) {
295 if ( wo.resizable_vars.mouseXPosition === 0 || !wo.resizable_vars.$target ) { return; }
296 // resize columns
297 var column,
298 total = 0,
299 vars = wo.resizable_vars,
300 $next = vars.$next,
301 tar = vars.storedSizes[ vars.target ],
302 leftEdge = event.pageX - vars.mouseXPosition;
303 if ( vars.overflow ) {
304 if ( tar + leftEdge > 0 ) {
305 vars.storedSizes[ vars.target ] += leftEdge;
306 ts.resizable.setWidth( vars.$target, vars.storedSizes[ vars.target ], true );
307 // update the entire table width
308 for ( column = 0; column < c.columns; column++ ) {
309 total += vars.storedSizes[ column ];
310 }
311 ts.resizable.setWidth( c.$table.add( $( c.namespace + '_extra_table' ) ), total );
312 }
313 if ( !$next.length ) {
314 // if expanding right-most column, scroll the wrapper
315 vars.$wrap[0].scrollLeft = c.$table.width();
316 }
317 } else if ( vars.fullWidth ) {
318 vars.storedSizes[ vars.target ] += leftEdge;
319 vars.storedSizes[ vars.next ] -= leftEdge;
320 ts.resizable.setWidths( c, wo );
321 } else {
322 vars.storedSizes[ vars.target ] += leftEdge;
323 ts.resizable.setWidths( c, wo );
324 }
325 vars.mouseXPosition = event.pageX;
326 // dynamically update sticky header widths
327 c.$table.triggerHandler('stickyHeadersUpdate');
328 },
329
330 stopResize : function( c, wo ) {
331 var vars = wo.resizable_vars;
332 ts.resizable.updateStoredSizes( c, wo );
333 if ( vars.useStorage ) {
334 // save all column widths
335 ts.storage( c.table, ts.css.resizableStorage, vars.storedSizes );
336 ts.storage( c.table, 'tablesorter-table-resized-width', c.$table.width() );
337 }
338 vars.mouseXPosition = 0;
339 vars.$target = vars.$next = null;
340 // will update stickyHeaders, just in case, see #912
341 c.$table.triggerHandler('stickyHeadersUpdate');
342 c.$table.triggerHandler('resizableComplete');
343 }
344 };
345
346 // this widget saves the column widths if
347 // $.tablesorter.storage function is included
348 // **************************
349 ts.addWidget({
350 id: 'resizable',
351 priority: 40,
352 options: {
353 resizable : true, // save column widths to storage
354 resizable_addLastColumn : false,
355 resizable_includeFooter: true,
356 resizable_widths : [],
357 resizable_throttle : false, // set to true (5ms) or any number 0-10 range
358 resizable_targetLast : false
359 },
360 init: function(table, thisWidget, c, wo) {
361 ts.resizable.init( c, wo );
362 },
363 format: function( table, c, wo ) {
364 ts.resizable.setHandlePosition( c, wo );
365 },
366 remove: function( table, c, wo, refreshing ) {
367 if (wo.$resizable_container) {
368 var namespace = c.namespace + 'tsresize';
369 c.$table.add( $( c.namespace + '_extra_table' ) )
370 .removeClass('hasResizable')
371 .children( 'thead' )
372 .unbind( 'contextmenu' + namespace );
373
374 wo.$resizable_container.remove();
375 ts.resizable.toggleTextSelection( c, wo, false );
376 ts.resizableReset( table, refreshing );
377 $( document ).unbind( 'mousemove' + namespace + ' mouseup' + namespace );
378 }
379 }
380 });
381
382 ts.resizableReset = function( table, refreshing ) {
383 $( table ).each(function() {
384 var index, $t,
385 c = this.config,
386 wo = c && c.widgetOptions,
387 vars = wo.resizable_vars;
388 if ( table && c && c.$headerIndexed.length ) {
389 // restore the initial table width
390 if ( vars.overflow && vars.tableWidth ) {
391 ts.resizable.setWidth( c.$table, vars.tableWidth, true );
392 if ( vars.useStorage ) {
393 ts.storage( table, 'tablesorter-table-resized-width', vars.tableWidth );
394 }
395 }
396 for ( index = 0; index < c.columns; index++ ) {
397 $t = c.$headerIndexed[ index ];
398 if ( wo.resizable_widths && wo.resizable_widths[ index ] ) {
399 ts.resizable.setWidth( $t, wo.resizable_widths[ index ], vars.overflow );
400 } else if ( !$t.hasClass( 'resizable-false' ) ) {
401 // don't clear the width of any column that is not resizable
402 ts.resizable.setWidth( $t, '', vars.overflow );
403 }
404 }
405
406 // reset stickyHeader widths
407 c.$table.triggerHandler( 'stickyHeadersUpdate' );
408 if ( ts.storage && !refreshing ) {
409 ts.storage( this, ts.css.resizableStorage, [] );
410 }
411 }
412 });
413 };
414
415 })( jQuery, window );