Browse Source

feat: Add institution info and filter to catalog (#308)

* Add institution info to aldine catalog book card
* Remove unneeded OS dependency step from CI pipeline

Co-authored-by: SteelWagstaff <steel@pressbooks.com>
pull/309/head
Felipe Dalcin 3 years ago committed by GitHub
parent
commit
3f0d1ab92b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .github/workflows/.pipeline.yml
  2. 74
      assets/scripts/routes/catalog.js
  3. 17
      assets/styles/components/_book.scss
  4. 4
      dist/mix-manifest.json
  5. 2
      dist/scripts/aldine.js
  6. 2
      dist/styles/aldine.css
  7. 34
      inc/helpers/namespace.php
  8. 5
      page-catalog.php
  9. 11
      partials/book.php
  10. 15
      partials/content-page-catalog.php

3
.github/workflows/.pipeline.yml

@ -41,9 +41,6 @@ jobs:
with:
node-version: '14'
- name: Install OS dependencies
run: sudo apt-get install libxml2-utils ghostscript poppler-utils
- name: Start required services
run: sudo systemctl start mysql.service

74
assets/scripts/routes/catalog.js

@ -165,6 +165,7 @@ export default {
} );
let licenses = document.querySelector( '.license-filters' );
let subjects = document.querySelector( '.subject-filters' );
let institutions = document.querySelector( '.institution-filters' );
let sorts = document.querySelector( '.sorts' );
let clearFilters = document.querySelector( '.clear-filters' );
clearFilters.hidden = false;
@ -172,47 +173,66 @@ export default {
if ( event.target.type !== 'radio' ) {
return;
}
let license = '';
let subject = '';
let filterValue = '*';
if ( subjects.querySelector( 'input[type="radio"]:checked' ).value ) {
subject = `[data-subject="${
subjects.querySelector( 'input[type="radio"]:checked' ).value
}"]`;
}
if ( event.target.value ) {
license = `[data-license="${event.target.value}"]`;
}
if ( license || subject ) {
filterValue = `${license}${subject}`;
const subject = subjects.querySelector( 'input[type="radio"]:checked' ).value
? `[data-subject="${subjects.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const institution = institutions.querySelector( 'input[type="radio"]:checked' ).value
? `[data-institution*="${institutions.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const license = event.target.value
? `[data-license="${event.target.value}"]`
: '';
const filterValue = subject || license || institution ? `${subject}${license}${institution}` : '*';
$grid.isotope( { filter: filterValue } );
} );
institutions.addEventListener( 'click', function ( event ) {
if ( event.target.type !== 'radio' ) {
return;
}
const subject = subjects.querySelector( 'input[type="radio"]:checked' ).value
? `[data-subject="${subjects.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const license = licenses.querySelector( 'input[type="radio"]:checked' ).value
? `[data-license="${licenses.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const institution = event.target.value
? `[data-institution*="${event.target.value}"]`
: '';
const filterValue = subject || license || institution ? `${subject}${license}${institution}` : '*';
$grid.isotope( { filter: filterValue } );
} );
subjects.addEventListener( 'click', function ( event ) {
if ( event.target.type !== 'radio' ) {
return;
}
let license = '';
let subject = '';
let filterValue = '*';
if ( licenses.querySelector( 'input[type="radio"]:checked' ).value ) {
license = `[data-license="${
licenses.querySelector( 'input[type="radio"]:checked' ).value
}"]`;
}
if ( event.target.value ) {
subject = `[data-subject="${event.target.value}"]`;
}
if ( license || subject ) {
filterValue = `${license}${subject}`;
}
const license = licenses.querySelector( 'input[type="radio"]:checked' ).value
? `[data-license="${licenses.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const institution = institutions.querySelector( 'input[type="radio"]:checked' ).value
? `[data-institution*="${institutions.querySelector( 'input[type="radio"]:checked' ).value}"]`
: '';
const subject = event.target.value
? `[data-subject="${event.target.value}"]`
: '';
const filterValue = subject || license || institution ? `${subject}${license}${institution}` : '*';
$grid.isotope( { filter: filterValue } );
} );
clearFilters.addEventListener( 'click', function () {
let allLicenses = document.getElementById( 'all-licenses' );
let allSubjects = document.getElementById( 'all-subjects' );
let allInstitutions = document.getElementById( 'all-institutions' );
allLicenses.checked = true;
allSubjects.checked = true;
allInstitutions.checked = true;
$grid.isotope( { filter: '*' } );
} );
sorts.addEventListener( 'click', function ( event ) {

17
assets/styles/components/_book.scss

@ -11,10 +11,13 @@
border: solid 2px var(--accent);
background: var(--accent);
a {
margin: 0;
a, p {
font-family: $font-family-sans-serif;
color: var(--accent-fg);
}
a {
margin: 0;
text-decoration: none;
text-align: center;
hyphens: auto;
@ -38,6 +41,16 @@
text-align: left;
}
&__institutions {
margin: .5rem 0 0 0;
font-size: 0.875rem;
text-align: left;
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
&__read-more {
margin: auto 0 0;
font-size: 1rem;

4
dist/mix-manifest.json vendored

@ -1,12 +1,12 @@
{
"/scripts/aldine.js": "/scripts/aldine.js?id=5c7f11a799783d9738e0",
"/scripts/aldine.js": "/scripts/aldine.js?id=f7e0ad5cfcc135b918f6",
"/scripts/call-to-action.js": "/scripts/call-to-action.js?id=33370b66c7af12320fc0",
"/scripts/catalog-admin.js": "/scripts/catalog-admin.js?id=e8d84fb090536b8e49e9",
"/scripts/customizer.js": "/scripts/customizer.js?id=14dca3944228dd789c27",
"/scripts/customizer-toggle.js": "/scripts/customizer-toggle.js?id=c31594589675d7c5662a",
"/scripts/page-section.js": "/scripts/page-section.js?id=19d5c30146ea1a763bcf",
"/styles/editor.css": "/styles/editor.css?id=b7c2449babe566571767",
"/styles/aldine.css": "/styles/aldine.css?id=1d7ece82cd6c0f6f8c90",
"/styles/aldine.css": "/styles/aldine.css?id=8d3bfb61118a9ee435a1",
"/fonts/pressbooks-theme.woff": "/fonts/pressbooks-theme.woff?id=2a7aae81673f4707bbe7",
"/images/banner.jpg": "/images/banner.jpg?id=04a813e0b4f94ddfef19",
"/images/catalog-header.jpg": "/images/catalog-header.jpg?id=223b9f7a23985f2a72df",

2
dist/scripts/aldine.js vendored

File diff suppressed because one or more lines are too long

2
dist/styles/aldine.css vendored

File diff suppressed because one or more lines are too long

34
inc/helpers/namespace.php

@ -148,6 +148,40 @@ function get_available_licenses( $catalog_data ) {
return $licenses;
}
/**
* Get institutions for catalog display.
*
* @return array
*/
function get_institutions(): array {
if ( ! defined( 'PB_PLUGIN_VERSION' ) ) {
return [];
}
return \Pressbooks\Metadata\get_institutions();
}
/**
* Get institutions currently in use.
*
* @param array $catalog_data Catalog data
* @param array $institutions Institutions
*
* @return array
*/
function get_available_institutions( array $catalog_data, array $institutions = [] ): array {
$institutions = $institutions ?? get_institutions();
$book_institutions = array_reduce( $catalog_data['books'], static function( $carry, $book ) {
$names = array_reduce( $book['metadata']['institutions'] ?? [], static function( $carry, $institution ) {
return array_merge( $carry, [ $institution['name'] ] );
}, [] );
return array_merge( $carry, $names );
}, [] );
return array_intersect( $institutions, $book_institutions );
}
/**
* Get subjects currently in use.
*

5
page-catalog.php

@ -9,20 +9,25 @@
* @package Aldine
*/
use function Aldine\Helpers\get_available_institutions;
use function Aldine\Helpers\get_available_licenses;
use function Aldine\Helpers\get_available_subjects;
use function Aldine\Helpers\get_catalog_data;
use function Aldine\Helpers\get_catalog_licenses;
use function Aldine\Helpers\get_institutions;
$current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$orderby = ( get_query_var( 'orderby' ) ) ? get_query_var( 'orderby' ) : 'title';
$subject = ( get_query_var( 'subject' ) ) ? get_query_var( 'subject' ) : '';
$license = ( get_query_var( 'license' ) ) ? get_query_var( 'license' ) : '';
$institution = get_query_var( 'institution' ) ?? '';
$catalog_data = get_catalog_data( $current_page, 9, $orderby, $license, $subject );
$previous_page = ( $current_page > 1 ) ? $current_page - 1 : 0;
$next_page = $current_page + 1;
$licenses = get_catalog_licenses();
$available_licenses = get_available_licenses( $catalog_data );
$institutions = get_institutions();
$available_institutions = get_available_institutions( $catalog_data, $institutions );
$subjects = ( defined( 'PB_PLUGIN_VERSION' ) ) ? \Pressbooks\Metadata\get_thema_subjects() : [];
$available_subjects = get_available_subjects( $catalog_data );

11
partials/book.php

@ -7,11 +7,12 @@
use function \Aldine\Helpers\maybe_truncate_string;
use function \Pressbooks\Metadata\is_bisac;
?>
<?php
$subject = ( isset( $book['subject'] ) && ! is_bisac( $book['subject'] ) ) ? substr( $book['subject'], 0, 2 ) : '';
$date = ( isset( $book['metadata']['datePublished'] ) ) ? str_replace( '-', '', $book['metadata']['datePublished'] ) : '';
$institutions = array_reduce( $book['metadata']['institutions'] ?? [], static function ( $carry, $item ) {
return array_merge( $carry, [ $item['name'] ] );
}, [] );
?>
<li class="book"
<?php
@ -19,6 +20,7 @@ if ( $date ) {
?>
data-date-published="<?php echo $date; ?>"<?php } ?>
data-license="<?php echo ( new \Pressbooks\Licensing() )->getLicenseFromUrl( $book['metadata']['license']['url'] ); ?>"
data-institution="<?php echo implode( ',', $institutions ); ?>"
<?php
if ( ! empty( $subject ) ) {
?>
@ -43,6 +45,11 @@ if ( $date ) {
<a href="<?php echo network_home_url( "/catalog/#$subject" ) ?>"><?php echo \Pressbooks\Metadata\get_subject_from_thema( $book['subject'] ); ?></a>
</p>
<?php } ?>
<?php if ( $institutions ) : ?>
<p class="book__institutions">
<?php echo implode( ', ', $institutions ); ?>
</p>
<?php endif; ?>
<p class="book__read-more">
<a href="<?php echo $book['link']; ?>"><?php _e( 'About this book', 'pressbooks-aldine' ); ?> <svg aria-hidden="true"><use xlink:href="#arrow-right" /></svg></a>
</p>

15
partials/content-page-catalog.php

@ -35,6 +35,21 @@
<?php endforeach; ?>
</div>
</fieldset>
<fieldset class="institution-filters">
<h2><?php _e( 'Filter by Institution', 'pressbooks-aldine' ); ?></h2>
<input type="radio" name="institution" id="all-institutions" value="" <?php checked( $institution, '' ); ?>>
<label for="all-institutions"><?php _e( 'All Institutions', 'pressbooks-aldine' ); ?> <svg class="checked"><use xlink:href="#checkmark" /></svg></label>
<?php
foreach ( $institutions as $key => $value ) :
if ( in_array( $key, $available_institutions, true ) ) :
?>
<input type="radio" name="institution" id="<?php echo $key; ?>" value="<?php echo $key; ?>" <?php checked( $institution, $key ); ?>>
<label for="<?php echo $key; ?>"><?php echo $value; ?> <svg class="checked"><use xlink:href="#checkmark" /></svg></label>
<?php
endif;
endforeach;
?>
</fieldset>
<fieldset class="license-filters">
<h2><?php _e( 'Filter by License', 'pressbooks-aldine' ); ?></h2>
<input type="radio" name="license" id="all-licenses" value="" <?php checked( $license, '' ); ?>>

Loading…
Cancel
Save