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

Add "reset all filters" button to filter dialog #6346

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
28 changes: 5 additions & 23 deletions src/components/filterdialog/filterIndicator.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import './filterIndicator.scss';
import { FILTER_SETTINGS } from '../../constants/filterSettings';

function getFilterStatus(query) {
return Boolean(
query.Filters
|| query.IsFavorite
|| query.VideoTypes
|| query.SeriesStatus
|| query.Is4K
|| (query.IsHD !== undefined && query.IsHD !== null)
|| query.IsSD
|| query.Is3D
|| query.HasSubtitles
|| query.HasTrailer
|| query.HasSpecialFeature
|| query.HasThemeSong
|| query.HasThemeVideo
|| query.IsMissing
|| query.ParentIndexNumber
|| query.Genres
|| query.Tags
|| query.Years
|| query.OfficialRatings
|| query.IsUnaired
);
export function getFilterStatus(query) {
return FILTER_SETTINGS.some(setting => {
return query[setting] !== undefined && query[setting] !== null;
});
}

export function setFilterStatus(page, query) {
Expand Down
27 changes: 27 additions & 0 deletions src/components/filterdialog/filterdialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dom from '../../scripts/dom';
import dialogHelper from '../dialogHelper/dialogHelper';
import { getFilterStatus } from './filterIndicator';
import globalize from '../../lib/globalize';
import union from 'lodash-es/union';
import Events from '../../utils/events.ts';
Expand All @@ -9,6 +10,7 @@ import './style.scss';
import ServerConnections from '../ServerConnections';
import template from './filterdialog.template.html';
import { stopMultiSelect } from '../../components/multiSelect/multiSelect';
import { FILTER_SETTINGS } from '../../constants/filterSettings';

function merge(resultItems, queryItems, delimiter) {
if (!queryItems) {
Expand Down Expand Up @@ -58,13 +60,19 @@ function renderFilters(context, result, query) {
});
}

function renderResetButton(context, query) {
const elem = context.querySelector('.btnResetAllFilters');
elem.classList.toggle('hide', !getFilterStatus(query));
}

function loadDynamicFilters(context, apiClient, userId, itemQuery) {
return apiClient.getJSON(apiClient.getUrl('Items/Filters', {
UserId: userId,
ParentId: itemQuery.ParentId,
IncludeItemTypes: itemQuery.IncludeItemTypes
})).then(function (result) {
renderFilters(context, result, itemQuery);
renderResetButton(context, itemQuery);
});
}

Expand Down Expand Up @@ -115,6 +123,8 @@ function updateFilterControls(context, options) {
function triggerChange(instance) {
stopMultiSelect();
Events.trigger(instance, 'filterchange');
// show or hide reset filter button on any filter change
renderResetButton(document.querySelector('.filterDialog'), instance.options.query);
}

function setVisibility(context, options) {
Expand Down Expand Up @@ -234,6 +244,21 @@ class FilterDialog {
triggerChange(this);
}

/**
* @private
*/
onResetAllFilters(context) {
const query = this.options.query;
FILTER_SETTINGS.forEach(setting => {
query[setting] = null;
});
query.StartIndex = 0;
for (const elem of context.querySelectorAll('input[type=checkbox]')) {
elem.checked = false;
}
triggerChange(this);
}

/**
* @param context {HTMLDivElement} The dialog
*/
Expand Down Expand Up @@ -401,6 +426,8 @@ class FilterDialog {
triggerChange(this);
}
});
const btnResetAllFilters = context.querySelector('.btnResetAllFilters');
btnResetAllFilters.addEventListener('click', () => this.onResetAllFilters(context));
}

show() {
Expand Down
5 changes: 5 additions & 0 deletions src/components/filterdialog/filterdialog.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@
<div class="collapseContent filterOptions">
</div>
</div>

<button is="emby-button" type="button" class="btnResetAllFilters hide raised block">
<span class="listItemIcon listItemIcon-transparent material-icons delete" aria-hidden="true"></span>
<span>${ResetAllFilters}</span>
</button>
</div>
8 changes: 8 additions & 0 deletions src/constants/filterSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** The allowed filter settings */
export const FILTER_SETTINGS = [
'Filters', 'HasSubtitles', 'HasTrailer', 'HasSpecialFeature',
'HasThemeSong', 'HasThemeVideo', 'Genres', 'OfficialRatings',
'Tags', 'VideoTypes', 'IsSD', 'IsHD', 'Is4K', 'Is3D',
'IsFavorite', 'IsMissing', 'IsUnaired', 'ParentIndexNumber',
'SeriesStatus', 'Years'
];
12 changes: 3 additions & 9 deletions src/scripts/settings/userSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Events from '../../utils/events.ts';
import { toBoolean } from '../../utils/string.ts';
import browser from '../browser';
import appSettings from './appSettings';
import { FILTER_SETTINGS } from '../../constants/filterSettings';

function onSaveTimeout() {
const self = this;
Expand All @@ -20,13 +21,6 @@ function saveServerPreferences(instance) {
const allowedSortSettings = ['SortBy', 'SortOrder'];

const filterSettingsPostfix = '-filter';
const allowedFilterSettings = [
'Filters', 'HasSubtitles', 'HasTrailer', 'HasSpecialFeature',
'HasThemeSong', 'HasThemeVideo', 'Genres', 'OfficialRatings',
'Tags', 'VideoTypes', 'IsSD', 'IsHD', 'Is4K', 'Is3D',
'IsFavorite', 'IsMissing', 'IsUnaired', 'ParentIndexNumber',
'SeriesStatus', 'Years'
];

function filterQuerySettings(query, allowedItems) {
return Object.keys(query)
Expand Down Expand Up @@ -548,7 +542,7 @@ export class UserSettings {
sortSettings = filterQuerySettings(JSON.parse(sortSettings), allowedSortSettings);
}
if (filterSettings) {
filterSettings = filterQuerySettings(JSON.parse(filterSettings), allowedFilterSettings);
filterSettings = filterQuerySettings(JSON.parse(filterSettings), FILTER_SETTINGS);
}

return Object.assign(query, sortSettings, filterSettings);
Expand All @@ -561,7 +555,7 @@ export class UserSettings {
*/
saveQuerySettings(key, query) {
const sortSettings = filterQuerySettings(query, allowedSortSettings);
const filterSettings = filterQuerySettings(query, allowedFilterSettings);
const filterSettings = filterQuerySettings(query, FILTER_SETTINGS);

this.set(key, JSON.stringify(sortSettings));
this.set(key + filterSettingsPostfix, JSON.stringify(filterSettings), false);
Expand Down
1 change: 1 addition & 0 deletions src/strings/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@
"ReplaceExistingImages": "Replace existing images",
"ReplaceTrickplayImages": "Replace existing trickplay images",
"Reset": "Reset",
"ResetAllFilters": "Reset all filters",
"ResetPassword": "Reset Password",
"ResolutionMatchSource": "Match Source",
"Restart": "Restart",
Expand Down
Loading