n8n/packages/editor-ui/src/components/mixins/genericHelpers.ts
2021-12-07 16:14:40 +01:00

90 lines
2.5 KiB
TypeScript

import { showMessage } from '@/components/mixins/showMessage';
import { debounce } from 'lodash';
import mixins from 'vue-typed-mixins';
export const genericHelpers = mixins(showMessage).extend({
data () {
return {
loadingService: null as any | null, // tslint:disable-line:no-any
debouncedFunctions: [] as any[], // tslint:disable-line:no-any
};
},
computed: {
isReadOnly (): boolean {
if (['NodeViewExisting', 'NodeViewNew'].includes(this.$route.name as string)) {
return false;
}
return true;
},
},
methods: {
displayTimer (msPassed: number, showMs = false): string {
if (msPassed < 60000) {
if (showMs === false) {
return `${this.$n(Math.floor(msPassed / 1000), 'decimal')} ${this.$i18n2.baseText('genericHelpers.sec')}`;
}
return `${this.$i18n2.number(msPassed / 1000, 'decimal')} ${this.$i18n2.baseText('genericHelpers.sec')}`;
}
const secondsPassed = Math.floor(msPassed / 1000);
const minutesPassed = Math.floor(secondsPassed / 60);
return `${this.$i18n2.number(minutesPassed, 'decimal')}:${this.$i18n2.number(secondsPassed, 'decimal')} ${this.$i18n2.baseText('genericHelpers.min')}`;
},
editAllowedCheck (): boolean {
if (this.isReadOnly) {
this.$showMessage({
// title: 'Workflow can not be changed!',
title: this.$i18n2.baseText('genericHelpers.showMessage.title'),
message: this.$i18n2.baseText('genericHelpers.showMessage.message'),
type: 'error',
duration: 0,
});
return false;
}
return true;
},
startLoading (text?: string) {
if (this.loadingService !== null) {
return;
}
// @ts-ignore
this.loadingService = this.$loading(
{
lock: true,
text: text || this.$i18n2.baseText('genericHelpers.loading'),
spinner: 'el-icon-loading',
background: 'rgba(255, 255, 255, 0.8)',
},
);
},
setLoadingText (text: string) {
this.loadingService.text = text;
},
stopLoading () {
if (this.loadingService !== null) {
this.loadingService.close();
this.loadingService = null;
}
},
async callDebounced (...inputParameters: any[]): Promise<void> { // tslint:disable-line:no-any
const functionName = inputParameters.shift() as string;
const debounceTime = inputParameters.shift() as number;
// @ts-ignore
if (this.debouncedFunctions[functionName] === undefined) {
// @ts-ignore
this.debouncedFunctions[functionName] = debounce(this[functionName], debounceTime, { leading: true });
}
// @ts-ignore
await this.debouncedFunctions[functionName].apply(this, inputParameters);
},
},
});