2
0
mirror of https://github.com/frappe/books.git synced 2025-02-04 13:08:29 +00:00
books/src/utils/ui.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

719 lines
16 KiB
TypeScript
Raw Normal View History

2022-04-20 12:08:47 +05:30
/**
* Utils to do UI stuff such as opening dialogs, toasts, etc.
* Basically anything that may directly or indirectly import a Vue file.
*/
import { t } from 'fyo';
import type { Doc } from 'fyo/model/doc';
2022-04-20 12:08:47 +05:30
import { Action } from 'fyo/model/types';
import { getActions } from 'fyo/utils';
2022-11-22 14:42:49 +05:30
import { getDbError, LinkValidationError, ValueError } from 'fyo/utils/errors';
import { getLedgerLink } from 'models/helpers';
import { Transfer } from 'models/inventory/Transfer';
import { Transactional } from 'models/Transactional/Transactional';
import { ModelNameEnum } from 'models/types';
2023-02-20 10:22:19 +05:30
import { Schema } from 'schemas/types';
2022-04-20 12:08:47 +05:30
import { handleErrorWithDialog } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
import router from 'src/router';
import { SelectFileOptions } from 'utils/types';
2022-04-20 12:08:47 +05:30
import { RouteLocationRaw } from 'vue-router';
import { stringifyCircular } from './';
import { evaluateHidden } from './doc';
import { showDialog, showToast } from './interactive';
import { selectFile } from './ipcCalls';
import { showSidebar } from './refs';
2022-04-20 12:08:47 +05:30
import {
2023-02-20 10:22:19 +05:30
ActionGroup,
DialogButton,
2022-04-20 12:08:47 +05:30
QuickEditOptions,
SettingsTab,
ToastOptions,
2023-02-20 10:22:19 +05:30
UIGroupedFields,
2022-04-20 12:08:47 +05:30
} from './types';
export const toastDurationMap = { short: 2_500, long: 5_000 } as const;
2022-04-20 12:08:47 +05:30
export async function openQuickEdit({
2022-11-22 14:42:49 +05:30
doc,
2022-04-20 12:08:47 +05:30
schemaName,
name,
2022-04-28 12:04:55 +05:30
hideFields = [],
showFields = [],
2022-04-20 12:08:47 +05:30
defaults = {},
listFilters = {},
2022-04-20 12:08:47 +05:30
}: QuickEditOptions) {
2022-11-22 14:42:49 +05:30
if (doc) {
schemaName = doc.schemaName;
name = doc.name;
}
if (!doc && (!schemaName || !name)) {
throw new ValueError(t`Schema Name or Name not passed to Open Quick Edit`);
}
2022-04-20 12:08:47 +05:30
const currentRoute = router.currentRoute.value;
const query = currentRoute.query;
let method: 'push' | 'replace' = 'push';
2022-04-28 12:04:55 +05:30
if (query.edit && query.schemaName === schemaName) {
2022-04-20 12:08:47 +05:30
method = 'replace';
}
2022-04-28 12:04:55 +05:30
if (query.name === name) {
return;
}
2022-04-20 12:08:47 +05:30
const forWhat = (defaults?.for ?? []) as string[];
if (forWhat[0] === 'not in') {
const purpose = forWhat[1]?.[0];
defaults = Object.assign({
for:
purpose === 'Sales'
? 'Purchases'
: purpose === 'Purchases'
? 'Sales'
: 'Both',
2022-04-20 12:08:47 +05:30
});
}
if (forWhat[0] === 'not in' && forWhat[1] === 'Sales') {
defaults = Object.assign({ for: 'Purchases' });
2022-04-20 12:08:47 +05:30
}
router[method]({
query: {
edit: 1,
2022-04-28 12:04:55 +05:30
schemaName,
2022-04-20 12:08:47 +05:30
name,
2022-04-28 12:04:55 +05:30
showFields,
2022-04-20 12:08:47 +05:30
hideFields,
2022-04-28 12:04:55 +05:30
defaults: stringifyCircular(defaults),
filters: JSON.stringify(listFilters),
2022-04-20 12:08:47 +05:30
},
});
}
export async function openSettings(tab: SettingsTab) {
await routeTo({ path: '/settings', query: { tab } });
2022-04-20 12:08:47 +05:30
}
export async function routeTo(route: RouteLocationRaw) {
2022-04-20 12:08:47 +05:30
if (
typeof route === 'string' &&
route === router.currentRoute.value.fullPath
) {
return;
}
return await router.push(route);
2022-04-20 12:08:47 +05:30
}
export async function deleteDocWithPrompt(doc: Doc) {
const schemaLabel = fyo.schemaMap[doc.schemaName]!.label;
let detail = t`This action is permanent.`;
2022-07-28 14:29:04 +05:30
if (doc.isTransactional && doc.isSubmitted) {
detail = t`This action is permanent and will delete associated ledger entries.`;
}
return await showDialog({
title: t`Delete ${getActionLabel(doc)}?`,
detail,
type: 'warning',
buttons: [
{
label: t`Yes`,
async action() {
try {
await doc.delete();
} catch (err) {
if (getDbError(err as Error) === LinkValidationError) {
showDialog({
title: t`Delete Failed`,
detail: t`Cannot delete ${schemaLabel} ${doc.name!} because of linked entries.`,
type: 'error',
});
} else {
handleErrorWithDialog(err as Error, doc);
}
return false;
}
return true;
2022-04-20 12:08:47 +05:30
},
isPrimary: true,
},
{
label: t`No`,
action() {
return false;
2022-04-20 12:08:47 +05:30
},
isEscape: true,
},
],
2022-04-20 12:08:47 +05:30
});
}
export async function cancelDocWithPrompt(doc: Doc) {
let detail = t`This action is permanent`;
if (['SalesInvoice', 'PurchaseInvoice'].includes(doc.schemaName)) {
const payments = (
await fyo.db.getAll('Payment', {
fields: ['name'],
filters: { cancelled: false },
})
).map(({ name }) => name);
const query = (
await fyo.db.getAll('PaymentFor', {
fields: ['parent'],
filters: {
referenceName: doc.name!,
},
})
).filter(({ parent }) => payments.includes(parent));
const paymentList = [...new Set(query.map(({ parent }) => parent))];
if (paymentList.length === 1) {
detail = t`This action is permanent and will cancel the following payment: ${
paymentList[0] as string
}`;
} else if (paymentList.length > 1) {
detail = t`This action is permanent and will cancel the following payments: ${paymentList.join(
', '
)}`;
}
}
return await showDialog({
title: t`Cancel ${getActionLabel(doc)}?`,
detail,
type: 'warning',
buttons: [
{
label: t`Yes`,
async action() {
try {
await doc.cancel();
} catch (err) {
handleErrorWithDialog(err as Error, doc);
return false;
}
return true;
2022-04-20 12:08:47 +05:30
},
isPrimary: true,
},
{
label: t`No`,
action() {
return false;
2022-04-20 12:08:47 +05:30
},
isEscape: true,
},
],
2022-04-20 12:08:47 +05:30
});
}
export function getActionsForDoc(doc?: Doc): Action[] {
2022-04-20 12:08:47 +05:30
if (!doc) return [];
const actions: Action[] = [
...getActions(doc),
2022-04-20 12:08:47 +05:30
getDuplicateAction(doc),
getDeleteAction(doc),
getCancelAction(doc),
];
return actions
.filter((d) => d.condition?.(doc) ?? true)
.map((d) => {
return {
2022-11-30 12:29:52 +05:30
group: d.group,
2022-04-20 12:08:47 +05:30
label: d.label,
component: d.component,
action: d.action,
};
});
}
2023-02-20 10:22:19 +05:30
export function getGroupedActionsForDoc(doc?: Doc): ActionGroup[] {
const actions = getActionsForDoc(doc);
2022-11-30 12:29:52 +05:30
const actionsMap = actions.reduce((acc, ac) => {
if (!ac.group) {
ac.group = '';
}
acc[ac.group] ??= {
group: ac.group,
label: ac.label ?? '',
type: ac.type ?? 'secondary',
actions: [],
};
acc[ac.group].actions.push(ac);
return acc;
2023-02-20 10:22:19 +05:30
}, {} as Record<string, ActionGroup>);
2022-11-30 12:29:52 +05:30
const grouped = Object.keys(actionsMap)
.filter(Boolean)
.sort()
.map((k) => actionsMap[k]);
return [grouped, actionsMap['']].flat().filter(Boolean);
2022-11-30 12:29:52 +05:30
}
2022-04-20 12:08:47 +05:30
function getCancelAction(doc: Doc): Action {
return {
label: t`Cancel`,
component: {
template: '<span class="text-red-700">{{ t`Cancel` }}</span>',
},
condition: (doc: Doc) => doc.canCancel,
async action() {
await commonDocCancel(doc);
2022-04-20 12:08:47 +05:30
},
};
}
function getDeleteAction(doc: Doc): Action {
return {
label: t`Delete`,
component: {
template: '<span class="text-red-700">{{ t`Delete` }}</span>',
},
2022-07-28 14:29:04 +05:30
condition: (doc: Doc) => doc.canDelete,
async action() {
await commongDocDelete(doc);
},
2022-04-20 12:08:47 +05:30
};
}
async function openEdit({ name, schemaName }: Doc) {
if (!name) {
return;
}
const route = getFormRoute(schemaName, name);
return await routeTo(route);
}
2022-04-20 12:08:47 +05:30
function getDuplicateAction(doc: Doc): Action {
const isSubmittable = !!doc.schema.isSubmittable;
return {
label: t`Duplicate`,
2022-11-30 12:29:52 +05:30
group: t`Create`,
2022-04-20 12:08:47 +05:30
condition: (doc: Doc) =>
!!(
((isSubmittable && doc.submitted) || !isSubmittable) &&
!doc.notInserted
2022-04-20 12:08:47 +05:30
),
async action() {
try {
const dupe = doc.duplicate();
await openEdit(dupe);
} catch (err) {
handleErrorWithDialog(err as Error, doc);
}
2022-04-20 12:08:47 +05:30
},
};
}
2023-02-20 10:22:19 +05:30
export function getFieldsGroupedByTabAndSection(
schema: Schema,
doc: Doc
2023-02-20 10:22:19 +05:30
): UIGroupedFields {
const grouped: UIGroupedFields = new Map();
for (const field of schema?.fields ?? []) {
const tab = field.tab ?? 'Main';
2023-02-20 10:22:19 +05:30
const section = field.section ?? 'Default';
if (!grouped.has(tab)) {
grouped.set(tab, new Map());
}
const tabbed = grouped.get(tab)!;
if (!tabbed.has(section)) {
tabbed.set(section, []);
}
if (field.meta) {
continue;
}
if (evaluateHidden(field, doc)) {
continue;
}
2023-02-20 10:22:19 +05:30
tabbed.get(section)!.push(field);
}
// Delete empty tabs and sections
for (const tkey of grouped.keys()) {
const section = grouped.get(tkey);
if (!section) {
grouped.delete(tkey);
continue;
}
for (const skey of section.keys()) {
const fields = section.get(skey);
if (!fields || !fields.length) {
section.delete(skey);
}
}
if (!section?.size) {
grouped.delete(tkey);
}
}
2023-02-20 10:22:19 +05:30
return grouped;
}
export function getFormRoute(
schemaName: string,
name: string
): RouteLocationRaw {
const route = fyo.models[schemaName]
?.getListViewSettings(fyo)
?.formRoute?.(name);
if (typeof route === 'string') {
return route;
}
if (
[
ModelNameEnum.SalesInvoice,
ModelNameEnum.PurchaseInvoice,
ModelNameEnum.JournalEntry,
ModelNameEnum.Shipment,
ModelNameEnum.PurchaseReceipt,
ModelNameEnum.StockMovement,
ModelNameEnum.Payment,
ModelNameEnum.Item,
].includes(schemaName as ModelNameEnum)
) {
return `/edit/${schemaName}/${name}`;
}
return `/list/${schemaName}?edit=1&schemaName=${schemaName}&name=${name}`;
}
export async function getDocFromNameIfExistsElseNew(
schemaName: string,
name?: string
) {
if (!name) {
return fyo.doc.getNewDoc(schemaName);
}
try {
return await fyo.doc.getDoc(schemaName, name);
} catch {
return fyo.doc.getNewDoc(schemaName);
}
}
export async function isPrintable(schemaName: string) {
const numTemplates = await fyo.db.count(ModelNameEnum.PrintTemplate, {
filters: { type: schemaName },
});
return numTemplates > 0;
}
export function toggleSidebar(value?: boolean) {
if (typeof value !== 'boolean') {
value = !showSidebar.value;
}
showSidebar.value = value;
}
export function focusOrSelectFormControl(
doc: Doc,
ref: any,
clear: boolean = true
) {
if (!doc?.fyo) {
return;
}
const naming = doc.fyo.schemaMap[doc.schemaName]?.naming;
if (naming !== 'manual' || doc.inserted) {
return;
}
if (!doc.fyo.doc.isTemporaryName(doc.name ?? '', doc.schema)) {
return;
}
if (Array.isArray(ref) && ref.length > 0) {
ref = ref[0];
}
if (!clear && typeof ref?.select === 'function') {
ref.select();
return;
}
if (typeof ref?.clear === 'function') {
ref.clear();
}
if (typeof ref?.focus === 'function') {
ref.focus();
}
doc.name = '';
}
export async function selectTextFile(filters?: SelectFileOptions['filters']) {
const options = {
title: t`Select File`,
filters,
};
const { success, canceled, filePath, data, name } = await selectFile(options);
if (canceled || !success) {
await showToast({
type: 'error',
message: t`File selection failed`,
});
return {};
}
const text = new TextDecoder().decode(data);
if (!text) {
await showToast({
type: 'error',
message: t`Empty file selected`,
});
return {};
}
return { text, filePath, name };
}
export enum ShortcutKey {
enter = 'enter',
ctrl = 'ctrl',
pmod = 'pmod',
shift = 'shift',
alt = 'alt',
delete = 'delete',
esc = 'esc',
}
export function getShortcutKeyMap(
platform: string
): Record<ShortcutKey, string> {
if (platform === 'Mac') {
return {
[ShortcutKey.alt]: '⌥',
[ShortcutKey.ctrl]: '⌃',
[ShortcutKey.pmod]: '⌘',
[ShortcutKey.shift]: 'shift',
[ShortcutKey.delete]: 'delete',
[ShortcutKey.esc]: 'esc',
[ShortcutKey.enter]: 'return',
};
}
return {
[ShortcutKey.alt]: 'Alt',
[ShortcutKey.ctrl]: 'Ctrl',
[ShortcutKey.pmod]: 'Ctrl',
[ShortcutKey.shift]: '⇧',
[ShortcutKey.delete]: 'Backspace',
[ShortcutKey.esc]: 'Esc',
[ShortcutKey.enter]: 'Enter',
};
}
export async function commongDocDelete(doc: Doc): Promise<boolean> {
const res = await deleteDocWithPrompt(doc);
if (!res) {
return false;
}
showActionToast(doc, 'delete');
router.back();
return true;
}
export async function commonDocCancel(doc: Doc): Promise<boolean> {
const res = await cancelDocWithPrompt(doc);
if (!res) {
return false;
}
showActionToast(doc, 'cancel');
return true;
}
export async function commonDocSync(
doc: Doc,
useDialog: boolean = false
): Promise<boolean> {
let success: boolean;
if (useDialog) {
success = !!(await showSubmitOrSyncDialog(doc, 'sync'));
} else {
success = await syncWithoutDialog(doc);
}
if (!success) {
return false;
}
showActionToast(doc, 'sync');
return true;
}
async function syncWithoutDialog(doc: Doc): Promise<boolean> {
try {
await doc.sync();
} catch (error) {
handleErrorWithDialog(error, doc);
return false;
}
return true;
}
export async function commonDocSubmit(doc: Doc): Promise<boolean> {
const success = await showSubmitOrSyncDialog(doc, 'submit');
if (!success) {
return false;
}
showSubmitToast(doc);
return true;
}
async function showSubmitOrSyncDialog(doc: Doc, type: 'submit' | 'sync') {
const label = getActionLabel(doc);
let title = t`Submit ${label}?`;
if (type === 'sync') {
title = t`Save ${label}?`;
}
let detail = t`Mark ${doc.schema.label} as submitted`;
if (type === 'sync') {
detail = t`Save ${doc.schema.label} to database`;
}
const yesAction = async () => {
try {
await doc[type]();
} catch (error) {
handleErrorWithDialog(error, doc);
return false;
}
return true;
};
const buttons: DialogButton[] = [
{
label: t`Yes`,
action: yesAction,
isPrimary: true,
},
{
label: t`No`,
action: () => false,
isEscape: true,
},
];
return await showDialog({
title,
detail,
buttons,
});
}
function showActionToast(doc: Doc, type: 'sync' | 'cancel' | 'delete') {
const label = getActionLabel(doc);
const message = {
sync: t`${label} saved`,
cancel: t`${label} cancelled`,
delete: t`${label} deleted`,
}[type];
showToast({ type: 'success', message, duration: 'short' });
}
function showSubmitToast(doc: Doc) {
const label = getActionLabel(doc);
const message = t`${label} submitted`;
const toastOption: ToastOptions = {
type: 'success',
message,
duration: 'long',
...getSubmitSuccessToastAction(doc),
};
showToast(toastOption);
}
function getSubmitSuccessToastAction(doc: Doc) {
const isStockTransfer = doc instanceof Transfer;
const isTransactional = doc instanceof Transactional;
if (isStockTransfer) {
return {
async action() {
const route = getLedgerLink(doc, 'StockLedger');
await routeTo(route);
},
actionText: t`View Stock Entries`,
};
}
if (isTransactional) {
return {
async action() {
const route = getLedgerLink(doc, 'GeneralLedger');
await routeTo(route);
},
actionText: t`View Accounting Entries`,
};
}
return {};
}
export function showCannotSaveOrSubmitToast(doc: Doc) {
const label = getActionLabel(doc);
let message = t`${label} already saved`;
if (doc.schema.isSubmittable && doc.isSubmitted) {
message = t`${label} already submitted`;
}
showToast({ type: 'warning', message, duration: 'short' });
}
export function showCannotCancelOrDeleteToast(doc: Doc) {
const label = getActionLabel(doc);
let message = t`${label} cannot be deleted`;
if (doc.schema.isSubmittable && !doc.isCancelled) {
message = t`${label} cannot be cancelled`;
}
showToast({ type: 'warning', message, duration: 'short' });
}
function getActionLabel(doc: Doc) {
const label = doc.schema.label || doc.schemaName;
if (doc.schema.naming === 'random') {
return label;
}
return doc.name || label;
}