2015-08-17 17:00:26 -07:00
/ * *
2018-11-23 12:29:20 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
* https : //www.drupal.org/node/2815083
* @ preserve
* * /
2015-08-17 17:00:26 -07:00
( function ( $ , Drupal , displace ) {
2018-11-23 12:29:20 +00:00
function TableHeader ( table ) {
var $table = $ ( table ) ;
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
this . $originalTable = $table ;
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
this . $originalHeader = $table . children ( 'thead' ) ;
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
this . $originalHeaderCells = this . $originalHeader . find ( '> tr > th' ) ;
this . displayWeight = null ;
this . $originalTable . addClass ( 'sticky-table' ) ;
this . tableHeight = $table [ 0 ] . clientHeight ;
this . tableOffset = this . $originalTable . offset ( ) ;
this . $originalTable . on ( 'columnschange' , { tableHeader : this } , function ( e , display ) {
var tableHeader = e . data . tableHeader ;
if ( tableHeader . displayWeight === null || tableHeader . displayWeight !== display ) {
tableHeader . recalculateSticky ( ) ;
}
tableHeader . displayWeight = display ;
} ) ;
this . createSticky ( ) ;
}
function forTables ( method , arg ) {
var tables = TableHeader . tables ;
var il = tables . length ;
for ( var i = 0 ; i < il ; i ++ ) {
tables [ i ] [ method ] ( arg ) ;
}
2015-08-17 17:00:26 -07:00
}
function tableHeaderInitHandler ( e ) {
var $tables = $ ( e . data . context ) . find ( 'table.sticky-enabled' ) . once ( 'tableheader' ) ;
var il = $tables . length ;
for ( var i = 0 ; i < il ; i ++ ) {
TableHeader . tables . push ( new TableHeader ( $tables [ i ] ) ) ;
}
forTables ( 'onScroll' ) ;
}
2018-11-23 12:29:20 +00:00
Drupal . behaviors . tableHeader = {
attach : function attach ( context ) {
$ ( window ) . one ( 'scroll.TableHeaderInit' , { context : context } , tableHeaderInitHandler ) ;
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
} ;
function scrollValue ( position ) {
return document . documentElement [ position ] || document . body [ position ] ;
2015-08-17 17:00:26 -07:00
}
function tableHeaderResizeHandler ( e ) {
forTables ( 'recalculateSticky' ) ;
}
function tableHeaderOnScrollHandler ( e ) {
forTables ( 'onScroll' ) ;
}
function tableHeaderOffsetChangeHandler ( e , offsets ) {
forTables ( 'stickyPosition' , offsets . top ) ;
}
$ ( window ) . on ( {
'resize.TableHeader' : tableHeaderResizeHandler ,
'scroll.TableHeader' : tableHeaderOnScrollHandler
} ) ;
2018-11-23 12:29:20 +00:00
$ ( document ) . on ( {
2015-08-17 17:00:26 -07:00
'columnschange.TableHeader' : tableHeaderResizeHandler ,
'drupalViewportOffsetChange.TableHeader' : tableHeaderOffsetChangeHandler
} ) ;
2018-11-23 12:29:20 +00:00
$ . extend ( TableHeader , {
2015-08-17 17:00:26 -07:00
tables : [ ]
} ) ;
2018-11-23 12:29:20 +00:00
$ . extend ( TableHeader . prototype , {
2015-08-17 17:00:26 -07:00
minHeight : 100 ,
tableOffset : null ,
tableHeight : null ,
stickyVisible : false ,
2018-11-23 12:29:20 +00:00
createSticky : function createSticky ( ) {
2015-08-17 17:00:26 -07:00
var $stickyHeader = this . $originalHeader . clone ( true ) ;
2018-11-23 12:29:20 +00:00
this . $stickyTable = $ ( '<table class="sticky-header"/>' ) . css ( {
visibility : 'hidden' ,
position : 'fixed' ,
top : '0px'
} ) . append ( $stickyHeader ) . insertBefore ( this . $originalTable ) ;
2015-08-17 17:00:26 -07:00
this . $stickyHeaderCells = $stickyHeader . find ( '> tr > th' ) ;
this . recalculateSticky ( ) ;
} ,
2018-11-23 12:29:20 +00:00
stickyPosition : function stickyPosition ( offsetTop , offsetLeft ) {
2015-08-17 17:00:26 -07:00
var css = { } ;
if ( typeof offsetTop === 'number' ) {
css . top = offsetTop + 'px' ;
}
if ( typeof offsetLeft === 'number' ) {
2018-11-23 12:29:20 +00:00
css . left = this . tableOffset . left - offsetLeft + 'px' ;
2015-08-17 17:00:26 -07:00
}
return this . $stickyTable . css ( css ) ;
} ,
2018-11-23 12:29:20 +00:00
checkStickyVisible : function checkStickyVisible ( ) {
2015-08-17 17:00:26 -07:00
var scrollTop = scrollValue ( 'scrollTop' ) ;
var tableTop = this . tableOffset . top - displace . offsets . top ;
var tableBottom = tableTop + this . tableHeight ;
var visible = false ;
2018-11-23 12:29:20 +00:00
if ( tableTop < scrollTop && scrollTop < tableBottom - this . minHeight ) {
2015-08-17 17:00:26 -07:00
visible = true ;
}
this . stickyVisible = visible ;
return visible ;
} ,
2018-11-23 12:29:20 +00:00
onScroll : function onScroll ( e ) {
2015-08-17 17:00:26 -07:00
this . checkStickyVisible ( ) ;
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
this . stickyPosition ( null , scrollValue ( 'scrollLeft' ) ) ;
this . $stickyTable . css ( 'visibility' , this . stickyVisible ? 'visible' : 'hidden' ) ;
} ,
2018-11-23 12:29:20 +00:00
recalculateSticky : function recalculateSticky ( event ) {
2015-08-17 17:00:26 -07:00
this . tableHeight = this . $originalTable [ 0 ] . clientHeight ;
displace . offsets . top = displace . calculateOffset ( 'top' ) ;
this . tableOffset = this . $originalTable . offset ( ) ;
this . stickyPosition ( displace . offsets . top , scrollValue ( 'scrollLeft' ) ) ;
var $that = null ;
var $stickyCell = null ;
var display = null ;
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
var il = this . $originalHeaderCells . length ;
for ( var i = 0 ; i < il ; i ++ ) {
$that = $ ( this . $originalHeaderCells [ i ] ) ;
$stickyCell = this . $stickyHeaderCells . eq ( $that . index ( ) ) ;
display = $that . css ( 'display' ) ;
if ( display !== 'none' ) {
2018-11-23 12:29:20 +00:00
$stickyCell . css ( { width : $that . css ( 'width' ) , display : display } ) ;
} else {
2015-08-17 17:00:26 -07:00
$stickyCell . css ( 'display' , 'none' ) ;
}
}
this . $stickyTable . css ( 'width' , this . $originalTable . outerWidth ( ) ) ;
}
} ) ;
Drupal . TableHeader = TableHeader ;
2019-01-24 08:00:03 +00:00
} ) ( jQuery , Drupal , window . Drupal . displace ) ;