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

[Part 15] Add autocomplete history settings UI #452

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 24 additions & 7 deletions assets/js/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { assertNotNull, assertNotUndefined } from './utils/assert';
import { $, $$ } from './utils/dom';
import { $, $$, hideIf } from './utils/dom';
import store from './utils/store';

function setupThemeSettings() {
Expand All @@ -28,17 +28,34 @@ function setupThemeSettings() {
themeColorSelect.addEventListener('change', themePreviewCallback);
}

function setupAutocompleteSettings() {
const autocompleteSettings = assertNotNull($<HTMLElement>('.autocomplete-settings'));
const autocompleteSearchHistorySettings = assertNotNull($<HTMLElement>('.autocomplete-search-history-settings'));
const enableSearchAutocomplete = assertNotNull($<HTMLInputElement>('#user_enable_search_ac'));
const userSearchHistoryHidden = assertNotNull($<HTMLInputElement>('#user_autocomplete_search_history_hidden'));

// Don't show search history settings if autocomplete is entirely disabled.
enableSearchAutocomplete.addEventListener('change', () => {
hideIf(!enableSearchAutocomplete.checked, autocompleteSettings);
});

userSearchHistoryHidden.addEventListener('change', () => {
hideIf(userSearchHistoryHidden.checked, autocompleteSearchHistorySettings);
});
}

export function setupSettings() {
if (!$('#js-setting-table')) return;

const localCheckboxes = $$<HTMLInputElement>('[data-tab="local"] input[type="checkbox"]');

// Local settings
localCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', () => {
store.set(checkbox.id.replace('user_', ''), checkbox.checked);
for (const input of $$<HTMLInputElement>('[data-tab="local"] input')) {
input.addEventListener('change', () => {
const newValue = input.type === 'checkbox' ? input.checked : input.value;

store.set(input.id.replace('user_', ''), newValue);
});
});
}

setupThemeSettings();
setupAutocompleteSettings();
}
15 changes: 15 additions & 0 deletions assets/js/utils/__tests__/dom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
findFirstTextNode,
disableEl,
enableEl,
hideIf,
} from '../dom';
import { getRandomArrayItem, getRandomIntBetween } from '../../../test/randomness';
import { fireEvent } from '@testing-library/dom';
Expand Down Expand Up @@ -444,4 +445,18 @@ describe('DOM Utilities', () => {
expect(result).toBe(undefined);
});
});

describe('hideIf', () => {
it('should add "hidden" class if condition is true', () => {
const element = document.createElement('div');
hideIf(true, element);
expect(element).toHaveClass('hidden');
});
it('should remove "hidden" class if condition is false', () => {
const element = document.createElement('div');
element.classList.add('hidden');
hideIf(false, element);
expect(element).not.toHaveClass('hidden');
});
});
});
8 changes: 8 additions & 0 deletions assets/js/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@ export function escapeCss(css: string): string {
export function findFirstTextNode<N extends Node>(of: Node): N {
return Array.prototype.filter.call(of.childNodes, el => el.nodeType === Node.TEXT_NODE)[0];
}

export function hideIf(condition: boolean, element: HTMLElement) {
if (condition) {
element.classList.add('hidden');
} else {
element.classList.remove('hidden');
}
}
28 changes: 26 additions & 2 deletions lib/philomena_web/templates/setting/edit.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,32 @@ h1 Content Settings
.fieldlabel: i Show streams marked as NSFW on the channels page.
.field
=> label f, :enable_search_ac, "Enable search auto-completion"
=> checkbox f, :enable_search_ac, checked: @conn.cookies["enable_search_ac"] === "true"
.fieldlabel: i Enable the auto-completion of tags in search fields.
=> checkbox f, :enable_search_ac, checked: @conn.cookies["enable_search_ac"] == "true"

.autocomplete-settings class=if(@conn.cookies["enable_search_ac"] != "true", do: "hidden", else: "")
.field
=> label f,
:autocomplete_search_history_hidden,
"Hide search history in auto-completion"
=> checkbox f,
:autocomplete_search_history_hidden,
checked: @conn.cookies["autocomplete_search_history_hidden"] == "true"

.autocomplete-search-history-settings[
class=if(@conn.cookies["autocomplete_search_history_hidden"] == "true", do: "hidden", else: "")
]
.field
=> label f,
:autocomplete_search_history_max_suggestions_when_typing,
"Maximum number of search history suggestions in autocompletion when typing"
=> number_input f,
:autocomplete_search_history_max_suggestions_when_typing,
min: 0,
max: 10,
step: 1,
value: @conn.cookies["autocomplete_search_history_max_suggestions_when_typing"] || 3,
class: "input"

= if staff?(@conn.assigns.current_user) do
.field
=> label f, :hide_staff_tools
Expand Down