Files
CRM_Montzar/apps/web/src/components/QuickCreate.vue
T

790 lines
17 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import UiIcon from '@/components/UiIcon.vue';
const FAB_SIZE = 36;
const FAB_MARGIN_BOTTOM = 24;
const FAB_MARGIN_LEFT = 24;
const POS_KEY = 'mento.quickCreateFab_v2';
type FabPos = { left: number; top: number };
type ItemIcon = 'user' | 'building' | 'dollar' | 'calendar-plus' | 'document';
const items: Array<{
key: string;
title: string;
desc: string;
icon: ItemIcon;
tone: string;
to: RouteLocationRaw;
}> = [
{
key: 'person',
title: 'افزودن شخص',
desc: 'مخاطب جدید را ثبت کنید',
icon: 'user',
tone: 'person',
to: { name: 'person-new' },
},
{
key: 'company',
title: 'افزودن شرکت',
desc: 'شرکت یا سازمان جدید اضافه کنید',
icon: 'building',
tone: 'company',
to: { name: 'company-new' },
},
{
key: 'deal',
title: 'افزودن معامله',
desc: 'فرصت یا معامله جدید ایجاد کنید',
icon: 'dollar',
tone: 'deal',
to: { name: 'deal-new' },
},
{
key: 'activity',
title: 'افزودن فعالیت',
desc: 'تماس، جلسه، ایمیل و ... ثبت کنید',
icon: 'calendar-plus',
tone: 'activity',
to: { name: 'activity-new' },
},
{
key: 'note',
title: 'افزودن کارت (یادداشت)',
desc: 'یادداشت یا کارت جدید ایجاد کنید',
icon: 'document',
tone: 'note',
to: { name: 'notes' },
},
];
const route = useRoute();
const router = useRouter();
/** صفحات با نوار اکشن ثابت موبایل — FAB نباید وسط صفحه رها شود */
const hidden = computed(
() => route.name === 'person-detail' || route.name === 'finance-incomes',
);
const isQuickCreateOpen = ref(false);
const ready = ref(false);
const fabPos = ref<FabPos>({
left: FAB_MARGIN_LEFT,
top: typeof window !== 'undefined' ? window.innerHeight - FAB_MARGIN_BOTTOM - FAB_SIZE : 600,
});
const viewport = ref({ w: 1280, h: 800 });
const dragging = ref(false);
let dragMoved = false;
let dragOffsetX = 0;
let dragOffsetY = 0;
function mobileTabbarPx(): number {
if (typeof window === 'undefined' || viewport.value.w > 900) return 0;
const bar = document.querySelector('.mobile-tabbar');
if (bar instanceof HTMLElement) {
const h = bar.getBoundingClientRect().height;
if (h > 0) return h;
}
return 64;
}
function defaultPos(): FabPos {
const extra = mobileTabbarPx();
return {
left: FAB_MARGIN_LEFT,
top: Math.max(8, viewport.value.h - FAB_MARGIN_BOTTOM - extra - FAB_SIZE),
};
}
function clampPos(pos: FabPos): FabPos {
const margin = 8;
const bottomGap = mobileTabbarPx();
return {
left: Math.min(Math.max(margin, pos.left), viewport.value.w - FAB_SIZE - margin),
top: Math.min(
Math.max(margin, pos.top),
viewport.value.h - FAB_SIZE - margin - bottomGap,
),
};
}
function loadPos() {
try {
const raw = localStorage.getItem(POS_KEY);
if (!raw) {
fabPos.value = defaultPos();
return;
}
const parsed = JSON.parse(raw) as FabPos;
if (typeof parsed.left !== 'number' || typeof parsed.top !== 'number') {
fabPos.value = defaultPos();
return;
}
fabPos.value = clampPos(parsed);
} catch {
fabPos.value = defaultPos();
}
}
function savePos() {
localStorage.setItem(POS_KEY, JSON.stringify(fabPos.value));
}
function onFabPointerDown(ev: PointerEvent) {
if (ev.button !== 0) return;
dragMoved = false;
dragOffsetX = ev.clientX - fabPos.value.left;
dragOffsetY = ev.clientY - fabPos.value.top;
(ev.currentTarget as HTMLElement).setPointerCapture(ev.pointerId);
}
function onFabPointerMove(ev: PointerEvent) {
if (!((ev.currentTarget as HTMLElement).hasPointerCapture?.(ev.pointerId))) return;
const next = clampPos({
left: ev.clientX - dragOffsetX,
top: ev.clientY - dragOffsetY,
});
if (
Math.abs(next.left - fabPos.value.left) > 4 ||
Math.abs(next.top - fabPos.value.top) > 4
) {
dragMoved = true;
dragging.value = true;
}
if (dragMoved) fabPos.value = next;
}
function onFabPointerUp(ev: PointerEvent) {
(ev.currentTarget as HTMLElement).releasePointerCapture?.(ev.pointerId);
if (dragMoved) {
fabPos.value = clampPos(fabPos.value);
savePos();
dragging.value = false;
return;
}
dragging.value = false;
toggle();
}
function syncViewport() {
viewport.value = { w: window.innerWidth, h: window.innerHeight };
}
function onResize() {
syncViewport();
fabPos.value = clampPos(fabPos.value);
}
const fabStyle = computed(() => ({
left: `${fabPos.value.left}px`,
top: `${fabPos.value.top}px`,
right: 'auto',
bottom: 'auto',
}));
const panelPlacement = computed(() => {
const edge = 16;
const spaceAbove = fabPos.value.top - edge;
const spaceBelow = viewport.value.h - (fabPos.value.top + FAB_SIZE) - edge;
return spaceAbove >= spaceBelow ? 'above' : 'below';
});
const panelStyle = computed(() => {
const gap = 12;
const edge = 16;
const width = Math.min(300, viewport.value.w - 32);
let left = fabPos.value.left;
left = Math.min(left, viewport.value.w - width - edge);
left = Math.max(edge, left);
const placeAbove = panelPlacement.value === 'above';
const maxHeight = Math.max(
0,
(placeAbove
? fabPos.value.top - edge
: viewport.value.h - (fabPos.value.top + FAB_SIZE) - edge) - gap,
);
const shared = {
left: `${left}px`,
right: 'auto',
width: `${width}px`,
maxHeight: `${maxHeight}px`,
transformOrigin: placeAbove ? 'bottom left' : 'top left',
};
if (placeAbove) {
return {
...shared,
bottom: `${viewport.value.h - fabPos.value.top + gap}px`,
top: 'auto',
};
}
return {
...shared,
top: `${fabPos.value.top + FAB_SIZE + gap}px`,
bottom: 'auto',
};
});
function close() {
isQuickCreateOpen.value = false;
}
function toggle() {
isQuickCreateOpen.value = !isQuickCreateOpen.value;
}
function go(to: RouteLocationRaw) {
close();
void router.push(to);
}
function onKeydown(ev: KeyboardEvent) {
if (ev.key === 'Escape' && isQuickCreateOpen.value) {
ev.preventDefault();
close();
}
}
watch(hidden, (value) => {
if (value) close();
});
watch(
() => route.fullPath,
() => {
close();
},
);
onMounted(() => {
syncViewport();
loadPos();
ready.value = true;
window.addEventListener('resize', onResize);
window.addEventListener('keydown', onKeydown);
});
onBeforeUnmount(() => {
window.removeEventListener('resize', onResize);
window.removeEventListener('keydown', onKeydown);
});
</script>
<template>
<Teleport to="body">
<div v-if="!hidden" class="qc" dir="rtl">
<Transition name="qc-overlay">
<button
v-if="isQuickCreateOpen"
class="overlay"
type="button"
aria-label="بستن ایجاد سریع"
@click="close"
/>
</Transition>
<Transition name="qc-panel">
<section
v-if="isQuickCreateOpen"
class="panel"
:data-place="panelPlacement"
aria-labelledby="qc-title"
:style="panelStyle"
>
<h2 id="qc-title">ایجاد سریع</h2>
<div class="items">
<button
v-for="(item, index) in items"
:key="item.key"
class="item"
type="button"
:style="{ '--qc-i': index }"
@click="go(item.to)"
>
<span class="item-ico" :class="item.tone">
<UiIcon :name="item.icon" :size="18" />
</span>
<span class="copy">
<strong>{{ item.title }}</strong>
<small>{{ item.desc }}</small>
</span>
</button>
</div>
</section>
</Transition>
<button
v-if="ready"
class="fab"
type="button"
:class="{ dragging, open: isQuickCreateOpen }"
:style="fabStyle"
:aria-expanded="isQuickCreateOpen"
:aria-label="isQuickCreateOpen ? 'بستن ایجاد سریع' : 'ایجاد سریع'"
title="ایجاد سریع"
@pointerdown="onFabPointerDown"
@pointermove="onFabPointerMove"
@pointerup="onFabPointerUp"
@pointercancel="onFabPointerUp"
>
<span class="fab-pulse" aria-hidden="true" />
<span class="fab-shine" aria-hidden="true" />
<span class="fab-face" aria-hidden="true">
<UiIcon :name="isQuickCreateOpen ? 'x' : 'apps-plus'" :size="20" />
</span>
</button>
</div>
</Teleport>
</template>
<style scoped>
.qc {
direction: rtl;
}
.overlay {
position: fixed;
inset: 0;
z-index: 90;
border: 0;
padding: 0;
background: rgba(15, 23, 42, 0.42);
cursor: pointer;
}
.panel {
--qc-ease: cubic-bezier(0.22, 1, 0.36, 1);
position: fixed;
z-index: 92;
width: 300px;
max-height: min(480px, calc(100dvh - 80px));
box-sizing: border-box;
display: flex;
flex-direction: column;
background: #fff;
border: 1px solid var(--line);
border-radius: 14px;
padding: 12px 12px 10px;
box-shadow:
0 18px 40px -18px rgba(15, 23, 42, 0.35),
0 8px 18px -10px rgba(13, 148, 136, 0.18),
0 0 0 1px rgba(255, 255, 255, 0.65) inset;
overflow: hidden;
will-change: transform, opacity;
}
.qc-overlay-enter-active,
.qc-overlay-leave-active {
transition: opacity 0.16s ease;
}
.qc-overlay-enter-from,
.qc-overlay-leave-to {
opacity: 0;
}
.qc-panel-enter-active {
transition:
opacity 0.18s var(--qc-ease),
transform 0.2s var(--qc-ease);
}
.qc-panel-leave-active {
transition:
opacity 0.14s ease,
transform 0.16s ease;
}
.qc-panel-enter-from {
opacity: 0;
transform: translateY(8px) scale(0.97);
}
.panel[data-place='above'].qc-panel-enter-from {
transform: translateY(-8px) scale(0.97);
}
.qc-panel-leave-to {
opacity: 0;
transform: translateY(4px) scale(0.98);
}
.panel[data-place='above'].qc-panel-leave-to {
transform: translateY(-4px) scale(0.98);
}
.qc-panel-enter-active .items {
overflow: hidden;
}
.qc-panel-enter-active .item {
animation: qc-item-in 0.2s var(--qc-ease) both;
animation-delay: calc(0.02s + var(--qc-i, 0) * 0.02s);
}
.qc-panel-enter-active h2 {
animation: qc-item-in 0.16s var(--qc-ease) both;
}
@keyframes qc-item-in {
from {
opacity: 0;
transform: translateY(6px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (prefers-reduced-motion: reduce) {
.qc-overlay-enter-active,
.qc-overlay-leave-active,
.qc-panel-enter-active,
.qc-panel-leave-active,
.qc-panel-enter-active .item,
.qc-panel-enter-active h2 {
transition: none !important;
animation: none !important;
}
}
.panel h2 {
flex-shrink: 0;
margin: 0 0 10px;
text-align: right;
font-size: var(--fs-md);
font-weight: 700;
color: var(--ink);
}
.items {
min-height: 0;
overflow-x: hidden;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 6px;
padding-left: 2px;
overscroll-behavior: contain;
scrollbar-width: none;
}
.items::-webkit-scrollbar {
display: none;
width: 0;
height: 0;
}
.item {
width: 100%;
min-height: 46px;
display: flex;
align-items: center;
gap: 10px;
padding: 6px 10px;
border: 1px solid var(--line-light);
border-radius: var(--radius);
background: #fff;
box-shadow: var(--shadow-xs);
font: inherit;
text-align: right;
cursor: pointer;
transition: background var(--transition-fast), border-color var(--transition-fast);
}
.item:hover {
background: var(--surface-hover);
border-color: var(--line-strong);
}
.item-ico {
width: 34px;
height: 34px;
border-radius: var(--radius);
display: grid;
place-items: center;
color: #fff;
flex-shrink: 0;
}
.item-ico.person {
background: #0d9488;
}
.item-ico.company {
background: #0891b2;
}
.item-ico.deal {
background: #7c3aed;
}
.item-ico.activity {
background: #ea580c;
}
.item-ico.note {
background: #2563eb;
}
.copy {
display: grid;
gap: 2px;
min-width: 0;
text-align: right;
}
.copy strong {
font-size: var(--fs-sm);
font-weight: 600;
color: var(--ink);
line-height: 1.2;
}
.copy small {
font-size: var(--fs-xs);
color: var(--muted);
line-height: 1.3;
}
.fab {
--fab-ease: cubic-bezier(0.22, 1, 0.36, 1);
position: fixed;
z-index: 93;
box-sizing: border-box;
width: var(--control-h);
height: var(--control-h);
padding: 0;
margin: 0;
border: 0;
border-radius: var(--radius);
overflow: visible;
background: linear-gradient(145deg, #0f766e 0%, #14b8a6 55%, #2dd4bf 100%);
background-size: 140% 140%;
color: #fff;
display: grid;
place-items: center;
line-height: 0;
direction: ltr;
isolation: isolate;
box-shadow:
0 4px 14px rgba(13, 148, 136, 0.35),
0 0 0 0 rgba(45, 212, 191, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.28);
cursor: grab;
touch-action: none;
user-select: none;
transition:
filter 0.35s var(--fab-ease),
box-shadow 0.35s var(--fab-ease),
transform 0.35s var(--fab-ease),
background 0.4s var(--fab-ease);
animation:
fab-glow 3.4s ease-in-out infinite,
fab-gradient 7s ease-in-out infinite;
}
.fab-pulse,
.fab-shine,
.fab-face {
grid-area: 1 / 1;
}
.fab-pulse {
position: absolute;
inset: 0;
border-radius: inherit;
pointer-events: none;
z-index: 0;
box-shadow: 0 0 0 0 rgba(45, 212, 191, 0.42);
animation: fab-ring 2.8s var(--fab-ease) infinite;
}
.fab-shine {
position: absolute;
inset: 0;
border-radius: inherit;
overflow: hidden;
pointer-events: none;
z-index: 1;
}
.fab-shine::after {
content: '';
position: absolute;
top: -20%;
bottom: -20%;
left: -40%;
width: 42%;
background: linear-gradient(
105deg,
transparent 0%,
rgba(255, 255, 255, 0.08) 35%,
rgba(255, 255, 255, 0.42) 50%,
rgba(255, 255, 255, 0.08) 65%,
transparent 100%
);
transform: translateX(-160%) skewX(-18deg);
animation: fab-sheen 4.8s var(--fab-ease) infinite;
}
.fab-face {
position: relative;
z-index: 2;
display: grid;
place-items: center;
place-content: center;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
pointer-events: none;
transition: transform 0.45s var(--fab-ease);
animation: fab-breathe 3.4s ease-in-out infinite;
}
.fab :deep(.ico),
.fab :deep(svg) {
display: block;
width: 20px;
height: 20px;
max-width: 20px;
max-height: 20px;
margin: 0;
stroke-width: 1.7;
filter: drop-shadow(0 1px 1px rgba(15, 23, 42, 0.18));
transition: transform 0.45s var(--fab-ease), opacity 0.25s ease;
}
.fab:hover {
filter: brightness(1.07) saturate(1.05);
transform: translateY(-1px) scale(1.04);
box-shadow:
0 10px 24px rgba(13, 148, 136, 0.42),
0 0 0 5px rgba(45, 212, 191, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.32);
}
.fab:hover .fab-face {
animation-play-state: paused;
transform: scale(1.06);
}
.fab.open {
background: linear-gradient(145deg, #115e59 0%, #0d9488 100%);
box-shadow:
0 8px 22px rgba(15, 23, 42, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.18);
animation: none;
}
.fab.open .fab-pulse,
.fab.open .fab-shine::after {
animation: none;
opacity: 0;
}
.fab.open .fab-face {
animation: none;
transform: rotate(90deg) scale(1.02);
}
.fab.dragging {
cursor: grabbing;
transform: scale(1.08);
box-shadow:
0 12px 28px rgba(13, 148, 136, 0.48),
0 0 0 6px rgba(45, 212, 191, 0.14),
inset 0 1px 0 rgba(255, 255, 255, 0.3);
animation: none;
transition-duration: 0.12s;
}
.fab.dragging .fab-pulse,
.fab.dragging .fab-shine::after,
.fab.dragging .fab-face {
animation: none;
}
@keyframes fab-glow {
0%,
100% {
box-shadow:
0 4px 14px rgba(13, 148, 136, 0.32),
0 0 0 0 rgba(45, 212, 191, 0.22),
inset 0 1px 0 rgba(255, 255, 255, 0.24);
}
50% {
box-shadow:
0 8px 22px rgba(13, 148, 136, 0.48),
0 0 18px 2px rgba(45, 212, 191, 0.2),
inset 0 1px 0 rgba(255, 255, 255, 0.34);
}
}
@keyframes fab-gradient {
0%,
100% {
background-position: 0% 40%;
}
50% {
background-position: 100% 60%;
}
}
@keyframes fab-ring {
0% {
box-shadow: 0 0 0 0 rgba(45, 212, 191, 0.4);
opacity: 1;
}
70% {
box-shadow: 0 0 0 12px rgba(45, 212, 191, 0);
opacity: 0;
}
100% {
opacity: 0;
}
}
@keyframes fab-sheen {
0%,
55% {
transform: translateX(-160%) skewX(-18deg);
}
85%,
100% {
transform: translateX(320%) skewX(-18deg);
}
}
@keyframes fab-breathe {
0%,
100% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
}
@media (prefers-reduced-motion: reduce) {
.fab,
.fab-pulse,
.fab-shine::after,
.fab-face {
animation: none !important;
}
.fab:hover,
.fab.open .fab-face {
transform: none;
}
}
</style>