Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add set_field action #WIK-16194 #20

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, ChangeDetectionStrategy, Input, ElementRef, signal } from '@angular/core';
import { Component, ChangeDetectionStrategy, Input, ElementRef, signal, Signal } from '@angular/core';
import { AITableFieldMenu } from '../../types/field';
import { AITableField, AITable } from '../../core';
import { AITable, AITableField } from '../../core';
import {
ThyDropdownMenuItemDirective,
ThyDropdownMenuItemNameDirective,
Expand All @@ -9,6 +9,7 @@ import {
} from 'ngx-tethys/dropdown';
import { ThyIcon } from 'ngx-tethys/icon';
import { ThyDivider } from 'ngx-tethys/divider';
import { getRecordOrField } from '../../utils';

@Component({
selector: 'field-menu',
Expand All @@ -25,7 +26,7 @@ import { ThyDivider } from 'ngx-tethys/divider';
]
})
export class FieldMenu {
@Input({ required: true }) field!: AITableField;
@Input({ required: true }) fieldId!: string;

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

Expand All @@ -34,7 +35,7 @@ export class FieldMenu {
@Input() origin!: HTMLElement | ElementRef<any>;

execute(menu: AITableFieldMenu) {
huanhuanwa marked this conversation as resolved.
Show resolved Hide resolved
const field = signal({ ...this.field });
const field = getRecordOrField(this.aiTable.fields, this.fieldId) as Signal<AITableField>;
menu.exec && menu.exec(this.aiTable, field, this.origin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<thy-input-group>
<input
thyInput
thyAutofocus
[thyAutofocus]="true"
name="fieldName"
[maxlength]="fieldMaxLength"
[(ngModel)]="aiField().name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ThyIcon } from 'ngx-tethys/icon';
import { ThyPopoverRef } from 'ngx-tethys/popover';
import { ThyListItem } from 'ngx-tethys/list';
import { of } from 'rxjs';
import { ThyAutofocusDirective } from 'ngx-tethys/shared';

@Component({
selector: 'ai-table-field-property-editor',
Expand All @@ -41,7 +42,8 @@ import { of } from 'rxjs';
ThyButton,
ThyFormModule,
ThyListItem,
NgTemplateOutlet
NgTemplateOutlet,
ThyAutofocusDirective
],
host: {
class: 'field-property-editor d-block pl-5 pr-5 pb-5 pt-4'
Expand Down Expand Up @@ -95,7 +97,8 @@ export class AITableFieldPropertyEditor {

editFieldProperty() {
if (this.isUpdate) {
//TODO: updateField
const path = this.aiTable.fields().findIndex((item) => item.id === this.aiField().id);
Actions.setField(this.aiTable, this.aiField(), [path]);
} else {
Actions.addField(this.aiTable, this.aiField(), [this.aiTable.fields().length]);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/grid/src/constants/field.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AITable, AITableField } from '../core';
import { AITableFieldMenu } from '../types/field';
import { AI_TABLE_GRID_FIELD_SERVICE_MAP } from '../services/field.service';
import { ElementRef, WritableSignal } from '@angular/core';
import { ElementRef, signal, Signal, WritableSignal } from '@angular/core';

export const DividerMenuItem = {
id: 'divider'
Expand All @@ -11,9 +11,10 @@ export const EditFieldPropertyItem = {
id: 'editFieldProperty',
name: '编辑列',
icon: 'edit',
exec: (aiTable: AITable, field: WritableSignal<AITableField>, origin?: HTMLElement | ElementRef<any>) => {
exec: (aiTable: AITable, field: Signal<AITableField>, origin?: HTMLElement | ElementRef<any>) => {
const fieldService = AI_TABLE_GRID_FIELD_SERVICE_MAP.get(aiTable);
origin && fieldService?.editFieldProperty(origin, aiTable, field, true);
const copyField: WritableSignal<AITableField> = signal(JSON.parse(JSON.stringify(field())));
pubuzhixing8 marked this conversation as resolved.
Show resolved Hide resolved
origin && fieldService?.editFieldProperty(origin, aiTable, copyField, true);
}
};

Expand Down
32 changes: 30 additions & 2 deletions packages/grid/src/core/action/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ActionName, AddFieldAction, AIFieldPath, AITable, AITableField } from '../types';
import { ActionName, AddFieldAction, AIFieldPath, AITable, AITableField, SetFieldAction } from '../types';
import { AITableQueries } from '../utils';

export function addField(aiTable: AITable, field: AITableField, path: AIFieldPath) {
const operation: AddFieldAction = {
Expand All @@ -9,6 +10,33 @@ export function addField(aiTable: AITable, field: AITableField, path: AIFieldPat
aiTable.apply(operation);
}

export function setField(aiTable: AITable, value: Partial<AITableField>, path: AIFieldPath) {
const node = AITableQueries.getField(aiTable, path);
huanhuanwa marked this conversation as resolved.
Show resolved Hide resolved
if (node) {
const field: Partial<AITableField> = {};
const newField: Partial<AITableField> = {};
for (const k in value) {
if (node[k] !== value[k]) {
if (node.hasOwnProperty(k)) {
field[k] = node[k];
}
newField[k] = value[k];
}
}

const operation: SetFieldAction = {
type: ActionName.SetField,
field,
newField,
path
};

aiTable.apply(operation);
}
}


export const FieldActions = {
addField
addField,
setField
};
12 changes: 12 additions & 0 deletions packages/grid/src/core/action/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const apply = (aiTable: AITable, records: AITableRecords, fields: AITableFields,
...newRecord
};
});
break;
}

case ActionName.SetField: {
const [fieldIndex] = options.path;
if (fieldIndex > -1) {
fields.splice(fieldIndex, 1, {
...fields[fieldIndex],
...options.newField
});
}
break;
}
}
return {
Expand Down
16 changes: 14 additions & 2 deletions packages/grid/src/core/types/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export type Path = AIRecordPath | AIFieldPath | AIFieldValuePath;
export enum ActionName {
UpdateFieldValue = 'update_field_value',
AddRecord = 'add_record',
AddField = 'add_field'
AddField = 'add_field',
SetField = 'set_field'
}

export enum ExecuteType {
Expand Down Expand Up @@ -39,4 +40,15 @@ export type AddFieldAction = {
field: AITableField;
};

export type AITableAction = UpdateFieldValueAction | AddRecordAction | AddFieldAction;
export type SetFieldAction = {
type: ActionName.SetField;
huanhuanwa marked this conversation as resolved.
Show resolved Hide resolved
path: AIFieldPath;
field: Partial<AITableField>;
newField: Partial<AITableField>;
};

export type AITableAction =
| UpdateFieldValueAction
| AddRecordAction
| AddFieldAction
| SetFieldAction;
7 changes: 7 additions & 0 deletions packages/grid/src/core/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ export const AITableQueries = {
throw new Error(`can not find field at path [${path}]`);
}
return aiTable.records()[path[0]].value[field.id];
},

getField(aiTable: AITable, path: AIFieldPath): AITableField {
if (!aiTable) {
throw new Error(`aiTable does not exist [${path}]`);
huanhuanwa marked this conversation as resolved.
Show resolved Hide resolved
}
return aiTable.fields()[path[0]];
}
};
4 changes: 2 additions & 2 deletions packages/grid/src/grid.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
[ngClass]="{ highlight: aiTable.selection().selectedFields.has(field.id) }"
[ngStyle]="{ width: field.width + 'px' }"
>
<span>
<span class="text-truncate">
<thy-icon [thyIconName]="field.icon!" class="mr-2 text-muted"></thy-icon>
<span>{{ field.name }}</span>
</span>
<a class="grid-field-action" thyAction thyActiveClass="active" thyIcon="more-vertical" [thyDropdown]="fieldMenu" href="javascript:;">
<thy-dropdown-menu #fieldMenu>
<field-menu [origin]="fieldAction" [field]="field" [aiTable]="aiTable" [fieldMenus]="fieldMenus"></field-menu>
<field-menu [origin]="fieldAction" [fieldId]="field.id" [aiTable]="aiTable" [fieldMenus]="fieldMenus"></field-menu>
</thy-dropdown-menu>
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/src/types/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface AITableFieldMenu {
id: string;
name?: string;
icon?: string;
exec?: (aiTable: AITable, field: WritableSignal<AITableField>, origin?: HTMLElement | ElementRef<any>) => void;
exec?: (aiTable: AITable, field: Signal<AITableField>, origin?: HTMLElement | ElementRef<any>) => void;
hidden?: (aiTable: AITable, field: Signal<AITableField>) => boolean;
disabled?: (aiTable: AITable, field: Signal<AITableField>) => boolean;
}