mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
385 lines
12 KiB
385 lines
12 KiB
3 years ago
|
<template>
|
||
2 years ago
|
<div :class="{ 'pt-10': !hideLabel }">
|
||
3 years ago
|
<v-dialog v-model="dropOrUpload" max-width="600">
|
||
3 years ago
|
<v-card max-width="600">
|
||
3 years ago
|
<v-tabs height="30">
|
||
|
<v-tab>
|
||
2 years ago
|
<v-icon small class="mr-1"> mdi-file-upload-outline </v-icon>
|
||
3 years ago
|
<span class="caption text-capitalize">Upload</span>
|
||
|
</v-tab>
|
||
|
<v-tab>
|
||
2 years ago
|
<v-icon small class="mr-1"> mdi-link-variant </v-icon>
|
||
3 years ago
|
<span class="caption text-capitalize">URL</span>
|
||
|
</v-tab>
|
||
|
|
||
|
<v-tab-item>
|
||
2 years ago
|
<div class="nc-excel-import-tab-item">
|
||
3 years ago
|
<div
|
||
|
class="nc-droppable d-flex align-center justify-center flex-column"
|
||
|
:style="{
|
||
2 years ago
|
background: dragOver ? '#7772' : '',
|
||
3 years ago
|
}"
|
||
|
@click="$refs.file.click()"
|
||
|
@drop.prevent="dropHandler"
|
||
|
@dragover.prevent="dragOver = true"
|
||
|
@dragenter.prevent="dragOver = true"
|
||
|
@dragexit="dragOver = false"
|
||
|
@dragleave="dragOver = false"
|
||
|
@dragend="dragOver = false"
|
||
|
>
|
||
2 years ago
|
<x-icon :color="['primary', 'grey']" size="50"> mdi-file-plus-outline </x-icon>
|
||
|
<p class="title mb-1 mt-2">
|
||
3 years ago
|
<!-- Select File to Upload-->
|
||
|
{{ $t('msg.info.upload') }}
|
||
3 years ago
|
</p>
|
||
|
<p class="grey--text mb-1">
|
||
3 years ago
|
<!-- or drag and drop file-->
|
||
|
{{ $t('msg.info.upload_sub') }}
|
||
3 years ago
|
</p>
|
||
|
|
||
3 years ago
|
<p v-if="quickImportType == 'excel'" class="caption grey--text">
|
||
|
<!-- Supported: .xls, .xlsx, .xlsm, .ods, .ots -->
|
||
3 years ago
|
{{ $t('msg.info.excelSupport') }}
|
||
3 years ago
|
</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
</v-tab-item>
|
||
|
<v-tab-item>
|
||
|
<div class="nc-excel-import-tab-item align-center">
|
||
|
<div class="pa-4 d-100 h-100">
|
||
|
<v-form ref="form" v-model="valid">
|
||
|
<div class="d-flex">
|
||
3 years ago
|
<!--label="Enter excel file url"-->
|
||
3 years ago
|
<v-text-field
|
||
|
v-model="url"
|
||
|
hide-details="auto"
|
||
|
type="url"
|
||
2 years ago
|
:label="quickImportType === 'excel' ? $t('msg.info.excelURL') : $t('msg.info.csvURL')"
|
||
3 years ago
|
class="caption"
|
||
|
outlined
|
||
|
dense
|
||
2 years ago
|
:rules="[
|
||
|
v => !!v || $t('general.required'),
|
||
|
v =>
|
||
|
!/(10)(\.([2]([0-5][0-5]|[01234][6-9])|[1][0-9][0-9]|[1-9][0-9]|[0-9])){3}|(172)\.(1[6-9]|2[0-9]|3[0-1])(\.(2[0-4][0-9]|25[0-5]|[1][0-9][0-9]|[1-9][0-9]|[0-9])){2}|(192)\.(168)(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){2}|(0.0.0.0)|localhost?/g.test(
|
||
|
v
|
||
|
) || errorMessages.ipBlockList,
|
||
|
v =>
|
||
|
quickImportType === 'excel'
|
||
|
? /.*\.(xls|xlsx|xlsm|ods|ots)/.test(v) || errorMessages.importExcel
|
||
|
: /.*\.(csv)/.test(v) || errorMessages.importCSV,
|
||
|
]"
|
||
3 years ago
|
/>
|
||
3 years ago
|
<v-btn v-t="['c:project:create:excel:load-url']" class="ml-3" color="primary" @click="loadUrl">
|
||
3 years ago
|
<!--Load-->
|
||
|
{{ $t('general.load') }}
|
||
3 years ago
|
</v-btn>
|
||
|
</div>
|
||
|
</v-form>
|
||
|
</div>
|
||
|
</div>
|
||
|
</v-tab-item>
|
||
|
</v-tabs>
|
||
|
|
||
3 years ago
|
<div class="px-4 pb-2">
|
||
|
<div class="d-flex">
|
||
|
<v-spacer />
|
||
3 years ago
|
<span class="caption pointer grey--text" @click="showMore = !showMore">
|
||
|
{{ showMore ? $t('general.hideAll') : $t('general.showMore') }}
|
||
3 years ago
|
<v-icon small color="grey lighten-1">mdi-menu-{{ showMore ? 'up' : 'down' }}</v-icon>
|
||
3 years ago
|
</span>
|
||
|
</div>
|
||
2 years ago
|
<div class="mb-2 pt-2 nc-excel-import-options" :style="{ maxHeight: showMore ? '100px' : '0' }">
|
||
3 years ago
|
<p />
|
||
3 years ago
|
<!--hint="# of rows to parse to infer data type"-->
|
||
3 years ago
|
<v-text-field
|
||
|
v-model="parserConfig.maxRowsToParse"
|
||
|
style="max-width: 250px"
|
||
|
class="caption mx-auto"
|
||
|
dense
|
||
|
persistent-hint
|
||
3 years ago
|
:hint="$t('msg.info.footMsg')"
|
||
3 years ago
|
outlined
|
||
|
type="number"
|
||
|
/>
|
||
|
</div>
|
||
|
</div>
|
||
3 years ago
|
</v-card>
|
||
|
</v-dialog>
|
||
|
|
||
|
<v-tooltip bottom>
|
||
2 years ago
|
<template #activator="{ on }">
|
||
3 years ago
|
<input
|
||
3 years ago
|
v-if="quickImportType == 'excel'"
|
||
3 years ago
|
ref="file"
|
||
|
class="nc-excel-import-input"
|
||
|
type="file"
|
||
|
style="display: none"
|
||
3 years ago
|
accept=".xlsx, .xls, .xlsm, .ods, .ots"
|
||
3 years ago
|
@change="_change($event)"
|
||
2 years ago
|
/>
|
||
3 years ago
|
<input
|
||
|
v-if="quickImportType == 'csv'"
|
||
|
ref="file"
|
||
|
class="nc-excel-import-input"
|
||
|
type="file"
|
||
|
style="display: none"
|
||
|
accept=".csv"
|
||
|
@change="_change($event)"
|
||
2 years ago
|
/>
|
||
|
<v-btn v-if="!hideLabel" small outlined v-on="on" @click="$refs.file.click()">
|
||
|
<v-icon small class="mr-1"> mdi-file-excel-outline </v-icon>
|
||
3 years ago
|
<!--Import-->
|
||
|
{{ $t('activity.import') }}
|
||
3 years ago
|
</v-btn>
|
||
|
</template>
|
||
|
<span class="caption">Create template from Excel</span>
|
||
|
</v-tooltip>
|
||
|
|
||
3 years ago
|
<v-dialog v-if="templateData" v-model="templateEditorModal" max-width="1000">
|
||
3 years ago
|
<v-card class="pa-6" min-width="500">
|
||
3 years ago
|
<template-editor :project-template.sync="templateData" excel-import :quick-import-type="quickImportType">
|
||
2 years ago
|
<template #toolbar="{ valid }">
|
||
3 years ago
|
<h3 class="mt-2 grey--text">
|
||
3 years ago
|
<!--Import Excel-->
|
||
|
<span v-if="quickImportType === 'excel'">
|
||
|
{{ $t('activity.importExcel') }}
|
||
|
</span>
|
||
|
<!--Import CSV-->
|
||
|
<span v-if="quickImportType === 'csv'">
|
||
|
{{ $t('activity.importCSV') }}
|
||
|
</span>
|
||
|
: {{ filename }}
|
||
3 years ago
|
</h3>
|
||
|
<v-spacer />
|
||
3 years ago
|
<v-spacer />
|
||
|
<create-project-from-template-btn
|
||
|
:template-data="templateData"
|
||
|
:import-data="importData"
|
||
3 years ago
|
:import-to-project="importToProject"
|
||
3 years ago
|
excel-import
|
||
3 years ago
|
:valid="valid"
|
||
|
create-gql-text="Import as GQL Project"
|
||
|
create-rest-text="Import as REST Project"
|
||
2 years ago
|
@closeModal="$emit('closeModal'), (templateEditorModal = false)"
|
||
3 years ago
|
>
|
||
3 years ago
|
<!--Import Excel-->
|
||
3 years ago
|
<span v-if="quickImportType === 'excel'">
|
||
|
{{ $t('activity.importExcel') }}
|
||
|
</span>
|
||
|
<!--Import CSV-->
|
||
|
<span v-if="quickImportType === 'csv'">
|
||
|
{{ $t('activity.importCSV') }}
|
||
|
</span>
|
||
3 years ago
|
</create-project-from-template-btn>
|
||
3 years ago
|
</template>
|
||
|
</template-editor>
|
||
|
</v-card>
|
||
|
</v-dialog>
|
||
|
</div>
|
||
3 years ago
|
</template>
|
||
|
|
||
|
<script>
|
||
3 years ago
|
// import XLSX from 'xlsx'
|
||
2 years ago
|
import TemplateEditor from '~/components/templates/Editor';
|
||
|
import CreateProjectFromTemplateBtn from '~/components/templates/CreateProjectFromTemplateBtn';
|
||
|
import ExcelUrlTemplateAdapter from '~/components/import/templateParsers/ExcelUrlTemplateAdapter';
|
||
|
import ExcelTemplateAdapter from '~/components/import/templateParsers/ExcelTemplateAdapter';
|
||
3 years ago
|
|
||
3 years ago
|
export default {
|
||
3 years ago
|
name: 'QuickImport',
|
||
3 years ago
|
components: { CreateProjectFromTemplateBtn, TemplateEditor },
|
||
3 years ago
|
props: {
|
||
|
hideLabel: Boolean,
|
||
3 years ago
|
value: Boolean,
|
||
3 years ago
|
importToProject: Boolean,
|
||
2 years ago
|
quickImportType: String,
|
||
3 years ago
|
},
|
||
|
data() {
|
||
|
return {
|
||
3 years ago
|
templateEditorModal: false,
|
||
3 years ago
|
valid: null,
|
||
3 years ago
|
templateData: null,
|
||
|
importData: null,
|
||
|
dragOver: false,
|
||
3 years ago
|
url: '',
|
||
|
showMore: false,
|
||
|
parserConfig: {
|
||
2 years ago
|
maxRowsToParse: 500,
|
||
3 years ago
|
},
|
||
3 years ago
|
filename: '',
|
||
|
errorMessages: {
|
||
2 years ago
|
importExcel:
|
||
|
'Target file is not an accepted file type. The accepted file types are .xls, .xlsx, .xlsm, .ods, .ots!',
|
||
|
importCSV: 'Target file is not an accepted file type. The accepted file type is .csv!',
|
||
|
ipBlockList: 'IP Not allowed!',
|
||
|
},
|
||
|
};
|
||
3 years ago
|
},
|
||
|
computed: {
|
||
|
dropOrUpload: {
|
||
|
set(v) {
|
||
2 years ago
|
this.$emit('input', v);
|
||
3 years ago
|
},
|
||
|
get() {
|
||
2 years ago
|
return this.value;
|
||
|
},
|
||
|
},
|
||
3 years ago
|
},
|
||
|
mounted() {
|
||
3 years ago
|
if (this.$route && this.$route.query && this.$route.query.excelUrl) {
|
||
2 years ago
|
this.url = this.$route.query.excelUrl;
|
||
|
this.loadUrl();
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
methods: {
|
||
3 years ago
|
selectFile() {
|
||
2 years ago
|
this.$refs.file.files = null;
|
||
|
this.$refs.file.click();
|
||
3 years ago
|
},
|
||
|
|
||
3 years ago
|
_change(event) {
|
||
2 years ago
|
const files = event.target.files;
|
||
3 years ago
|
if (files && files[0]) {
|
||
2 years ago
|
this._file(files[0]);
|
||
|
event.target.value = '';
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
async _file(file) {
|
||
2 years ago
|
this.templateData = null;
|
||
|
this.importData = null;
|
||
|
this.$store.commit('loader/MutMessage', 'Loading excel file');
|
||
|
let i = 0;
|
||
3 years ago
|
const int = setInterval(() => {
|
||
2 years ago
|
this.$store.commit('loader/MutMessage', `Loading excel file${'.'.repeat(++i % 4)}`);
|
||
|
}, 1000);
|
||
|
this.dropOrUpload = false;
|
||
|
const reader = new FileReader();
|
||
|
this.filename = file.name;
|
||
3 years ago
|
|
||
2 years ago
|
reader.onload = async e => {
|
||
|
const ab = e.target.result;
|
||
|
await this.parseAndExtractData('file', ab, file.name);
|
||
|
this.$store.commit('loader/MutMessage', null);
|
||
3 years ago
|
|
||
2 years ago
|
clearInterval(int);
|
||
|
};
|
||
3 years ago
|
|
||
2 years ago
|
const handleEvent = event => {
|
||
|
this.$store.commit('loader/MutMessage', `${event.type}: ${event.loaded} bytes transferred`);
|
||
|
};
|
||
3 years ago
|
|
||
2 years ago
|
reader.addEventListener('progress', handleEvent);
|
||
|
reader.onerror = e => {
|
||
|
console.log('error', e);
|
||
|
this.$store.commit('loader/MutClear');
|
||
|
};
|
||
|
reader.readAsArrayBuffer(file);
|
||
3 years ago
|
},
|
||
3 years ago
|
|
||
|
async parseAndExtractData(type, val, name) {
|
||
3 years ago
|
try {
|
||
2 years ago
|
let templateGenerator;
|
||
|
this.templateData = null;
|
||
|
this.importData = null;
|
||
3 years ago
|
switch (type) {
|
||
|
case 'file':
|
||
2 years ago
|
templateGenerator = new ExcelTemplateAdapter(name, val, this.parserConfig);
|
||
|
break;
|
||
3 years ago
|
case 'url':
|
||
2 years ago
|
templateGenerator = new ExcelUrlTemplateAdapter(
|
||
|
val,
|
||
|
this.$store,
|
||
|
this.parserConfig,
|
||
|
this.$api,
|
||
|
this.quickImportType
|
||
|
);
|
||
|
break;
|
||
3 years ago
|
}
|
||
2 years ago
|
await templateGenerator.init();
|
||
|
templateGenerator.parse();
|
||
|
this.templateData = templateGenerator.getTemplate();
|
||
|
this.importData = templateGenerator.getData();
|
||
|
this.templateEditorModal = true;
|
||
3 years ago
|
} catch (e) {
|
||
2 years ago
|
console.log(e);
|
||
|
this.$toast.error(await this._extractSdkResponseErrorMsg(e)).goAway(3000);
|
||
3 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
dropHandler(ev) {
|
||
2 years ago
|
this.dragOver = false;
|
||
|
let file;
|
||
3 years ago
|
if (ev.dataTransfer.items) {
|
||
|
// Use DataTransferItemList interface to access the file(s)
|
||
|
if (ev.dataTransfer.items.length && ev.dataTransfer.items[0].kind === 'file') {
|
||
2 years ago
|
file = ev.dataTransfer.items[0].getAsFile();
|
||
3 years ago
|
}
|
||
|
} else if (ev.dataTransfer.files.length) {
|
||
2 years ago
|
file = ev.dataTransfer.files[0];
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (!file) {
|
||
2 years ago
|
return;
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (this.quickImportType === 'excel') {
|
||
|
if (!/.*\.(xls|xlsx|xlsm|ods|ots)/.test(file.name)) {
|
||
2 years ago
|
return this.$toast.error(this.errorMessages.importExcel).goAway(3000);
|
||
3 years ago
|
}
|
||
|
} else if (this.quickImportType === 'csv') {
|
||
|
if (!/.*\.(csv)/.test(file.name)) {
|
||
2 years ago
|
return this.$toast.error(this.errorMessages.importCSV).goAway(3000);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
2 years ago
|
this._file(file);
|
||
3 years ago
|
},
|
||
|
dragOverHandler(ev) {
|
||
|
// Prevent default behavior (Prevent file from being opened)
|
||
2 years ago
|
ev.preventDefault();
|
||
3 years ago
|
},
|
||
|
|
||
|
async loadUrl() {
|
||
3 years ago
|
if ((this.$refs.form && !this.$refs.form.validate()) || !this.url) {
|
||
2 years ago
|
return;
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
this.$store.commit('loader/MutMessage', 'Loading excel file from url');
|
||
3 years ago
|
|
||
2 years ago
|
let i = 0;
|
||
3 years ago
|
const int = setInterval(() => {
|
||
2 years ago
|
this.$store.commit('loader/MutMessage', `Loading excel file${'.'.repeat(++i % 4)}`);
|
||
|
}, 1000);
|
||
3 years ago
|
|
||
2 years ago
|
this.dropOrUpload = false;
|
||
3 years ago
|
|
||
2 years ago
|
await this.parseAndExtractData('url', this.url, '');
|
||
|
clearInterval(int);
|
||
|
this.$store.commit('loader/MutClear');
|
||
|
},
|
||
|
},
|
||
|
};
|
||
3 years ago
|
</script>
|
||
|
|
||
|
<style scoped>
|
||
3 years ago
|
.nc-droppable {
|
||
|
width: 100%;
|
||
|
min-height: 200px;
|
||
|
border-radius: 4px;
|
||
3 years ago
|
border: 2px dashed #ddd;
|
||
|
}
|
||
|
|
||
3 years ago
|
.nc-excel-import-tab-item {
|
||
3 years ago
|
min-height: 400px;
|
||
|
padding: 20px;
|
||
|
display: flex;
|
||
|
align-items: stretch;
|
||
3 years ago
|
width: 100%;
|
||
|
}
|
||
|
|
||
3 years ago
|
.nc-excel-import-options {
|
||
2 years ago
|
transition: 0.4s max-height;
|
||
3 years ago
|
overflow: hidden;
|
||
3 years ago
|
}
|
||
3 years ago
|
</style>
|