2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Search icon that expands to a search bar and collapse on 'x' or blur.
|
|
|
|
* Takes a `SearchModel` that controls the search behavior.
|
|
|
|
*/
|
2022-10-19 23:06:05 +00:00
|
|
|
import { allCommands, createGroup } from 'app/client/components/commands';
|
2022-10-28 16:11:08 +00:00
|
|
|
import { makeT } from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { reportError } from 'app/client/models/AppModel';
|
|
|
|
import { SearchModel } from 'app/client/models/SearchModel';
|
2022-10-19 23:06:05 +00:00
|
|
|
import { hoverTooltip } from 'app/client/ui/tooltips';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { cssHoverCircle, cssTopBarBtn } from 'app/client/ui/TopBarCss';
|
2021-05-19 07:07:51 +00:00
|
|
|
import { labeledSquareCheckbox } from 'app/client/ui2018/checkbox';
|
2022-09-06 01:51:57 +00:00
|
|
|
import { mediaSmall, theme, vars } from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { icon } from 'app/client/ui2018/icons';
|
|
|
|
import { dom, input, styled } from 'grainjs';
|
|
|
|
import { noTestId, TestId } from 'grainjs';
|
|
|
|
import debounce = require('lodash/debounce');
|
|
|
|
|
|
|
|
export * from 'app/client/models/SearchModel';
|
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('ui2018.search');
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const EXPAND_TIME = .5;
|
|
|
|
|
|
|
|
const searchWrapper = styled('div', `
|
|
|
|
display: flex;
|
2021-02-28 05:27:34 +00:00
|
|
|
flex: initial;
|
2020-10-02 15:10:00 +00:00
|
|
|
align-items: center;
|
|
|
|
box-sizing: border-box;
|
|
|
|
border: 1px solid transparent;
|
2021-11-18 16:21:53 +00:00
|
|
|
padding: 0px 16px;
|
2020-10-02 15:10:00 +00:00
|
|
|
width: 50px;
|
|
|
|
height: 100%;
|
|
|
|
max-height: 50px;
|
|
|
|
transition: width 0.4s;
|
2021-05-19 07:07:51 +00:00
|
|
|
position: relative;
|
2020-10-02 15:10:00 +00:00
|
|
|
&-expand {
|
2021-02-28 05:27:34 +00:00
|
|
|
width: 100% !important;
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.searchBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-02-28 05:27:34 +00:00
|
|
|
@media ${mediaSmall} {
|
|
|
|
& {
|
|
|
|
width: 32px;
|
|
|
|
padding: 0px;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const expandedSearch = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-grow: 0;
|
|
|
|
align-items: center;
|
|
|
|
width: 0;
|
|
|
|
opacity: 0;
|
2021-11-18 16:21:53 +00:00
|
|
|
align-self: stretch;
|
2020-10-02 15:10:00 +00:00
|
|
|
transition: width ${EXPAND_TIME}s, opacity ${EXPAND_TIME / 2}s ${EXPAND_TIME / 2}s;
|
|
|
|
.${searchWrapper.className}-expand > & {
|
|
|
|
width: auto;
|
|
|
|
flex-grow: 1;
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const searchInput = styled(input, `
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.topHeaderBg};
|
|
|
|
color: ${theme.inputFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
outline: none;
|
|
|
|
border: none;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
padding-left: 4px;
|
|
|
|
box-sizing: border-box;
|
2021-11-18 16:21:53 +00:00
|
|
|
align-self: stretch;
|
2020-10-02 15:10:00 +00:00
|
|
|
width: 0;
|
|
|
|
transition: width ${EXPAND_TIME}s;
|
|
|
|
.${searchWrapper.className}-expand & {
|
|
|
|
width: 100%;
|
|
|
|
}
|
2022-09-06 01:51:57 +00:00
|
|
|
&::placeholder {
|
|
|
|
color: ${theme.inputPlaceholderFg};
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssArrowBtn = styled('div', `
|
|
|
|
font-size: 14px;
|
|
|
|
padding: 3px;
|
|
|
|
cursor: pointer;
|
2021-05-19 07:07:51 +00:00
|
|
|
margin: 2px 4px;
|
2020-10-02 15:10:00 +00:00
|
|
|
visibility: hidden;
|
2021-05-19 07:07:51 +00:00
|
|
|
width: 24px;
|
|
|
|
height: 24px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.searchPrevNextButtonBg};
|
|
|
|
--icon-color: ${theme.searchPrevNextButtonFg};
|
2021-05-19 07:07:51 +00:00
|
|
|
border-radius: 3px;
|
|
|
|
text-align: center;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
.${searchWrapper.className}-expand & {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCloseBtn = styled(icon, `
|
|
|
|
cursor: pointer;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.controlFg};
|
2021-05-19 07:07:51 +00:00
|
|
|
margin-left: 4px;
|
|
|
|
flex-shrink: 0;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssLabel = styled('span', `
|
|
|
|
font-size: ${vars.smallFontSize};
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2021-05-19 07:07:51 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
margin-right: 12px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
2021-05-19 07:07:51 +00:00
|
|
|
const cssOptions = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
background: ${theme.topHeaderBg};
|
2021-05-19 07:07:51 +00:00
|
|
|
position: absolute;
|
|
|
|
right: 0;
|
2022-09-06 01:51:57 +00:00
|
|
|
top: 48px;
|
2021-05-19 07:07:51 +00:00
|
|
|
z-index: 1;
|
2022-04-13 17:26:59 +00:00
|
|
|
padding: 2px 4px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
2021-05-19 07:07:51 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssShortcut = styled('span', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2021-05-19 07:07:51 +00:00
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export function searchBar(model: SearchModel, testId: TestId = noTestId) {
|
2021-05-19 07:07:51 +00:00
|
|
|
let keepExpanded = false;
|
|
|
|
|
2021-10-29 18:13:10 +00:00
|
|
|
const focusAndSelect = () => { inputElem.focus(); inputElem.select(); };
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const commandGroup = createGroup({
|
2021-10-29 18:13:10 +00:00
|
|
|
find: focusAndSelect,
|
2020-10-02 15:10:00 +00:00
|
|
|
// On Mac, Firefox has a default behaviour witch causes to close the search bar on Cmd+g and
|
|
|
|
// Cmd+shirt+G. Returning false is a Mousetrap convenience which prevents that.
|
2021-05-23 17:43:11 +00:00
|
|
|
findNext: () => { model.findNext().catch(reportError); return false; },
|
|
|
|
findPrev: () => { model.findPrev().catch(reportError); return false; },
|
2020-10-02 15:10:00 +00:00
|
|
|
}, null, true);
|
|
|
|
|
|
|
|
const toggleMenu = debounce((_value?: boolean) => {
|
|
|
|
model.isOpen.set(_value === undefined ? !model.isOpen.get() : _value);
|
|
|
|
}, 100);
|
2021-05-19 07:07:51 +00:00
|
|
|
const inputElem: HTMLInputElement = searchInput(model.value, {onInput: true},
|
2022-10-28 16:11:08 +00:00
|
|
|
{type: 'text', placeholder: t('SearchInDocument')},
|
2021-05-19 07:07:51 +00:00
|
|
|
dom.on('blur', () => (
|
|
|
|
keepExpanded ?
|
|
|
|
setTimeout(() => inputElem.focus(), 0) :
|
|
|
|
toggleMenu(false)
|
|
|
|
)),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.onKeyDown({
|
2022-02-25 10:57:16 +00:00
|
|
|
Enter: (ev) => ev.shiftKey ? model.findPrev() : model.findNext(),
|
2021-05-25 12:24:36 +00:00
|
|
|
Escape: () => { keepExpanded = false; toggleMenu(false); },
|
2020-10-02 15:10:00 +00:00
|
|
|
// Catch both Tab and Shift+Tab to prevent focus entering unrelated editable label.
|
|
|
|
Tab: () => toggleMenu(false),
|
|
|
|
}),
|
|
|
|
dom.on('focus', () => toggleMenu(true)),
|
|
|
|
commandGroup.attach(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Releases focus when closing the search bar, otherwise users could keep typing in without
|
|
|
|
// noticing.
|
|
|
|
const lis = model.isOpen.addListener(val => val || inputElem.blur());
|
|
|
|
|
|
|
|
return searchWrapper(
|
|
|
|
testId('wrapper'),
|
|
|
|
searchWrapper.cls('-expand', model.isOpen),
|
|
|
|
dom.autoDispose(commandGroup),
|
|
|
|
dom.autoDispose(lis),
|
2022-11-14 14:34:50 +00:00
|
|
|
// Make sure we don't attempt to call delayed callback after disposal.
|
|
|
|
dom.onDispose(() => toggleMenu.cancel()),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssHoverCircle(
|
|
|
|
cssTopBarBtn('Search',
|
|
|
|
testId('icon'),
|
2021-10-29 18:13:10 +00:00
|
|
|
dom.on('click', focusAndSelect),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip('Search', {key: 'topBarBtnTooltip'}),
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
expandedSearch(
|
|
|
|
testId('input'),
|
|
|
|
inputElem,
|
2021-05-19 07:07:51 +00:00
|
|
|
dom.domComputed((use) => {
|
|
|
|
const noMatch = use(model.noMatch);
|
|
|
|
const isEmpty = use(model.isEmpty);
|
|
|
|
if (isEmpty) { return null; }
|
2022-10-28 16:11:08 +00:00
|
|
|
if (noMatch) { return cssLabel(t("NoResults")); }
|
2021-05-19 07:07:51 +00:00
|
|
|
return [
|
|
|
|
cssArrowBtn(
|
|
|
|
icon('Dropdown'),
|
|
|
|
testId('next'),
|
|
|
|
// Prevent focus from being stolen from the input
|
|
|
|
dom.on('mousedown', (event) => event.preventDefault()),
|
|
|
|
dom.on('click', () => model.findNext()),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip(
|
|
|
|
[
|
2022-10-28 16:11:08 +00:00
|
|
|
t('FindNext'),
|
2022-10-19 23:06:05 +00:00
|
|
|
cssShortcut(`(${['Enter', allCommands.findNext.humanKeys].join(', ')})`),
|
|
|
|
],
|
|
|
|
{key: 'searchArrowBtnTooltip'}
|
|
|
|
),
|
2021-05-19 07:07:51 +00:00
|
|
|
),
|
|
|
|
cssArrowBtn(
|
|
|
|
icon('DropdownUp'),
|
|
|
|
testId('prev'),
|
|
|
|
// Prevent focus from being stolen from the input
|
|
|
|
dom.on('mousedown', (event) => event.preventDefault()),
|
|
|
|
dom.on('click', () => model.findPrev()),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip(
|
|
|
|
[
|
2022-10-28 16:11:08 +00:00
|
|
|
t('FindPrevious'),
|
2022-10-19 23:06:05 +00:00
|
|
|
cssShortcut(allCommands.findPrev.getKeysDesc()),
|
|
|
|
],
|
|
|
|
{key: 'searchArrowBtnTooltip'}
|
|
|
|
),
|
2021-05-19 07:07:51 +00:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssCloseBtn('CrossSmall',
|
|
|
|
testId('close'),
|
2021-05-19 07:07:51 +00:00
|
|
|
dom.on('click', () => toggleMenu(false))),
|
|
|
|
cssOptions(
|
2022-04-13 17:26:59 +00:00
|
|
|
labeledSquareCheckbox(model.multiPage, dom.text(model.allLabel)),
|
2021-05-19 07:07:51 +00:00
|
|
|
dom.on('mouseenter', () => keepExpanded = true),
|
|
|
|
dom.on('mouseleave', () => keepExpanded = false),
|
|
|
|
testId('option-all-pages'),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|