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.
383 lines
12 KiB
383 lines
12 KiB
3 years ago
|
<template>
|
||
3 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>
|
||
|
<v-icon small class="mr-1">
|
||
|
mdi-file-upload-outline
|
||
3 years ago
|
</v-icon>
|
||
3 years ago
|
<span class="caption text-capitalize">Upload</span>
|
||
|
</v-tab>
|
||
|
<v-tab>
|
||
|
<v-icon small class="mr-1">
|
||
|
mdi-link-variant
|
||
|
</v-icon>
|
||
|
<span class="caption text-capitalize">URL</span>
|
||
|
</v-tab>
|
||
|
|
||
|
<v-tab-item>
|
||
|
<div class="nc-excel-import-tab-item ">
|
||
|
<div
|
||
|
class="nc-droppable d-flex align-center justify-center flex-column"
|
||
|
:style="{
|
||
3 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"
|
||
|
>
|
||
|
<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"
|
||
3 years ago
|
:label="quickImportType == 'excel' ? $t('msg.info.excelURL') : $t('msg.info.csvURL') "
|
||
3 years ago
|
class="caption"
|
||
|
outlined
|
||
|
dense
|
||
3 years ago
|
:rules="[v => !!v || $t('general.required') ]"
|
||
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>
|
||
|
<div class="mb-2 pt-2 nc-excel-import-options" :style="{ maxHeight: showMore ? '100px' : '0'}">
|
||
|
<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>
|
||
|
<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)"
|
||
|
>
|
||
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)"
|
||
|
>
|
||
3 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">
|
||
3 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"
|
||
3 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'
|
||
3 years ago
|
import TemplateEditor from '~/components/templates/Editor'
|
||
|
import CreateProjectFromTemplateBtn from '~/components/templates/CreateProjectFromTemplateBtn'
|
||
3 years ago
|
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,
|
||
|
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: {
|
||
|
maxRowsToParse: 500
|
||
3 years ago
|
},
|
||
|
filename: ''
|
||
3 years ago
|
}
|
||
|
},
|
||
|
computed: {
|
||
|
dropOrUpload: {
|
||
|
set(v) {
|
||
|
this.$emit('input', v)
|
||
|
},
|
||
|
get() {
|
||
|
return this.value
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
mounted() {
|
||
3 years ago
|
if (this.$route && this.$route.query && this.$route.query.excelUrl) {
|
||
|
this.url = this.$route.query.excelUrl
|
||
|
this.loadUrl()
|
||
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
methods: {
|
||
3 years ago
|
|
||
|
selectFile() {
|
||
|
this.$refs.file.files = null
|
||
|
this.$refs.file.click()
|
||
|
},
|
||
|
|
||
3 years ago
|
_change(event) {
|
||
|
const files = event.target.files
|
||
3 years ago
|
if (files && files[0]) {
|
||
|
this._file(files[0])
|
||
3 years ago
|
event.target.value = ''
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
async _file(file) {
|
||
3 years ago
|
this.templateData = null
|
||
|
this.importData = null
|
||
3 years ago
|
this.$store.commit('loader/MutMessage', 'Loading excel file')
|
||
3 years ago
|
let i = 0
|
||
|
const int = setInterval(() => {
|
||
3 years ago
|
this.$store.commit('loader/MutMessage', `Loading excel file${'.'.repeat(++i % 4)}`)
|
||
3 years ago
|
}, 1000)
|
||
3 years ago
|
this.dropOrUpload = false
|
||
3 years ago
|
const reader = new FileReader()
|
||
3 years ago
|
this.filename = file.name
|
||
3 years ago
|
|
||
|
reader.onload = async(e) => {
|
||
3 years ago
|
const ab = e.target.result
|
||
3 years ago
|
await this.parseAndExtractData('file', ab, file.name)
|
||
3 years ago
|
this.$store.commit('loader/MutMessage', null)
|
||
|
|
||
3 years ago
|
clearInterval(int)
|
||
|
}
|
||
|
|
||
|
const handleEvent = (event) => {
|
||
3 years ago
|
this.$store.commit('loader/MutMessage', `${event.type}: ${event.loaded} bytes transferred`)
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
reader.addEventListener('progress', handleEvent)
|
||
3 years ago
|
reader.onerror = (e) => {
|
||
|
console.log('error', e)
|
||
3 years ago
|
this.$store.commit('loader/MutClear')
|
||
3 years ago
|
}
|
||
|
reader.readAsArrayBuffer(file)
|
||
3 years ago
|
},
|
||
3 years ago
|
|
||
|
async parseAndExtractData(type, val, name) {
|
||
3 years ago
|
try {
|
||
|
let templateGenerator
|
||
|
this.templateData = null
|
||
|
this.importData = null
|
||
|
switch (type) {
|
||
|
case 'file':
|
||
|
templateGenerator = new ExcelTemplateAdapter(name, val, this.parserConfig)
|
||
|
break
|
||
|
case 'url':
|
||
3 years ago
|
templateGenerator = new ExcelUrlTemplateAdapter(val, this.$store, this.parserConfig, this.$api)
|
||
3 years ago
|
break
|
||
|
}
|
||
|
await templateGenerator.init()
|
||
|
templateGenerator.parse()
|
||
|
this.templateData = templateGenerator.getTemplate()
|
||
|
this.importData = templateGenerator.getData()
|
||
|
this.templateEditorModal = true
|
||
|
} catch (e) {
|
||
|
console.log(e)
|
||
3 years ago
|
this.$toast
|
||
|
.error(await this._extractSdkResponseErrorMsg(e))
|
||
|
.goAway(3000)
|
||
3 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
dropHandler(ev) {
|
||
3 years ago
|
this.dragOver = false
|
||
3 years ago
|
let file
|
||
|
if (ev.dataTransfer.items) {
|
||
|
// Use DataTransferItemList interface to access the file(s)
|
||
|
if (ev.dataTransfer.items.length && ev.dataTransfer.items[0].kind === 'file') {
|
||
|
file = ev.dataTransfer.items[0].getAsFile()
|
||
|
}
|
||
|
} else if (ev.dataTransfer.files.length) {
|
||
|
file = ev.dataTransfer.files[0]
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (!file) {
|
||
|
return
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (this.quickImportType === 'excel') {
|
||
|
if (!/.*\.(xls|xlsx|xlsm|ods|ots)/.test(file.name)) {
|
||
|
return this.$toast.error('Dropped file is not an accepted file type. The accepted file types are .xls, .xlsx, .xlsm, .ods, .ots!').goAway(3000)
|
||
|
}
|
||
|
} else if (this.quickImportType === 'csv') {
|
||
|
if (!/.*\.(csv)/.test(file.name)) {
|
||
|
return this.$toast.error('Dropped file is not an accepted file type. The accepted file type is .csv!').goAway(3000)
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
this._file(file)
|
||
3 years ago
|
},
|
||
|
dragOverHandler(ev) {
|
||
|
// Prevent default behavior (Prevent file from being opened)
|
||
|
ev.preventDefault()
|
||
3 years ago
|
},
|
||
|
|
||
|
async loadUrl() {
|
||
3 years ago
|
if ((this.$refs.form && !this.$refs.form.validate()) || !this.url) {
|
||
3 years ago
|
return
|
||
|
}
|
||
|
|
||
|
this.$store.commit('loader/MutMessage', 'Loading excel file from url')
|
||
3 years ago
|
|
||
|
let i = 0
|
||
|
const int = setInterval(() => {
|
||
3 years ago
|
this.$store.commit('loader/MutMessage', `Loading excel file${'.'.repeat(++i % 4)}`)
|
||
3 years ago
|
}, 1000)
|
||
|
|
||
|
this.dropOrUpload = false
|
||
|
|
||
|
await this.parseAndExtractData('url', this.url, '')
|
||
|
clearInterval(int)
|
||
3 years ago
|
this.$store.commit('loader/MutClear')
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
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 {
|
||
|
transition: .4s max-height;
|
||
3 years ago
|
overflow: hidden;
|
||
3 years ago
|
}
|
||
3 years ago
|
</style>
|