2015-06-08 14:04:39 +00:00
/ * *
2017-05-19 22:12:53 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
2017-05-23 14:30:14 +00:00
* https : //www.drupal.org/node/2815083
2017-05-19 22:12:53 +00:00
* @ preserve
* * /
2015-06-08 14:04:39 +00:00
2012-09-26 18:26:15 +00:00
( function ( $ , Drupal , window ) {
2014-01-27 21:41:32 +00:00
function TableResponsive ( table ) {
this . table = table ;
this . $table = $ ( table ) ;
this . showText = Drupal . t ( 'Show all columns' ) ;
2015-03-23 10:58:52 +00:00
this . hideText = Drupal . t ( 'Hide lower priority columns' ) ;
2014-01-27 21:41:32 +00:00
this . $headers = this . $table . find ( 'th' ) ;
2017-05-19 22:12:53 +00:00
this . $link = $ ( '<button type="button" class="link tableresponsive-toggle"></button>' ) . attr ( 'title' , Drupal . t ( 'Show table cells that were hidden to make the table fit within a small screen.' ) ) . on ( 'click' , $ . proxy ( this , 'eventhandlerToggleColumns' ) ) ;
2014-01-27 21:41:32 +00:00
this . $table . before ( $ ( '<div class="tableresponsive-toggle-columns"></div>' ) . append ( this . $link ) ) ;
2017-05-19 22:12:53 +00:00
$ ( window ) . on ( 'resize.tableresponsive' , $ . proxy ( this , 'eventhandlerEvaluateColumnVisibility' ) ) . trigger ( 'resize.tableresponsive' ) ;
2014-01-27 21:41:32 +00:00
}
2012-09-26 18:26:15 +00:00
2018-03-05 09:14:08 +00:00
Drupal . behaviors . tableResponsive = {
2021-12-18 06:12:16 +00:00
attach ( context , settings ) {
once ( 'tableresponsive' , 'table.responsive-enabled' , context ) . forEach ( table => {
2021-08-10 10:05:02 +00:00
TableResponsive . tables . push ( new TableResponsive ( table ) ) ;
} ) ;
2018-03-05 09:14:08 +00:00
}
2021-12-18 06:12:16 +00:00
2018-03-05 09:14:08 +00:00
} ;
2017-05-19 22:12:53 +00:00
$ . extend ( TableResponsive , {
2014-01-27 21:41:32 +00:00
tables : [ ]
} ) ;
2017-05-19 22:12:53 +00:00
$ . extend ( TableResponsive . prototype , {
2021-12-18 06:12:16 +00:00
eventhandlerEvaluateColumnVisibility ( e ) {
const pegged = parseInt ( this . $link . data ( 'pegged' ) , 10 ) ;
const hiddenLength = this . $headers . filter ( '.priority-medium:hidden, .priority-low:hidden' ) . length ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
if ( hiddenLength > 0 ) {
2022-01-28 09:53:59 +00:00
this . $link . show ( ) ;
this . $link [ 0 ] . textContent = this . showText ;
2014-01-27 21:41:32 +00:00
}
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
if ( ! pegged && hiddenLength === 0 ) {
2022-01-28 09:53:59 +00:00
this . $link . hide ( ) ;
this . $link [ 0 ] . textContent = this . hideText ;
2014-01-27 21:41:32 +00:00
}
} ,
2021-12-18 06:12:16 +00:00
eventhandlerToggleColumns ( e ) {
2014-01-27 21:41:32 +00:00
e . preventDefault ( ) ;
2021-12-18 06:12:16 +00:00
const self = this ;
const $hiddenHeaders = this . $headers . filter ( '.priority-medium:hidden, .priority-low:hidden' ) ;
2014-01-27 21:41:32 +00:00
this . $revealedCells = this . $revealedCells || $ ( ) ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
if ( $hiddenHeaders . length > 0 ) {
$hiddenHeaders . each ( function ( index , element ) {
2021-12-18 06:12:16 +00:00
const $header = $ ( this ) ;
const position = $header . prevAll ( 'th' ) . length ;
2014-01-27 21:41:32 +00:00
self . $table . find ( 'tbody tr' ) . each ( function ( ) {
2021-12-18 06:12:16 +00:00
const $cells = $ ( this ) . find ( 'td' ) . eq ( position ) ;
2014-01-27 21:41:32 +00:00
$cells . show ( ) ;
self . $revealedCells = $ ( ) . add ( self . $revealedCells ) . add ( $cells ) ;
} ) ;
$header . show ( ) ;
self . $revealedCells = $ ( ) . add ( self . $revealedCells ) . add ( $header ) ;
2012-09-26 18:26:15 +00:00
} ) ;
2022-01-28 09:53:59 +00:00
this . $link [ 0 ] . textContent = this . hideText ;
this . $link . data ( 'pegged' , 1 ) ;
2017-05-19 22:12:53 +00:00
} else {
2021-10-27 20:02:41 +00:00
this . $revealedCells . hide ( ) ;
this . $revealedCells . each ( function ( index , element ) {
2021-12-18 06:12:16 +00:00
const $cell = $ ( this ) ;
const properties = $cell . attr ( 'style' ) . split ( ';' ) ;
const newProps = [ ] ;
const match = /^display\s*:\s*none$/ ;
2020-01-30 09:08:38 +00:00
2021-12-18 06:12:16 +00:00
for ( let i = 0 ; i < properties . length ; i ++ ) {
const prop = properties [ i ] ;
2021-10-27 20:02:41 +00:00
prop . trim ( ) ;
2021-12-18 06:12:16 +00:00
const isDisplayNone = match . exec ( prop ) ;
2020-01-30 09:08:38 +00:00
2021-10-27 20:02:41 +00:00
if ( isDisplayNone ) {
continue ;
2014-01-27 21:41:32 +00:00
}
2017-05-19 22:12:53 +00:00
2021-10-27 20:02:41 +00:00
newProps . push ( prop ) ;
}
$cell . attr ( 'style' , newProps . join ( ';' ) ) ;
} ) ;
2022-01-28 09:53:59 +00:00
this . $link [ 0 ] . textContent = this . showText ;
this . $link . data ( 'pegged' , 0 ) ;
2021-10-27 20:02:41 +00:00
$ ( window ) . trigger ( 'resize.tableresponsive' ) ;
}
2012-09-26 18:26:15 +00:00
}
2021-12-18 06:12:16 +00:00
2014-01-27 21:41:32 +00:00
} ) ;
Drupal . TableResponsive = TableResponsive ;
2017-05-19 22:12:53 +00:00
} ) ( jQuery , Drupal , window ) ;