import { ChangeDetectionStrategy, Component, computed, contentChild, contentChildren, inject, input, model, output, type TemplateRef, viewChild, ViewEncapsulation, } from "@angular/core"; import { SdActivatedModalProvider } from "../../core/modal/sd-activated-modal.provider"; import { SdAnchor } from "../../controls/button/sd-anchor"; import { SdButton } from "../../controls/button/sd-button"; import { SdCommandDirective } from "../../core/commands/sd-command"; import { SdForm } from "../../controls/form/sd-form"; import { SdSheet } from "../sheet/sd-sheet"; import { SdSheetColumn } from "../sheet/sd-sheet-column"; import { SdSheetColumnCellTemplate } from "../sheet/sd-sheet-column-cell-template"; import type { SdViewType } from "../../core/routing/injectViewTypeSignal"; import type { SortingDef } from "../../core/selection/useSortingManager"; import { SdBaseContainer } from "./sd-base-container"; import { NgIcon } from "@ng-icons/core"; import { NgTemplateOutlet } from "@angular/common"; import { tablerCirclePlus, tablerDeviceFloppy, tablerEraser, tablerRestore, tablerSearch, } from "@ng-icons/tabler-icons"; @Component({ selector: "sd-crud-list", changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, standalone: true, imports: [ SdBaseContainer, SdButton, NgIcon, NgTemplateOutlet, SdSheet, SdForm, SdAnchor, SdSheetColumn, SdSheetColumnCellTemplate, ], hostDirectives: [{ directive: SdCommandDirective, outputs: ["sdSaveCommand"] }], host: { "(sdSaveCommand)": "onSaveButtonClick()", }, template: ` @if (viewType() === "page" && (!readonly() || commandTplRef())) { @if (!readonly()) { 저장 (CTRL+S) } } @else if (!readonly() || commandTplRef()) { @if (!readonly()) { 저장 (CTRL+S) } } @if ((viewType() === "modal" && selectMode() != null) || bottomCommandTplRef()) { @if (bottomCommandTplRef()) {
} @if (selectMode() != null) { 선택 해제 @if (selectMode() === "multi") { 확인({{ selectedKeys().length }}) } }
}
@if (filterTplRef()) {
조회
} @if (!readonly() || toolTplRef()) {
@if (!readonly()) { 등록 @if (selectMode() !== "single") { 선택 삭제 @if (hasSelectedDeleted()) { 선택 복구 } } } @if (toolTplRef()) { }
} @if (readonly()) {
} @else { }
@if (!readonly()) {
}
`, }) export class SdCrudList { private readonly _sdActivatedModal = inject(SdActivatedModalProvider, { optional: true }); ready = model(false); initialized = input(false); busyCount = model(0); restricted = input(false); readonly = input(false); viewType = input.required(); selectMode = input<"single" | "multi">(); key = input.required(); formCtrl = viewChild("formCtrl"); filterSubmit = output(); submit = output(); create = output(); delete = output(); restore = output(); items = input([]); selectedKeys = model[]>([]); currDeletedItems = input([]); currDeletedSet = computed(() => new Set(this.currDeletedItems())); currSelectedItems = computed(() => this.items().filter((it) => { const key = this.trackByFn()(it); return key != null && this.selectedKeys().includes(key); }), ); currentPage = model(0); totalPageCount = input(0); sorts = model([]); trackByFn = input.required<(item: TItem) => TKey>(); commandTplRef = contentChild>("commandTpl"); filterTplRef = contentChild>("filterTpl"); toolTplRef = contentChild>("toolTpl"); bottomCommandTplRef = contentChild>("bottomCommandTpl"); columnControls = contentChildren(SdSheetColumn); hasSelectedDeleted = computed(() => this.currSelectedItems().some((it) => this.isDeleted(it))); hasSelectedNotDeleted = computed(() => this.currSelectedItems().some((it) => !this.isDeleted(it)), ); getItemCellStyleFn = (item: TItem): string | undefined => this.isDeleted(item) ? "text-decoration: line-through;" : undefined; isDeleted(item: TItem) { return this.currDeletedSet().has(item); } onSaveButtonClick() { this.formCtrl()?.requestSubmit(); } onModalSelectionCancelClick(): void { this.selectedKeys.set([]); if (this.selectMode() === "single") { this._sdActivatedModal?.contentComponent().close.emit({ selectedKeys: [] }); } } onModalSelectionConfirmClick() { const selectedKeys = this.selectedKeys(); this._sdActivatedModal?.contentComponent().close.emit({ selectedKeys }); } onSelectedKeysChange() { if (this.viewType() !== "modal") return; if (this.selectMode() !== "single") return; const selectedKeys = this.selectedKeys(); if (selectedKeys.length !== 1) return; this._sdActivatedModal?.contentComponent().close.emit({ selectedKeys }); } protected readonly tablerDeviceFloppy = tablerDeviceFloppy; protected readonly tablerCirclePlus = tablerCirclePlus; protected readonly tablerEraser = tablerEraser; protected readonly tablerRestore = tablerRestore; protected readonly tablerSearch = tablerSearch; }