mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
5ca2148c7e
* ⚡ Adjust `format` script * 🔥 Remove exemption for `editor-ui` * 🎨 Prettify * 👕 Fix lint
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
/**
|
|
* Creates event listeners for `data-action` attribute to allow for actions to be called from locale without using
|
|
* unsafe onclick attribute
|
|
*/
|
|
import { reactive, del, computed, onMounted, onUnmounted, getCurrentInstance } from 'vue';
|
|
|
|
const state = reactive({
|
|
customActions: {} as Record<string, Function>,
|
|
});
|
|
|
|
export default () => {
|
|
function registerCustomAction(key: string, action: Function) {
|
|
state.customActions[key] = action;
|
|
}
|
|
function unregisterCustomAction(key: string) {
|
|
del(state.customActions, key);
|
|
}
|
|
function delegateClick(e: MouseEvent) {
|
|
const clickedElement = e.target;
|
|
if (!(clickedElement instanceof Element) || clickedElement.tagName !== 'A') return;
|
|
|
|
const actionAttribute = clickedElement.getAttribute('data-action');
|
|
if (actionAttribute && typeof availableActions.value[actionAttribute] === 'function') {
|
|
e.preventDefault();
|
|
availableActions.value[actionAttribute]();
|
|
}
|
|
}
|
|
|
|
function reload() {
|
|
if (window.top) {
|
|
window.top.location.reload();
|
|
} else {
|
|
window.location.reload();
|
|
}
|
|
}
|
|
|
|
const availableActions = computed<{ [key: string]: Function }>(() => ({
|
|
reload,
|
|
...state.customActions,
|
|
}));
|
|
|
|
onMounted(() => {
|
|
const instance = getCurrentInstance();
|
|
window.addEventListener('click', delegateClick);
|
|
instance?.proxy.$root.$on('registerGlobalLinkAction', registerCustomAction);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
const instance = getCurrentInstance();
|
|
window.removeEventListener('click', delegateClick);
|
|
instance?.proxy.$root.$off('registerGlobalLinkAction', registerCustomAction);
|
|
});
|
|
|
|
return {
|
|
registerCustomAction,
|
|
unregisterCustomAction,
|
|
};
|
|
};
|