-
-
Notifications
You must be signed in to change notification settings - Fork 71
Don't request category default filters on pages that don't have categories #1827
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bfffa0f
minor YARD fix for the default_filter users helper
Oaphi ef52471
switched default_filter action's response status to a constant
Oaphi d00f8ad
minor improvement to default_filter action's check for category param
Oaphi c2e7179
reduced reliance of filters JS asset on jQuery
Oaphi 09e75ee
properly linted filters JS asset
Oaphi 4085392
added types for Prettier & plugin needed for stroustrup brace style
Oaphi 8a093dd
added Prettier config to enable strousrup brace style formatting
Oaphi 19eeb9b
relinted filters JS asset to match our brace styling
Oaphi 4f202e5
removed unnecessary default filter requests on pages that don't have …
Oaphi edb7bd2
added tests for the default_filter action
Oaphi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * @type {import('prettier').Config} | ||
| */ | ||
| const config = { | ||
| braceStyle: "stroustrup", | ||
| plugins: ["prettier-plugin-brace-style"], | ||
| printWidth: 120, | ||
| singleQuote: true, | ||
| tabWidth: 2, | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,158 +1,169 @@ | ||
| $(() => { | ||
| $('.js-filter-select').toArray().forEach(async (el) => { | ||
| const $select = $(el); | ||
| const $form = $select.closest('form'); | ||
| const $formFilters = $form.find('.form--filter'); | ||
| const $saveButton = $form.find('.filter-save'); | ||
| const $isDefaultCheckbox = $form.find('.filter-is-default'); | ||
| const categoryId = $isDefaultCheckbox.val()?.toString(); | ||
| let defaultFilter = await QPixel.defaultFilter(categoryId); | ||
| const $deleteButton = $form.find('.filter-delete'); | ||
|
|
||
| // Enables/Disables Save & Delete buttons programatically | ||
| async function computeEnables() { | ||
| const filters = await QPixel.filters(); | ||
| const filterName = $select.val()?.toString(); | ||
|
|
||
| // Nothing set | ||
| if (!filterName) { | ||
| $saveButton.prop('disabled', true); | ||
| $deleteButton.prop('disabled', true); | ||
| return; | ||
| } | ||
| document.addEventListener('DOMContentLoaded', () => { | ||
| $('.js-filter-select') | ||
| .toArray() | ||
| .forEach(async (el) => { | ||
| const $select = $(el); | ||
| const $form = $select.closest('form'); | ||
| const $formFilters = $form.find('.form--filter'); | ||
| const $saveButton = $form.find('.filter-save'); | ||
| const $isDefaultCheckbox = $form.find('.filter-is-default'); | ||
| const categoryId = $isDefaultCheckbox.val()?.toString(); | ||
| let defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; | ||
| const $deleteButton = $form.find('.filter-delete'); | ||
|
|
||
| // Enables/Disables Save & Delete buttons programatically | ||
| async function computeEnables() { | ||
| const filters = await QPixel.filters(); | ||
| const filterName = $select.val()?.toString(); | ||
|
|
||
| // Nothing set | ||
| if (!filterName) { | ||
| $saveButton.prop('disabled', true); | ||
| $deleteButton.prop('disabled', true); | ||
| return; | ||
| } | ||
|
|
||
| const filter = filters[filterName] | ||
| const filter = filters[filterName]; | ||
|
|
||
| // New filter | ||
| if (!filter) { | ||
| $saveButton.prop('disabled', false); | ||
| $deleteButton.prop('disabled', true); | ||
| return; | ||
| } | ||
| // New filter | ||
| if (!filter) { | ||
| $saveButton.prop('disabled', false); | ||
| $deleteButton.prop('disabled', true); | ||
| return; | ||
| } | ||
|
|
||
| // Not a new filter | ||
| $deleteButton.prop('disabled', filter.system); | ||
| // Not a new filter | ||
| $deleteButton.prop('disabled', filter.system); | ||
|
|
||
| const hasChanges = [...$formFilters].some((el) => { | ||
Oaphi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const filterValue = filter[el.dataset.name]; | ||
| let elValue = /** @type {string | undefined[]} */ ($(el).val()); | ||
| if (filterValue?.constructor == Array) { | ||
| elValue = elValue ?? []; | ||
| return filterValue.length != elValue.length || filterValue.some((v, i) => v[1] != elValue[i]); | ||
| } | ||
| else { | ||
| return filterValue ? filterValue != elValue : elValue; | ||
| } | ||
| }); | ||
| const defaultStatusChanged = $isDefaultCheckbox.prop('checked') != (defaultFilter === $select.val()); | ||
| $saveButton.prop('disabled', !defaultStatusChanged && (filter.system || !hasChanges)); | ||
| } | ||
|
|
||
| async function initializeSelect() { | ||
| defaultFilter = await QPixel.defaultFilter(categoryId); | ||
| $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); | ||
| const filters = await QPixel.filters(); | ||
|
|
||
| function template(option) { | ||
| if (option.id == '') { return 'Default'; } | ||
|
|
||
| const filter = filters[option.id]; | ||
| const name = `<span>${option.text}</span>`; | ||
| const systemIndicator = filter?.system | ||
| ? ' <span has-font-size-caption">(System)</span>' | ||
| : ''; | ||
| const newIndicator = !filter | ||
| ? ' <span has-font-size-caption">(New)</span>' | ||
| : ''; | ||
| return $(name + systemIndicator + newIndicator); | ||
| const hasChanges = [...$formFilters].some((el) => { | ||
| const filterValue = filter[el.dataset.name]; | ||
| let elValue = /** @type {string | undefined[]} */ ($(el).val()); | ||
| if (filterValue?.constructor == Array) { | ||
| elValue = elValue ?? []; | ||
| return filterValue.length != elValue.length || filterValue.some((v, i) => v[1] != elValue[i]); | ||
| } | ||
| else { | ||
| return filterValue ? filterValue != elValue : elValue; | ||
| } | ||
| }); | ||
| const defaultStatusChanged = $isDefaultCheckbox.prop('checked') != (defaultFilter === $select.val()); | ||
| $saveButton.prop('disabled', !defaultStatusChanged && (filter.system || !hasChanges)); | ||
| } | ||
|
|
||
| // Clear out any old options | ||
| $select.children().filter((_, /** @type{HTMLOptionElement} */ option) => { | ||
| return option.value && !filters[option.value]; | ||
| }).detach(); | ||
| async function initializeSelect() { | ||
| defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; | ||
| $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); | ||
| const filters = await QPixel.filters(); | ||
|
|
||
| $select.select2({ | ||
| data: Object.keys(filters).map((filterName) => { | ||
| return { | ||
| id: filterName, | ||
| text: filterName | ||
| function template(option) { | ||
| if (option.id == '') { | ||
| return 'Default'; | ||
| } | ||
| }), | ||
| tags: true, | ||
| templateResult: template, | ||
| templateSelection: template | ||
| }); | ||
|
|
||
| $select.on('select2:select', /** @type {(event: Select2.Event) => void} */ (async (evt) => { | ||
| const filterName = evt.params.data.id; | ||
| const preset = filters[filterName]; | ||
| const filter = filters[option.id]; | ||
| const name = `<span>${option.text}</span>`; | ||
| const systemIndicator = filter?.system ? ' <span has-font-size-caption">(System)</span>' : ''; | ||
| const newIndicator = !filter ? ' <span has-font-size-caption">(New)</span>' : ''; | ||
| return $(name + systemIndicator + newIndicator); | ||
| } | ||
|
|
||
| $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); | ||
| // Clear out any old options | ||
| $select | ||
| .children() | ||
| .filter((_, /** @type{HTMLOptionElement} */ option) => { | ||
| return option.value && !filters[option.value]; | ||
| }) | ||
| .detach(); | ||
|
|
||
| $select.select2({ | ||
| data: Object.keys(filters).map((filterName) => { | ||
| return { | ||
| id: filterName, | ||
| text: filterName, | ||
| }; | ||
| }), | ||
| tags: true, | ||
| templateResult: template, | ||
| templateSelection: template, | ||
| }); | ||
|
|
||
| $select.on( | ||
| 'select2:select', | ||
| /** @type {(event: Select2.Event) => void} */ ( | ||
| async (evt) => { | ||
| const filterName = evt.params.data.id; | ||
| const preset = filters[filterName]; | ||
|
|
||
| $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); | ||
| computeEnables(); | ||
|
|
||
| // Name is not one of the presets, i.e user is creating a new preset | ||
| if (!preset) { | ||
| return; | ||
| } | ||
|
|
||
| for (const [name, value] of Object.entries(preset)) { | ||
| const $el = $form.find(`.form--filter[data-name=${name}]`); | ||
| if (value?.constructor == Array) { | ||
Oaphi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $el.val(null); | ||
| for (const val of value) { | ||
| $el.append(new Option(val[0], val[1].toString(), false, true)); | ||
| } | ||
| $el.trigger('change'); | ||
| } | ||
| else { | ||
| $el.val(/** @type {string} */ (value)).trigger('change'); | ||
| } | ||
| } | ||
| } | ||
| ), | ||
| ); | ||
| computeEnables(); | ||
| } | ||
|
|
||
| // Name is not one of the presets, i.e user is creating a new preset | ||
| if (!preset) { | ||
| return; | ||
| } | ||
| initializeSelect(); | ||
|
|
||
| for (const [name, value] of Object.entries(preset)) { | ||
| const $el = $form.find(`.form--filter[data-name=${name}]`); | ||
| if (value?.constructor == Array) { | ||
| $el.val(null); | ||
| for (const val of value) { | ||
| $el.append(new Option(val[0], val[1].toString(), false, true)); | ||
| } | ||
| $el.trigger('change'); | ||
| } | ||
| else { | ||
| $el.val(/** @type {string} */ (value)).trigger('change'); | ||
| } | ||
| // Enable saving when the filter is changed | ||
| $formFilters.on('change', computeEnables); | ||
| $isDefaultCheckbox.on('change', computeEnables); | ||
|
|
||
| async function saveFilter() { | ||
| if (!$form[0].reportValidity()) { | ||
| return; | ||
| } | ||
| })); | ||
| computeEnables(); | ||
| } | ||
|
|
||
| initializeSelect(); | ||
| const filter = /** @type {QPixelFilter} */ ({}); | ||
|
|
||
| // Enable saving when the filter is changed | ||
| $formFilters.on('change', computeEnables); | ||
| $isDefaultCheckbox.on('change', computeEnables); | ||
| for (const el of $formFilters) { | ||
| filter[el.dataset.name] = $(el).val(); | ||
| } | ||
|
|
||
| async function saveFilter() { | ||
| if (!$form[0].reportValidity()) { return; } | ||
| await QPixel.setFilter($select.val()?.toString(), filter, categoryId, $isDefaultCheckbox.prop('checked')); | ||
|
|
||
| const filter = /** @type {QPixelFilter} */({}); | ||
| defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; | ||
|
|
||
| for (const el of $formFilters) { | ||
| filter[el.dataset.name] = $(el).val(); | ||
| // Reinitialize to get new options | ||
| await initializeSelect(); | ||
| } | ||
|
|
||
| await QPixel.setFilter($select.val()?.toString(), filter, categoryId, $isDefaultCheckbox.prop('checked')); | ||
| defaultFilter = await QPixel.defaultFilter(categoryId); | ||
| $saveButton.on('click', saveFilter); | ||
|
|
||
| // Reinitialize to get new options | ||
| await initializeSelect(); | ||
| } | ||
|
|
||
| $saveButton.on('click', saveFilter); | ||
| function clear() { | ||
| $select.val(null).trigger('change'); | ||
| $form.find('.form--filter').val(null).trigger('change'); | ||
| $isDefaultCheckbox.prop('checked', false); | ||
| computeEnables(); | ||
| } | ||
|
|
||
| function clear() { | ||
| $select.val(null).trigger('change'); | ||
| $form.find('.form--filter').val(null).trigger('change'); | ||
| $isDefaultCheckbox.prop('checked', false); | ||
| computeEnables(); | ||
| } | ||
| $deleteButton?.on('click', async (_evt) => { | ||
| if (confirm(`Are you sure you want to delete ${$select.val()}?`)) { | ||
| await QPixel.deleteFilter($select.val()?.toString()); | ||
| // Reinitialize to get new options | ||
| await initializeSelect(); | ||
| clear(); | ||
| } | ||
| }); | ||
|
|
||
| $deleteButton?.on('click', async (_evt) => { | ||
| if (confirm(`Are you sure you want to delete ${$select.val()}?`)) { | ||
| await QPixel.deleteFilter($select.val()?.toString()); | ||
| // Reinitialize to get new options | ||
| await initializeSelect(); | ||
| clear(); | ||
| } | ||
| $form.find('.filter-clear').on('click', clear); | ||
| }); | ||
|
|
||
| $form.find('.filter-clear').on('click', clear); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.