2021-11-03 11:44:28 +00:00
|
|
|
import { reportError } from 'app/client/models/AppModel';
|
|
|
|
import { ColumnRec, DocModel, ViewFieldRec, ViewRec, ViewSectionRec } from 'app/client/models/DocModel';
|
|
|
|
import { CustomComputed } from 'app/client/models/modelUtil';
|
|
|
|
import { attachColumnFilterMenu } from 'app/client/ui/ColumnFilterMenu';
|
|
|
|
import { addFilterMenu } from 'app/client/ui/FilterBar';
|
|
|
|
import { hoverTooltip } from 'app/client/ui/tooltips';
|
|
|
|
import { makeViewLayoutMenu } from 'app/client/ui/ViewLayoutMenu';
|
|
|
|
import { basicButton, primaryButton } from 'app/client/ui2018/buttons';
|
|
|
|
import { colors, vars } from 'app/client/ui2018/cssVars';
|
|
|
|
import { icon } from 'app/client/ui2018/icons';
|
|
|
|
import { menu } from 'app/client/ui2018/menus';
|
|
|
|
import { Sort } from 'app/common/SortSpec';
|
|
|
|
import { Computed, dom, fromKo, IDisposableOwner, makeTestId, Observable, styled } from 'grainjs';
|
|
|
|
import { PopupControl } from 'popweasel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import difference = require('lodash/difference');
|
|
|
|
|
|
|
|
const testId = makeTestId('test-section-menu-');
|
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
const TOOLTIP_DELAY_OPEN = 750;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
async function doSave(docModel: DocModel, viewSection: ViewSectionRec): Promise<void> {
|
|
|
|
await docModel.docData.bundleActions("Update Sort&Filter settings", () => Promise.all([
|
|
|
|
viewSection.activeSortJson.save(), // Save sort
|
|
|
|
viewSection.saveFilters(), // Save filter
|
|
|
|
viewSection.activeFilterBar.save(), // Save bar
|
|
|
|
]));
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
function doRevert(viewSection: ViewSectionRec) {
|
|
|
|
viewSection.activeSortJson.revert(); // Revert sort
|
|
|
|
viewSection.revertFilters(); // Revert filter
|
|
|
|
viewSection.activeFilterBar.revert(); // Revert bar
|
|
|
|
}
|
|
|
|
|
|
|
|
export function viewSectionMenu(owner: IDisposableOwner, docModel: DocModel, viewSection: ViewSectionRec,
|
|
|
|
viewModel: ViewRec, isReadonly: Observable<boolean>) {
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-04-27 19:05:23 +00:00
|
|
|
const popupControls = new WeakMap<ViewFieldRec, PopupControl>();
|
2021-04-30 17:28:52 +00:00
|
|
|
const anyFilter = Computed.create(owner, (use) => Boolean(use(viewSection.filteredFields).length));
|
2021-04-27 19:05:23 +00:00
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
const displaySaveObs: Computed<boolean> = Computed.create(owner, (use) => (
|
|
|
|
use(viewSection.filterSpecChanged)
|
|
|
|
|| !use(viewSection.activeSortJson.isSaved)
|
|
|
|
|| !use(viewSection.activeFilterBar.isSaved)
|
|
|
|
));
|
|
|
|
|
2021-05-06 12:23:50 +00:00
|
|
|
const save = () => { doSave(docModel, viewSection).catch(reportError); };
|
2021-04-30 17:28:52 +00:00
|
|
|
const revert = () => doRevert(viewSection);
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssFilterMenuWrapper(
|
|
|
|
cssFixHeight.cls(''),
|
|
|
|
cssFilterMenuWrapper.cls('-unsaved', displaySaveObs),
|
|
|
|
testId('wrapper'),
|
|
|
|
cssMenu(
|
|
|
|
testId('sortAndFilter'),
|
|
|
|
cssFilterIconWrapper(
|
|
|
|
testId('filter-icon'),
|
|
|
|
cssFilterIconWrapper.cls('-any', anyFilter),
|
|
|
|
cssFilterIcon('Filter')
|
|
|
|
),
|
2021-05-06 12:23:50 +00:00
|
|
|
menu(ctl => [
|
2021-04-30 17:28:52 +00:00
|
|
|
dom.domComputed(use => {
|
|
|
|
use(viewSection.activeSortJson.isSaved); // Rebuild sort panel if sort gets saved. A little hacky.
|
|
|
|
return makeSortPanel(viewSection, use(viewSection.activeSortSpec),
|
|
|
|
(row: number) => docModel.columns.getRowModel(row));
|
|
|
|
}),
|
|
|
|
dom.domComputed(viewSection.filteredFields, fields =>
|
2021-05-06 12:23:50 +00:00
|
|
|
makeFilterPanel(viewSection, fields, popupControls, () => ctl.close())),
|
2021-04-30 17:28:52 +00:00
|
|
|
makeAddFilterButton(viewSection, popupControls),
|
|
|
|
makeFilterBarToggle(viewSection.activeFilterBar),
|
|
|
|
dom.domComputed(displaySaveObs, displaySave => [
|
2020-10-02 15:10:00 +00:00
|
|
|
displaySave ? cssMenuInfoHeader(
|
|
|
|
cssSaveButton('Save', testId('btn-save'),
|
2021-05-06 12:23:50 +00:00
|
|
|
dom.on('click', () => { save(); ctl.close(); }),
|
2021-04-30 17:28:52 +00:00
|
|
|
dom.boolAttr('disabled', isReadonly)),
|
2020-10-02 15:10:00 +00:00
|
|
|
basicButton('Revert', testId('btn-revert'),
|
2021-05-06 12:23:50 +00:00
|
|
|
dom.on('click', () => { revert(); ctl.close(); }))
|
2020-10-02 15:10:00 +00:00
|
|
|
) : null,
|
2021-04-30 17:28:52 +00:00
|
|
|
]),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
dom.maybe(displaySaveObs, () => cssSaveIconsWrapper(
|
|
|
|
cssSmallIconWrapper(
|
|
|
|
cssIcon('Tick'), cssSmallIconWrapper.cls('-green'),
|
|
|
|
dom.on('click', save),
|
|
|
|
hoverTooltip(() => 'Save', {key: 'sortFilterButton', openDelay: TOOLTIP_DELAY_OPEN}),
|
|
|
|
testId('small-btn-save'),
|
2021-05-05 15:15:15 +00:00
|
|
|
dom.hide(isReadonly),
|
2021-04-30 17:28:52 +00:00
|
|
|
),
|
|
|
|
cssSmallIconWrapper(
|
|
|
|
cssIcon('CrossSmall'), cssSmallIconWrapper.cls('-gray'),
|
|
|
|
dom.on('click', revert),
|
|
|
|
hoverTooltip(() => 'Revert', {key: 'sortFilterButton', openDelay: TOOLTIP_DELAY_OPEN}),
|
|
|
|
testId('small-btn-revert'),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
cssMenu(
|
|
|
|
testId('viewLayout'),
|
|
|
|
cssFixHeight.cls(''),
|
|
|
|
cssDotsIconWrapper(cssIcon('Dots')),
|
|
|
|
menu(_ctl => makeViewLayoutMenu(viewModel, viewSection, isReadonly.get()))
|
|
|
|
)
|
|
|
|
];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:44:28 +00:00
|
|
|
function makeSortPanel(section: ViewSectionRec, sortSpec: Sort.SortSpec, getColumn: (row: number) => ColumnRec) {
|
|
|
|
const changedColumns = difference(sortSpec, Sort.parseSortColRefs(section.sortColRefs.peek()));
|
|
|
|
const sortColumns = sortSpec.map(colSpec => {
|
2020-10-02 15:10:00 +00:00
|
|
|
// colRef is a rowId of a column or its negative value (indicating descending order).
|
2021-11-03 11:44:28 +00:00
|
|
|
const col = getColumn(Sort.getColRef(colSpec));
|
2020-10-02 15:10:00 +00:00
|
|
|
return cssMenuText(
|
|
|
|
cssMenuIconWrapper(
|
2021-11-03 11:44:28 +00:00
|
|
|
cssMenuIconWrapper.cls('-changed', changedColumns.includes(colSpec)),
|
|
|
|
cssMenuIconWrapper.cls(Sort.isAscending(colSpec) ? '-asc' : '-desc'),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssIcon('Sort',
|
2021-11-03 11:44:28 +00:00
|
|
|
dom.style('transform', Sort.isAscending(colSpec) ? 'scaleY(-1)' : 'none'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.on('click', () => {
|
2021-11-03 11:44:28 +00:00
|
|
|
section.activeSortSpec(Sort.flipSort(sortSpec, colSpec));
|
2020-10-02 15:10:00 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
cssMenuTextLabel(col.colId()),
|
|
|
|
cssMenuIconWrapper(
|
|
|
|
cssIcon('Remove', testId('btn-remove-sort'), dom.on('click', () => {
|
2021-11-03 11:44:28 +00:00
|
|
|
if (Sort.findCol(sortSpec, colSpec)) {
|
|
|
|
section.activeSortSpec(Sort.removeCol(sortSpec, colSpec));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
),
|
|
|
|
testId('sort-col')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssMenuInfoHeader('Sorted by', testId('heading-sorted')),
|
2021-04-14 15:17:45 +00:00
|
|
|
sortColumns.length > 0 ? sortColumns : cssGrayedMenuText('(Default)')
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:05:23 +00:00
|
|
|
export function makeAddFilterButton(viewSectionRec: ViewSectionRec,
|
|
|
|
popupControls: WeakMap<ViewFieldRec, PopupControl>) {
|
|
|
|
return dom.domComputed((use) => {
|
|
|
|
const fields = use(use(viewSectionRec.viewFields).getObservable());
|
|
|
|
return cssMenuText(
|
|
|
|
cssMenuIconWrapper(
|
|
|
|
cssIcon('Plus'),
|
|
|
|
addFilterMenu(fields, popupControls, {
|
|
|
|
placement: 'bottom-end',
|
|
|
|
// Attach content to triggerElem's parent, which is needed to prevent view section menu to
|
|
|
|
// close when clicking an item of the add filter menu.
|
|
|
|
attach: null
|
|
|
|
}),
|
|
|
|
testId('plus-button'),
|
|
|
|
dom.on('click', (ev) => ev.stopPropagation()),
|
|
|
|
),
|
|
|
|
cssMenuTextLabel('Add Filter'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:17:45 +00:00
|
|
|
export function makeFilterBarToggle(activeFilterBar: CustomComputed<boolean>) {
|
|
|
|
return cssMenuText(
|
|
|
|
cssMenuIconWrapper(
|
|
|
|
testId('btn'),
|
|
|
|
cssMenuIconWrapper.cls('-changed', (use) => !use(activeFilterBar.isSaved)),
|
|
|
|
dom.domComputed((use) => {
|
|
|
|
const filterBar = use(activeFilterBar);
|
|
|
|
const isSaved = use(activeFilterBar.isSaved);
|
|
|
|
return cssIcon(filterBar ? "Tick" : (isSaved ? "Plus" : "CrossSmall"),
|
|
|
|
cssIcon.cls('-green', Boolean(filterBar)),
|
|
|
|
testId('icon'));
|
|
|
|
}),
|
|
|
|
),
|
2021-05-04 10:27:15 +00:00
|
|
|
dom.on('click', () => activeFilterBar(!activeFilterBar.peek())),
|
2021-04-14 15:17:45 +00:00
|
|
|
cssMenuTextLabel("Toggle Filter Bar"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-27 19:05:23 +00:00
|
|
|
function makeFilterPanel(section: ViewSectionRec, filteredFields: ViewFieldRec[],
|
2021-05-06 12:23:50 +00:00
|
|
|
popupControls: WeakMap<ViewFieldRec, PopupControl>,
|
|
|
|
onCloseContent: () => void) {
|
2020-10-02 15:10:00 +00:00
|
|
|
const fields = filteredFields.map(field => {
|
|
|
|
const fieldChanged = Computed.create(null, fromKo(field.activeFilter.isSaved), (_use, isSaved) => !isSaved);
|
|
|
|
return cssMenuText(
|
|
|
|
dom.autoDispose(fieldChanged),
|
|
|
|
cssMenuIconWrapper(
|
|
|
|
cssMenuIconWrapper.cls('-changed', fieldChanged),
|
|
|
|
cssIcon('FilterSimple'),
|
2021-04-27 19:05:23 +00:00
|
|
|
attachColumnFilterMenu(section, field, {
|
|
|
|
placement: 'bottom-end',
|
|
|
|
trigger: ['click', (_el, popupControl) => popupControls.set(field, popupControl)],
|
2021-05-06 12:23:50 +00:00
|
|
|
onCloseContent,
|
2021-04-27 19:05:23 +00:00
|
|
|
}),
|
2021-04-14 15:17:45 +00:00
|
|
|
testId('filter-icon'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
cssMenuTextLabel(field.label()),
|
2021-04-14 15:17:45 +00:00
|
|
|
cssMenuIconWrapper(cssIcon('Remove', testId('btn-remove-filter')), dom.on('click', () => field.activeFilter(''))),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('filter-col')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssMenuInfoHeader('Filtered by', {style: 'margin-top: 4px'}, testId('heading-filtered')),
|
2021-04-14 15:17:45 +00:00
|
|
|
filteredFields.length > 0 ? fields : cssGrayedMenuText('(Not filtered)')
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
const clsOldUI = styled('div', ``);
|
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
const cssFixHeight = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-top: -3px; /* Section header is 24px, so need to move this up a little bit */
|
2021-04-30 17:28:52 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenu = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
display: inline-flex;
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
border-radius: 3px;
|
|
|
|
border: 1px solid transparent;
|
|
|
|
&.${clsOldUI.className} {
|
|
|
|
margin-top: 0px;
|
|
|
|
border-radius: 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover, &.weasel-popup-open {
|
|
|
|
background-color: ${colors.mediumGrey};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssIconWrapper = styled('div', `
|
|
|
|
padding: 3px;
|
|
|
|
border-radius: 3px;
|
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuIconWrapper = styled(cssIconWrapper, `
|
2021-04-14 15:17:45 +00:00
|
|
|
display: flex;
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: -3px 0;
|
|
|
|
width: 22px;
|
|
|
|
height: 22px;
|
|
|
|
|
|
|
|
&:hover, &.weasel-popup-open {
|
|
|
|
background-color: ${colors.mediumGrey};
|
|
|
|
}
|
|
|
|
&-changed {
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
}
|
|
|
|
&-changed:hover, &-changed:hover.weasel-popup-open {
|
|
|
|
background-color: ${colors.darkGreen};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
const cssFilterMenuWrapper = styled('div', `
|
|
|
|
display: inline-flex;
|
|
|
|
margin-right: 10px;
|
|
|
|
border-radius: 3px;
|
|
|
|
align-items: center;
|
|
|
|
&-unsaved {
|
|
|
|
border: 1px solid ${colors.lightGreen};
|
|
|
|
}
|
|
|
|
& .${cssMenu.className} {
|
|
|
|
border: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssIcon = styled(icon, `
|
|
|
|
flex: none;
|
|
|
|
cursor: pointer;
|
|
|
|
background-color: ${colors.slate};
|
|
|
|
|
|
|
|
.${cssMenuIconWrapper.className}-changed & {
|
|
|
|
background-color: white;
|
|
|
|
}
|
|
|
|
|
|
|
|
.${clsOldUI.className} & {
|
|
|
|
background-color: white;
|
|
|
|
}
|
2021-04-14 15:17:45 +00:00
|
|
|
|
|
|
|
&-green {
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssDotsIconWrapper = styled(cssIconWrapper, `
|
|
|
|
border-radius: 0px 2px 2px 0px;
|
|
|
|
|
|
|
|
.${clsOldUI.className} & {
|
|
|
|
border-radius: 0px;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssFilterIconWrapper = styled(cssIconWrapper, `
|
|
|
|
border-radius: 2px 0px 0px 2px;
|
2021-04-30 17:28:52 +00:00
|
|
|
.${cssFilterMenuWrapper.className}-unsaved & {
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssFilterIcon = styled(cssIcon, `
|
2021-04-30 17:28:52 +00:00
|
|
|
.${cssFilterIconWrapper.className}-any & {
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
}
|
|
|
|
.${cssFilterMenuWrapper.className}-unsaved & {
|
|
|
|
background-color: white;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuInfoHeader = styled('div', `
|
|
|
|
font-weight: ${vars.bigControlTextWeight};
|
|
|
|
padding: 8px 24px 8px 24px;
|
|
|
|
cursor: default;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuText = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
padding: 0px 24px 8px 24px;
|
|
|
|
cursor: default;
|
2021-04-14 15:17:45 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssGrayedMenuText = styled(cssMenuText, `
|
|
|
|
color: ${colors.slate};
|
|
|
|
padding-left: 24px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuTextLabel = styled('span', `
|
|
|
|
flex-grow: 1;
|
|
|
|
padding: 0 4px;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSaveButton = styled(primaryButton, `
|
|
|
|
margin-right: 8px;
|
|
|
|
`);
|
2021-04-30 17:28:52 +00:00
|
|
|
|
|
|
|
const cssSmallIconWrapper = styled('div', `
|
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
border-radius: 8px;
|
2021-05-05 15:15:15 +00:00
|
|
|
margin: 0 5px 0 5px;
|
|
|
|
|
2021-04-30 17:28:52 +00:00
|
|
|
&-green {
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
}
|
|
|
|
&-gray {
|
|
|
|
background-color: ${colors.slate};
|
|
|
|
}
|
|
|
|
& > .${cssIcon.className} {
|
|
|
|
background-color: white;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
const cssSaveIconsWrapper = styled('div', `
|
2021-05-05 15:15:15 +00:00
|
|
|
padding: 0 1px 0 1px;
|
2021-04-30 17:28:52 +00:00
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
`);
|