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.
1406 lines
54 KiB
1406 lines
54 KiB
3 years ago
|
<template>
|
||
3 years ago
|
<div class="h-100" style="min-height: 500px">
|
||
3 years ago
|
<v-toolbar v-if="!viewMode" class="elevation-0">
|
||
3 years ago
|
<slot name="toolbar" :valid="valid">
|
||
3 years ago
|
<v-tooltip bottom>
|
||
3 years ago
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-btn small outlined v-on="on" @click="$toast.info('Happy hacking!').goAway(3000)">
|
||
|
<v-icon small class="mr-1"> mdi-file-excel-outline </v-icon>
|
||
3 years ago
|
Import
|
||
|
</v-btn>
|
||
|
</template>
|
||
|
<span class="caption">Create template from Excel</span>
|
||
|
</v-tooltip>
|
||
|
|
||
|
<v-spacer />
|
||
|
|
||
3 years ago
|
<v-icon class="mr-3" @click="helpModal = true"> mdi-information-outline </v-icon>
|
||
3 years ago
|
|
||
3 years ago
|
<v-btn small outlined class="mr-1" @click="project = { tables: [] }">
|
||
3 years ago
|
<v-icon small> mdi-close </v-icon>
|
||
3 years ago
|
Reset
|
||
|
</v-btn>
|
||
3 years ago
|
<v-btn small outlined class="mr-1" @click="createTableClick">
|
||
3 years ago
|
<v-icon small> mdi-plus </v-icon>
|
||
3 years ago
|
New table
|
||
|
</v-btn>
|
||
3 years ago
|
<v-btn color="primary" outlined small class="mr-1" :loading="loading" :disabled="loading" @click="saveTemplate">
|
||
|
{{ id || localId ? 'Update in' : 'Submit to' }} NocoDB
|
||
3 years ago
|
</v-btn>
|
||
|
</slot>
|
||
3 years ago
|
</v-toolbar>
|
||
3 years ago
|
<v-divider class="mt-6" />
|
||
3 years ago
|
<v-container class="text-center" style="height: calc(100% - 64px); overflow-y: auto">
|
||
3 years ago
|
<v-form ref="form" v-model="valid">
|
||
3 years ago
|
<v-row fluid class="justify-center">
|
||
|
<v-col cols="12">
|
||
3 years ago
|
<v-card class="elevation-0">
|
||
3 years ago
|
<v-card-text>
|
||
3 years ago
|
<p v-if="project.tables && quickImportType === 'excel'" class="caption grey--text mt-4">
|
||
3 years ago
|
{{ project.tables.length }} sheet{{ project.tables.length > 1 ? 's' : '' }}
|
||
3 years ago
|
available for import
|
||
3 years ago
|
</p>
|
||
|
|
||
3 years ago
|
<v-expansion-panels
|
||
|
v-if="project.tables && project.tables.length"
|
||
|
v-model="expansionPanel"
|
||
|
:multiple="viewMode"
|
||
|
accordion
|
||
|
>
|
||
3 years ago
|
<v-expansion-panel v-for="(table, i) in project.tables" :key="i">
|
||
3 years ago
|
<v-expansion-panel-header :id="`tn_${table.table_name}`">
|
||
3 years ago
|
<v-text-field
|
||
|
v-if="editableTn[i]"
|
||
3 years ago
|
:value="table.table_name"
|
||
3 years ago
|
class="font-weight-bold"
|
||
3 years ago
|
style="max-width: 300px"
|
||
|
outlinedk
|
||
|
autofocus
|
||
|
dense
|
||
|
hide-details
|
||
3 years ago
|
@input="e => onTableNameUpdate(table, e)"
|
||
|
@click="e => viewMode || e.stopPropagation()"
|
||
3 years ago
|
@blur="$set(editableTn, i, false)"
|
||
|
@keydown.enter="$set(editableTn, i, false)"
|
||
3 years ago
|
/>
|
||
|
<span
|
||
|
v-else
|
||
3 years ago
|
class="font-weight-bold"
|
||
3 years ago
|
@click="e => viewMode || (e.stopPropagation(), $set(editableTn, i, true))"
|
||
3 years ago
|
>
|
||
|
<v-icon color="primary lighten-1">mdi-table</v-icon>
|
||
3 years ago
|
{{ table.table_name }}
|
||
3 years ago
|
</span>
|
||
|
|
||
|
<v-spacer />
|
||
3 years ago
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-icon
|
||
3 years ago
|
v-if="!viewMode && project.tables.length > 1"
|
||
3 years ago
|
class="flex-grow-0 mr-2"
|
||
|
small
|
||
|
color="grey"
|
||
|
@click.stop="deleteTable(i)"
|
||
|
v-on="on"
|
||
|
>
|
||
|
mdi-delete-outline
|
||
3 years ago
|
</v-icon>
|
||
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
3 years ago
|
<span>Delete Table</span>
|
||
|
</v-tooltip>
|
||
3 years ago
|
</v-expansion-panel-header>
|
||
|
<v-expansion-panel-content>
|
||
|
<!-- <v-toolbar>
|
||
|
<v-spacer></v-spacer>
|
||
|
<v-btn outlined small @click='showColCreateDialog(table,i)'>New Column
|
||
|
</v-btn>
|
||
|
</v-toolbar>-->
|
||
|
|
||
|
<template>
|
||
3 years ago
|
<v-simple-table v-if="table.columns.length" dense class="my-4">
|
||
3 years ago
|
<thead>
|
||
|
<tr>
|
||
3 years ago
|
<th class="caption text-left pa-1">
|
||
3 years ago
|
<!--Column Name-->
|
||
3 years ago
|
{{ $t('labels.columnName') }}
|
||
3 years ago
|
</th>
|
||
3 years ago
|
<th class="caption text-left pa-1" colspan="4">
|
||
3 years ago
|
<!--Column Type-->
|
||
3 years ago
|
{{ $t('labels.columnType') }}
|
||
3 years ago
|
</th>
|
||
|
<th />
|
||
3 years ago
|
<!-- <th class='text-center'>Related Table</th>-->
|
||
|
<!-- <th class='text-center'>Related Column</th>-->
|
||
3 years ago
|
</tr>
|
||
|
</thead>
|
||
|
<tbody>
|
||
3 years ago
|
<tr v-for="(col, j) in table.columns" :key="j" :data-exp="i">
|
||
|
<td class="pa-1 text-left" :style="{ width: viewMode ? '33%' : '15%' }">
|
||
3 years ago
|
<span v-if="viewMode" class="body-1">
|
||
3 years ago
|
{{ col.column_name }}
|
||
3 years ago
|
</span>
|
||
|
|
||
|
<v-text-field
|
||
|
v-else
|
||
3 years ago
|
:ref="`cn_${table.table_name}_${j}`"
|
||
|
:value="col.column_name"
|
||
3 years ago
|
outlined
|
||
|
dense
|
||
|
class="caption"
|
||
3 years ago
|
:placeholder="$t('labels.columnName')"
|
||
3 years ago
|
hide-details="auto"
|
||
3 years ago
|
:rules="[
|
||
3 years ago
|
v => !!v || 'Column name required',
|
||
|
v =>
|
||
|
!table.columns.some(c => c !== col && c.column_name === v) ||
|
||
|
'Duplicate column not allowed',
|
||
3 years ago
|
]"
|
||
3 years ago
|
@input="e => onColumnNameUpdate(col, e, table.table_name)"
|
||
3 years ago
|
/>
|
||
|
</td>
|
||
|
|
||
|
<template v-if="viewMode">
|
||
|
<td
|
||
3 years ago
|
:style="{
|
||
3 years ago
|
width: (viewMode && isRelation(col)) || isLookupOrRollup(col) ? '33%' : '',
|
||
3 years ago
|
}"
|
||
3 years ago
|
:colspan="isRelation(col) || isLookupOrRollup(col) ? 3 : 5"
|
||
3 years ago
|
class="text-left"
|
||
|
>
|
||
|
<v-icon small>
|
||
|
{{ getIcon(col.uidt) }}
|
||
|
</v-icon>
|
||
|
<span class="caption">{{ col.uidt }}</span>
|
||
|
</td>
|
||
3 years ago
|
<td v-if="isRelation(col) || isLookupOrRollup(col)" class="text-left">
|
||
3 years ago
|
<span
|
||
|
v-if="isRelation(col)"
|
||
|
class="caption pointer primary--text"
|
||
3 years ago
|
@click="navigateToTable(col.ref_table_name)"
|
||
3 years ago
|
>
|
||
3 years ago
|
{{ col.ref_table_name }}
|
||
3 years ago
|
</span>
|
||
3 years ago
|
<template v-else-if="isLookup(col)">
|
||
3 years ago
|
<span
|
||
|
class="caption pointer primary--text"
|
||
3 years ago
|
@click="navigateToTable(col.ref_table_name && col.ref_table_name.table_name)"
|
||
3 years ago
|
>
|
||
3 years ago
|
{{ col.ref_table_name && col.ref_table_name.table_name }}
|
||
3 years ago
|
</span>
|
||
3 years ago
|
<span class="caption">({{ col.ref_column_name }})</span>
|
||
3 years ago
|
</template>
|
||
|
|
||
3 years ago
|
<template v-else-if="isRollup(col)">
|
||
3 years ago
|
<span
|
||
|
class="caption pointer primary--text"
|
||
3 years ago
|
@click="navigateToTable(col.ref_table_name && col.ref_table_name.table_name)"
|
||
3 years ago
|
>
|
||
3 years ago
|
{{ col.ref_table_name && col.ref_table_name.table_name }}
|
||
3 years ago
|
</span>
|
||
|
<span class="caption">({{ col.fn }})</span>
|
||
3 years ago
|
</template>
|
||
|
</td>
|
||
|
</template>
|
||
|
|
||
|
<template v-else>
|
||
3 years ago
|
<td class="pa-1 text-left" style="width: 200px; max-width: 200px">
|
||
3 years ago
|
<v-autocomplete
|
||
3 years ago
|
:ref="`uidt_${table.table_name}_${j}`"
|
||
3 years ago
|
style="max-width: 200px"
|
||
3 years ago
|
:value="col.uidt"
|
||
3 years ago
|
placeholder="Column Data Type"
|
||
3 years ago
|
outlined
|
||
|
dense
|
||
|
class="caption"
|
||
|
hide-details="auto"
|
||
3 years ago
|
:rules="[v => !!v || 'Column data type required']"
|
||
3 years ago
|
:items="
|
||
|
col.uidt === 'ForeignKey'
|
||
|
? [
|
||
3 years ago
|
...uiTypes,
|
||
|
{
|
||
|
name: 'ForeignKey',
|
||
|
icon: 'mdi-link-variant',
|
||
|
virtual: 1,
|
||
|
},
|
||
|
]
|
||
3 years ago
|
: uiTypes
|
||
|
"
|
||
3 years ago
|
item-text="name"
|
||
|
item-value="name"
|
||
3 years ago
|
@input="v => onUidtChange(col.uidt, v, col, table)"
|
||
3 years ago
|
>
|
||
3 years ago
|
<template #item="{ item: { name } }">
|
||
3 years ago
|
<v-chip v-if="colors[name]" :color="colors[name]" small>
|
||
3 years ago
|
{{ name }}
|
||
|
</v-chip>
|
||
3 years ago
|
<span v-else class="caption">{{ name }}</span>
|
||
3 years ago
|
</template>
|
||
3 years ago
|
<template #selection="{ item: { name } }">
|
||
3 years ago
|
<v-chip v-if="colors[name]" :color="colors[name]" small style="max-width: 100px">
|
||
3 years ago
|
{{ name }}
|
||
|
</v-chip>
|
||
3 years ago
|
<span v-else class="caption">{{ name }}</span>
|
||
3 years ago
|
</template>
|
||
|
</v-autocomplete>
|
||
|
</td>
|
||
|
|
||
3 years ago
|
<template v-if="isRelation(col) || isLookupOrRollup(col)">
|
||
3 years ago
|
<td class="pa-1 text-left">
|
||
3 years ago
|
<v-autocomplete
|
||
3 years ago
|
:value="col.ref_table_name"
|
||
3 years ago
|
placeholder="Related table"
|
||
|
outlined
|
||
|
class="caption"
|
||
|
dense
|
||
|
hide-details="auto"
|
||
3 years ago
|
:rules="[v => !!v || 'Related table name required', ...getRules(col, table)]"
|
||
3 years ago
|
:items="
|
||
|
isLookupOrRollup(col)
|
||
3 years ago
|
? getRelatedTables(table.table_name, isRollup(col))
|
||
3 years ago
|
: project.tables
|
||
|
"
|
||
|
:item-text="
|
||
3 years ago
|
t => (isLookupOrRollup(col) ? `${t.table_name} (${t.type})` : t.table_name)
|
||
3 years ago
|
"
|
||
3 years ago
|
:item-value="t => (isLookupOrRollup(col) ? t : t.table_name)"
|
||
3 years ago
|
:value-comparator="compareRel"
|
||
3 years ago
|
@input="v => onRtnChange(col.ref_table_name, v, col, table)"
|
||
3 years ago
|
/>
|
||
|
</td>
|
||
|
|
||
3 years ago
|
<td v-if="isRelation(col)" class="pa-1">
|
||
3 years ago
|
<template v-if="col.uidt !== 'ForeignKey'">
|
||
3 years ago
|
<span v-if="viewMode" class="caption">
|
||
3 years ago
|
<!-- {{ col.type }}-->
|
||
|
</span>
|
||
|
<v-autocomplete
|
||
|
v-else
|
||
|
:value="col.type"
|
||
|
placeholder="Relation Type"
|
||
|
outlined
|
||
|
class="caption"
|
||
|
dense
|
||
|
hide-details="auto"
|
||
3 years ago
|
:rules="[v => !!v || 'Relation type required']"
|
||
3 years ago
|
:items="[
|
||
|
{ text: 'Many To Many', value: 'mm' },
|
||
|
{ text: 'Has Many', value: 'hm' },
|
||
|
]"
|
||
3 years ago
|
@input="v => onRTypeChange(col.type, v, col, table)"
|
||
3 years ago
|
/>
|
||
|
</template>
|
||
3 years ago
|
</td>
|
||
3 years ago
|
<td v-if="isLookupOrRollup(col)" class="pa-1">
|
||
3 years ago
|
<span v-if="viewMode" class="caption">
|
||
3 years ago
|
{{ col.ref_column_name }}
|
||
3 years ago
|
</span>
|
||
|
|
||
|
<v-autocomplete
|
||
|
v-else
|
||
3 years ago
|
v-model="col.ref_column_name"
|
||
3 years ago
|
placeholder="Related table column"
|
||
|
outlined
|
||
|
dense
|
||
|
class="caption"
|
||
|
hide-details="auto"
|
||
3 years ago
|
:rules="[v => !!v || 'Related column name required']"
|
||
3 years ago
|
:items="
|
||
|
(
|
||
|
project.tables.find(
|
||
3 years ago
|
t =>
|
||
3 years ago
|
t.table_name ===
|
||
3 years ago
|
((col.ref_table_name && col.ref_table_name.table_name) ||
|
||
3 years ago
|
col.ref_table_name)
|
||
3 years ago
|
) || { columns: [] }
|
||
3 years ago
|
).columns.filter(v => !isVirtual(v))
|
||
3 years ago
|
"
|
||
3 years ago
|
item-text="column_name"
|
||
|
item-value="column_name"
|
||
3 years ago
|
/>
|
||
|
</td>
|
||
3 years ago
|
<td v-if="isRollup(col)" class="pa-1">
|
||
3 years ago
|
<span v-if="viewMode" class="caption">
|
||
3 years ago
|
{{ col.fn }}
|
||
|
</span>
|
||
|
|
||
|
<v-autocomplete
|
||
|
v-else
|
||
|
v-model="col.fn"
|
||
|
placeholder="Rollup function"
|
||
|
outlined
|
||
|
dense
|
||
|
class="caption"
|
||
|
hide-details="auto"
|
||
3 years ago
|
:rules="[v => !!v || 'Rollup aggregate function name required']"
|
||
3 years ago
|
:items="rollupFnList"
|
||
|
/>
|
||
|
</td>
|
||
|
</template>
|
||
3 years ago
|
<template v-if="isSelect(col)">
|
||
3 years ago
|
<td class="pa-1 text-left" colspan="2">
|
||
3 years ago
|
<span v-if="viewMode" class="caption">
|
||
3 years ago
|
{{ col.dtxp }}
|
||
|
</span>
|
||
|
<v-text-field
|
||
|
v-model="col.dtxp"
|
||
|
placeholder="Select options"
|
||
|
outlined
|
||
|
class="caption"
|
||
|
dense
|
||
|
hide-details
|
||
|
/>
|
||
|
</td>
|
||
|
</template>
|
||
3 years ago
|
<td
|
||
3 years ago
|
v-if="!isRollup(col)"
|
||
|
:colspan="
|
||
3 years ago
|
isLookupOrRollup(col) || isRelation(col) || isSelect(col)
|
||
3 years ago
|
? isRollup(col)
|
||
|
? 0
|
||
|
: 1
|
||
|
: 3
|
||
|
"
|
||
3 years ago
|
/>
|
||
3 years ago
|
<td style="max-width: 50px; width: 50px">
|
||
3 years ago
|
<v-tooltip v-if="!viewMode && j == 0" bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<x-icon small class="mr-1" color="primary" v-on="on"> mdi-key-star </x-icon>
|
||
3 years ago
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
|
<span>Primary Value</span>
|
||
|
</v-tooltip>
|
||
|
<v-tooltip v-else bottom>
|
||
3 years ago
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-icon
|
||
3 years ago
|
class="flex-grow-0"
|
||
|
small
|
||
|
color="grey"
|
||
3 years ago
|
@click.stop="deleteTableColumn(i, j, col, table)"
|
||
3 years ago
|
v-on="on"
|
||
|
>
|
||
|
mdi-delete-outline
|
||
|
</v-icon>
|
||
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
3 years ago
|
<span>Delete Column</span>
|
||
|
</v-tooltip>
|
||
3 years ago
|
</td>
|
||
|
</template>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</v-simple-table>
|
||
|
|
||
|
<div v-if="!viewMode" class="text-center">
|
||
3 years ago
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-icon class="mx-2" small @click="addNewColumnRow(table, 'Number')" v-on="on">
|
||
|
{{ getIcon('Number') }}
|
||
3 years ago
|
</v-icon>
|
||
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
|
<span>Add Number Column</span>
|
||
|
</v-tooltip>
|
||
|
|
||
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-icon class="mx-2" small @click="addNewColumnRow(table, 'SingleLineText')" v-on="on">
|
||
|
{{ getIcon('SingleLineText') }}
|
||
3 years ago
|
</v-icon>
|
||
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
|
<span>Add SingleLineText Column</span>
|
||
|
</v-tooltip>
|
||
|
|
||
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-icon class="mx-2" small @click="addNewColumnRow(table, 'LongText')" v-on="on">
|
||
|
{{ getIcon('LongText') }}
|
||
3 years ago
|
</v-icon>
|
||
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
|
<span>Add LongText Column</span>
|
||
|
</v-tooltip>
|
||
|
|
||
3 years ago
|
<!-- <v-tooltip bottom>
|
||
3 years ago
|
<template #activator="{ on }">
|
||
|
<v-icon
|
||
|
class="mx-2"
|
||
|
small
|
||
|
@click="addNewColumnRow(table, 'LinkToAnotherRecord')"
|
||
|
v-on="on"
|
||
|
>
|
||
|
{{ getIcon("LinkToAnotherRecord") }}
|
||
|
</v-icon>
|
||
|
</template>
|
||
|
<span>Add LinkToAnotherRecord Column</span>
|
||
|
</v-tooltip>
|
||
|
|
||
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
|
<v-icon
|
||
|
class="mx-2"
|
||
|
small
|
||
|
@click="addNewColumnRow(table, 'Lookup')"
|
||
|
v-on="on"
|
||
|
>
|
||
|
{{ getIcon("Lookup") }}
|
||
|
</v-icon>
|
||
|
</template>
|
||
|
<span>Add Lookup Column</span>
|
||
|
</v-tooltip>
|
||
|
|
||
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
|
<v-icon
|
||
|
class="mx-2"
|
||
|
small
|
||
|
@click="addNewColumnRow(table, 'Rollup')"
|
||
|
v-on="on"
|
||
|
>
|
||
|
{{ getIcon("Rollup") }}
|
||
|
</v-icon>
|
||
|
</template>
|
||
|
<span>Add Rollup Column</span>
|
||
3 years ago
|
</v-tooltip> -->
|
||
3 years ago
|
|
||
|
<v-tooltip bottom>
|
||
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-btn class="mx-2" small @click="addNewColumnRow(table)" v-on="on"> + column </v-btn>
|
||
3 years ago
|
</template>
|
||
|
<!-- TODO: i18n -->
|
||
|
<span>Add Other Column</span>
|
||
|
</v-tooltip>
|
||
3 years ago
|
</div>
|
||
|
</template>
|
||
|
</v-expansion-panel-content>
|
||
|
</v-expansion-panel>
|
||
|
</v-expansion-panels>
|
||
3 years ago
|
<!-- Disable Gradient Generator at time being -->
|
||
|
<!-- <div v-if="!viewMode" class="mx-auto" style="max-width: 600px">
|
||
|
<template v-if="!quickImport">
|
||
3 years ago
|
<gradient-generator
|
||
|
v-model="project.image_url"
|
||
|
class="d-100 mt-4"
|
||
|
/>
|
||
3 years ago
|
|
||
|
<v-row>
|
||
|
<v-col>
|
||
|
<v-text-field
|
||
|
v-model="project.category"
|
||
3 years ago
|
:rules="[(v) => !!v || 'Category name required']"
|
||
3 years ago
|
class="caption"
|
||
|
outlined
|
||
|
dense
|
||
|
label="Project Category"
|
||
|
/>
|
||
|
</v-col>
|
||
|
<v-col>
|
||
|
<v-text-field
|
||
|
v-model="project.tags"
|
||
|
class="caption"
|
||
|
outlined
|
||
|
dense
|
||
|
label="Project Tags"
|
||
|
/>
|
||
|
</v-col>
|
||
|
</v-row>
|
||
|
<div>
|
||
|
<v-textarea
|
||
|
v-model="project.description"
|
||
3 years ago
|
class="caption"
|
||
|
outlined
|
||
|
dense
|
||
3 years ago
|
label="Project Description"
|
||
|
@click="counter++"
|
||
3 years ago
|
/>
|
||
3 years ago
|
</div>
|
||
|
</template>
|
||
3 years ago
|
</div> -->
|
||
3 years ago
|
</v-card-text>
|
||
|
</v-card>
|
||
|
</v-col>
|
||
|
</v-row>
|
||
|
</v-form>
|
||
|
</v-container>
|
||
|
|
||
|
<v-dialog v-model="createTablesDialog" max-width="500">
|
||
|
<v-card>
|
||
3 years ago
|
<v-card-title>
|
||
|
<!--Enter table name-->
|
||
3 years ago
|
{{ $t('msg.info.enterTableName') }}
|
||
3 years ago
|
</v-card-title>
|
||
3 years ago
|
<v-card-text>
|
||
|
<v-text-field
|
||
|
v-model="tableNamesInput"
|
||
|
autofocus
|
||
|
hide-details
|
||
|
dense
|
||
|
outlined
|
||
|
label="Enter comma separated table names"
|
||
|
@keydown.enter="addTables"
|
||
|
/>
|
||
|
</v-card-text>
|
||
|
<v-card-actions>
|
||
|
<v-spacer />
|
||
3 years ago
|
<v-btn outlined small @click="createTablesDialog = false">
|
||
3 years ago
|
<!-- Cancel -->
|
||
3 years ago
|
{{ $t('general.cancel') }}
|
||
3 years ago
|
</v-btn>
|
||
|
<v-btn outlined color="primary" small @click="addTables">
|
||
3 years ago
|
<!-- Save -->
|
||
3 years ago
|
{{ $t('general.save') }}
|
||
3 years ago
|
</v-btn>
|
||
|
</v-card-actions>
|
||
|
</v-card>
|
||
|
</v-dialog>
|
||
|
<v-dialog v-model="createTableColumnsDialog" max-width="500">
|
||
|
<v-card>
|
||
|
<v-card-title>Enter column name</v-card-title>
|
||
|
<v-card-text>
|
||
|
<v-text-field
|
||
|
v-model="columnNamesInput"
|
||
|
autofocus
|
||
|
dense
|
||
|
outlined
|
||
|
hide-details
|
||
|
label="Enter comma separated column names"
|
||
|
@keydown.enter="addColumns"
|
||
|
/>
|
||
|
</v-card-text>
|
||
|
<v-card-actions>
|
||
|
<v-spacer />
|
||
3 years ago
|
<v-btn outlined small @click="createTableColumnsDialog = false">
|
||
3 years ago
|
<!-- Cancel -->
|
||
3 years ago
|
{{ $t('general.cancel') }}
|
||
3 years ago
|
</v-btn>
|
||
|
<v-btn outlined color="primary" small @click="addColumns">
|
||
3 years ago
|
<!-- Save -->
|
||
3 years ago
|
{{ $t('general.save') }}
|
||
3 years ago
|
</v-btn>
|
||
|
</v-card-actions>
|
||
|
</v-card>
|
||
|
</v-dialog>
|
||
|
|
||
|
<help v-model="helpModal" />
|
||
|
|
||
|
<v-tooltip v-if="!viewMode" left>
|
||
3 years ago
|
<template #activator="{ on }">
|
||
3 years ago
|
<v-btn fixed fab large color="primary" right style="top: 45%" @click="createTableClick" v-on="on">
|
||
3 years ago
|
<v-icon>mdi-plus</v-icon>
|
||
|
</v-btn>
|
||
|
</template>
|
||
3 years ago
|
<span class="caption">
|
||
|
<!--Add new table-->
|
||
3 years ago
|
{{ $t('tooltip.addTable') }}
|
||
3 years ago
|
</span>
|
||
3 years ago
|
</v-tooltip>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
3 years ago
|
import { isVirtualCol } from 'nocodb-sdk';
|
||
|
import { uiTypes, getUIDTIcon, UITypes } from '~/components/project/spreadsheet/helpers/uiTypes';
|
||
|
import GradientGenerator from '~/components/templates/GradientGenerator';
|
||
|
import Help from '~/components/templates/Help';
|
||
|
|
||
|
const LinkToAnotherRecord = 'LinkToAnotherRecord';
|
||
|
const Lookup = 'Lookup';
|
||
|
const Rollup = 'Rollup';
|
||
|
const defaultColProp = {};
|
||
3 years ago
|
|
||
|
export default {
|
||
3 years ago
|
name: 'TemplateEditor',
|
||
3 years ago
|
components: { Help, GradientGenerator },
|
||
3 years ago
|
props: {
|
||
3 years ago
|
id: [Number, String],
|
||
3 years ago
|
viewMode: Boolean,
|
||
3 years ago
|
projectTemplate: Object,
|
||
3 years ago
|
quickImport: Boolean,
|
||
3 years ago
|
quickImportType: String,
|
||
3 years ago
|
},
|
||
|
data: () => ({
|
||
3 years ago
|
loading: false,
|
||
3 years ago
|
localId: null,
|
||
3 years ago
|
valid: false,
|
||
3 years ago
|
url: '',
|
||
3 years ago
|
githubConfigForm: false,
|
||
|
helpModal: false,
|
||
|
editableTn: {},
|
||
|
expansionPanel: 0,
|
||
|
project: {
|
||
3 years ago
|
name: 'Project name',
|
||
3 years ago
|
tables: [],
|
||
3 years ago
|
},
|
||
3 years ago
|
tableNamesInput: '',
|
||
|
columnNamesInput: '',
|
||
3 years ago
|
createTablesDialog: false,
|
||
|
createTableColumnsDialog: false,
|
||
|
selectedTable: null,
|
||
3 years ago
|
uiTypes: uiTypes.filter(t => !isVirtualCol(t.name)),
|
||
3 years ago
|
rollupFnList: [
|
||
3 years ago
|
{ text: 'count', value: 'count' },
|
||
|
{ text: 'min', value: 'min' },
|
||
|
{ text: 'max', value: 'max' },
|
||
|
{ text: 'avg', value: 'avg' },
|
||
|
{ text: 'min', value: 'min' },
|
||
|
{ text: 'sum', value: 'sum' },
|
||
|
{ text: 'countDistinct', value: 'countDistinct' },
|
||
|
{ text: 'sumDistinct', value: 'sumDistinct' },
|
||
3 years ago
|
{ text: 'avgDistinct', value: 'avgDistinct' },
|
||
3 years ago
|
],
|
||
|
colors: {
|
||
3 years ago
|
LinkToAnotherRecord: 'blue lighten-5',
|
||
|
Rollup: 'pink lighten-5',
|
||
3 years ago
|
Lookup: 'green lighten-5',
|
||
|
},
|
||
3 years ago
|
}),
|
||
|
computed: {
|
||
3 years ago
|
counter: {
|
||
|
get() {
|
||
3 years ago
|
return this.$store.state.templateC;
|
||
3 years ago
|
},
|
||
|
set(c) {
|
||
3 years ago
|
this.$store.commit('mutTemplateC', c);
|
||
|
},
|
||
3 years ago
|
},
|
||
3 years ago
|
updateFilename() {
|
||
3 years ago
|
return this.url && this.url.split('/').pop();
|
||
|
},
|
||
3 years ago
|
},
|
||
|
watch: {
|
||
|
project: {
|
||
|
deep: true,
|
||
|
handler() {
|
||
|
const template = {
|
||
|
...this.project,
|
||
3 years ago
|
tables: (this.project.tables || []).map(t => {
|
||
3 years ago
|
const table = {
|
||
|
...t,
|
||
|
columns: [],
|
||
|
hasMany: [],
|
||
|
manyToMany: [],
|
||
|
belongsTo: [],
|
||
3 years ago
|
v: [],
|
||
|
};
|
||
3 years ago
|
|
||
|
for (const column of t.columns || []) {
|
||
3 years ago
|
if (this.isRelation(column)) {
|
||
3 years ago
|
if (column.type === 'hm') {
|
||
3 years ago
|
table.hasMany.push({
|
||
3 years ago
|
table_name: column.ref_table_name,
|
||
3 years ago
|
title: column.column_name,
|
||
|
});
|
||
3 years ago
|
} else if (column.type === 'mm') {
|
||
3 years ago
|
table.manyToMany.push({
|
||
3 years ago
|
ref_table_name: column.ref_table_name,
|
||
3 years ago
|
title: column.column_name,
|
||
|
});
|
||
3 years ago
|
} else if (column.uidt === UITypes.ForeignKey) {
|
||
|
table.belongsTo.push({
|
||
3 years ago
|
table_name: column.ref_table_name,
|
||
3 years ago
|
title: column.column_name,
|
||
|
});
|
||
3 years ago
|
}
|
||
|
} else if (this.isLookup(column)) {
|
||
3 years ago
|
if (column.ref_table_name) {
|
||
3 years ago
|
table.v.push({
|
||
3 years ago
|
title: column.column_name,
|
||
3 years ago
|
lk: {
|
||
3 years ago
|
ltn: column.ref_table_name.table_name,
|
||
|
type: column.ref_table_name.type,
|
||
3 years ago
|
lcn: column.ref_column_name,
|
||
|
},
|
||
|
});
|
||
3 years ago
|
}
|
||
|
} else if (this.isRollup(column)) {
|
||
3 years ago
|
if (column.ref_table_name) {
|
||
3 years ago
|
table.v.push({
|
||
3 years ago
|
title: column.column_name,
|
||
3 years ago
|
rl: {
|
||
3 years ago
|
rltn: column.ref_table_name.table_name,
|
||
|
rlcn: column.ref_column_name,
|
||
|
type: column.ref_table_name.type,
|
||
3 years ago
|
fn: column.fn,
|
||
|
},
|
||
|
});
|
||
3 years ago
|
}
|
||
|
} else {
|
||
3 years ago
|
table.columns.push(column);
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
return table;
|
||
|
}),
|
||
|
};
|
||
|
this.$emit('update:projectTemplate', template);
|
||
|
},
|
||
|
},
|
||
3 years ago
|
},
|
||
|
|
||
|
created() {
|
||
3 years ago
|
document.addEventListener('keydown', this.handleKeyDown);
|
||
3 years ago
|
},
|
||
|
destroyed() {
|
||
3 years ago
|
document.removeEventListener('keydown', this.handleKeyDown);
|
||
3 years ago
|
},
|
||
|
mounted() {
|
||
3 years ago
|
this.parseAndLoadTemplate();
|
||
|
const input = this.$refs.projec && this.$refs.project.$el.querySelector('input');
|
||
3 years ago
|
if (input) {
|
||
3 years ago
|
input.focus();
|
||
|
input.select();
|
||
3 years ago
|
}
|
||
|
},
|
||
|
methods: {
|
||
3 years ago
|
createTableClick() {
|
||
3 years ago
|
this.createTablesDialog = true;
|
||
|
this.$e('c:table:create:navdraw');
|
||
3 years ago
|
},
|
||
3 years ago
|
parseAndLoadTemplate() {
|
||
|
if (this.projectTemplate) {
|
||
3 years ago
|
this.parseTemplate(this.projectTemplate);
|
||
|
this.expansionPanel = Array.from({ length: this.project.tables.length }, (_, i) => i);
|
||
3 years ago
|
}
|
||
|
},
|
||
3 years ago
|
getIcon(type) {
|
||
3 years ago
|
return getUIDTIcon(type);
|
||
3 years ago
|
},
|
||
|
getRelatedTables(tableName, rollup = false) {
|
||
3 years ago
|
const tables = [];
|
||
3 years ago
|
for (const t of this.projectTemplate.tables) {
|
||
3 years ago
|
if (tableName === t.table_name) {
|
||
3 years ago
|
for (const hm of t.hasMany) {
|
||
3 years ago
|
const rTable = this.project.tables.find(t1 => t1.table_name === hm.table_name);
|
||
3 years ago
|
tables.push({
|
||
|
...rTable,
|
||
3 years ago
|
type: 'hm',
|
||
|
});
|
||
3 years ago
|
}
|
||
|
for (const mm of t.manyToMany) {
|
||
3 years ago
|
const rTable = this.project.tables.find(t1 => t1.table_name === mm.ref_table_name);
|
||
3 years ago
|
tables.push({
|
||
|
...rTable,
|
||
3 years ago
|
type: 'mm',
|
||
|
});
|
||
3 years ago
|
}
|
||
|
} else {
|
||
|
for (const hm of t.hasMany) {
|
||
3 years ago
|
if (hm.table_name === tableName && !rollup) {
|
||
3 years ago
|
tables.push({
|
||
|
...t,
|
||
3 years ago
|
type: 'bt',
|
||
|
});
|
||
3 years ago
|
}
|
||
|
}
|
||
|
for (const mm of t.manyToMany) {
|
||
3 years ago
|
if (mm.ref_table_name === tableName) {
|
||
3 years ago
|
tables.push({
|
||
|
...t,
|
||
3 years ago
|
type: 'mm',
|
||
|
});
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return tables;
|
||
3 years ago
|
},
|
||
|
validateAndFocus() {
|
||
|
if (!this.$refs.form.validate()) {
|
||
3 years ago
|
const input = this.$el.querySelector('.v-input.error--text');
|
||
3 years ago
|
this.expansionPanel =
|
||
|
input &&
|
||
|
input.parentElement &&
|
||
|
input.parentElement.parentElement &&
|
||
3 years ago
|
+input.parentElement.parentElement.dataset.exp;
|
||
3 years ago
|
setTimeout(() => {
|
||
3 years ago
|
input.querySelector('input,select').focus();
|
||
|
}, 500);
|
||
|
return false;
|
||
3 years ago
|
}
|
||
3 years ago
|
return true;
|
||
3 years ago
|
},
|
||
|
deleteTable(i) {
|
||
3 years ago
|
const deleteTable = this.project.tables[i];
|
||
3 years ago
|
for (const table of this.project.tables) {
|
||
|
if (table === deleteTable) {
|
||
3 years ago
|
continue;
|
||
3 years ago
|
}
|
||
3 years ago
|
table.columns = table.columns.filter(c => c.ref_table_name !== deleteTable.table_name);
|
||
3 years ago
|
}
|
||
3 years ago
|
this.project.tables.splice(i, 1);
|
||
3 years ago
|
},
|
||
3 years ago
|
deleteTableColumn(i, j, col, table) {
|
||
3 years ago
|
const deleteTable = this.project.tables[i];
|
||
|
const deleteColumn = deleteTable.columns[j];
|
||
3 years ago
|
|
||
3 years ago
|
let rTable, index;
|
||
3 years ago
|
// if relation column, delete the corresponding relation from other table
|
||
|
if (col.uidt === UITypes.LinkToAnotherRecord) {
|
||
3 years ago
|
if (col.type === 'hm') {
|
||
3 years ago
|
rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
index =
|
||
|
rTable &&
|
||
3 years ago
|
rTable.columns.findIndex(c => c.uidt === UITypes.ForeignKey && c.ref_table_name === table.table_name);
|
||
3 years ago
|
} else if (col.type === 'mm') {
|
||
3 years ago
|
rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
index =
|
||
|
rTable &&
|
||
|
rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'mm'
|
||
|
);
|
||
3 years ago
|
}
|
||
|
} else if (col.uidt === UITypes.ForeignKey) {
|
||
3 years ago
|
rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
index =
|
||
|
rTable &&
|
||
|
rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'hm'
|
||
|
);
|
||
3 years ago
|
}
|
||
|
|
||
|
if (rTable && index > -1) {
|
||
3 years ago
|
rTable.columns.splice(index, 1);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
for (const table of this.project.tables) {
|
||
|
if (table === deleteTable) {
|
||
3 years ago
|
continue;
|
||
3 years ago
|
}
|
||
3 years ago
|
table.columns = table.columns.filter(
|
||
3 years ago
|
c => c.ref_table_name !== deleteTable.table_name || c.ref_column_name !== deleteColumn.column_name
|
||
|
);
|
||
3 years ago
|
}
|
||
3 years ago
|
deleteTable.columns.splice(j, 1);
|
||
3 years ago
|
},
|
||
|
addTables() {
|
||
|
if (!this.tableNamesInput) {
|
||
3 years ago
|
return;
|
||
3 years ago
|
}
|
||
|
// todo: fix
|
||
3 years ago
|
const re = /(?:^|,\s*)(\w+)(?:\(((\w+)(?:\s*,\s*\w+)?)?\)){0,1}(?=\s*,|\s*$)/g;
|
||
|
let m;
|
||
3 years ago
|
// eslint-disable-next-line no-cond-assign
|
||
3 years ago
|
while ((m = re.exec(this.tableNamesInput))) {
|
||
3 years ago
|
if (this.project.tables.some(t => t.table_name === m[1])) {
|
||
3 years ago
|
this.$toast.info(`Table '${m[1]}' is already exist`).goAway(1000);
|
||
|
continue;
|
||
3 years ago
|
}
|
||
|
|
||
|
this.project.tables.push({
|
||
3 years ago
|
table_name: m[1],
|
||
3 years ago
|
columns: (m[2] ? m[2].split(/\s*,\s*/) : [])
|
||
3 years ago
|
.map(col => ({
|
||
3 years ago
|
column_name: col,
|
||
3 years ago
|
...defaultColProp,
|
||
3 years ago
|
}))
|
||
3 years ago
|
.filter((v, i, arr) => i === arr.findIndex(c => c.column_name === v.column_name)),
|
||
|
});
|
||
3 years ago
|
}
|
||
3 years ago
|
this.createTablesDialog = false;
|
||
|
this.tableNamesInput = '';
|
||
3 years ago
|
},
|
||
|
compareRel(a, b) {
|
||
3 years ago
|
return ((a && a.table_name) || a) === ((b && b.table_name) || b) && (a && a.type) === (b && b.type);
|
||
3 years ago
|
},
|
||
|
addColumns() {
|
||
|
if (!this.columnNamesInput) {
|
||
3 years ago
|
return;
|
||
3 years ago
|
}
|
||
3 years ago
|
const table = this.project.tables[this.expansionPanel];
|
||
3 years ago
|
for (const col of this.columnNamesInput.split(/\s*,\s*/)) {
|
||
3 years ago
|
if (table.columns.some(c => c.column_name === col)) {
|
||
3 years ago
|
this.$toast.info(`Column '${col}' is already exist`).goAway(1000);
|
||
|
continue;
|
||
3 years ago
|
}
|
||
|
|
||
|
table.columns.push({
|
||
3 years ago
|
column_name: col,
|
||
3 years ago
|
...defaultColProp,
|
||
|
});
|
||
3 years ago
|
}
|
||
3 years ago
|
this.columnNamesInput = '';
|
||
|
this.createTableColumnsDialog = false;
|
||
3 years ago
|
|
||
|
this.$nextTick(() => {
|
||
3 years ago
|
const input = this.$refs[`uidt_${table.table_name}_${table.columns.length - 1}`][0].$el.querySelector('input');
|
||
|
input.focus();
|
||
3 years ago
|
this.$nextTick(() => {
|
||
3 years ago
|
input.select();
|
||
|
});
|
||
|
});
|
||
3 years ago
|
},
|
||
|
showColCreateDialog(table) {
|
||
3 years ago
|
this.createTableColumnsDialog = true;
|
||
|
this.selectedTable = table;
|
||
3 years ago
|
},
|
||
|
|
||
|
isRelation(col) {
|
||
3 years ago
|
return col.uidt === 'LinkToAnotherRecord' || col.uidt === 'ForeignKey';
|
||
3 years ago
|
},
|
||
|
isLookup(col) {
|
||
3 years ago
|
return col.uidt === 'Lookup';
|
||
3 years ago
|
},
|
||
|
isRollup(col) {
|
||
3 years ago
|
return col.uidt === 'Rollup';
|
||
3 years ago
|
},
|
||
|
isVirtual(col) {
|
||
3 years ago
|
return col && uiTypes.some(ut => ut.name === col.uidt && ut.virtual);
|
||
3 years ago
|
},
|
||
|
isLookupOrRollup(col) {
|
||
3 years ago
|
return this.isLookup(col) || this.isRollup(col);
|
||
3 years ago
|
},
|
||
|
isSelect(col) {
|
||
3 years ago
|
return col.uidt === 'MultiSelect' || col.uidt === 'SingleSelect';
|
||
3 years ago
|
},
|
||
|
addNewColumnRow(table, uidt) {
|
||
|
table.columns.push({
|
||
3 years ago
|
column_name: `title${table.columns.length + 1}`,
|
||
3 years ago
|
...defaultColProp,
|
||
|
uidt,
|
||
|
...(uidt === LinkToAnotherRecord
|
||
|
? {
|
||
3 years ago
|
type: 'mm',
|
||
3 years ago
|
}
|
||
3 years ago
|
: {}),
|
||
|
});
|
||
3 years ago
|
this.$nextTick(() => {
|
||
3 years ago
|
const input = this.$refs[`cn_${table.table_name}_${table.columns.length - 1}`][0].$el.querySelector('input');
|
||
|
input.focus();
|
||
|
input.select();
|
||
|
});
|
||
3 years ago
|
},
|
||
|
|
||
|
async handleKeyDown({ metaKey, key, altKey, shiftKey, ctrlKey }) {
|
||
|
if (!(metaKey && ctrlKey) && !(altKey && shiftKey)) {
|
||
3 years ago
|
return;
|
||
3 years ago
|
}
|
||
|
switch (key && key.toLowerCase()) {
|
||
3 years ago
|
case 't':
|
||
3 years ago
|
this.createTablesDialog = true;
|
||
|
break;
|
||
3 years ago
|
case 'c':
|
||
3 years ago
|
this.createTableColumnsDialog = true;
|
||
|
break;
|
||
3 years ago
|
case 'a':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel]);
|
||
|
break;
|
||
3 years ago
|
case 'j':
|
||
3 years ago
|
this.copyJSON();
|
||
|
break;
|
||
3 years ago
|
case 's':
|
||
3 years ago
|
await this.saveTemplate();
|
||
|
break;
|
||
3 years ago
|
case 'arrowup':
|
||
3 years ago
|
this.expansionPanel = this.expansionPanel ? --this.expansionPanel : this.project.tables.length - 1;
|
||
|
break;
|
||
3 years ago
|
case 'arrowdown':
|
||
3 years ago
|
this.expansionPanel = ++this.expansionPanel % this.project.tables.length;
|
||
|
break;
|
||
3 years ago
|
|
||
3 years ago
|
case '1':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'Number');
|
||
|
break;
|
||
3 years ago
|
case '2':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'SingleLineText');
|
||
|
break;
|
||
3 years ago
|
case '3':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'LongText');
|
||
|
break;
|
||
3 years ago
|
case '4':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'LinkToAnotherRecord');
|
||
|
break;
|
||
3 years ago
|
case '5':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'Lookup');
|
||
|
break;
|
||
3 years ago
|
case '6':
|
||
3 years ago
|
this.addNewColumnRow(this.project.tables[this.expansionPanel], 'Rollup');
|
||
|
break;
|
||
3 years ago
|
}
|
||
|
},
|
||
|
copyJSON() {
|
||
|
if (!this.validateAndFocus()) {
|
||
3 years ago
|
this.$toast.info('Please fill all the required column!').goAway(5000);
|
||
|
return;
|
||
3 years ago
|
}
|
||
3 years ago
|
const el = document.createElement('textarea');
|
||
|
el.addEventListener('focusin', e => e.stopPropagation());
|
||
|
el.value = JSON.stringify(this.projectTemplate, null, 2);
|
||
|
el.style = { position: 'absolute', left: '-9999px' };
|
||
|
document.body.appendChild(el);
|
||
|
el.select();
|
||
|
document.execCommand('copy');
|
||
|
document.body.removeChild(el);
|
||
|
this.$toast.success('Successfully copied JSON data to clipboard!').goAway(3000);
|
||
|
return true;
|
||
3 years ago
|
},
|
||
|
openUrl() {
|
||
3 years ago
|
window.open(this.url, '_blank');
|
||
3 years ago
|
},
|
||
|
async loadUrl() {
|
||
|
try {
|
||
3 years ago
|
let template = (await this.$axios.get(this.url)).data;
|
||
3 years ago
|
|
||
3 years ago
|
if (typeof template === 'string') {
|
||
3 years ago
|
template = JSON.parse(template);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
this.parseTemplate(template);
|
||
3 years ago
|
} catch (e) {
|
||
3 years ago
|
this.$toast.error(e.message).goAway(5000);
|
||
3 years ago
|
}
|
||
|
},
|
||
|
|
||
|
parseTemplate({ tables = [], ...rest }) {
|
||
|
const parsedTemplate = {
|
||
|
...rest,
|
||
3 years ago
|
tables: tables.map(({ manyToMany = [], hasMany = [], belongsTo = [], v = [], columns = [], ...rest }) => ({
|
||
|
...rest,
|
||
|
columns: [
|
||
|
...columns,
|
||
|
...manyToMany.map(mm => ({
|
||
|
column_name: mm.title || `${rest.table_name} <=> ${mm.ref_table_name}`,
|
||
|
uidt: LinkToAnotherRecord,
|
||
|
type: 'mm',
|
||
|
...mm,
|
||
|
})),
|
||
|
...hasMany.map(hm => ({
|
||
|
column_name: hm.title || `${rest.table_name} => ${hm.table_name}`,
|
||
|
uidt: LinkToAnotherRecord,
|
||
|
type: 'hm',
|
||
|
ref_table_name: hm.table_name,
|
||
|
...hm,
|
||
|
})),
|
||
|
...belongsTo.map(bt => ({
|
||
|
column_name: bt.title || `${rest.table_name} => ${bt.ref_table_name}`,
|
||
|
uidt: UITypes.ForeignKey,
|
||
|
ref_table_name: bt.table_name,
|
||
|
...bt,
|
||
|
})),
|
||
|
...v.map(v => {
|
||
|
const res = {
|
||
|
column_name: v.title,
|
||
|
ref_table_name: {
|
||
|
...v,
|
||
|
},
|
||
|
};
|
||
|
if (v.lk) {
|
||
|
res.uidt = Lookup;
|
||
|
res.ref_table_name.table_name = v.lk.ltn;
|
||
|
res.ref_column_name = v.lk.lcn;
|
||
|
res.ref_table_name.type = v.lk.type;
|
||
|
} else if (v.rl) {
|
||
|
res.uidt = Rollup;
|
||
|
res.ref_table_name.table_name = v.rl.rltn;
|
||
|
res.ref_column_name = v.rl.rlcn;
|
||
|
res.ref_table_name.type = v.rl.type;
|
||
|
res.fn = v.rl.fn;
|
||
|
}
|
||
|
return res;
|
||
|
}),
|
||
|
],
|
||
|
})),
|
||
|
};
|
||
3 years ago
|
|
||
3 years ago
|
this.project = parsedTemplate;
|
||
3 years ago
|
},
|
||
|
|
||
|
async projectTemplateCreate() {
|
||
|
if (!this.validateAndFocus()) {
|
||
3 years ago
|
this.$toast.info('Please fill all the required column!').goAway(5000);
|
||
|
return;
|
||
3 years ago
|
}
|
||
|
|
||
|
try {
|
||
3 years ago
|
const githubConfig = this.$store.state.github;
|
||
3 years ago
|
|
||
|
// const token = await models.store.where({ key: 'GITHUB_TOKEN' }).first()
|
||
|
// const branch = await models.store.where({ key: 'GITHUB_BRANCH' }).first()
|
||
|
// const filePath = await models.store.where({ key: 'GITHUB_FILE_PATH' }).first()
|
||
|
// const templateRepo = await models.store.where({ key: 'PROJECT_TEMPLATES_REPO' }).first()
|
||
|
|
||
|
if (!githubConfig.token || !githubConfig.repo) {
|
||
3 years ago
|
throw new Error('Missing token or template path');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
const data = JSON.stringify(this.projectTemplate, 0, 2);
|
||
|
const filename = this.updateFilename || `${this.projectTemplate.name}_${Date.now()}.json`;
|
||
|
const filePath = `${githubConfig.filePath ? githubConfig.filePath + '/' : ''}${filename}`;
|
||
|
const apiPath = `https://api.github.com/repos/${githubConfig.repo}/contents/${filePath}`;
|
||
3 years ago
|
|
||
3 years ago
|
let sha;
|
||
3 years ago
|
if (this.updateFilename) {
|
||
3 years ago
|
const {
|
||
3 years ago
|
data: { sha: _sha },
|
||
3 years ago
|
} = await this.$axios({
|
||
3 years ago
|
url: `https://api.github.com/repos/${githubConfig.repo}/contents/${filePath}`,
|
||
3 years ago
|
method: 'get',
|
||
3 years ago
|
headers: {
|
||
3 years ago
|
Authorization: 'token ' + githubConfig.token,
|
||
|
},
|
||
|
});
|
||
|
sha = _sha;
|
||
3 years ago
|
}
|
||
|
|
||
|
await this.$axios({
|
||
|
url: apiPath,
|
||
3 years ago
|
method: 'put',
|
||
3 years ago
|
headers: {
|
||
3 years ago
|
'Content-Type': 'application/json',
|
||
3 years ago
|
Authorization: 'token ' + githubConfig.token,
|
||
3 years ago
|
},
|
||
|
data: {
|
||
|
message: `templates : init template ${filename}`,
|
||
|
content: Base64.encode(data),
|
||
|
sha,
|
||
3 years ago
|
branch: githubConfig.branch,
|
||
|
},
|
||
|
});
|
||
3 years ago
|
|
||
3 years ago
|
this.url = `https://raw.githubusercontent.com/${githubConfig.repo}/${githubConfig.branch}/${filePath}`;
|
||
3 years ago
|
|
||
3 years ago
|
this.$toast.success('Template generated and saved successfully!').goAway(4000);
|
||
3 years ago
|
} catch (e) {
|
||
3 years ago
|
this.$toast.error(e.message).goAway(5000);
|
||
3 years ago
|
}
|
||
|
},
|
||
|
navigateToTable(tn) {
|
||
3 years ago
|
const index = this.projectTemplate.tables.findIndex(t => t.table_name === tn);
|
||
3 years ago
|
if (Array.isArray(this.expansionPanel)) {
|
||
3 years ago
|
this.expansionPanel.push(index);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
this.expansionPanel = index;
|
||
3 years ago
|
}
|
||
|
|
||
|
this.$nextTick(() => {
|
||
3 years ago
|
const accord = this.$el.querySelector(`#tn_${tn}`);
|
||
|
accord.focus();
|
||
|
accord.scrollIntoView();
|
||
|
});
|
||
3 years ago
|
},
|
||
3 years ago
|
|
||
3 years ago
|
async saveTemplate() {
|
||
3 years ago
|
this.loading = true;
|
||
3 years ago
|
try {
|
||
|
if (this.id || this.localId) {
|
||
3 years ago
|
await this.$axios.put(
|
||
3 years ago
|
`${process.env.NC_API_URL}/api/v1/nc/templates/${this.id || this.localId}`,
|
||
3 years ago
|
this.projectTemplate,
|
||
|
{
|
||
|
params: {
|
||
3 years ago
|
token: this.$store.state.template,
|
||
|
},
|
||
3 years ago
|
}
|
||
3 years ago
|
);
|
||
|
this.$toast.success('Template updated successfully').goAway(3000);
|
||
3 years ago
|
} else if (!this.$store.state.template) {
|
||
|
if (!this.copyJSON()) {
|
||
3 years ago
|
return;
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
this.$toast.info('Initiating Github for template').goAway(3000);
|
||
3 years ago
|
const res = await this.$axios.post(
|
||
|
`${process.env.NC_API_URL}/api/v1/projectTemplateCreate`,
|
||
|
this.projectTemplate
|
||
3 years ago
|
);
|
||
|
this.$toast.success('Initiated Github successfully').goAway(3000);
|
||
|
window.open(res.data.path, '_blank');
|
||
3 years ago
|
} else {
|
||
3 years ago
|
const res = await this.$axios.post(`${process.env.NC_API_URL}/api/v1/nc/templates`, this.projectTemplate, {
|
||
|
params: {
|
||
|
token: this.$store.state.template,
|
||
|
},
|
||
|
});
|
||
|
this.localId = res.data.id;
|
||
|
this.$toast.success('Template updated successfully').goAway(3000);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
this.$emit('saved');
|
||
3 years ago
|
} catch (e) {
|
||
3 years ago
|
this.$toast.error(e.message).goAway(3000);
|
||
3 years ago
|
} finally {
|
||
3 years ago
|
this.loading = false;
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
|
getRules(col, table) {
|
||
3 years ago
|
return v =>
|
||
3 years ago
|
col.uidt !== UITypes.LinkToAnotherRecord ||
|
||
|
!table.columns.some(
|
||
3 years ago
|
c =>
|
||
3 years ago
|
c !== col &&
|
||
|
c.uidt === UITypes.LinkToAnotherRecord &&
|
||
|
c.type === col.type &&
|
||
3 years ago
|
c.ref_table_name === col.ref_table_name
|
||
3 years ago
|
) ||
|
||
3 years ago
|
'Duplicate relation is not allowed';
|
||
3 years ago
|
},
|
||
|
onTableNameUpdate(oldTable, newVal) {
|
||
3 years ago
|
const oldVal = oldTable.table_name;
|
||
|
this.$set(oldTable, 'table_name', newVal);
|
||
3 years ago
|
|
||
3 years ago
|
for (const table of this.project.tables) {
|
||
|
for (const col of table.columns) {
|
||
|
if (col.uidt === UITypes.LinkToAnotherRecord) {
|
||
3 years ago
|
if (col.ref_table_name === oldVal) {
|
||
3 years ago
|
this.$set(col, 'ref_table_name', newVal);
|
||
3 years ago
|
}
|
||
3 years ago
|
} else if (col.uidt === UITypes.Rollup || col.uidt === UITypes.Lookup) {
|
||
3 years ago
|
if (col.ref_table_name && col.ref_table_name.table_name === oldVal) {
|
||
3 years ago
|
this.$set(col.ref_table_name, 'table_name', newVal);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
onColumnNameUpdate(oldCol, newVal, tn) {
|
||
3 years ago
|
const oldVal = oldCol.column_name;
|
||
|
this.$set(oldCol, 'column_name', newVal);
|
||
3 years ago
|
|
||
|
for (const table of this.project.tables) {
|
||
|
for (const col of table.columns) {
|
||
|
if (col.uidt === UITypes.Rollup || col.uidt === UITypes.Lookup) {
|
||
3 years ago
|
if (col.ref_table_name && col.ref_column_name === oldVal && col.ref_table_name.table_name === tn) {
|
||
3 years ago
|
this.$set(col, 'ref_column_name', newVal);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
},
|
||
|
async onRtnChange(oldVal, newVal, col, table) {
|
||
3 years ago
|
this.$set(col, 'ref_table_name', newVal);
|
||
3 years ago
|
|
||
3 years ago
|
await this.$nextTick();
|
||
3 years ago
|
|
||
3 years ago
|
if (col.uidt !== UITypes.LinkToAnotherRecord && col.uidt !== UITypes.ForeignKey) {
|
||
|
return;
|
||
3 years ago
|
}
|
||
|
|
||
|
if (oldVal) {
|
||
3 years ago
|
const rTable = this.project.tables.find(t => t.table_name === oldVal);
|
||
3 years ago
|
// delete relation from other table if exist
|
||
|
|
||
3 years ago
|
let index = -1;
|
||
3 years ago
|
if (col.uidt === UITypes.LinkToAnotherRecord && col.type === 'mm') {
|
||
3 years ago
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'mm'
|
||
|
);
|
||
|
} else if (col.uidt === UITypes.LinkToAnotherRecord && col.type === 'hm') {
|
||
|
index = rTable.columns.findIndex(c => c.uidt === UITypes.ForeignKey && c.ref_table_name === table.table_name);
|
||
3 years ago
|
} else if (col.uidt === UITypes.ForeignKey) {
|
||
3 years ago
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'hm'
|
||
|
);
|
||
3 years ago
|
}
|
||
|
|
||
|
if (index > -1) {
|
||
3 years ago
|
rTable.columns.splice(index, 1);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
if (newVal) {
|
||
3 years ago
|
const rTable = this.project.tables.find(t => t.table_name === newVal);
|
||
3 years ago
|
|
||
|
// check relation relation exist in other table
|
||
|
// if not create a relation
|
||
3 years ago
|
if (col.uidt === UITypes.LinkToAnotherRecord && col.type === 'mm') {
|
||
3 years ago
|
if (
|
||
|
!rTable.columns.find(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'mm'
|
||
3 years ago
|
)
|
||
|
) {
|
||
3 years ago
|
rTable.columns.push({
|
||
3 years ago
|
column_name: `title${rTable.columns.length + 1}`,
|
||
3 years ago
|
uidt: UITypes.LinkToAnotherRecord,
|
||
3 years ago
|
type: 'mm',
|
||
3 years ago
|
ref_table_name: table.table_name,
|
||
|
});
|
||
3 years ago
|
}
|
||
3 years ago
|
} else if (col.uidt === UITypes.LinkToAnotherRecord && col.type === 'hm') {
|
||
|
if (!rTable.columns.find(c => c.uidt === UITypes.ForeignKey && c.ref_table_name === table.table_name)) {
|
||
3 years ago
|
rTable.columns.push({
|
||
3 years ago
|
column_name: `title${rTable.columns.length + 1}`,
|
||
3 years ago
|
uidt: UITypes.ForeignKey,
|
||
3 years ago
|
ref_table_name: table.table_name,
|
||
|
});
|
||
3 years ago
|
}
|
||
|
} else if (col.uidt === UITypes.ForeignKey) {
|
||
3 years ago
|
if (
|
||
|
!rTable.columns.find(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'hm'
|
||
3 years ago
|
)
|
||
|
) {
|
||
3 years ago
|
rTable.columns.push({
|
||
3 years ago
|
column_name: `title${rTable.columns.length + 1}`,
|
||
3 years ago
|
uidt: UITypes.LinkToAnotherRecord,
|
||
3 years ago
|
type: 'hm',
|
||
3 years ago
|
ref_table_name: table.table_name,
|
||
|
});
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
onRTypeChange(oldType, newType, col, table) {
|
||
3 years ago
|
this.$set(col, 'type', newType);
|
||
3 years ago
|
|
||
3 years ago
|
const rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
|
||
3 years ago
|
let index = -1;
|
||
3 years ago
|
|
||
|
// find column and update relation
|
||
|
// or create a new column
|
||
|
|
||
3 years ago
|
if (oldType === 'hm') {
|
||
3 years ago
|
index = rTable.columns.findIndex(c => c.uidt === UITypes.ForeignKey && c.ref_table_name === table.table_name);
|
||
3 years ago
|
} else if (oldType === 'mm') {
|
||
3 years ago
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'mm'
|
||
|
);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
const rCol = index === -1 ? { column_name: `title${rTable.columns.length + 1}` } : { ...rTable.columns[index] };
|
||
|
index = index === -1 ? rTable.columns.length : index;
|
||
3 years ago
|
|
||
|
if (newType === 'mm') {
|
||
3 years ago
|
rCol.type = 'mm';
|
||
|
rCol.uidt = UITypes.LinkToAnotherRecord;
|
||
3 years ago
|
} else if (newType === 'hm') {
|
||
3 years ago
|
rCol.type = 'bt';
|
||
|
rCol.uidt = UITypes.ForeignKey;
|
||
3 years ago
|
}
|
||
3 years ago
|
rCol.ref_table_name = table.table_name;
|
||
3 years ago
|
|
||
3 years ago
|
this.$set(rTable.columns, index, rCol);
|
||
3 years ago
|
},
|
||
|
onUidtChange(oldVal, newVal, col, table) {
|
||
3 years ago
|
this.$set(col, 'uidt', newVal);
|
||
|
this.$set(col, 'dtxp', undefined);
|
||
3 years ago
|
|
||
|
// delete relation column from other table
|
||
|
// if previous type is relation
|
||
|
|
||
3 years ago
|
let index = -1;
|
||
|
let rTable;
|
||
3 years ago
|
|
||
|
if (oldVal === UITypes.LinkToAnotherRecord) {
|
||
3 years ago
|
rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
if (rTable) {
|
||
3 years ago
|
if (col.type === 'hm') {
|
||
3 years ago
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.ForeignKey && c.ref_table_name === table.table_name
|
||
3 years ago
|
);
|
||
3 years ago
|
} else if (col.type === 'mm') {
|
||
3 years ago
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'mm'
|
||
|
);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
} else if (oldVal === UITypes.ForeignKey) {
|
||
3 years ago
|
rTable = this.project.tables.find(t => t.table_name === col.ref_table_name);
|
||
3 years ago
|
if (rTable) {
|
||
|
index = rTable.columns.findIndex(
|
||
3 years ago
|
c => c.uidt === UITypes.LinkToAnotherRecord && c.ref_table_name === table.table_name && c.type === 'hm'
|
||
|
);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
if (rTable && index > -1) {
|
||
3 years ago
|
rTable.columns.splice(index, 1);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
col.ref_table_name = undefined;
|
||
|
col.type = undefined;
|
||
|
col.ref_column_name = undefined;
|
||
3 years ago
|
|
||
|
if (col.uidt === LinkToAnotherRecord) {
|
||
3 years ago
|
col.type = col.type || 'mm';
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
|
},
|
||
|
};
|
||
3 years ago
|
</script>
|
||
|
|
||
|
<style scoped>
|
||
3 years ago
|
/deep/ .v-select__selections {
|
||
3 years ago
|
flex-wrap: nowrap;
|
||
|
}
|
||
3 years ago
|
</style>
|