Browse Source

Merge pull request #10202 from abpframework/feat/10200

add component and enumList properties to entity props
pull/10228/head
Mehmet Erim 5 years ago
committed by GitHub
parent
commit
23a264950c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      npm/ng-packs/packages/theme-shared/extensions/src/lib/components/extensible-table/extensible-table.component.html
  2. 34
      npm/ng-packs/packages/theme-shared/extensions/src/lib/components/extensible-table/extensible-table.component.ts
  3. 7
      npm/ng-packs/packages/theme-shared/extensions/src/lib/models/entity-props.ts
  4. 2
      npm/ng-packs/packages/theme-shared/extensions/src/lib/tokens/extensions.token.ts

26
npm/ng-packs/packages/theme-shared/extensions/src/lib/components/extensible-table/extensible-table.component.html

@ -25,14 +25,24 @@
> >
<ng-template let-row="row" let-i="index" ngx-datatable-cell-template> <ng-template let-row="row" let-i="index" ngx-datatable-cell-template>
<ng-container *abpPermission="prop.permission"> <ng-container *abpPermission="prop.permission">
<div <ng-container *ngIf="row['_' + prop.name]?.visible">
*ngIf="row['_' + prop.name]?.visible" <div
[innerHTML]="row['_' + prop.name]?.value | async" *ngIf="!row['_' + prop.name].component; else component"
(click)=" [innerHTML]="row['_' + prop.name]?.value | async"
prop.action && prop.action({ getInjected: getInjected, record: row, index: i }) (click)="
" prop.action && prop.action({ getInjected: getInjected, record: row, index: i })
[class.pointer]="prop.action" "
></div> [class.pointer]="prop.action"
></div>
</ng-container>
<ng-template #component>
<ng-container
*ngComponentOutlet="
row['_' + prop.name].component;
injector: row['_' + prop.name].injector
"
></ng-container>
</ng-template>
</ng-container> </ng-container>
</ng-template> </ng-template>
</ngx-datatable-column> </ngx-datatable-column>

34
npm/ng-packs/packages/theme-shared/extensions/src/lib/components/extensible-table/extensible-table.component.ts

@ -1,4 +1,5 @@
import { import {
ABP,
ConfigStateService, ConfigStateService,
getShortDateFormat, getShortDateFormat,
getShortDateShortTimeFormat, getShortDateShortTimeFormat,
@ -29,7 +30,8 @@ import { EntityActionList } from '../../models/entity-actions';
import { EntityProp, EntityPropList } from '../../models/entity-props'; import { EntityProp, EntityPropList } from '../../models/entity-props';
import { PropData } from '../../models/props'; import { PropData } from '../../models/props';
import { ExtensionsService } from '../../services/extensions.service'; import { ExtensionsService } from '../../services/extensions.service';
import { EXTENSIONS_IDENTIFIER } from '../../tokens/extensions.token'; import { EXTENSIONS_IDENTIFIER, PROP_DATA_STREAM } from '../../tokens/extensions.token';
const DEFAULT_ACTIONS_COLUMN_WIDTH = 150; const DEFAULT_ACTIONS_COLUMN_WIDTH = 150;
@Component({ @Component({
@ -71,7 +73,7 @@ export class ExtensibleTableComponent<R = any> implements OnChanges {
constructor( constructor(
@Inject(LOCALE_ID) private locale: string, @Inject(LOCALE_ID) private locale: string,
private config: ConfigStateService, private config: ConfigStateService,
injector: Injector, private injector: Injector,
) { ) {
this.getInjected = injector.get.bind(injector); this.getInjected = injector.get.bind(injector);
const extensions = injector.get(ExtensionsService); const extensions = injector.get(ExtensionsService);
@ -106,6 +108,12 @@ export class ExtensibleTableComponent<R = any> implements OnChanges {
: '<div class="text-center text-danger"><i class="fa fa-times"></i></div>'; : '<div class="text-center text-danger"><i class="fa fa-times"></i></div>';
} }
private getEnum(rowValue: any, list: Array<ABP.Option<any>>) {
if (!list) return rowValue;
const { key } = list.find(({ value }) => value === rowValue);
return key;
}
getContent(prop: EntityProp<R>, data: PropData): Observable<string> { getContent(prop: EntityProp<R>, data: PropData): Observable<string> {
return prop.valueResolver(data).pipe( return prop.valueResolver(data).pipe(
map(value => { map(value => {
@ -118,6 +126,8 @@ export class ExtensibleTableComponent<R = any> implements OnChanges {
return this.getDate(value, getShortTimeFormat(this.config)); return this.getDate(value, getShortTimeFormat(this.config));
case ePropType.DateTime: case ePropType.DateTime:
return this.getDate(value, getShortDateShortTimeFormat(this.config)); return this.getDate(value, getShortDateShortTimeFormat(this.config));
case ePropType.Enum:
return this.getEnum(value, prop.enumList);
default: default:
return value; return value;
// More types can be handled in the future // More types can be handled in the future
@ -132,10 +142,26 @@ export class ExtensibleTableComponent<R = any> implements OnChanges {
this.data = data.currentValue.map((record, index) => { this.data = data.currentValue.map((record, index) => {
this.propList.forEach(prop => { this.propList.forEach(prop => {
const propData = { getInjected: this.getInjected, record, index } as any; const propData = { getInjected: this.getInjected, record, index } as any;
record[`_${prop.value.name}`] = { const value = this.getContent(prop.value, propData);
const propKey = `_${prop.value.name}`;
record[propKey] = {
visible: prop.value.visible(propData), visible: prop.value.visible(propData),
value: this.getContent(prop.value, propData), value,
}; };
if (prop.value.component) {
const injector = Injector.create(
[
{
provide: PROP_DATA_STREAM,
useValue: value,
},
],
this.injector,
);
record[propKey].injector = injector;
record[propKey].component = prop.value.component;
}
}); });
return record; return record;

7
npm/ng-packs/packages/theme-shared/extensions/src/lib/models/entity-props.ts

@ -1,6 +1,7 @@
import { Type } from '@angular/core'; import { Type } from '@angular/core';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { O } from 'ts-toolbelt'; import { O } from 'ts-toolbelt';
import { ABP } from '@abp/ng.core';
import { ActionCallback } from './actions'; import { ActionCallback } from './actions';
import { import {
Prop, Prop,
@ -27,6 +28,8 @@ export class EntityProp<R = any> extends Prop<R> {
readonly sortable: boolean; readonly sortable: boolean;
readonly valueResolver: PropCallback<R, Observable<any>>; readonly valueResolver: PropCallback<R, Observable<any>>;
readonly action: ActionCallback<R>; readonly action: ActionCallback<R>;
readonly component: Type<any>;
readonly enumList: Array<ABP.Option<any>>;
constructor(options: EntityPropOptions<R>) { constructor(options: EntityPropOptions<R>) {
super( super(
@ -42,6 +45,8 @@ export class EntityProp<R = any> extends Prop<R> {
this.sortable = options.sortable || false; this.sortable = options.sortable || false;
this.valueResolver = options.valueResolver || (data => of(data.record[this.name])); this.valueResolver = options.valueResolver || (data => of(data.record[this.name]));
this.action = options.action; this.action = options.action;
this.component = options.component;
this.enumList = options.enumList;
} }
static create<R = any>(options: EntityPropOptions<R>) { static create<R = any>(options: EntityPropOptions<R>) {
@ -63,6 +68,8 @@ export type EntityPropOptions<R = any> = O.Optional<
| 'sortable' | 'sortable'
| 'valueResolver' | 'valueResolver'
| 'action' | 'action'
| 'component'
| 'enumList'
>; >;
export type EntityPropDefaults<R = any> = Record<string, EntityProp<R>[]>; export type EntityPropDefaults<R = any> = Record<string, EntityProp<R>[]>;

2
npm/ng-packs/packages/theme-shared/extensions/src/lib/tokens/extensions.token.ts

@ -1,6 +1,7 @@
import { InjectionToken } from '@angular/core'; import { InjectionToken } from '@angular/core';
import { ActionCallback, ReadonlyActionData as ActionData } from '../models/actions'; import { ActionCallback, ReadonlyActionData as ActionData } from '../models/actions';
import { ExtensionsService } from '../services/extensions.service'; import { ExtensionsService } from '../services/extensions.service';
import { Observable } from 'rxjs';
export const EXTENSIONS_IDENTIFIER = new InjectionToken<string>('EXTENSIONS_IDENTIFIER'); export const EXTENSIONS_IDENTIFIER = new InjectionToken<string>('EXTENSIONS_IDENTIFIER');
export type ActionKeys = Extract<'entityActions' | 'toolbarActions', keyof ExtensionsService>; export type ActionKeys = Extract<'entityActions' | 'toolbarActions', keyof ExtensionsService>;
@ -11,3 +12,4 @@ export const EXTENSIONS_ACTION_DATA = new InjectionToken<ActionData>('EXTENSIONS
export const EXTENSIONS_ACTION_CALLBACK = new InjectionToken<ActionCallback<unknown>>( export const EXTENSIONS_ACTION_CALLBACK = new InjectionToken<ActionCallback<unknown>>(
'EXTENSIONS_ACTION_DATA', 'EXTENSIONS_ACTION_DATA',
); );
export const PROP_DATA_STREAM = new InjectionToken<Observable<any>>('PROP_DATA_STREAM');

Loading…
Cancel
Save