Skip to content

Commit

Permalink
Add show/hide buttons to cover TOC (fix #146) (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Zimmerman committed Mar 26, 2018
1 parent b6ef40d commit 8e9b505
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 24 deletions.
50 changes: 32 additions & 18 deletions assets/src/scripts/routes/home.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
export default {
init() {
jQuery( $ => {
// $( document.body ).on( 'click', function ( e ) {
// let $target = $( e.target );
// if ( $target.is( '[data-toggle=dropdown]' ) ) {
// return;
// }

// $( '.dropdown-menu.show' ).each( function () {
// let $dropdown = $( this );
// $dropdown.removeClass( 'show' );
// } );
// } );

// $( document.body ).on( 'click', '[data-toggle=dropdown]', function ( e ) {
// let $target = $( this );
// let $dropdown = $target.parent( '.dropdown' ).find( '.dropdown-menu' );
// $dropdown.toggleClass( 'show' );
// } );

$( document.body ).on( 'click', '.js-toggle-block', function ( e ) {
let $target = $( this );
$target.parents( '.js-block' ).toggleClass( 'block-toggle--visible' );
$target.toggleClass( '--visible' );
} );

$( document.body ).on( 'click', '.toc__toggle #show', function ( e ) {
let $target = $( this );
let $dropdowns = $( '.toc__list .dropdown' );
$dropdowns.each( function ( el ) {
$( this )
.find( 'button' )
.attr( 'aria-expanded', 'true' );
$( this )
.find(
'h3 + .inner-content, .toc__front-matter-list, .toc__back-matter-list'
)
.removeAttr( 'hidden' );
} );
$target.parents( '.toc__toggle' ).attr( 'aria-expanded', 'true' );
} );

$( document.body ).on( 'click', '.toc__toggle #hide', function ( e ) {
let $target = $( this );
let $dropdowns = $( '.toc__list .dropdown' );
$dropdowns.each( function ( el ) {
$( this )
.find( 'button' )
.attr( 'aria-expanded', 'false' );
$( this )
.find(
'h3 + .inner-content, .toc__front-matter-list, .toc__back-matter-list'
)
.attr( 'hidden', 'true' );
} );
$target.parents( '.toc__toggle' ).attr( 'aria-expanded', 'false' );
} );
} );
},
finalize() {
Expand Down
22 changes: 22 additions & 0 deletions assets/src/styles/components/_toc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,25 @@ ol.toc__chapters {
}
}
}


.toc__toggle {
margin-top: 2rem;
text-align: center;
}

.toc__toggle[aria-expanded="false"] #hide {
display: none;
}

.toc__toggle[aria-expanded="false"] #show {
display: inline-block;
}

.toc__toggle[aria-expanded="true"] #hide {
display: inline-block;
}

.toc__toggle[aria-expanded="true"] #show {
display: none;
}
4 changes: 2 additions & 2 deletions dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/scripts/book.js": "/scripts/book.js?id=b6f58c3abcbb9a472fe6",
"/styles/book.css": "/styles/book.css?id=f1d0be60ef7a0c43d6b4",
"/scripts/book.js": "/scripts/book.js?id=48a288842a7ea7a4a89b",
"/styles/book.css": "/styles/book.css?id=c248aa0123445b327332",
"/scripts/sharer.js": "/scripts/sharer.js?id=b257fe1ee31a1838f343"
}
2 changes: 1 addition & 1 deletion dist/scripts/book.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/styles/book.css

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions partials/content-cover-toc.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<section class="block block-toc">
<?php if ( pb_get_first_post_id() ) : ?>
<h2 class="block__title block-toc__title"><?php _e( 'Contents', 'pressbooks-book' ); ?></h2><?php
include( locate_template( 'partials/content-toc.php' ) );
endif;
include( locate_template( 'partials/content-toc.php' ) ); ?>
<div class="toc__toggle" aria-expanded="false">
<button id="show" class="button"><?php _e( 'Show All Contents' ); ?></button>
<button id="hide" class="button"><?php _e( 'Hide All Contents' ); ?></button>
</div>
<?php endif;
/**
* Append content to cover table of contents block.
*
Expand Down

0 comments on commit 8e9b505

Please sign in to comment.