/* eslint-disable @typescript-eslint/no-explicit-any */ import { Component, Prop, Watch, Vue, Ref } from "vue-property-decorator"; // Sub components import Status from "@/components/Status/index.vue"; import CheckBox from "@/components/CheckBox/index.vue"; import Dropdown from "@/components/Dropdown/index.vue"; import Button from "@/components/Button/index.vue"; import EmptyState from "@/components/EmptyState/index.vue"; import { ITableActions, LoadPatternTblCard, TableColumnInfo } from "@/types"; import Th from "./_th/index.vue"; import Td from "./_td/index.vue"; import Card from "./_card/index.vue"; import LoadingTable from "./_loading/table.vue"; // Components // Interfaces @Component({ components: { Status, CheckBox, Th, Td, Card, Button, EmptyState, Dropdown, LoadingTable, }, }) export default class Logic extends Vue { // eslint-disable-next-line @typescript-eslint/no-explicit-any @Prop({ type: Array }) readonly actions?: ITableActions[]; @Prop({ type: String }) readonly actionColWidth?: string @Prop({ type: Array }) data?: any[] | null; @Prop({ type: String }) readonly routeName?: string; @Prop({ type: Boolean }) readonly loading?: boolean; @Prop({ type: Boolean }) readonly hasSubCustom?: boolean; @Prop({ type: Boolean }) readonly hasCustom?: boolean; @Prop({ type: Boolean, default: true }) readonly hasImage?: boolean; @Prop({ type: Boolean }) readonly bordered?: boolean; @Prop({ type: Boolean }) readonly selectable?: boolean; @Prop({ type: String }) readonly dropdownWidth?: string; @Prop({ type: Array, required: true }) readonly columns!: TableColumnInfo[]; @Prop({ type: String }) readonly emptyTitle?: string; @Prop({ type: String }) readonly emptyCaption?: string; @Prop({ type: Array, default: () => [] }) readonly emptyButton!: []; @Prop({ type: String }) readonly image!: string; @Prop({ type: Boolean, default: false }) readonly inCard!: boolean; @Prop({ type: Object }) readonly params?: any; @Ref("table") readonly tableRef!: HTMLElement; @Prop({ type: Array }) value?: number[]; @Prop({ type: Number, default: 5 }) readonly loadingNumber!: number; @Prop({ type: String }) readonly loadPattern!: LoadPatternTblCard; @Prop({ type: Number, default: 2 }) readonly actionLimit?: number; isMyCardScreen = false; isAllRowSelected = false; tableOverflow = false; showOutside = true; curScrBrk = ""; iTableRef: HTMLElement | null = null; set isCardScreen(val: boolean) { this.isMyCardScreen = val; } get isCardScreen(): boolean { if (this.isMyCardScreen) { if (this.loading) { return true; } if (this.data) { if (this.data.length) { return true; } } } return false; } get isLoadingYet() { return this.loading; } get hasData() { if (this.data) { if (this.data.length) { return true; } } return false; } get isEmpty() { if (this.data) { if (!this.data.length) { return true; } } return false; } get model() { return this.value; } set model(value) { let sameAddressValue = this.value; sameAddressValue = value; this.$emit("input", sameAddressValue); } @Watch("isAllRowSelected") watchAllSelected(): void { const d: number[] = []; this.model = []; if (this.data && this.isAllRowSelected) { for (let index = 0; index < this.data.length; index += 1) { d.push(index); } } this.model = d; } resetAllSelected() { this.model = []; } handleSingleAction(customPayload): boolean { if (this.actions && this.actions.length === 1) { return this.actions[0].isShow(customPayload); } return true; } isRowSelected(index: number): string | null { const isSelected = this.model?.includes(index); return isSelected ? "selected" : null; } selectRow(): void { this.isAllRowSelected = false; } findLinkId(td: any) { const tdAsArray = Object.values(td); const tdValuesAsArray = Object.values(tdAsArray); const hasId = (obj: any): obj is any => obj?.id !== undefined; const id = tdValuesAsArray.filter(hasId); return id.length ? id[0].id : null; } findClickFunc(td: any) { const tdAsArray = Object.values(td); const tdValuesAsArray = Object.values(tdAsArray); const hasClickFunc = (obj: any): obj is any => obj?.click !== undefined; const clickFunc = tdValuesAsArray.filter(hasClickFunc); return clickFunc.length ? clickFunc[0].click : null; } isOverflowing(): void { if (this.tableRef) { if (this.tableRef.scrollWidth !== this.tableRef.clientWidth) { this.tableOverflow = true; } else { this.tableOverflow = false; } } } /** * It toggles rendered components * from virtual DOM at 768px */ handleResponsiveComponents(): void { const viewPortWidth = window.innerWidth; if (viewPortWidth >= 768) { this.isCardScreen = false; this.chkTdSize(); } else { this.isCardScreen = true; } } onDropDonwClk(toggle) { this.showOutside = !toggle; } screens = { md: 768, lg: 992, xl: 1200, }; getWidth(width) { if (!width || !this.iTableRef) { return "auto"; } if (typeof width === "object") { const inW = this.iTableRef.getBoundingClientRect().width; const [md, slg, xl] = Object.values(this.screens); if (width.xl && inW >= xl) { return width.xl; } if (width.lg && inW >= slg) { return width.lg; } if (width.md && inW >= md) { return width.md; } return width.md; } return width; } chkTdSize() { if (!this.iTableRef) return; const inW = window.innerWidth; const [md, slg, xl] = Object.values(this.screens); if (inW >= xl) { if (this.curScrBrk !== "xl") { this.curScrBrk = "xl"; } } else if (inW >= slg) { if (this.curScrBrk !== "lg") { this.curScrBrk = "lg"; } } else if (inW >= md) { if (this.curScrBrk !== "md") { this.curScrBrk = "md"; } } else if (this.curScrBrk) { this.curScrBrk = ""; } } created(): void { this.handleResponsiveComponents(); window.addEventListener("resize", this.handleResponsiveComponents); window.addEventListener("resize", this.isOverflowing); } destroyed(): void { window.removeEventListener("resize", this.handleResponsiveComponents); window.removeEventListener("resize", this.isOverflowing); } mounted(): void { this.isOverflowing(); this.$nextTick(() => { const { tableRef } = this; if (tableRef) { this.iTableRef = tableRef; } else { this.iTableRef = this.$el.querySelector("table"); } this.chkTdSize(); }); } }