Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: announce filters once applied (resolves #130) #241

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/Controllers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ public function queriedResourceTerms()
return $terms;
}

public function filterCount()
{
$count = 0;
global $wp_query;
if ($wp_query->tax_query) {
foreach ($wp_query->tax_query->queries as $value) {
foreach ($value['terms'] as $t) {
$count++;
}
}
}
if (isset($_GET['language'])) {
foreach ($_GET['language'] as $lang) {
$count++;
}
}
return $count;
}

public function availableLanguages()
{
if (function_exists('pll_the_languages') && function_exists('pll_current_language')) {
Expand Down
5 changes: 2 additions & 3 deletions app/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
}
}

/** Add class if sidebar is active */
if (display_sidebar()) {
$classes[] = 'sidebar-primary';
if (isset($_GET['filtered']) && $_GET['filtered'] === '1') {
$classes[] = 'filtered';
}

/** Clean up class names for custom templates */
Expand Down
11 changes: 10 additions & 1 deletion resources/assets/scripts/routes/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import addNotification from '../util/addNotification';
import Cookies from 'cookies.js';
import Pinecone from '@platform-coop-toolkit/pinecone';
import { __, sprintf } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';

export default {
Expand All @@ -13,6 +13,15 @@ export default {
const showFilters = document.querySelector( '#show-filters' );
const hideFilters = document.querySelector( '#hide-filters' );

if (document.body.classList.contains('filtered')) {
const currentFilterCount = document.body.dataset.filters;
if ( currentFilterCount ) {
setTimeout(function() {
speak(sprintf(_n('%s filter applied. Resource list updated.', '%s filters applied. Resource list updated.', parseInt(currentFilterCount), 'coop-library'), currentFilterCount), 'assertive');
}, 1000);
}
}

if ( showFilters && hideFilters && filterContainer ) {
new Pinecone.FilterList( filterContainer, showFilters, hideFilters );
}
Expand Down
6 changes: 5 additions & 1 deletion resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<!doctype html>
<html class="no-js" {!! get_language_attributes() !!}>
@include('partials.head')
<body @php body_class() @endphp>
<body @php body_class() @endphp
@if($filter_count)
data-filters="{{ $filter_count }}"
@endif
>
@php do_action('get_header') @endphp
@include('partials.header')
<div class="wrap container" role="document">
Expand Down
1 change: 1 addition & 0 deletions resources/views/partials/filters.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="filter-wrapper">
<button type="button" class="button button--borderless" id="show-filters">@svg('filter', 'icon--filter', ['focusable' => 'false', 'aria-hidden' => 'true']) {{ __('Filter', 'coop-library' ) }}</button>
<form name="filters" class="filters" action="{{ (isset($_GET['s'])) ? home_url('/') : get_post_type_archive_link('lc_resource') }}">
<input type="hidden" name="filtered" value="1" />
@if(isset($_GET['s']))
<input type="hidden" name="s" value="{{ $_GET['s'] }}" />
<input type="hidden" name="post_type" value="lc_resource" />
Expand Down