2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* UI 2018 Checkboxes
|
|
|
|
*
|
|
|
|
* Includes:
|
|
|
|
* - squareCheckbox
|
|
|
|
* - circleCheckbox
|
|
|
|
* - labeledSquareCheckbox
|
|
|
|
* - labeledCircleCheckbox
|
|
|
|
*
|
|
|
|
* Checkboxes support passing in DomElementArgs, which can be used to register click handlers, set
|
|
|
|
* the disabled property, and other HTML <input> element behaviors.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* squareCheckbox(observable(true)),
|
|
|
|
* labeledSquareCheckbox(observable(false), 'Include other values', dom.prop('disabled', true)),
|
|
|
|
*/
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
import { theme } from 'app/client/ui2018/cssVars';
|
2021-06-01 13:19:44 +00:00
|
|
|
import { Computed, dom, DomArg, styled } from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { Observable } from 'grainjs';
|
|
|
|
|
|
|
|
export const cssLabel = styled('label', `
|
|
|
|
position: relative;
|
|
|
|
display: inline-flex;
|
|
|
|
min-width: 0px;
|
|
|
|
margin-bottom: 0px;
|
|
|
|
|
|
|
|
outline: none;
|
|
|
|
user-select: none;
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
--color: ${theme.checkboxBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
--color: ${theme.checkboxBorderHover};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-05-04 09:54:30 +00:00
|
|
|
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// TODO: the !important markings are to trump bootstrap, and should be removed when it's gone.
|
|
|
|
export const cssCheckboxSquare = styled('input', `
|
|
|
|
-webkit-appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
margin: 0 !important;
|
|
|
|
padding: 0;
|
|
|
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
|
|
|
display: inline-block;
|
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
outline: none !important;
|
|
|
|
|
|
|
|
--radius: 3px;
|
|
|
|
|
2021-06-01 13:19:44 +00:00
|
|
|
&:checked:enabled, &:indeterminate:enabled {
|
2022-09-06 01:51:57 +00:00
|
|
|
--color: ${theme.controlPrimaryBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
&:disabled {
|
2022-09-06 01:51:57 +00:00
|
|
|
--color: ${theme.checkboxDisabledBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
cursor: not-allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
&::before, &::after {
|
|
|
|
content: '';
|
|
|
|
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
|
|
|
|
height: 16px;
|
|
|
|
width: 16px;
|
|
|
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
border: 1px solid var(--color);
|
|
|
|
border-radius: var(--radius);
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:19:44 +00:00
|
|
|
&:checked::before, &:disabled::before, &:indeterminate::before {
|
2020-10-02 15:10:00 +00:00
|
|
|
background-color: var(--color);
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
&:not(:checked):indeterminate::after {
|
|
|
|
-webkit-mask-image: var(--icon-Minus);
|
|
|
|
}
|
|
|
|
|
|
|
|
&:not(:disabled)::after {
|
|
|
|
background-color: ${theme.checkboxBg};
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:19:44 +00:00
|
|
|
&:checked::after, &:indeterminate::after {
|
2020-10-02 15:10:00 +00:00
|
|
|
content: '';
|
|
|
|
position: absolute;
|
|
|
|
height: 16px;
|
|
|
|
width: 16px;
|
|
|
|
-webkit-mask-image: var(--icon-Tick);
|
|
|
|
-webkit-mask-size: contain;
|
|
|
|
-webkit-mask-position: center;
|
|
|
|
-webkit-mask-repeat: no-repeat;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.controlPrimaryFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssCheckboxCircle = styled(cssCheckboxSquare, `
|
|
|
|
--radius: 100%;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssLabelText = styled('span', `
|
|
|
|
margin-left: 8px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
font-weight: initial; /* negate bootstrap */
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
`);
|
|
|
|
|
|
|
|
type CheckboxArg = DomArg<HTMLInputElement>;
|
|
|
|
|
|
|
|
function checkbox(
|
2021-11-03 11:44:28 +00:00
|
|
|
obs: Observable<boolean>, cssCheckbox: typeof cssCheckboxSquare,
|
|
|
|
label: DomArg, right: boolean, ...domArgs: CheckboxArg[]
|
2020-10-02 15:10:00 +00:00
|
|
|
) {
|
2021-11-03 11:44:28 +00:00
|
|
|
const field = cssCheckbox(
|
2020-10-02 15:10:00 +00:00
|
|
|
{ type: 'checkbox' },
|
|
|
|
dom.prop('checked', obs),
|
|
|
|
dom.on('change', (ev, el) => obs.set(el.checked)),
|
|
|
|
...domArgs
|
2021-11-03 11:44:28 +00:00
|
|
|
);
|
|
|
|
const text = label ? cssLabelText(label) : null;
|
|
|
|
if (right) {
|
|
|
|
return cssReversedLabel([text, cssInlineRelative(field)]);
|
|
|
|
}
|
|
|
|
return cssLabel(field, text);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function squareCheckbox(obs: Observable<boolean>, ...domArgs: CheckboxArg[]) {
|
2021-11-03 11:44:28 +00:00
|
|
|
return checkbox(obs, cssCheckboxSquare, '', false, ...domArgs);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function circleCheckbox(obs: Observable<boolean>, ...domArgs: CheckboxArg[]) {
|
2021-11-03 11:44:28 +00:00
|
|
|
return checkbox(obs, cssCheckboxCircle, '', false, ...domArgs);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 04:47:59 +00:00
|
|
|
export function labeledSquareCheckbox(obs: Observable<boolean>, label: DomArg, ...domArgs: CheckboxArg[]) {
|
2021-11-03 11:44:28 +00:00
|
|
|
return checkbox(obs, cssCheckboxSquare, label, false, ...domArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function labeledLeftSquareCheckbox(obs: Observable<boolean>, label: DomArg, ...domArgs: CheckboxArg[]) {
|
|
|
|
return checkbox(obs, cssCheckboxSquare, label, true, ...domArgs);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 04:47:59 +00:00
|
|
|
export function labeledCircleCheckbox(obs: Observable<boolean>, label: DomArg, ...domArgs: CheckboxArg[]) {
|
2021-11-03 11:44:28 +00:00
|
|
|
return checkbox(obs, cssCheckboxCircle, label, false, ...domArgs);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-06-01 13:19:44 +00:00
|
|
|
|
|
|
|
export const Indeterminate = 'indeterminate';
|
|
|
|
export type TriState = boolean|'indeterminate';
|
|
|
|
|
|
|
|
function triStateCheckbox(
|
|
|
|
obs: Observable<TriState>, cssCheckbox: typeof cssCheckboxSquare, label: string = '', ...domArgs: CheckboxArg[]
|
|
|
|
) {
|
|
|
|
const checkboxObs = Computed.create(null, obs, (_use, state) => state === true)
|
|
|
|
.onWrite((checked) => obs.set(checked));
|
|
|
|
return checkbox(
|
2021-11-03 11:44:28 +00:00
|
|
|
checkboxObs, cssCheckbox, label, false,
|
2021-06-01 13:19:44 +00:00
|
|
|
dom.prop('indeterminate', (use) => use(obs) === 'indeterminate'),
|
|
|
|
dom.autoDispose(checkboxObs),
|
|
|
|
...domArgs
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function triStateSquareCheckbox(obs: Observable<TriState>, ...domArgs: CheckboxArg[]) {
|
|
|
|
return triStateCheckbox(obs, cssCheckboxSquare, '', ...domArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function labeledTriStateSquareCheckbox(obs: Observable<TriState>, label: string, ...domArgs: CheckboxArg[]) {
|
|
|
|
return triStateCheckbox(obs, cssCheckboxSquare, label, ...domArgs);
|
|
|
|
}
|
2021-11-03 11:44:28 +00:00
|
|
|
|
|
|
|
const cssInlineRelative = styled('div', `
|
|
|
|
display: inline-block;
|
|
|
|
position: relative;
|
|
|
|
height: 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssReversedLabel = styled(cssLabel, `
|
|
|
|
justify-content: space-between;
|
|
|
|
gap: 8px;
|
|
|
|
& .${cssLabelText.className} {
|
|
|
|
margin: 0px;
|
|
|
|
}
|
|
|
|
`);
|