mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
4 changed files with 157 additions and 5 deletions
@ -0,0 +1,58 @@ |
|||||||
|
import { useStyleTag } from '@vueuse/core' |
||||||
|
import type { ColumnType, GridColumnType } from 'nocodb-sdk' |
||||||
|
import type { Ref } from 'vue' |
||||||
|
import type GridView from '../../nocodb/src/lib/models/GridView' |
||||||
|
import useMetas from '~/composables/useMetas' |
||||||
|
|
||||||
|
// todo: update swagger
|
||||||
|
export default (view: Ref<(GridView & { id?: string }) | undefined>, scopeId: string) => { |
||||||
|
const { css, load: loadCss, unload: unloadCss } = useStyleTag('') |
||||||
|
|
||||||
|
const gridViewCols = ref<Record<string, GridColumnType>>({}) |
||||||
|
const resizingCol = ref('') |
||||||
|
const resizingColWidth = ref('200px') |
||||||
|
|
||||||
|
const { $api } = useNuxtApp() |
||||||
|
|
||||||
|
const { metas } = useMetas() |
||||||
|
|
||||||
|
const columns = computed<ColumnType[]>(() => metas?.value?.[(view?.value as any)?.fk_model_id as string]?.columns) |
||||||
|
|
||||||
|
watch( |
||||||
|
// todo : update type in swagger
|
||||||
|
() => [gridViewCols, resizingCol, resizingColWidth], |
||||||
|
() => { |
||||||
|
let style = '' |
||||||
|
for (const c of columns?.value || []) { |
||||||
|
const val = gridViewCols?.value?.[c?.id as string]?.width || '200px' |
||||||
|
|
||||||
|
if (val && c.title !== resizingCol?.value) { |
||||||
|
style += `[${scopeId}] [data-col="${c.title}"]{min-width:${val};max-width:${val};width: ${val};}` |
||||||
|
} else { |
||||||
|
style += `[${scopeId}] [data-col="${c.title}"]{min-width:${resizingColWidth?.value};max-width:${resizingColWidth?.value};width: ${resizingColWidth?.value};}` |
||||||
|
} |
||||||
|
} |
||||||
|
css.value = style |
||||||
|
}, |
||||||
|
{ deep: true, immediate: true }, |
||||||
|
) |
||||||
|
|
||||||
|
const loadGridViewColumns = async () => { |
||||||
|
if (!view.value?.id) return |
||||||
|
const colsData: GridColumnType[] = await $api.dbView.gridColumnsList(view.value.id) |
||||||
|
gridViewCols.value = colsData.reduce<Record<string, GridColumnType>>( |
||||||
|
(o, col) => ({ |
||||||
|
...o, |
||||||
|
[col.fk_column_id as string]: col, |
||||||
|
}), |
||||||
|
{}, |
||||||
|
) |
||||||
|
loadCss() |
||||||
|
} |
||||||
|
|
||||||
|
const updateWidth = (id: string, width: string) => { |
||||||
|
if (gridViewCols?.value?.[id]) gridViewCols.value[id].width = width |
||||||
|
} |
||||||
|
|
||||||
|
return { loadGridViewColumns, updateWidth, resizingCol, resizingColWidth, loadCss, unloadCss } |
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
import { defineNuxtPlugin } from '#app' |
||||||
|
|
||||||
|
export default defineNuxtPlugin((nuxtApp) => { |
||||||
|
nuxtApp.vueApp.directive('xc-ver-resize', { |
||||||
|
created: (el: Element) => { |
||||||
|
// create resizer element
|
||||||
|
const resizer = document.createElement('div') |
||||||
|
resizer.className = 'resizer' |
||||||
|
resizer.style.position = 'absolute' |
||||||
|
resizer.style.height = '100%' |
||||||
|
resizer.style.width = '3px' |
||||||
|
resizer.style.top = '0' |
||||||
|
resizer.style.right = '-1px' |
||||||
|
resizer.style.zIndex = '999' |
||||||
|
|
||||||
|
// add resizer to element
|
||||||
|
el.appendChild(resizer) |
||||||
|
resizer.addEventListener('mousedown', initDrag, false) |
||||||
|
|
||||||
|
const instance = getCurrentInstance() |
||||||
|
const emit = |
||||||
|
instance?.emit ?? |
||||||
|
((arg, data) => { |
||||||
|
const event = new CustomEvent(arg, { detail: data }) |
||||||
|
;(<HTMLElement>el).dispatchEvent(event) |
||||||
|
}) |
||||||
|
|
||||||
|
let startX: number |
||||||
|
let startWidth: number |
||||||
|
|
||||||
|
// bind event handlers
|
||||||
|
function initDrag(e: MouseEvent) { |
||||||
|
document.body.style.cursor = 'col-resize' |
||||||
|
startX = e.clientX |
||||||
|
startWidth = parseInt(document.defaultView?.getComputedStyle(el)?.width || '0', 10) |
||||||
|
document.documentElement.addEventListener('mousemove', doDrag, false) |
||||||
|
document.documentElement.addEventListener('mouseup', stopDrag, false) |
||||||
|
} |
||||||
|
|
||||||
|
let width: number | string |
||||||
|
|
||||||
|
// emit event on dragging
|
||||||
|
function doDrag(e: MouseEvent) { |
||||||
|
width = `${startWidth + e.clientX - startX}px` |
||||||
|
emit('xcresizing', width) |
||||||
|
} |
||||||
|
|
||||||
|
// remove handlers and emit events on drag end
|
||||||
|
function stopDrag() { |
||||||
|
resizer.classList.remove('primary') |
||||||
|
document.body.style.cursor = '' |
||||||
|
document.documentElement.removeEventListener('mousemove', doDrag, false) |
||||||
|
document.documentElement.removeEventListener('mouseup', stopDrag, false) |
||||||
|
emit('xcresize', width) |
||||||
|
emit('xcresized') |
||||||
|
} |
||||||
|
}, |
||||||
|
}) |
||||||
|
}) |
Loading…
Reference in new issue