74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
function getVm(context) {
|
|
if (!context) return null;
|
|
return context.proxy || context;
|
|
}
|
|
|
|
function getRootRefs(context) {
|
|
const vm = getVm(context);
|
|
return vm?.$root?.$refs || {};
|
|
}
|
|
|
|
function normalizeMessage(message, fallback = 'tr:error.network') {
|
|
if (!message) {
|
|
return fallback;
|
|
}
|
|
|
|
if (typeof message === 'string') {
|
|
return message;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
export function showMessage(context, message, title = 'tr:message.title', parameters = {}, onClose = null) {
|
|
const refs = getRootRefs(context);
|
|
refs.messageDialog?.open?.(normalizeMessage(message, 'tr:message.title'), title, parameters, onClose);
|
|
}
|
|
|
|
export function showSuccess(context, message, title = 'tr:message.title', parameters = {}, onClose = null) {
|
|
showMessage(context, message, title, parameters, onClose);
|
|
}
|
|
|
|
export function showInfo(context, message, title = 'tr:message.title', parameters = {}, onClose = null) {
|
|
showMessage(context, message, title, parameters, onClose);
|
|
}
|
|
|
|
export function showError(context, message, fallback = 'tr:error.network') {
|
|
const refs = getRootRefs(context);
|
|
refs.errorDialog?.open?.(normalizeMessage(message, fallback));
|
|
}
|
|
|
|
export function showApiError(context, error, fallback = 'tr:error.network') {
|
|
const responseError = error?.response?.data?.error;
|
|
|
|
if (typeof responseError === 'string') {
|
|
const normalized = responseError.startsWith('tr:') || responseError.includes(' ')
|
|
? responseError
|
|
: `tr:error.${responseError}`;
|
|
showError(context, normalized, fallback);
|
|
return;
|
|
}
|
|
|
|
showError(context, fallback, fallback);
|
|
}
|
|
|
|
export async function confirmAction(context, options = {}) {
|
|
const refs = getRootRefs(context);
|
|
return refs.chooseDialog?.open?.(options) ?? false;
|
|
}
|
|
|
|
export default {
|
|
install(app) {
|
|
const getAppContext = () => app._instance?.proxy;
|
|
|
|
app.config.globalProperties.$feedback = {
|
|
showMessage: (...args) => showMessage(getAppContext(), ...args),
|
|
showSuccess: (...args) => showSuccess(getAppContext(), ...args),
|
|
showInfo: (...args) => showInfo(getAppContext(), ...args),
|
|
showError: (...args) => showError(getAppContext(), ...args),
|
|
showApiError: (...args) => showApiError(getAppContext(), ...args),
|
|
confirmAction: (...args) => confirmAction(getAppContext(), ...args)
|
|
};
|
|
}
|
|
};
|