2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2022-09-27 21:06:02 +00:00
|
|
|
import {ACIndex, ACItem, ACResults, buildHighlightedDom, normalizeText} from "app/client/lib/ACIndex";
|
2022-09-21 14:30:54 +00:00
|
|
|
import {cssSelectItem} from "app/client/lib/ACSelect";
|
|
|
|
import {Autocomplete, IAutocompleteOptions} from "app/client/lib/autocomplete";
|
2022-09-27 21:06:02 +00:00
|
|
|
import {colors, testId, theme} from "app/client/ui2018/cssVars";
|
2023-09-21 16:57:58 +00:00
|
|
|
import {icon} from "app/client/ui2018/icons";
|
2022-09-21 14:30:54 +00:00
|
|
|
import {menuCssClass} from "app/client/ui2018/menus";
|
|
|
|
import {
|
|
|
|
cssEmailInput,
|
|
|
|
cssEmailInputContainer,
|
|
|
|
cssMailIcon,
|
|
|
|
cssMemberImage,
|
|
|
|
cssMemberListItem,
|
|
|
|
cssMemberPrimary,
|
|
|
|
cssMemberSecondary,
|
|
|
|
cssMemberText,
|
|
|
|
} from "app/client/ui/UserItem";
|
|
|
|
import {createUserImage, cssUserImage} from "app/client/ui/UserImage";
|
2024-04-02 11:22:58 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2022-09-27 21:06:02 +00:00
|
|
|
import {Computed, computed, dom, DomElementArg, Holder, IDisposableOwner, Observable, styled} from "grainjs";
|
|
|
|
import {cssMenuItem} from "popweasel";
|
2022-09-21 14:30:54 +00:00
|
|
|
|
2022-12-09 15:46:03 +00:00
|
|
|
const t = makeT('ACUserManager');
|
2022-10-28 16:11:08 +00:00
|
|
|
|
2022-09-21 14:30:54 +00:00
|
|
|
export interface ACUserItem extends ACItem {
|
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
id: number;
|
|
|
|
picture?: string | null; // when present, a url to a public image of unspecified dimensions.
|
|
|
|
isNew?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildACMemberEmail(
|
|
|
|
owner: IDisposableOwner,
|
|
|
|
options: {
|
|
|
|
acIndex: ACIndex<ACUserItem>;
|
|
|
|
emailObs: Observable<string>;
|
2022-09-27 21:06:02 +00:00
|
|
|
save: (value: string) => void;
|
2022-09-21 14:30:54 +00:00
|
|
|
prompt?: {email: string},
|
|
|
|
},
|
|
|
|
...args: DomElementArg[]
|
|
|
|
) {
|
2022-09-27 21:06:02 +00:00
|
|
|
const {acIndex, emailObs, save, prompt} = options;
|
2022-09-21 14:30:54 +00:00
|
|
|
const acHolder = Holder.create<Autocomplete<ACUserItem>>(owner);
|
|
|
|
let emailInput: HTMLInputElement;
|
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const isValid = Observable.create(owner, true);
|
|
|
|
|
2022-09-21 14:30:54 +00:00
|
|
|
const isOpen = () => !acHolder.isEmpty();
|
|
|
|
const acOpen = () => acHolder.isEmpty() && Autocomplete.create(acHolder, emailInput, acOptions);
|
|
|
|
const acClose = () => acHolder.clear();
|
|
|
|
const finish = () => {
|
|
|
|
acClose();
|
|
|
|
emailObs.set("");
|
|
|
|
emailInput.value = emailObs.get();
|
|
|
|
emailInput.focus();
|
|
|
|
};
|
2022-09-27 21:06:02 +00:00
|
|
|
const onEnter = () => {
|
2022-09-21 14:30:54 +00:00
|
|
|
isOpen() ? commitIfValid() : acOpen();
|
|
|
|
};
|
|
|
|
|
|
|
|
const commitIfValid = () => {
|
|
|
|
const item = acHolder.get()?.getSelectedItem();
|
|
|
|
if (item) {
|
|
|
|
emailObs.set(item.value);
|
|
|
|
}
|
|
|
|
emailInput.setCustomValidity("");
|
2022-09-27 21:06:02 +00:00
|
|
|
isValid.set(emailInput.checkValidity());
|
|
|
|
|
2022-09-21 14:30:54 +00:00
|
|
|
const selectedEmail = item?.value || emailObs.get();
|
|
|
|
try {
|
2022-09-27 21:06:02 +00:00
|
|
|
if (selectedEmail && isValid.get()) {
|
2022-09-21 14:30:54 +00:00
|
|
|
save(emailObs.get());
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-09-27 21:06:02 +00:00
|
|
|
emailInput.setCustomValidity(e.message);
|
2022-09-21 14:30:54 +00:00
|
|
|
} finally {
|
|
|
|
emailInput.reportValidity();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const maybeShowAddNew = async (results: ACResults<ACUserItem>, text: string): Promise<ACResults<ACUserItem>> => {
|
|
|
|
const cleanText = normalizeText(text);
|
|
|
|
const items = results.items
|
|
|
|
.filter(item => item.cleanText.includes(cleanText))
|
2022-09-27 21:06:02 +00:00
|
|
|
.sort((a, b) => a.cleanText.localeCompare(b.cleanText));
|
2022-09-21 14:30:54 +00:00
|
|
|
results.items = items;
|
2022-10-17 20:25:15 +00:00
|
|
|
if (!results.items.length && cleanText) {
|
2022-09-21 14:30:54 +00:00
|
|
|
const newObject = {
|
|
|
|
value: text,
|
|
|
|
cleanText,
|
|
|
|
name: "",
|
|
|
|
email: "",
|
|
|
|
isNew: true,
|
|
|
|
label: text,
|
|
|
|
id: 0,
|
|
|
|
};
|
2024-04-26 20:34:16 +00:00
|
|
|
results.extraItems.push(newObject);
|
2022-09-21 14:30:54 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderSearchItem = (item: ACUserItem, highlightFunc: any): HTMLLIElement => (item?.isNew ? cssSelectItem(
|
|
|
|
cssMemberListItem(
|
|
|
|
cssUserImagePlus(
|
2023-09-21 16:57:58 +00:00
|
|
|
cssPlusIcon('Plus'),
|
2022-09-21 14:30:54 +00:00
|
|
|
cssUserImage.cls("-large"),
|
|
|
|
cssUserImagePlus.cls('-invalid', (use) => !use(enableAdd),
|
2022-09-27 21:06:02 +00:00
|
|
|
)),
|
2022-09-21 14:30:54 +00:00
|
|
|
cssMemberText(
|
2022-12-06 13:24:54 +00:00
|
|
|
cssMemberPrimaryPlus(t("Invite new member")),
|
2024-04-02 11:22:58 +00:00
|
|
|
getGristConfig().notifierEnabled ? cssMemberSecondaryPlus(
|
|
|
|
dom.text(use => t("We'll email an invite to {{email}}", {email: use(emailObs)}))
|
|
|
|
) : null,
|
2022-09-21 14:30:54 +00:00
|
|
|
),
|
|
|
|
testId("um-add-email")
|
|
|
|
)
|
|
|
|
) : cssSelectItem(
|
|
|
|
cssMemberListItem(
|
|
|
|
cssMemberImage(createUserImage(item, "large")),
|
|
|
|
cssMemberText(
|
|
|
|
cssMemberPrimaryPlus(item.name, testId("um-member-name")),
|
|
|
|
cssMemberSecondaryPlus(buildHighlightedDom(item.label, highlightFunc, cssMatchText))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const enableAdd: Computed<boolean> = computed((use) => Boolean(use(emailObs) && use(isValid)));
|
2022-09-21 14:30:54 +00:00
|
|
|
|
|
|
|
const acOptions: IAutocompleteOptions<ACUserItem> = {
|
|
|
|
attach: null,
|
|
|
|
menuCssClass: `${menuCssClass} test-acselect-dropdown`,
|
|
|
|
search: (term) => maybeShowAddNew(acIndex.search(term), term),
|
|
|
|
renderItem: renderSearchItem,
|
|
|
|
getItemText: (item) => item.value,
|
|
|
|
onClick: commitIfValid,
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = cssEmailInputContainer(
|
|
|
|
cssMailIcon("Mail"),
|
|
|
|
(emailInput = cssEmailInput(
|
|
|
|
emailObs,
|
2022-09-27 21:06:02 +00:00
|
|
|
{onInput: true, isValid},
|
2022-12-06 13:24:54 +00:00
|
|
|
{type: "email", placeholder: t("Enter email address")},
|
2022-09-21 14:30:54 +00:00
|
|
|
dom.on("input", acOpen),
|
|
|
|
dom.on("focus", acOpen),
|
|
|
|
dom.on("click", acOpen),
|
|
|
|
dom.on("blur", acClose),
|
|
|
|
dom.onKeyDown({
|
|
|
|
Escape: finish,
|
2022-09-27 21:06:02 +00:00
|
|
|
Enter: onEnter,
|
2022-09-21 14:30:54 +00:00
|
|
|
ArrowDown: acOpen,
|
|
|
|
Tab: commitIfValid,
|
|
|
|
}),
|
2022-09-27 21:06:02 +00:00
|
|
|
)),
|
2022-09-21 14:30:54 +00:00
|
|
|
cssEmailInputContainer.cls('-green', enableAdd),
|
2022-09-27 21:06:02 +00:00
|
|
|
...args
|
2022-09-21 14:30:54 +00:00
|
|
|
);
|
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
// Reset custom validity that we sometimes set.
|
|
|
|
owner.autoDispose(emailObs.addListener(() => emailInput.setCustomValidity("")));
|
|
|
|
|
2022-09-21 14:30:54 +00:00
|
|
|
if (prompt) { setTimeout(() => emailInput.focus(), 0); }
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const cssMemberPrimaryPlus = styled(cssMemberPrimary, `
|
2022-09-21 14:30:54 +00:00
|
|
|
.${cssSelectItem.className}.selected & {
|
|
|
|
color: ${theme.menuItemSelectedFg};
|
|
|
|
}
|
2022-09-27 21:06:02 +00:00
|
|
|
`);
|
2022-09-21 14:30:54 +00:00
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const cssMemberSecondaryPlus = styled(cssMemberSecondary, `
|
2022-09-21 14:30:54 +00:00
|
|
|
.${cssSelectItem.className}.selected & {
|
|
|
|
color: ${theme.menuItemSelectedFg};
|
|
|
|
}
|
2022-09-27 21:06:02 +00:00
|
|
|
`);
|
2022-09-21 14:30:54 +00:00
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const cssMatchText = styled("span", `
|
2022-09-21 14:30:54 +00:00
|
|
|
color: ${theme.autocompleteMatchText};
|
|
|
|
.${cssSelectItem.className}.selected & {
|
|
|
|
color: ${theme.autocompleteSelectedMatchText};
|
|
|
|
}
|
2022-09-27 21:06:02 +00:00
|
|
|
`);
|
2022-09-21 14:30:54 +00:00
|
|
|
|
2022-09-27 21:06:02 +00:00
|
|
|
const cssUserImagePlus = styled(cssUserImage, `
|
2022-09-21 14:30:54 +00:00
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
margin: auto 0;
|
|
|
|
|
|
|
|
&-invalid {
|
|
|
|
background-color: ${colors.mediumGrey};
|
|
|
|
}
|
|
|
|
|
|
|
|
.${cssMenuItem.className}-sel & {
|
|
|
|
background-color: ${theme.menuItemIconSelectedFg};
|
|
|
|
color: ${theme.menuItemSelectedBg};
|
|
|
|
}
|
2022-09-27 21:06:02 +00:00
|
|
|
`);
|
2023-09-21 16:57:58 +00:00
|
|
|
|
|
|
|
const cssPlusIcon = styled(icon, `
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
`);
|