Browse Source

Add institution filter, minor improvements

pull/308/head
Felipe Dalcin 4 years ago
parent
commit
2cc32e8963
  1. 74
      assets/scripts/routes/catalog.js
  2. 2
      dist/mix-manifest.json
  3. 2
      dist/scripts/aldine.js
  4. 34
      inc/helpers/namespace.php
  5. 5
      page-catalog.php
  6. 1
      partials/book.php
  7. 15
      partials/content-page-catalog.php

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 ) {

2
dist/mix-manifest.json vendored

@ -1,5 +1,5 @@
{
"/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",

2
dist/scripts/aldine.js 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
*
* @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 );

1
partials/book.php

@ -20,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 ) ) {
?>

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