2021-11-05 10:25:05 +00:00
|
|
|
import { Command } from 'app/client/components/commands';
|
2023-08-04 12:33:33 +00:00
|
|
|
import { FocusLayer } from 'app/client/lib/FocusLayer';
|
2022-10-28 16:11:08 +00:00
|
|
|
import { makeT } from 'app/client/lib/localization';
|
2021-11-05 10:25:05 +00:00
|
|
|
import { NeedUpgradeError, reportError } from 'app/client/models/errors';
|
2022-07-26 17:49:35 +00:00
|
|
|
import { textButton } from 'app/client/ui2018/buttons';
|
2021-11-05 10:25:05 +00:00
|
|
|
import { cssCheckboxSquare, cssLabel, cssLabelText } from 'app/client/ui2018/checkbox';
|
2022-09-06 01:51:57 +00:00
|
|
|
import { testId, theme, vars } from 'app/client/ui2018/cssVars';
|
2021-11-05 10:25:05 +00:00
|
|
|
import { IconName } from 'app/client/ui2018/IconList';
|
|
|
|
import { icon } from 'app/client/ui2018/icons';
|
|
|
|
import { cssSelectBtn } from 'app/client/ui2018/select';
|
|
|
|
import { BindableValue, Computed, dom, DomElementArg, DomElementMethod, IDomArgs,
|
|
|
|
MaybeObsArray, MutableObsArray, Observable, styled } from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as weasel from 'popweasel';
|
|
|
|
|
2022-12-09 15:46:03 +00:00
|
|
|
const t = makeT('menus');
|
2022-10-28 16:11:08 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export interface IOptionFull<T> {
|
|
|
|
value: T;
|
|
|
|
label: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
icon?: IconName;
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:15:27 +00:00
|
|
|
let _lastOpenedController: weasel.IOpenController|null = null;
|
|
|
|
|
|
|
|
// Close opened menu if any, otherwise do nothing.
|
2022-09-01 08:07:15 +00:00
|
|
|
// WARN: current implementation does not handle submenus correctly. Does not seem a problem as of
|
|
|
|
// today though, as there is no submenu in UI.
|
2022-02-16 14:15:27 +00:00
|
|
|
export function closeRegisteredMenu() {
|
|
|
|
if (_lastOpenedController) { _lastOpenedController.close(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register `ctl` to make sure it is closed when `closeMenu()` is called.
|
|
|
|
export function registerMenuOpen(ctl: weasel.IOpenController) {
|
|
|
|
_lastOpenedController = ctl;
|
|
|
|
ctl.onDispose(() => _lastOpenedController = null);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// For string options, we can use a string for label and value without wrapping into an object.
|
|
|
|
export type IOption<T> = (T & string) | IOptionFull<T>;
|
|
|
|
|
|
|
|
export function menu(createFunc: weasel.MenuCreateFunc, options?: weasel.IMenuOptions): DomElementMethod {
|
2022-02-16 14:15:27 +00:00
|
|
|
const wrappedCreateFunc = (ctl: weasel.IOpenController) => {
|
|
|
|
registerMenuOpen(ctl);
|
|
|
|
return createFunc(ctl);
|
|
|
|
};
|
|
|
|
return weasel.menu(wrappedCreateFunc, {...defaults, ...options});
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Weasel doesn't allow other options for submenus, but probably should.
|
|
|
|
export type ISubMenuOptions = weasel.ISubMenuOptions & weasel.IPopupOptions;
|
|
|
|
|
|
|
|
export function menuItemSubmenu(
|
|
|
|
submenu: weasel.MenuCreateFunc,
|
|
|
|
options: ISubMenuOptions,
|
|
|
|
...args: DomElementArg[]
|
|
|
|
): Element {
|
|
|
|
return weasel.menuItemSubmenu(submenu, {...defaults, ...options}, ...args);
|
|
|
|
}
|
|
|
|
|
2022-02-07 14:09:23 +00:00
|
|
|
export const cssMenuElem = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
font-family: ${vars.fontFamily};
|
|
|
|
font-size: ${vars.mediumFontSize};
|
|
|
|
line-height: initial;
|
|
|
|
max-width: 400px;
|
|
|
|
padding: 8px 0px 16px 0px;
|
2022-09-06 01:51:57 +00:00
|
|
|
box-shadow: 0 2px 20px 0 ${theme.menuShadow};
|
2020-10-02 15:10:00 +00:00
|
|
|
min-width: 160px;
|
2023-04-20 13:07:45 +00:00
|
|
|
z-index: ${vars.menuZIndex};
|
2022-09-06 01:51:57 +00:00
|
|
|
--weaseljs-selected-background-color: ${theme.menuItemSelectedBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
--weaseljs-menu-item-padding: 8px 24px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBg};
|
2020-10-09 21:39:13 +00:00
|
|
|
|
|
|
|
@media print {
|
|
|
|
& {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const menuItemStyle = `
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: center;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemFg};
|
|
|
|
--icon-color: ${theme.accentIcon};
|
2020-10-02 15:10:00 +00:00
|
|
|
.${weasel.cssMenuItem.className}-sel {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemSelectedFg};
|
|
|
|
--icon-color: ${theme.menuItemSelectedFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
&.disabled {
|
|
|
|
cursor: default;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemDisabledFg};
|
|
|
|
--icon-color: ${theme.menuItemDisabledFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
export const menuCssClass = cssMenuElem.className;
|
|
|
|
|
|
|
|
// Add grist-floating-menu class to support existing browser tests
|
|
|
|
const defaults = { menuCssClass: menuCssClass + ' grist-floating-menu' };
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
export interface SelectOptions<T> extends weasel.ISelectUserOptions {
|
|
|
|
/** Additional DOM element args to pass to each select option. */
|
|
|
|
renderOptionArgs?: (option: IOptionFull<T | null>) => DomElementArg;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Creates a select dropdown widget. The observable `obs` reflects the value of the selected
|
|
|
|
* option, and `optionArray` is an array (regular or observable) of option values and labels.
|
|
|
|
* These may be either strings, or {label, value, icon, disabled} objects. Icons are optional
|
|
|
|
* and must be IconName strings from 'app/client/ui2018/IconList'.
|
|
|
|
*
|
|
|
|
* The type of value may be any type at all; it is opaque to this widget.
|
|
|
|
*
|
|
|
|
* If obs is set to an invalid or disabled value, then defLabel option is used to determine the
|
|
|
|
* label that the select box will show, blank by default.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* const fruit = observable("apple");
|
|
|
|
* select(fruit, ["apple", "banana", "mango"]);
|
|
|
|
*
|
|
|
|
* const employee = observable(17);
|
|
|
|
* const allEmployees = Observable.create(owner, [
|
|
|
|
* {value: 12, label: "Bob", disabled: true},
|
|
|
|
* {value: 17, label: "Alice"},
|
|
|
|
* {value: 21, label: "Eve"},
|
|
|
|
* ]);
|
|
|
|
* select(employee, allEmployees, {defLabel: "Select employee:"});
|
|
|
|
*
|
2022-12-20 02:06:39 +00:00
|
|
|
* const name = observable("alice");
|
|
|
|
* const names = ["alice", "bob", "carol"];
|
|
|
|
* select(name, names, {renderOptionArgs: (op) => console.log(`Rendered option ${op.value}`)});
|
|
|
|
*
|
2020-10-02 15:10:00 +00:00
|
|
|
* Note that this select element is not compatible with browser address autofill for usage in
|
|
|
|
* forms, and that formSelect should be used for this purpose.
|
|
|
|
*/
|
|
|
|
export function select<T>(obs: Observable<T>, optionArray: MaybeObsArray<IOption<T>>,
|
2022-12-20 02:06:39 +00:00
|
|
|
options: SelectOptions<T> = {}) {
|
|
|
|
const {renderOptionArgs, ...weaselOptions} = options;
|
2020-10-02 15:10:00 +00:00
|
|
|
const _menu = cssSelectMenuElem(testId('select-menu'));
|
|
|
|
const _btn = cssSelectBtn(testId('select-open'));
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
const {menuCssClass: menuClass, ...otherOptions} = weaselOptions;
|
2020-10-02 15:10:00 +00:00
|
|
|
const selectOptions = {
|
|
|
|
buttonArrow: cssInlineCollapseIcon('Collapse'),
|
2021-08-12 18:06:40 +00:00
|
|
|
menuCssClass: _menu.className + ' ' + (menuClass || ''),
|
2020-10-02 15:10:00 +00:00
|
|
|
buttonCssClass: _btn.className,
|
2021-08-12 18:06:40 +00:00
|
|
|
...otherOptions,
|
2020-10-02 15:10:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return weasel.select(obs, optionArray, selectOptions, (op) =>
|
|
|
|
cssOptionRow(
|
|
|
|
op.icon ? cssOptionRowIcon(op.icon) : null,
|
2023-03-02 14:12:49 +00:00
|
|
|
cssOptionLabel(t(op.label)),
|
2022-12-20 02:06:39 +00:00
|
|
|
renderOptionArgs ? renderOptionArgs(op) : null,
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('select-row')
|
|
|
|
)
|
|
|
|
) as HTMLElement; // TODO: should be changed in weasel
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as select(), but the main element looks like a link rather than a button.
|
|
|
|
*/
|
|
|
|
export function linkSelect<T>(obs: Observable<T>, optionArray: MaybeObsArray<IOption<T>>,
|
|
|
|
options: weasel.ISelectUserOptions = {}) {
|
|
|
|
const _btn = cssSelectBtnLink(testId('select-open'));
|
|
|
|
const elem = select(obs, optionArray, {buttonCssClass: _btn.className, ...options});
|
|
|
|
// It feels strange to have focus stay on this link; remove tabIndex that makes it focusable.
|
|
|
|
elem.removeAttribute('tabIndex');
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2021-09-15 06:12:34 +00:00
|
|
|
export interface IMultiSelectUserOptions {
|
|
|
|
placeholder?: string;
|
|
|
|
error?: Observable<boolean>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a select dropdown widget that supports selecting multiple options.
|
|
|
|
*
|
|
|
|
* The observable array `selectedOptions` reflects the selected options, and
|
|
|
|
* `availableOptions` is an array (normal or observable) of selectable options.
|
|
|
|
* These may either be strings, or {label, value} objects.
|
|
|
|
*/
|
|
|
|
export function multiSelect<T>(selectedOptions: MutableObsArray<T>,
|
|
|
|
availableOptions: MaybeObsArray<IOption<T>>,
|
|
|
|
options: IMultiSelectUserOptions = {},
|
|
|
|
...domArgs: DomElementArg[]) {
|
|
|
|
const selectedOptionsSet = Computed.create(null, selectedOptions, (_use, opts) => new Set(opts));
|
|
|
|
|
|
|
|
const selectedOptionsText = Computed.create(null, selectedOptionsSet, (use, selectedOpts) => {
|
|
|
|
if (selectedOpts.size === 0) {
|
2022-12-06 13:35:10 +00:00
|
|
|
return options.placeholder ?? t("Select fields");
|
2021-09-15 06:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const optionArray = Array.isArray(availableOptions) ? availableOptions : use(availableOptions);
|
|
|
|
return optionArray
|
|
|
|
.filter(opt => selectedOpts.has(weasel.getOptionFull(opt).value))
|
|
|
|
.map(opt => weasel.getOptionFull(opt).label)
|
|
|
|
.join(', ');
|
|
|
|
});
|
|
|
|
|
|
|
|
function buildMultiSelectMenu(ctl: weasel.IOpenController) {
|
|
|
|
return cssMultiSelectMenu(
|
|
|
|
{ tabindex: '-1' }, // Allow menu to be focused.
|
|
|
|
dom.cls(menuCssClass),
|
2023-08-04 12:33:33 +00:00
|
|
|
FocusLayer.attach({pauseMousetrap: true}),
|
2021-09-15 06:12:34 +00:00
|
|
|
dom.onKeyDown({
|
|
|
|
Enter: () => ctl.close(),
|
|
|
|
Escape: () => ctl.close()
|
|
|
|
}),
|
|
|
|
elem => {
|
|
|
|
// Set focus on open, so that keyboard events work.
|
|
|
|
setTimeout(() => elem.focus(), 0);
|
|
|
|
|
|
|
|
// Sets menu width to match parent container (button) width.
|
|
|
|
const style = elem.style;
|
|
|
|
style.minWidth = ctl.getTriggerElem().getBoundingClientRect().width + 'px';
|
|
|
|
style.marginLeft = style.marginRight = '0';
|
|
|
|
},
|
|
|
|
dom.domComputed(selectedOptionsSet, selectedOpts => {
|
|
|
|
return dom.forEach(availableOptions, option => {
|
|
|
|
const fullOption = weasel.getOptionFull(option);
|
|
|
|
return cssCheckboxLabel(
|
|
|
|
cssCheckboxSquare(
|
|
|
|
{type: 'checkbox'},
|
|
|
|
dom.prop('checked', selectedOpts.has(fullOption.value)),
|
|
|
|
dom.on('change', (_ev, elem) => {
|
|
|
|
if (elem.checked) {
|
|
|
|
selectedOptions.push(fullOption.value);
|
|
|
|
} else {
|
|
|
|
selectedOpts.delete(fullOption.value);
|
|
|
|
selectedOptions.set([...selectedOpts]);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
dom.style('position', 'relative'),
|
|
|
|
testId('multi-select-menu-option-checkbox')
|
|
|
|
),
|
|
|
|
cssCheckboxText(fullOption.label, testId('multi-select-menu-option-text')),
|
|
|
|
testId('multi-select-menu-option')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
testId('multi-select-menu')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssSelectBtn(
|
|
|
|
dom.autoDispose(selectedOptionsSet),
|
|
|
|
dom.autoDispose(selectedOptionsText),
|
2021-11-09 20:03:12 +00:00
|
|
|
cssMultiSelectSummary(
|
|
|
|
dom.text(selectedOptionsText),
|
|
|
|
cssMultiSelectSummary.cls('-placeholder', use => use(selectedOptionsSet).size === 0)
|
|
|
|
),
|
2021-09-15 06:12:34 +00:00
|
|
|
icon('Dropdown'),
|
|
|
|
elem => {
|
|
|
|
weasel.setPopupToCreateDom(elem, ctl => buildMultiSelectMenu(ctl), weasel.defaultMenuOptions);
|
|
|
|
},
|
|
|
|
dom.style('border', use => {
|
2022-09-06 01:51:57 +00:00
|
|
|
return options.error && use(options.error)
|
|
|
|
? `1px solid ${theme.selectButtonBorderInvalid}`
|
|
|
|
: `1px solid ${theme.selectButtonBorder}`;
|
2021-09-15 06:12:34 +00:00
|
|
|
}),
|
|
|
|
...domArgs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Creates a select dropdown widget that is more ideal for forms. Implemented using the <select>
|
|
|
|
* element to work with browser form autofill and typing in the desired value to quickly set it.
|
|
|
|
* The appearance of the opened menu is OS dependent.
|
|
|
|
*
|
|
|
|
* The observable `obs` reflects the value of the selected option, and `optionArray` is an
|
|
|
|
* array (regular or observable) of option values and labels. These may be either strings,
|
|
|
|
* or {label, value} objects.
|
|
|
|
*
|
|
|
|
* If obs is set to an empty string value, then defLabel option is used to determine the
|
|
|
|
* label that the select box will show, blank by default.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* const fruit = observable("");
|
|
|
|
* formSelect(fruit, ["apple", "banana", "mango"], {defLabel: "Select fruit:"});
|
|
|
|
*/
|
|
|
|
export function formSelect(obs: Observable<string>, optionArray: MaybeObsArray<IOption<string>>,
|
|
|
|
options: {defaultLabel?: string} = {}) {
|
|
|
|
const {defaultLabel = ""} = options;
|
|
|
|
const container: Element = cssSelectBtnContainer(
|
|
|
|
dom('select', {class: cssSelectBtn.className, style: 'height: 42px; padding: 12px 30px 12px 12px;'},
|
|
|
|
dom.prop('value', obs),
|
|
|
|
dom.on('change', (_, elem) => { obs.set(elem.value); }),
|
|
|
|
dom('option', {value: '', hidden: 'hidden'}, defaultLabel),
|
|
|
|
dom.forEach(optionArray, (option) => {
|
|
|
|
const obj: weasel.IOptionFull<string> = weasel.getOptionFull(option);
|
|
|
|
return dom('option', {value: obj.value}, obj.label);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
cssCollapseIcon('Collapse')
|
|
|
|
);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function inputMenu(createFunc: weasel.MenuCreateFunc, options?: weasel.IMenuOptions): DomElementMethod {
|
|
|
|
// Triggers the input menu on 'input' events, if the input has text inside.
|
|
|
|
function inputTrigger(triggerElem: Element, ctl: weasel.PopupControl): void {
|
|
|
|
dom.onElem(triggerElem, 'input', () => {
|
|
|
|
(triggerElem as HTMLInputElement).value.length > 0 ? ctl.open() : ctl.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return weasel.inputMenu(createFunc, {
|
|
|
|
trigger: [inputTrigger],
|
|
|
|
menuCssClass: `${cssMenuElem.className} ${cssInputButtonMenuElem.className}`,
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// A menu item that leads to the billing page if the desired operation requires an upgrade.
|
|
|
|
// Such menu items are marked with a little sparkle unicode.
|
|
|
|
export function upgradableMenuItem(needUpgrade: boolean, action: () => void, ...rem: any[]) {
|
|
|
|
if (needUpgrade) {
|
|
|
|
return menuItem(() => reportError(new NeedUpgradeError()), ...rem, " *");
|
|
|
|
} else {
|
|
|
|
return menuItem(action, ...rem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:49:35 +00:00
|
|
|
export function upgradeText(needUpgrade: boolean, onClick: () => void) {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (!needUpgrade) { return null; }
|
2022-12-06 13:35:10 +00:00
|
|
|
return menuText(dom('span', t("* Workspaces are available on team plans. "),
|
|
|
|
cssUpgradeTextButton(t("Upgrade now"), dom.on('click', () => onClick()))));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an autocomplete element and tie it to an input or textarea element.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* const employees = ['Thomas', 'June', 'Bethany', 'Mark', 'Marjorey', 'Zachary'];
|
|
|
|
* const inputElem = input(...);
|
|
|
|
* autocomplete(inputElem, employees);
|
|
|
|
*/
|
|
|
|
export function autocomplete(
|
|
|
|
inputElem: HTMLInputElement,
|
|
|
|
choices: MaybeObsArray<string>,
|
2021-09-15 06:12:34 +00:00
|
|
|
options: weasel.IAutocompleteOptions = {}
|
2020-10-02 15:10:00 +00:00
|
|
|
) {
|
|
|
|
return weasel.autocomplete(inputElem, choices, {
|
|
|
|
...defaults, ...options,
|
2020-12-21 22:14:40 +00:00
|
|
|
menuCssClass: defaults.menuCssClass + ' ' + cssSelectMenuElem.className + ' ' + (options.menuCssClass || '')
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
/**
|
|
|
|
* Creates simple (not reactive) static menu that looks like a select-box.
|
|
|
|
* Primary usage is for menus, where you want to control how the options and a default
|
|
|
|
* label will look. Label is not updated or changed when one of the option is clicked, for those
|
|
|
|
* use cases use a select component.
|
|
|
|
* Icons are optional, can use custom elements instead of labels and options.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* selectMenu(selectTitle("Title", "Script"), () => [
|
|
|
|
* selectOption(() => ..., "Option1", "Database"),
|
|
|
|
* selectOption(() => ..., "Option2", "Script"),
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* // Control disabled state (if the menu will be opened or not)
|
|
|
|
*
|
|
|
|
* const disabled = observable(false);
|
|
|
|
* selectMenu(selectTitle("Title", "Script"), () => [
|
|
|
|
* selectOption(() => ..., "Option1", "Database"),
|
|
|
|
* selectOption(() => ..., "Option2", "Script"),
|
|
|
|
* ], disabled);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function selectMenu(
|
|
|
|
label: DomElementArg,
|
|
|
|
items: () => DomElementArg[],
|
|
|
|
...args: IDomArgs<HTMLDivElement>
|
|
|
|
) {
|
2021-11-30 08:59:04 +00:00
|
|
|
const _menu = cssSelectMenuElem(testId('select-menu'));
|
2021-11-05 10:25:05 +00:00
|
|
|
return cssSelectBtn(
|
|
|
|
label,
|
|
|
|
icon('Dropdown'),
|
|
|
|
menu(
|
|
|
|
items,
|
|
|
|
{
|
|
|
|
...weasel.defaultMenuOptions,
|
2021-11-30 08:59:04 +00:00
|
|
|
menuCssClass: _menu.className + ' grist-floating-menu',
|
2021-11-05 10:25:05 +00:00
|
|
|
stretchToSelector : `.${cssSelectBtn.className}`,
|
|
|
|
trigger : [(triggerElem, ctl) => {
|
|
|
|
const isDisabled = () => triggerElem.classList.contains('disabled');
|
|
|
|
dom.onElem(triggerElem, 'click', () => isDisabled() || ctl.toggle());
|
|
|
|
dom.onKeyElem(triggerElem as HTMLElement, 'keydown', {
|
|
|
|
ArrowDown: () => isDisabled() || ctl.open(),
|
|
|
|
ArrowUp: () => isDisabled() || ctl.open()
|
|
|
|
});
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
),
|
|
|
|
...args,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function selectTitle(label: BindableValue<string>, iconName?: BindableValue<IconName>) {
|
|
|
|
return cssOptionRow(
|
|
|
|
iconName ? dom.domComputed(iconName, (name) => cssOptionRowIcon(name)) : null,
|
|
|
|
dom.text(label)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function selectOption(
|
|
|
|
action: (item: HTMLElement) => void,
|
|
|
|
label: BindableValue<string>,
|
2022-01-25 09:38:21 +00:00
|
|
|
iconName?: BindableValue<IconName>,
|
|
|
|
...args: IDomArgs<HTMLElement>) {
|
|
|
|
return menuItem(action, selectTitle(label, iconName), ...args);
|
2021-11-05 10:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export const menuSubHeader = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuSubheaderFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
font-size: ${vars.xsmallFontSize};
|
|
|
|
text-transform: uppercase;
|
|
|
|
font-weight: ${vars.bigControlTextWeight};
|
|
|
|
padding: 8px 24px 16px 24px;
|
|
|
|
cursor: default;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const menuText = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
font-size: ${vars.smallFontSize};
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuText};
|
2020-10-02 15:10:00 +00:00
|
|
|
padding: 8px 24px 4px 24px;
|
|
|
|
max-width: 250px;
|
|
|
|
cursor: default;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const menuItem = styled(weasel.menuItem, menuItemStyle);
|
|
|
|
|
|
|
|
export const menuItemLink = styled(weasel.menuItemLink, menuItemStyle);
|
|
|
|
|
2020-12-04 23:29:29 +00:00
|
|
|
/**
|
|
|
|
* A version of menuItem which runs the action on next tick, allowing the menu to close even when
|
|
|
|
* the action causes the disabling of the element being clicked.
|
|
|
|
* TODO disabling the element should not prevent the menu from closing; once fixed in weasel, this
|
|
|
|
* can be removed.
|
|
|
|
*/
|
|
|
|
export const menuItemAsync: typeof weasel.menuItem = function(action, ...args) {
|
|
|
|
return menuItem(() => setTimeout(action, 0), ...args);
|
|
|
|
};
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export function menuItemCmd(cmd: Command, label: string, ...args: DomElementArg[]) {
|
|
|
|
return menuItem(
|
|
|
|
cmd.run,
|
|
|
|
dom('span', label, testId('cmd-name')),
|
|
|
|
cmd.humanKeys.length ? cssCmdKey(cmd.humanKeys[0]) : null,
|
|
|
|
cssMenuItemCmd.cls(''), // overrides some menu item styles
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:17:25 +00:00
|
|
|
export function menuAnnotate(text: string, ...args: DomElementArg[]) {
|
|
|
|
return cssAnnotateMenuItem(text, ...args);
|
2020-12-09 05:59:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export const menuDivider = styled(weasel.cssMenuDivider, `
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: 8px 0;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const menuIcon = styled(icon, `
|
|
|
|
flex: none;
|
|
|
|
margin-right: 8px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSelectMenuElem = styled(cssMenuElem, `
|
|
|
|
max-height: 400px;
|
|
|
|
overflow-y: auto;
|
|
|
|
|
|
|
|
--weaseljs-menu-item-padding: 8px 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSelectBtnContainer = styled('div', `
|
|
|
|
position: relative;
|
|
|
|
width: 100%;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSelectBtnLink = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
font-size: ${vars.mediumFontSize};
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.controlFg};
|
|
|
|
--icon-color: ${theme.controlFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
width: initial;
|
|
|
|
height: initial;
|
|
|
|
line-height: inherit;
|
|
|
|
background-color: initial;
|
|
|
|
padding: initial;
|
|
|
|
border: initial;
|
|
|
|
border-radius: initial;
|
|
|
|
box-shadow: initial;
|
|
|
|
cursor: pointer;
|
|
|
|
outline: none;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
|
|
|
|
&:hover, &:focus, &:active {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.controlHoverFg};
|
|
|
|
--icon-color: ${theme.controlHoverFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
box-shadow: initial;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
export const cssOptionRow = styled('span', `
|
2020-10-02 15:10:00 +00:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
width: 100%;
|
|
|
|
`);
|
|
|
|
|
2023-08-04 12:33:33 +00:00
|
|
|
export const cssOptionRowIcon = styled(icon, `
|
|
|
|
height: 16px;
|
|
|
|
width: 16px;
|
|
|
|
background-color: var(--icon-color, ${theme.menuItemIconFg});
|
|
|
|
margin: -3px 8px 0 2px;
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: 0 8px 0 0;
|
|
|
|
flex: none;
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}-sel & {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuItemSelectedFg};
|
|
|
|
}
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}.disabled & {
|
|
|
|
background-color: ${theme.menuItemDisabledFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2023-10-06 09:11:37 +00:00
|
|
|
export const cssOptionLabel = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
2023-10-06 09:11:37 +00:00
|
|
|
--grist-option-label-color: ${theme.menuItemFg};
|
|
|
|
--grist-option-label-color-sel: ${theme.menuItemSelectedFg};
|
|
|
|
--grist-option-label-color-disabled: ${theme.menuItemDisabledFg};
|
2022-09-06 01:51:57 +00:00
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className} & {
|
2023-10-06 09:11:37 +00:00
|
|
|
color: var(--grist-option-label-color);
|
2022-09-06 01:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}-sel & {
|
2023-10-06 09:11:37 +00:00
|
|
|
color: var(--grist-option-label-color-sel);
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuItemSelectedBg};
|
|
|
|
}
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}.disabled & {
|
2023-10-06 09:11:37 +00:00
|
|
|
color: var(--grist-option-label-color-disabled);
|
2022-09-06 01:51:57 +00:00
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssInlineCollapseIcon = styled(icon, `
|
|
|
|
margin: 0 2px;
|
|
|
|
pointer-events: none;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCollapseIcon = styled(icon, `
|
|
|
|
position: absolute;
|
|
|
|
right: 12px;
|
|
|
|
top: calc(50% - 8px);
|
|
|
|
pointer-events: none;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.selectButtonFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssInputButtonMenuElem = styled(cssMenuElem, `
|
|
|
|
padding: 4px 0px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuItemCmd = styled('div', `
|
|
|
|
justify-content: space-between;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCmdKey = styled('span', `
|
|
|
|
margin-left: 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemIconFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-right: -12px;
|
2022-05-03 05:20:31 +00:00
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}-sel > & {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemIconSelectedFg};
|
|
|
|
}
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}.disabled > & {
|
|
|
|
color: ${theme.menuItemDisabledFg};
|
2022-05-03 05:20:31 +00:00
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
2020-12-09 05:59:42 +00:00
|
|
|
|
|
|
|
const cssAnnotateMenuItem = styled('span', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.accentText};
|
2020-12-09 05:59:42 +00:00
|
|
|
text-transform: uppercase;
|
|
|
|
font-size: 8px;
|
|
|
|
vertical-align: super;
|
|
|
|
margin-top: -4px;
|
|
|
|
margin-left: 4px;
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
|
|
.${weasel.cssMenuItem.className}-sel > & {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.menuItemIconSelectedFg};
|
2020-12-09 05:59:42 +00:00
|
|
|
}
|
|
|
|
`);
|
2021-09-15 06:12:34 +00:00
|
|
|
|
|
|
|
const cssMultiSelectSummary = styled('div', `
|
|
|
|
flex: 1 1 0px;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.selectButtonFg};
|
2021-11-09 20:03:12 +00:00
|
|
|
|
|
|
|
&-placeholder {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.selectButtonPlaceholderFg};
|
2021-11-09 20:03:12 +00:00
|
|
|
}
|
2021-09-15 06:12:34 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMultiSelectMenu = styled(weasel.cssMenu, `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
max-height: calc(max(300px, 95vh - 300px));
|
|
|
|
max-width: 400px;
|
|
|
|
padding-bottom: 0px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBg};
|
2021-09-15 06:12:34 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCheckboxLabel = styled(cssLabel, `
|
|
|
|
padding: 8px 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCheckboxText = styled(cssLabelText, `
|
|
|
|
margin-right: 12px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2021-09-15 06:12:34 +00:00
|
|
|
white-space: pre;
|
|
|
|
`);
|
2022-07-26 17:49:35 +00:00
|
|
|
|
|
|
|
const cssUpgradeTextButton = styled(textButton, `
|
|
|
|
font-size: ${vars.smallFontSize};
|
|
|
|
`);
|