Skip to content

Commit

Permalink
feat: optimzie code
Browse files Browse the repository at this point in the history
  • Loading branch information
huanhuanwa committed Jul 30, 2024
1 parent 8041a09 commit f414ba6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ChangeDetectionStrategy, Input, ElementRef, signal, Signal } from '@angular/core';
import { AITableFieldMenu } from '../../types/field';
import { AITableFieldMenuItem } from '../../types/field';
import { AITable, AITableField } from '../../core';
import {
ThyDropdownMenuItemDirective,
Expand Down Expand Up @@ -30,11 +30,11 @@ export class FieldMenu {

@Input({ required: true }) aiTable!: AITable;

@Input({ required: true }) fieldMenus!: AITableFieldMenu[];
@Input({ required: true }) fieldMenus!: AITableFieldMenuItem[];

@Input() origin!: HTMLElement | ElementRef<any>;

execute(menu: AITableFieldMenu) {
execute(menu: AITableFieldMenuItem) {
const field = getRecordOrField(this.aiTable.fields, this.fieldId) as Signal<AITableField>;
menu.exec && menu.exec(this.aiTable, field, this.origin);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/constants/field.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AITable, AITableField } from '../core';
import { AITableFieldMenu } from '../types/field';
import { AITableFieldMenuItem } from '../types/field';
import { AI_TABLE_GRID_FIELD_SERVICE_MAP } from '../services/field.service';
import { ElementRef, signal, Signal, WritableSignal } from '@angular/core';

Expand All @@ -18,4 +18,4 @@ export const EditFieldPropertyItem = {
}
};

export const DefaultFieldMenus: AITableFieldMenu[] = [EditFieldPropertyItem];
export const DefaultFieldMenus: AITableFieldMenuItem[] = [EditFieldPropertyItem];
15 changes: 7 additions & 8 deletions packages/grid/src/core/action/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ export function addField(aiTable: AITable, field: AITableField, path: AIFieldPat
}

export function setField(aiTable: AITable, value: Partial<AITableField>, path: AIFieldPath) {
const node = AITableQueries.getField(aiTable, path);
if (node) {
const field: Partial<AITableField> = {};
const field = AITableQueries.getField(aiTable, path);
if (field) {
const oldField: Partial<AITableField> = {};
const newField: Partial<AITableField> = {};
for (const k in value) {
if (node[k] !== value[k]) {
if (node.hasOwnProperty(k)) {
field[k] = node[k];
if (field[k] !== value[k]) {
if (field.hasOwnProperty(k)) {
oldField[k] = field[k];
}
newField[k] = value[k];
}
}

const operation: SetFieldAction = {
type: ActionName.SetField,
field,
field: oldField,
newField,
path
};
Expand All @@ -35,7 +35,6 @@ export function setField(aiTable: AITable, value: Partial<AITableField>, path: A
}
}


export const FieldActions = {
addField,
setField
Expand Down
6 changes: 3 additions & 3 deletions packages/grid/src/core/action/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ActionName, AddRecordAction, AIRecordPath, UpdateFieldValueAction, AITa
import { AITableQueries } from '../utils';

export function updateFieldValue(aiTable: AITable, value: any, path: AIFieldValuePath) {
const node = AITableQueries.getFieldValue(aiTable, path);
if (node !== value) {
const field = AITableQueries.getFieldValue(aiTable, path);
if (field !== value) {
const operation: UpdateFieldValueAction = {
type: ActionName.UpdateFieldValue,
fieldValue: node,
fieldValue: field,
newFieldValue: value,
path
};
Expand Down
14 changes: 10 additions & 4 deletions packages/grid/src/core/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ export const AITableQueries = {
},
getFieldValue(aiTable: AITable, path: [number, number]): any {
if (!aiTable) {
throw new Error(`aiTable does not exist [${path}]`);
throw new Error(`aiTable does not exist`);
}
if (!aiTable.records()) {
throw new Error(`aiTable has no records [${path}]`);
throw new Error(`aiTable has no records`);
}
if (!aiTable.fields()) {
throw new Error(`aiTable has no fields [${path}]`);
throw new Error(`aiTable has no fields`);
}
if (!path) {
throw new Error(`path does not exist as path [${path}]`);
}

const field = aiTable.fields()[path[1]];
Expand All @@ -36,7 +39,10 @@ export const AITableQueries = {

getField(aiTable: AITable, path: AIFieldPath): AITableField {
if (!aiTable) {
throw new Error(`aiTable does not exist [${path}]`);
throw new Error(`aiTable does not exist`);
}
if (!path) {
throw new Error(`path does not exist as path [${path}]`);
}
return aiTable.fields()[path[0]];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/grid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ThyTag } from 'ngx-tethys/tag';
import { ThyPopoverModule } from 'ngx-tethys/popover';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { buildGridData } from './utils';
import { AIFieldConfig, AITableFieldMenu, AITableRowHeight } from './types';
import { AIFieldConfig, AITableFieldMenuItem, AITableRowHeight } from './types';
import {
Actions,
createAITable,
Expand Down Expand Up @@ -94,7 +94,7 @@ export class AITableGrid implements OnInit {

aiTableInitialized = output<AITable>();

fieldMenus!: AITableFieldMenu[];
fieldMenus!: AITableFieldMenuItem[];

gridData = computed(() => {
return buildGridData(this.aiRecords(), this.aiFields(), this.aiTable.selection());
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/types/field.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ElementRef, Signal, WritableSignal } from '@angular/core';
import { ElementRef, Signal } from '@angular/core';
import { AITable, AITableField } from '../core';

export interface AITableFieldMenu {
export interface AITableFieldMenuItem {
id: string;
name?: string;
icon?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/src/types/grid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AITableField, AITableFieldType, AITableRecord } from '../core';
import { AITableFieldMenu } from './field';
import { AITableFieldMenuItem } from './field';

export enum AITableRowHeight {
Short = 1,
Expand All @@ -26,5 +26,5 @@ export interface AITableSelection {
export interface AIFieldConfig {
fieldRenderers?: Partial<Record<AITableFieldType, AITableGridCellRenderSchema>>;
fieldPropertyEditor?: any;
fieldMenus?: AITableFieldMenu[];
fieldMenus?: AITableFieldMenuItem[];
}

0 comments on commit f414ba6

Please sign in to comment.