Skip to content
Snippets Groups Projects
SheetPage.vue 4.8 KiB
Newer Older
import { useInterfaceStore } from '@/app/stores/interface';
import { useDataStore } from '@/app/stores/data';
import { useAuthorizationStore } from '@/app/stores/authorization';
import { useWebsocketStore } from '@/app/stores/websocket';
import type { IEntity } from '@/app/interfaces/environment';
import type { IImageMainInfo } from '@/app/interfaces';
Дмитрий Малюгин's avatar
Дмитрий Малюгин committed
import { createEntity, fetchForEntities } from '@/app/helpers';
import cookies from '@/app/plugins/Cookie';
Дмитрий Малюгин's avatar
Дмитрий Малюгин committed
import { setDefaultPageBackground } from '@/app/helpers/images';

const dataStore = useDataStore();
const interfaceStore = useInterfaceStore();
const authorizationStore = useAuthorizationStore();
const websocketStore = useWebsocketStore();

const currentPageUuid = computed(() => cookies.get('current_page_uuid'));
const entities = computed(() => dataStore.entities);
const backgroundUrl = computed<string>(() => interfaceStore.pageBackground);
const defaultBackgroundUrl = computed<string>(() => interfaceStore.defaultPageBackground);
const isFetchedForBackground = computed(() => interfaceStore.isFetchedForBackground);
const isDarkMode = computed(() => interfaceStore.isDarkMode);
// const pageTitle = computed(() => dataStore.currentPage.page_title);

const isMenuVisible = ref<boolean>(false);
const isEditMode = ref<boolean>(false);
const isModalUploadFile = ref<boolean>(false);
const backgroundImageInfo = ref<IImageMainInfo>({
  image_url: backgroundUrl.value,
  image_width: 0,
  image_height: 0
});

onMounted(() => {
  const onKeydown = (event) => {
    if (event.key === 'Alt') isEditMode.value = !isEditMode.value;
    if (event.key === 'Escape') isMenuVisible.value = !isMenuVisible.value;
  };
  document.addEventListener('keydown', onKeydown);
  const getPageBackgroundData = {
    event: 'getPageBackground',
    body: {
      page_uuid: ''
    }
  };
  websocketStore.setInitialDataToSend(getPageBackgroundData);
});

const unwatchBackground = watch([isFetchedForBackground], () => {
  if (isFetchedForBackground.value) {
    fetchForEntities(currentPageUuid.value);
    unwatchBackground();
  }
});

const createNewEntity = (newEntity: IEntity) => {
  createEntity(newEntity);
};
const uploadFile = ($event: Event) => {
  const target = $event.target as HTMLInputElement;
  if (target && target.files && target.files?.[0]) {
    const image = new Image();
    const file = target.files![0];
    const url = URL.createObjectURL(file);
    image.src = url;
    image.onload = function () {
      backgroundImageInfo.value.image_url = url;
      backgroundImageInfo.value.image_width = image.width;
      backgroundImageInfo.value.image_height = image.height;
      isModalUploadFile.value = true;
    };
  }
};
const saveImage = (finalImageUrl: string) => {
  interfaceStore.editPageBackground(finalImageUrl);
  isModalUploadFile.value = false;
};
const openMenu = () => (isMenuVisible.value = true);
  <PageHeader v-model:isEditMode="isEditMode" :title="'Home page'" />
  <PageMenuButton @openMenu="openMenu" />
  <Drawer v-model:isVisible="isMenuVisible" theme="black">
    <template #header
      ><section class="flex justify-between items-center mb-4">
        <LogoAndLabel /></section
    ></template>
    <SidebarMenuContent class="relative z-50" />
  <TelegramSection />
  <CropImageModal
    v-model:isVisible="isModalUploadFile"
    v-model:imageInfo="backgroundImageInfo"
    @saveImage="saveImage"
  />
  <main
    id="pageContainer"
    class="flex flex-col"
    :style="`background-color: ${isDarkMode ? 'black' : 'light'}`"
  >
    <article style="min-height: 200px" class="backgroundContainer relative">
      <img
        :src="backgroundUrl"
        alt="Background image"
        class="w-full pointer-events-none select-none"
      />
      <PageBackgroundMenu
        :isBackgroundDefault="backgroundUrl !== defaultBackgroundUrl"
        @uploadFile="uploadFile"
        @setDefaultBackground="setDefaultPageBackground"
      />
    </article>
    <article class="flex items-start justify-center">
      <Suspense>
        <div ref="entitiesContainer" class="w-full pt-4">
          <EntitiesList
            :entities="entities"
            :isEditMode="isEditMode"
            @createEntity="createNewEntity"
          />
        </div>
        <template #fallback><BaseLoader /></template
      ></Suspense>
    </article>
  </main>
<style lang="scss">
.backgroundContainer > .changeImageBlock,
.backgroundContainer > .returnDefaultImageBlock {
  opacity: 0;
}
.backgroundContainer:hover > .changeImageBlock,
.backgroundContainer:hover > .returnDefaultImageBlock {
  opacity: 100;
}
.telegramContainer > .telegramText {
  opacity: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}
.telegramContainer:hover > .telegramText {
  opacity: 100;
  right: 104%;
}
.telegramContainer:hover > a {
  filter: brightness(0.75);
}
</style>