2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Creates a UI for column filter menu given a columnFilter model, a mapping of cell values to counts, and an onClose
|
|
|
|
* callback that's triggered on Apply or on Cancel. Changes to the UI result in changes to the underlying model,
|
|
|
|
* but on Cancel the model is reset to its initial state prior to menu closing.
|
|
|
|
*/
|
|
|
|
|
2021-05-27 11:06:26 +00:00
|
|
|
import {allInclusive, ColumnFilter} from 'app/client/models/ColumnFilter';
|
2021-05-06 12:23:50 +00:00
|
|
|
import {ColumnFilterMenuModel, IFilterCount} from 'app/client/models/ColumnFilterMenuModel';
|
2021-11-19 20:30:11 +00:00
|
|
|
import {ColumnRec, ViewFieldRec, ViewSectionRec} from 'app/client/models/DocModel';
|
|
|
|
import {FilterInfo} from 'app/client/models/entities/ViewSectionRec';
|
2021-06-17 15:26:43 +00:00
|
|
|
import {RowId, RowSource} from 'app/client/models/rowset';
|
|
|
|
import {ColumnFilterFunc, SectionFilter} from 'app/client/models/SectionFilter';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {TableData} from 'app/client/models/TableData';
|
|
|
|
import {basicButton, primaryButton} from 'app/client/ui2018/buttons';
|
2021-07-15 15:50:28 +00:00
|
|
|
import {cssLabel as cssCheckboxLabel, cssCheckboxSquare, cssLabelText, Indeterminate, labeledTriStateSquareCheckbox
|
|
|
|
} from 'app/client/ui2018/checkbox';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {colors, vars} from 'app/client/ui2018/cssVars';
|
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2021-03-08 16:31:06 +00:00
|
|
|
import {menuCssClass, menuDivider} from 'app/client/ui2018/menus';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {CellValue} from 'app/common/DocActions';
|
2021-06-17 15:26:43 +00:00
|
|
|
import {isEquivalentFilter} from "app/common/FilterState";
|
2022-05-24 14:59:12 +00:00
|
|
|
import {Computed, dom, DomElementArg, DomElementMethod, IDisposableOwner, input, makeTestId, Observable,
|
|
|
|
styled} from 'grainjs';
|
2021-06-17 15:26:43 +00:00
|
|
|
import concat = require('lodash/concat');
|
2020-10-02 15:10:00 +00:00
|
|
|
import identity = require('lodash/identity');
|
2021-05-06 12:23:50 +00:00
|
|
|
import noop = require('lodash/noop');
|
2021-06-17 15:26:43 +00:00
|
|
|
import partition = require('lodash/partition');
|
|
|
|
import some = require('lodash/some');
|
|
|
|
import tail = require('lodash/tail');
|
2022-05-24 14:59:12 +00:00
|
|
|
import debounce = require('lodash/debounce');
|
2021-04-14 15:17:45 +00:00
|
|
|
import {IOpenController, IPopupOptions, setPopupToCreateDom} from 'popweasel';
|
2021-06-11 14:32:05 +00:00
|
|
|
import {decodeObject} from 'app/plugin/objtypes';
|
2022-05-24 14:59:12 +00:00
|
|
|
import {isList, isNumberType, isRefListType} from 'app/common/gristTypes';
|
2021-07-15 15:50:28 +00:00
|
|
|
import {choiceToken} from 'app/client/widgets/ChoiceToken';
|
|
|
|
import {ChoiceOptions} from 'app/client/widgets/ChoiceTextBox';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
interface IFilterMenuOptions {
|
2021-03-25 15:14:03 +00:00
|
|
|
model: ColumnFilterMenuModel;
|
2020-10-02 15:10:00 +00:00
|
|
|
valueCounts: Map<CellValue, IFilterCount>;
|
|
|
|
doSave: (reset: boolean) => void;
|
|
|
|
onClose: () => void;
|
2021-07-15 15:50:28 +00:00
|
|
|
renderValue: (key: CellValue, value: IFilterCount) => DomElementArg;
|
2022-05-24 14:59:12 +00:00
|
|
|
valueParser?: (val: string) => any;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 15:14:03 +00:00
|
|
|
const testId = makeTestId('test-filter-menu-');
|
|
|
|
|
|
|
|
export function columnFilterMenu(owner: IDisposableOwner, opts: IFilterMenuOptions): HTMLElement {
|
2022-05-24 14:59:12 +00:00
|
|
|
const { model, doSave, onClose, renderValue, valueParser } = opts;
|
2021-03-25 15:14:03 +00:00
|
|
|
const { columnFilter } = model;
|
2020-10-02 15:10:00 +00:00
|
|
|
// Save the initial state to allow reverting back to it on Cancel
|
|
|
|
const initialStateJson = columnFilter.makeFilterJson();
|
|
|
|
|
|
|
|
// Map to keep track of displayed checkboxes
|
|
|
|
const checkboxMap: Map<CellValue, HTMLInputElement> = new Map();
|
|
|
|
|
2022-05-24 14:59:12 +00:00
|
|
|
// Listen for changes to filterFunc, and update checkboxes accordingly. Debounce is needed to
|
|
|
|
// prevent some weirdness when users click on a checkbox while focus was on a range input (causing
|
|
|
|
// sometimes the checkbox to not toggle)
|
|
|
|
const filterListener = columnFilter.filterFunc.addListener(debounce(func => {
|
2020-10-02 15:10:00 +00:00
|
|
|
for (const [value, elem] of checkboxMap) {
|
|
|
|
elem.checked = func(value);
|
|
|
|
}
|
2022-05-24 14:59:12 +00:00
|
|
|
}));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
const {searchValue: searchValueObs, filteredValues, filteredKeys, isSortedByCount} = model;
|
2021-03-08 16:31:06 +00:00
|
|
|
|
2021-03-25 15:14:03 +00:00
|
|
|
const isAboveLimitObs = Computed.create(owner, (use) => use(model.valuesBeyondLimit).length > 0);
|
|
|
|
const isSearchingObs = Computed.create(owner, (use) => Boolean(use(searchValueObs)));
|
2021-03-08 16:31:06 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
let searchInput: HTMLInputElement;
|
2022-05-24 14:59:12 +00:00
|
|
|
let minRangeInput: HTMLInputElement;
|
2020-10-02 15:10:00 +00:00
|
|
|
let reset = false;
|
|
|
|
|
2022-05-24 14:59:12 +00:00
|
|
|
// Gives focus to the searchInput on open (or to the min input if the range filter is present).
|
|
|
|
setTimeout(() => (minRangeInput || searchInput).select(), 0);
|
2021-03-08 16:31:06 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const filterMenu: HTMLElement = cssMenu(
|
|
|
|
{ tabindex: '-1' }, // Allow menu to be focused
|
|
|
|
testId('wrapper'),
|
|
|
|
dom.cls(menuCssClass),
|
|
|
|
dom.autoDispose(filterListener),
|
|
|
|
dom.onDispose(() => doSave(reset)), // Save on disposal, which should always happen as part of closing.
|
|
|
|
dom.onKeyDown({
|
|
|
|
Enter: () => onClose(),
|
|
|
|
Escape: () => onClose()
|
|
|
|
}),
|
2022-05-24 14:59:12 +00:00
|
|
|
|
|
|
|
// Filter by range
|
|
|
|
dom.maybe(isNumberType(columnFilter.columnType), () => [
|
|
|
|
cssRangeHeader('Filter by Range'),
|
|
|
|
cssRangeContainer(
|
|
|
|
minRangeInput = rangeInput('Min ', columnFilter.min, {valueParser}, testId('min')),
|
|
|
|
cssRangeInputSeparator('→'),
|
|
|
|
rangeInput('Max ', columnFilter.max, {valueParser}, testId('max')),
|
|
|
|
),
|
|
|
|
cssMenuDivider(),
|
|
|
|
]),
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
cssMenuHeader(
|
2021-03-08 16:31:06 +00:00
|
|
|
cssSearchIcon('Search'),
|
|
|
|
searchInput = cssSearch(
|
|
|
|
searchValueObs, { onInput: true },
|
|
|
|
testId('search-input'),
|
|
|
|
{ type: 'search', placeholder: 'Search values' },
|
|
|
|
dom.onKeyDown({
|
|
|
|
Enter: () => {
|
|
|
|
if (searchValueObs.get()) {
|
|
|
|
columnFilter.setState({included: filteredKeys.get()});
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-03-08 16:31:06 +00:00
|
|
|
},
|
|
|
|
Escape$: (ev) => {
|
|
|
|
if (searchValueObs.get()) {
|
|
|
|
searchValueObs.set('');
|
|
|
|
searchInput.focus();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
),
|
|
|
|
dom.maybe(searchValueObs, () => cssSearchIcon(
|
|
|
|
'CrossSmall', testId('search-close'),
|
|
|
|
dom.on('click', () => {
|
2020-10-02 15:10:00 +00:00
|
|
|
searchValueObs.set('');
|
2021-03-08 16:31:06 +00:00
|
|
|
searchInput.focus();
|
|
|
|
}),
|
|
|
|
)),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
cssMenuDivider(),
|
2021-03-08 16:31:06 +00:00
|
|
|
cssMenuItem(
|
|
|
|
dom.domComputed((use) => {
|
|
|
|
const searchValue = use(searchValueObs);
|
2021-03-18 13:16:05 +00:00
|
|
|
// This is necessary to avoid a known bug in grainjs where filteredKeys does not get
|
|
|
|
// recalculated.
|
|
|
|
use(filteredKeys);
|
2021-03-08 16:31:06 +00:00
|
|
|
const allSpec = searchValue ? {included: use(filteredKeys)} : {excluded: []};
|
|
|
|
const noneSpec = searchValue ? {excluded: use(filteredKeys)} : {included: []};
|
|
|
|
const state = use(columnFilter.state);
|
|
|
|
return [
|
|
|
|
cssSelectAll(
|
|
|
|
dom.text(searchValue ? 'All Shown' : 'All'),
|
|
|
|
cssSelectAll.cls('-disabled', isEquivalentFilter(state, allSpec)),
|
|
|
|
dom.on('click', () => columnFilter.setState(allSpec)),
|
2021-06-11 14:15:23 +00:00
|
|
|
testId('bulk-action'),
|
2021-03-08 16:31:06 +00:00
|
|
|
),
|
|
|
|
cssDotSeparator('•'),
|
|
|
|
cssSelectAll(
|
|
|
|
searchValue ? 'All Except' : 'None',
|
|
|
|
cssSelectAll.cls('-disabled', isEquivalentFilter(state, noneSpec)),
|
|
|
|
dom.on('click', () => columnFilter.setState(noneSpec)),
|
2021-06-11 14:15:23 +00:00
|
|
|
testId('bulk-action'),
|
2021-03-08 16:31:06 +00:00
|
|
|
)
|
|
|
|
];
|
2021-06-17 15:26:43 +00:00
|
|
|
}),
|
|
|
|
cssSortIcon(
|
|
|
|
'Sort',
|
|
|
|
cssSortIcon.cls('-active', isSortedByCount),
|
|
|
|
dom.on('click', () => isSortedByCount.set(!isSortedByCount.get())),
|
|
|
|
)
|
2021-03-08 16:31:06 +00:00
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssItemList(
|
|
|
|
testId('list'),
|
|
|
|
dom.maybe(use => use(filteredValues).length === 0, () => cssNoResults('No matching values')),
|
2021-03-25 15:14:03 +00:00
|
|
|
dom.domComputed(filteredValues, (values) => values.slice(0, model.limitShown).map(([key, value]) => (
|
|
|
|
cssMenuItem(
|
|
|
|
cssLabel(
|
2021-07-15 15:50:28 +00:00
|
|
|
cssCheckboxSquare(
|
|
|
|
{type: 'checkbox'},
|
2022-05-24 14:59:12 +00:00
|
|
|
dom.on('change', (_ev, elem) => {
|
|
|
|
elem.checked ? columnFilter.add(key) : columnFilter.delete(key);
|
|
|
|
}),
|
2021-07-15 15:50:28 +00:00
|
|
|
(elem) => { elem.checked = columnFilter.includes(key); checkboxMap.set(key, elem); },
|
|
|
|
dom.style('position', 'relative'),
|
|
|
|
),
|
|
|
|
renderValue(key, value),
|
2021-03-25 15:14:03 +00:00
|
|
|
),
|
|
|
|
cssItemCount(value.count.toLocaleString(), testId('count')))
|
|
|
|
))) // Include comma separator
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
cssMenuDivider(),
|
|
|
|
cssMenuFooter(
|
2021-03-25 15:14:03 +00:00
|
|
|
dom.domComputed((use) => {
|
|
|
|
const isAboveLimit = use(isAboveLimitObs);
|
|
|
|
const searchValue = use(isSearchingObs);
|
2021-06-17 15:26:43 +00:00
|
|
|
const otherValues = use(model.otherValues);
|
|
|
|
const anyOtherValues = Boolean(otherValues.length);
|
|
|
|
const valuesBeyondLimit = use(model.valuesBeyondLimit);
|
2021-03-25 15:14:03 +00:00
|
|
|
if (isAboveLimit) {
|
|
|
|
return searchValue ? [
|
2021-06-17 15:26:43 +00:00
|
|
|
buildSummary('Other Matching', valuesBeyondLimit, false, model),
|
|
|
|
buildSummary('Other Non-Matching', otherValues, true, model),
|
2021-03-25 15:14:03 +00:00
|
|
|
] : [
|
2021-06-17 15:26:43 +00:00
|
|
|
buildSummary('Other Values', concat(otherValues, valuesBeyondLimit), false, model),
|
|
|
|
buildSummary('Future Values', [], true, model),
|
2021-03-25 15:14:03 +00:00
|
|
|
];
|
|
|
|
} else {
|
2021-06-17 15:26:43 +00:00
|
|
|
return anyOtherValues ? [
|
|
|
|
buildSummary('Others', otherValues, true, model)
|
2021-03-25 15:14:03 +00:00
|
|
|
] : [
|
2021-06-17 15:26:43 +00:00
|
|
|
buildSummary('Future Values', [], true, model)
|
2021-03-25 15:14:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}),
|
2021-03-08 16:31:06 +00:00
|
|
|
cssMenuItem(
|
|
|
|
cssApplyButton('Apply', testId('apply-btn'),
|
|
|
|
dom.on('click', () => { reset = true; onClose(); })),
|
|
|
|
basicButton('Cancel', testId('cancel-btn'),
|
|
|
|
dom.on('click', () => { columnFilter.setState(initialStateJson); onClose(); } ))
|
|
|
|
)
|
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
return filterMenu;
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:59:12 +00:00
|
|
|
function rangeInput(placeholder: string, obs: Observable<number|undefined>,
|
|
|
|
opts: {valueParser?: (val: string) => any},
|
|
|
|
...args: DomElementArg[]) {
|
|
|
|
const valueParser = opts.valueParser || Number;
|
|
|
|
const formatValue = ((val: any) => val?.toString() || '');
|
|
|
|
let editMode = false;
|
|
|
|
let el: HTMLInputElement;
|
|
|
|
// keep input content in sync only when no edit are going on.
|
|
|
|
const lis = obs.addListener(() => editMode ? null : el.value = formatValue(obs.get()));
|
|
|
|
// handle change
|
|
|
|
const onBlur = () => {
|
|
|
|
onInput.flush();
|
|
|
|
editMode = false;
|
|
|
|
el.value = formatValue(obs.get());
|
|
|
|
};
|
|
|
|
const onInput = debounce(() => {
|
|
|
|
editMode = true;
|
|
|
|
const val = el.value ? valueParser(el.value) : undefined;
|
|
|
|
if (val === undefined || !isNaN(val)) {
|
|
|
|
obs.set(val);
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
return el = cssRangeInput(
|
|
|
|
{inputmode: 'numeric', placeholder, value: formatValue(obs.get())},
|
|
|
|
dom.on('input', onInput),
|
|
|
|
dom.on('blur', onBlur),
|
|
|
|
dom.autoDispose(lis),
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
/**
|
|
|
|
* Builds a tri-state checkbox that summaries the state of all the `values`. The special value
|
|
|
|
* `Future Values` which turns the filter into an inclusion filter or exclusion filter, can be
|
|
|
|
* added to the summary using `switchFilterType`. Uses `label` as label and also expects
|
|
|
|
* `model` as the column filter menu model.
|
|
|
|
*
|
|
|
|
* The checkbox appears checked if all values of the summary are included, unchecked if none, and in
|
|
|
|
* the indeterminate state if values are in mixed state.
|
|
|
|
*
|
|
|
|
* On user clicks, if checkbox is checked, it does uncheck all the values, and if the
|
|
|
|
* `switchFilterType` is true it also converts the filter into an inclusion filter. But if the
|
|
|
|
* checkbox is unchecked, or in the Indeterminate state, it does check all the values, and if the
|
2022-02-19 09:46:49 +00:00
|
|
|
* `switchFilterType` is true it also converts the filter into an exclusion filter.
|
2021-06-17 15:26:43 +00:00
|
|
|
*/
|
|
|
|
function buildSummary(label: string|Computed<string>, values: Array<[CellValue, IFilterCount]>,
|
|
|
|
switchFilterType: boolean, model: ColumnFilterMenuModel) {
|
|
|
|
const columnFilter = model.columnFilter;
|
|
|
|
const checkboxState = Computed.create(
|
|
|
|
null, columnFilter.isInclusionFilter, columnFilter.filterFunc,
|
|
|
|
(_use, isInclusionFilter) => {
|
|
|
|
|
|
|
|
// let's gather all sub options.
|
|
|
|
const subOptions = values.map((val) => ({getState: () => columnFilter.includes(val[0])}));
|
|
|
|
if (switchFilterType) {
|
|
|
|
subOptions.push({getState: () => !isInclusionFilter});
|
|
|
|
}
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
// At this point if sub options is still empty let's just return false (unchecked).
|
|
|
|
if (!subOptions.length) { return false; }
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
// let's compare the state for first sub options against all the others. If there is one
|
|
|
|
// different, then state should be `Indeterminate`, otherwise, the state will the the same as
|
|
|
|
// the one of the first sub option.
|
|
|
|
const first = subOptions[0].getState();
|
|
|
|
if (some(tail(subOptions), (val) => val.getState() !== first)) { return Indeterminate; }
|
|
|
|
return first;
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
}).onWrite((val) => {
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
if (switchFilterType) {
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
// Note that if `includeFutureValues` is true, we only needs to toggle the filter type
|
|
|
|
// between exclusive and inclusive. Doing this will automatically excludes/includes all
|
|
|
|
// other values, so no need for extra steps.
|
|
|
|
const state = val ?
|
|
|
|
{excluded: model.filteredKeys.get().filter((key) => !columnFilter.includes(key))} :
|
|
|
|
{included: model.filteredKeys.get().filter((key) => columnFilter.includes(key))};
|
|
|
|
columnFilter.setState(state);
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
} else {
|
2021-03-25 15:14:03 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
const keys = values.map(([key]) => key);
|
|
|
|
if (val) {
|
|
|
|
columnFilter.addMany(keys);
|
|
|
|
} else {
|
|
|
|
columnFilter.deleteMany(keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-03-25 15:14:03 +00:00
|
|
|
|
|
|
|
return cssMenuItem(
|
2021-06-17 15:26:43 +00:00
|
|
|
dom.autoDispose(checkboxState),
|
2021-03-25 15:14:03 +00:00
|
|
|
testId('summary'),
|
2021-06-17 15:26:43 +00:00
|
|
|
labeledTriStateSquareCheckbox(
|
|
|
|
checkboxState,
|
|
|
|
`${label} ${formatUniqueCount(values)}`.trim()
|
2021-03-25 15:14:03 +00:00
|
|
|
),
|
2021-06-17 15:26:43 +00:00
|
|
|
cssItemCount(formatCount(values), testId('count')),
|
2021-03-25 15:14:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
function formatCount(values: Array<[CellValue, IFilterCount]>) {
|
|
|
|
const count = getCount(values);
|
|
|
|
return count ? count.toLocaleString() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatUniqueCount(values: Array<[CellValue, IFilterCount]>) {
|
|
|
|
const count = values.length;
|
|
|
|
return count ? '(' + count.toLocaleString() + ')' : '';
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:51:04 +00:00
|
|
|
/**
|
|
|
|
* Returns a new `Map` object to holds pairs of `CellValue` and `IFilterCount`. For `Bool`, `Choice`
|
|
|
|
* and `ChoiceList` type of column, the map is initialized with all possible values in order to make
|
|
|
|
* sure they get shown to the user.
|
|
|
|
*/
|
|
|
|
function getEmptyCountMap(fieldOrColumn: ViewFieldRec|ColumnRec): Map<CellValue, IFilterCount> {
|
|
|
|
const columnType = fieldOrColumn.origCol().type();
|
|
|
|
let values: any[] = [];
|
|
|
|
if (columnType === 'Bool') {
|
|
|
|
values = [true, false];
|
|
|
|
} else if (['Choice', 'ChoiceList'].includes(columnType)) {
|
|
|
|
const options = fieldOrColumn.origCol().widgetOptionsJson;
|
|
|
|
values = options.prop('choices')();
|
|
|
|
}
|
2022-05-24 07:10:15 +00:00
|
|
|
return new Map(values.map((v) => [v, {label: String(v), count: 0, displayValue: v}]));
|
2022-05-19 08:51:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Returns content for the newly created columnFilterMenu; for use with setPopupToCreateDom().
|
|
|
|
*/
|
2021-11-19 20:30:11 +00:00
|
|
|
export function createFilterMenu(openCtl: IOpenController, sectionFilter: SectionFilter, filterInfo: FilterInfo,
|
2021-06-17 15:26:43 +00:00
|
|
|
rowSource: RowSource, tableData: TableData, onClose: () => void = noop) {
|
2020-10-02 15:10:00 +00:00
|
|
|
// Go through all of our shown and hidden rows, and count them up by the values in this column.
|
2021-11-19 20:30:11 +00:00
|
|
|
const fieldOrColumn = filterInfo.fieldOrColumn;
|
|
|
|
const columnType = fieldOrColumn.origCol.peek().type.peek();
|
2022-05-24 07:10:15 +00:00
|
|
|
const visibleColumnType = fieldOrColumn.visibleColModel.peek()?.type.peek() || columnType;
|
|
|
|
const {keyMapFunc, labelMapFunc, valueMapFunc} = getMapFuncs(columnType, tableData, filterInfo.fieldOrColumn);
|
2021-11-19 20:30:11 +00:00
|
|
|
const activeFilterBar = sectionFilter.viewSection.activeFilterBar;
|
2022-05-24 14:59:12 +00:00
|
|
|
const valueParser = (fieldOrColumn as any).createValueParser?.();
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-11-19 20:30:11 +00:00
|
|
|
function getFilterFunc(col: ViewFieldRec|ColumnRec, colFilter: ColumnFilterFunc|null) {
|
|
|
|
return col.getRowId() === fieldOrColumn.getRowId() ? null : colFilter;
|
2021-06-17 15:26:43 +00:00
|
|
|
}
|
|
|
|
const filterFunc = Computed.create(null, use => sectionFilter.buildFilterFunc(getFilterFunc, use));
|
|
|
|
openCtl.autoDispose(filterFunc);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-06-17 15:26:43 +00:00
|
|
|
const [allRows, hiddenRows] = partition(Array.from(rowSource.getAllRows()), filterFunc.get());
|
2022-05-19 08:51:04 +00:00
|
|
|
const valueCounts = getEmptyCountMap(fieldOrColumn);
|
2022-05-24 07:10:15 +00:00
|
|
|
addCountsToMap(valueCounts, allRows, {keyMapFunc, labelMapFunc, columnType,
|
|
|
|
valueMapFunc});
|
2021-06-17 15:26:43 +00:00
|
|
|
addCountsToMap(valueCounts, hiddenRows, {keyMapFunc, labelMapFunc, columnType,
|
2022-05-24 07:10:15 +00:00
|
|
|
areHiddenRows: true, valueMapFunc});
|
2021-06-17 15:26:43 +00:00
|
|
|
|
2022-05-24 14:59:12 +00:00
|
|
|
const valueCountsArr = Array.from(valueCounts);
|
|
|
|
const columnFilter = ColumnFilter.create(openCtl, filterInfo.filter.peek(), columnType, visibleColumnType,
|
|
|
|
valueCountsArr.map((arr) => arr[0]));
|
|
|
|
sectionFilter.setFilterOverride(fieldOrColumn.getRowId(), columnFilter); // Will be removed on menu disposal
|
|
|
|
const model = ColumnFilterMenuModel.create(openCtl, columnFilter, valueCountsArr);
|
2021-06-17 15:26:43 +00:00
|
|
|
|
2021-03-08 16:31:06 +00:00
|
|
|
return columnFilterMenu(openCtl, {
|
2021-03-25 15:14:03 +00:00
|
|
|
model,
|
2020-10-02 15:10:00 +00:00
|
|
|
valueCounts,
|
2021-05-06 12:23:50 +00:00
|
|
|
onClose: () => { openCtl.close(); onClose(); },
|
2020-10-02 15:10:00 +00:00
|
|
|
doSave: (reset: boolean = false) => {
|
|
|
|
const spec = columnFilter.makeFilterJson();
|
2021-04-20 15:16:03 +00:00
|
|
|
// If filter is moot and filter bar is hidden, let's remove the filter.
|
2021-11-19 20:30:11 +00:00
|
|
|
sectionFilter.viewSection.setFilter(
|
|
|
|
fieldOrColumn.origCol().origColRef(),
|
|
|
|
spec === allInclusive && !activeFilterBar.peek() ? '' : spec
|
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
if (reset) {
|
|
|
|
sectionFilter.resetTemporaryRows();
|
|
|
|
}
|
|
|
|
},
|
2021-11-19 20:30:11 +00:00
|
|
|
renderValue: getRenderFunc(columnType, fieldOrColumn),
|
2022-05-24 14:59:12 +00:00
|
|
|
valueParser,
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:06:40 +00:00
|
|
|
/**
|
2022-05-24 07:10:15 +00:00
|
|
|
* Returns three callback functions, `keyMapFunc`, `labelMapFunc`
|
|
|
|
* and `valueMapFunc`, which map row ids to cell values, labels
|
|
|
|
* and visible col value respectively.
|
2021-08-12 18:06:40 +00:00
|
|
|
*
|
|
|
|
* The functions vary based on the `columnType`. For example,
|
|
|
|
* Reference Lists have a unique `labelMapFunc` that returns a list
|
|
|
|
* of all labels in a given cell, rather than a single label.
|
|
|
|
*
|
|
|
|
* Used by ColumnFilterMenu to compute counts of unique cell
|
|
|
|
* values and display them with an appropriate label.
|
|
|
|
*/
|
2021-11-19 20:30:11 +00:00
|
|
|
function getMapFuncs(columnType: string, tableData: TableData, fieldOrColumn: ViewFieldRec|ColumnRec) {
|
|
|
|
const keyMapFunc = tableData.getRowPropFunc(fieldOrColumn.colId())!;
|
|
|
|
const labelGetter = tableData.getRowPropFunc(fieldOrColumn.displayColModel().colId())!;
|
2021-12-15 22:31:53 +00:00
|
|
|
const formatter = fieldOrColumn.visibleColFormatter();
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
let labelMapFunc: (rowId: number) => string | string[];
|
2022-05-24 07:10:15 +00:00
|
|
|
const valueMapFunc: (rowId: number) => any = (rowId: number) => decodeObject(labelGetter(rowId)!);
|
|
|
|
|
2021-08-12 18:06:40 +00:00
|
|
|
if (isRefListType(columnType)) {
|
|
|
|
labelMapFunc = (rowId: number) => {
|
|
|
|
const maybeLabels = labelGetter(rowId);
|
|
|
|
if (!maybeLabels) { return ''; }
|
|
|
|
const labels = isList(maybeLabels) ? maybeLabels.slice(1) : [maybeLabels];
|
|
|
|
return labels.map(l => formatter.formatAny(l));
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
labelMapFunc = (rowId: number) => formatter.formatAny(labelGetter(rowId));
|
|
|
|
}
|
2022-05-24 07:10:15 +00:00
|
|
|
return {keyMapFunc, labelMapFunc, valueMapFunc};
|
2021-08-12 18:06:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
/**
|
|
|
|
* Returns a callback function for rendering values in a filter menu.
|
|
|
|
*
|
|
|
|
* For example, Choice and Choice List columns will differ from other
|
|
|
|
* column types by rendering their values as colored tokens instead of
|
|
|
|
* text.
|
|
|
|
*/
|
2021-11-19 20:30:11 +00:00
|
|
|
function getRenderFunc(columnType: string, fieldOrColumn: ViewFieldRec|ColumnRec) {
|
2021-07-15 15:50:28 +00:00
|
|
|
if (['Choice', 'ChoiceList'].includes(columnType)) {
|
2021-11-19 20:30:11 +00:00
|
|
|
const options = fieldOrColumn.widgetOptionsJson.peek();
|
2021-07-15 15:50:28 +00:00
|
|
|
const choiceSet: Set<string> = new Set(options.choices || []);
|
|
|
|
const choiceOptions: ChoiceOptions = options.choiceOptions || {};
|
|
|
|
|
|
|
|
return (_key: CellValue, value: IFilterCount) => {
|
|
|
|
if (value.label === '') {
|
|
|
|
return cssItemValue(value.label);
|
|
|
|
}
|
|
|
|
|
|
|
|
return choiceToken(
|
|
|
|
value.label,
|
|
|
|
{
|
|
|
|
fillColor: choiceOptions[value.label]?.fillColor,
|
|
|
|
textColor: choiceOptions[value.label]?.textColor,
|
2022-04-07 14:58:16 +00:00
|
|
|
fontBold: choiceOptions[value.label]?.fontBold ?? false,
|
|
|
|
fontUnderline: choiceOptions[value.label]?.fontUnderline ?? false,
|
|
|
|
fontItalic: choiceOptions[value.label]?.fontItalic ?? false,
|
|
|
|
fontStrikethrough: choiceOptions[value.label]?.fontStrikethrough ?? false,
|
2022-06-06 17:42:51 +00:00
|
|
|
invalid: !choiceSet.has(value.label),
|
2021-07-15 15:50:28 +00:00
|
|
|
},
|
|
|
|
dom.cls(cssToken.className),
|
|
|
|
testId('choice-token')
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return (key: CellValue, value: IFilterCount) =>
|
|
|
|
cssItemValue(value.label === undefined ? String(key) : value.label);
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:32:05 +00:00
|
|
|
interface ICountOptions {
|
2021-08-12 18:06:40 +00:00
|
|
|
columnType: string;
|
2022-05-24 07:10:15 +00:00
|
|
|
// returns the indexing key for the filter
|
2021-06-11 14:32:05 +00:00
|
|
|
keyMapFunc?: (v: any) => any;
|
2022-05-24 07:10:15 +00:00
|
|
|
// returns the string representation of the value (can involves some formatting).
|
2021-06-11 14:32:05 +00:00
|
|
|
labelMapFunc?: (v: any) => any;
|
2022-05-24 07:10:15 +00:00
|
|
|
// returns the underlying value (useful for comparison)
|
|
|
|
valueMapFunc: (v: any) => any;
|
2021-06-17 15:26:43 +00:00
|
|
|
areHiddenRows?: boolean;
|
2021-06-11 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
2022-05-24 07:10:15 +00:00
|
|
|
* For each row id in Iterable, adds a key mapped with `keyMapFunc` and a value object with a
|
|
|
|
* `label` mapped with `labelMapFunc` and a `count` representing the total number of times the key
|
|
|
|
* has been encountered and a `displayValues` mapped with `valueMapFunc`.
|
2021-06-11 14:32:05 +00:00
|
|
|
*
|
|
|
|
* The optional column type controls how complex cell values are decomposed into keys (e.g. Choice Lists have
|
|
|
|
* the possible choices as keys).
|
2022-04-18 14:40:29 +00:00
|
|
|
* Note that this logic is replicated in BaseView.prototype.filterByThisCellValue.
|
2020-10-02 15:10:00 +00:00
|
|
|
*/
|
2021-06-17 15:26:43 +00:00
|
|
|
function addCountsToMap(valueMap: Map<CellValue, IFilterCount>, rowIds: RowId[],
|
|
|
|
{ keyMapFunc = identity, labelMapFunc = identity, columnType,
|
2022-05-24 07:10:15 +00:00
|
|
|
areHiddenRows = false, valueMapFunc }: ICountOptions) {
|
2021-06-11 14:32:05 +00:00
|
|
|
|
|
|
|
for (const rowId of rowIds) {
|
|
|
|
let key = keyMapFunc(rowId);
|
|
|
|
|
|
|
|
// If row contains a list and the column is a Choice List, treat each choice as a separate key
|
2021-08-12 18:06:40 +00:00
|
|
|
if (isList(key) && (columnType === 'ChoiceList')) {
|
2021-06-17 15:26:43 +00:00
|
|
|
const list = decodeObject(key) as unknown[];
|
2022-06-10 21:34:12 +00:00
|
|
|
if (!list.length) {
|
|
|
|
// If the list is empty, add an item for the whole list, otherwise the row will be missing from filters.
|
|
|
|
addSingleCountToMap(valueMap, '', () => '', () => '', areHiddenRows);
|
|
|
|
}
|
2021-06-17 15:26:43 +00:00
|
|
|
for (const item of list) {
|
2022-05-24 07:10:15 +00:00
|
|
|
addSingleCountToMap(valueMap, item, () => item, () => item, areHiddenRows);
|
2021-06-17 15:26:43 +00:00
|
|
|
}
|
2021-06-11 14:32:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
// If row contains a Reference List, treat each reference as a separate key
|
|
|
|
if (isList(key) && isRefListType(columnType)) {
|
|
|
|
const refIds = decodeObject(key) as unknown[];
|
2022-06-10 21:34:12 +00:00
|
|
|
if (!refIds.length) {
|
|
|
|
// If the list is empty, add an item for the whole list, otherwise the row will be missing from filters.
|
|
|
|
addSingleCountToMap(valueMap, null, () => null, () => null, areHiddenRows);
|
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
const refLabels = labelMapFunc(rowId);
|
2022-05-24 07:10:15 +00:00
|
|
|
const displayValues = valueMapFunc(rowId);
|
2021-08-12 18:06:40 +00:00
|
|
|
refIds.forEach((id, i) => {
|
2022-05-24 07:10:15 +00:00
|
|
|
addSingleCountToMap(valueMap, id, () => refLabels[i], () => displayValues[i], areHiddenRows);
|
2021-08-12 18:06:40 +00:00
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
// For complex values, serialize the value to allow them to be properly stored
|
|
|
|
if (Array.isArray(key)) { key = JSON.stringify(key); }
|
2022-05-24 07:10:15 +00:00
|
|
|
addSingleCountToMap(valueMap, key, () => labelMapFunc(rowId), () => valueMapFunc(rowId), areHiddenRows);
|
2021-06-11 14:32:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-17 15:26:43 +00:00
|
|
|
* Adds the `value` to `valueMap` using `labelGetter` to get the label and increments `count` unless
|
|
|
|
* isHiddenRow is true.
|
2021-06-11 14:32:05 +00:00
|
|
|
*/
|
2022-05-24 07:10:15 +00:00
|
|
|
function addSingleCountToMap(valueMap: Map<CellValue, IFilterCount>, value: any, label: () => any,
|
|
|
|
displayValue: () => any, isHiddenRow: boolean) {
|
2021-06-17 15:26:43 +00:00
|
|
|
if (!valueMap.has(value)) {
|
2022-05-24 07:10:15 +00:00
|
|
|
valueMap.set(value, { label: label(), count: 0, displayValue: displayValue() });
|
2021-06-17 15:26:43 +00:00
|
|
|
}
|
|
|
|
if (!isHiddenRow) {
|
|
|
|
valueMap.get(value)!.count++;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:14:03 +00:00
|
|
|
function getCount(values: Array<[CellValue, IFilterCount]>) {
|
|
|
|
return values.reduce((acc, val) => acc + val[1].count, 0);
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:17:45 +00:00
|
|
|
const defaultPopupOptions: IPopupOptions = {
|
|
|
|
placement: 'bottom-start',
|
|
|
|
boundaries: 'viewport',
|
|
|
|
trigger: ['click'],
|
|
|
|
};
|
|
|
|
|
2021-05-06 12:23:50 +00:00
|
|
|
interface IColumnFilterMenuOptions extends IPopupOptions {
|
|
|
|
// callback for when the content of the menu is closed by clicking the apply or revert buttons
|
|
|
|
onCloseContent?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:17:45 +00:00
|
|
|
// Helper to attach the column filter menu.
|
2021-11-19 20:30:11 +00:00
|
|
|
export function attachColumnFilterMenu(viewSection: ViewSectionRec, filterInfo: FilterInfo,
|
2021-05-06 12:23:50 +00:00
|
|
|
popupOptions: IColumnFilterMenuOptions): DomElementMethod {
|
2021-04-14 15:17:45 +00:00
|
|
|
const options = {...defaultPopupOptions, ...popupOptions};
|
|
|
|
return (elem) => {
|
|
|
|
const instance = viewSection.viewInstance();
|
|
|
|
if (instance && instance.createFilterMenu) { // Should be set if using BaseView
|
2021-11-19 20:30:11 +00:00
|
|
|
setPopupToCreateDom(elem, ctl =>
|
|
|
|
instance.createFilterMenu(ctl, filterInfo, popupOptions.onCloseContent), options);
|
2021-04-14 15:17:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssMenu = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
min-width: 400px;
|
|
|
|
max-width: 400px;
|
|
|
|
max-height: 90vh;
|
|
|
|
outline: none;
|
|
|
|
background-color: white;
|
2021-03-08 16:31:06 +00:00
|
|
|
padding-top: 0;
|
|
|
|
padding-bottom: 12px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssMenuHeader = styled('div', `
|
2021-03-08 16:31:06 +00:00
|
|
|
height: 40px;
|
2020-10-02 15:10:00 +00:00
|
|
|
flex-shrink: 0;
|
|
|
|
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
|
2021-03-08 16:31:06 +00:00
|
|
|
margin: 0 16px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssSelectAll = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
color: ${colors.lightGreen};
|
|
|
|
cursor: default;
|
|
|
|
user-select: none;
|
2021-03-08 16:31:06 +00:00
|
|
|
&-disabled {
|
|
|
|
color: ${colors.slate};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
const cssDotSeparator = styled('span', `
|
|
|
|
color: ${colors.lightGreen};
|
|
|
|
margin: 0 4px;
|
2021-06-17 15:26:43 +00:00
|
|
|
user-select: none;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssMenuDivider = styled(menuDivider, `
|
|
|
|
flex-shrink: 0;
|
2021-03-08 16:31:06 +00:00
|
|
|
margin: 0;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssItemList = styled('div', `
|
|
|
|
flex-shrink: 1;
|
|
|
|
overflow: auto;
|
|
|
|
min-height: 80px;
|
2021-03-08 16:31:06 +00:00
|
|
|
margin-top: 4px;
|
|
|
|
padding-bottom: 8px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssMenuItem = styled('div', `
|
|
|
|
display: flex;
|
2021-03-08 16:31:06 +00:00
|
|
|
padding: 8px 16px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
2021-07-15 15:50:28 +00:00
|
|
|
export const cssItemValue = styled(cssLabelText, `
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-right: 12px;
|
|
|
|
color: ${colors.dark};
|
2021-07-15 15:50:28 +00:00
|
|
|
white-space: pre;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssItemCount = styled('div', `
|
|
|
|
flex-grow: 1;
|
|
|
|
align-self: normal;
|
|
|
|
text-align: right;
|
|
|
|
color: ${colors.slate};
|
|
|
|
`);
|
|
|
|
const cssMenuFooter = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-shrink: 0;
|
2021-03-08 16:31:06 +00:00
|
|
|
flex-direction: column;
|
2021-03-25 15:14:03 +00:00
|
|
|
padding-top: 4px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
const cssApplyButton = styled(primaryButton, `
|
|
|
|
margin-right: 4px;
|
|
|
|
`);
|
|
|
|
const cssSearch = styled(input, `
|
|
|
|
flex-grow: 1;
|
|
|
|
min-width: 1px;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
|
2021-03-08 16:31:06 +00:00
|
|
|
font-size: ${vars.mediumFontSize};
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
margin: 0px 16px 0px 8px;
|
|
|
|
padding: 0px;
|
|
|
|
border: none;
|
|
|
|
outline: none;
|
|
|
|
|
|
|
|
`);
|
|
|
|
const cssSearchIcon = styled(icon, `
|
|
|
|
flex-shrink: 0;
|
|
|
|
margin-left: auto;
|
|
|
|
margin-right: 4px;
|
|
|
|
`);
|
|
|
|
const cssNoResults = styled(cssMenuItem, `
|
|
|
|
font-style: italic;
|
|
|
|
color: ${colors.slate};
|
|
|
|
justify-content: center;
|
|
|
|
`);
|
2021-06-17 15:26:43 +00:00
|
|
|
const cssSortIcon = styled(icon, `
|
|
|
|
--icon-color: ${colors.slate};
|
|
|
|
margin-left: auto;
|
|
|
|
&-active {
|
|
|
|
--icon-color: ${colors.lightGreen}
|
|
|
|
}
|
|
|
|
`);
|
2021-07-15 15:50:28 +00:00
|
|
|
const cssLabel = styled(cssCheckboxLabel, `
|
|
|
|
align-items: center;
|
|
|
|
font-weight: initial; /* negate bootstrap */
|
|
|
|
`);
|
|
|
|
const cssToken = styled('div', `
|
|
|
|
margin-left: 8px;
|
|
|
|
margin-right: 12px;
|
|
|
|
`);
|
2022-05-24 14:59:12 +00:00
|
|
|
const cssRangeHeader = styled(cssMenuItem, `
|
|
|
|
padding: unset;
|
|
|
|
border-radius: 0 0 3px 0;
|
|
|
|
text-transform: uppercase;
|
|
|
|
font-size: var(--grist-x-small-font-size);
|
|
|
|
margin: 16px 16px 6px 16px;
|
|
|
|
`);
|
|
|
|
const cssRangeContainer = styled(cssMenuItem, `
|
|
|
|
display: flex;
|
|
|
|
justify-content: left;
|
|
|
|
column-gap: 10px;
|
|
|
|
`);
|
|
|
|
const cssRangeInputSeparator = styled('span', `
|
|
|
|
font-weight: 600;
|
|
|
|
position: relative;
|
|
|
|
top: 3px;
|
|
|
|
color: var(--grist-color-slate);
|
|
|
|
`);
|
|
|
|
const cssRangeInput = styled('input', `
|
|
|
|
width: 80px;
|
|
|
|
`);
|