Skip to content

Commit 786d09b

Browse files
committed
feat: entry-actionbar
- can now pick from multiple models via entry-actionbar.
1 parent e3ef5b2 commit 786d09b

File tree

7 files changed

+126
-2
lines changed

7 files changed

+126
-2
lines changed

packages/data/src/lib/data.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ import { ResourceModule } from './resource/resource.module';
2424
import { HistoryService } from './sdk/history.service';
2525
import { SdkModule } from './sdk/sdk.module';
2626
import { EntryListSelectComponent } from './entry-list-select/entry-list-select.component';
27+
import { EntryActionbarComponent } from './entry-actionbar/entry-actionbar.component';
2728

2829
export const dataModuleConfig = {
2930
entryComponents: [
3031
DefaultEntryInputComponent,
3132
DefaultEntryOutputComponent,
3233
AdminEntryInputComponent,
3334
EntrySelectComponent,
35+
EntryActionbarComponent,
3436
EntryListSelectComponent,
3537
EntryListPopComponent,
3638
],
@@ -46,6 +48,7 @@ export const dataModuleConfig = {
4648
EntryListSelectComponent,
4749
CrudComponent,
4850
EntrySelectComponent,
51+
EntryActionbarComponent,
4952
EntryListPopComponent,
5053
],
5154
imports: [FormsModule, CommonModule, UiModule, SdkModule, FilesModule, AuthModule, ResourceModule, DndModule],
@@ -57,6 +60,7 @@ export const dataModuleConfig = {
5760
EntryPopComponent,
5861
CrudComponent,
5962
EntrySelectComponent,
63+
EntryActionbarComponent,
6064
EntryListSelectComponent,
6165
EntryListPopComponent,
6266
RouterModule,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { ChangeDetectorRef, Component, ElementRef, EventEmitter, forwardRef, Input, OnInit } from '@angular/core';
2+
import { NG_VALUE_ACCESSOR } from '@angular/forms';
3+
import { Item } from '@ec.components/core';
4+
import { ModelConfigService } from '../model-config/model-config.service';
5+
import { Action, ActionbarComponent, ListComponent, selectTemplate } from '@ec.components/ui';
6+
import EntryResource from 'ec.sdk/lib/resources/publicAPI/EntryResource';
7+
import LiteEntryResource from 'ec.sdk/lib/resources/publicAPI/LiteEntryResource';
8+
import { CrudConfig } from '../crud/crud-config.interface';
9+
import { ResourceConfig } from '../resource-config/resource-config.service';
10+
import { SdkService } from '../sdk/sdk.service';
11+
12+
@Component({
13+
selector: 'ec-entry-actionbar',
14+
template: selectTemplate,
15+
/* encapsulation: ViewEncapsulation.None, */
16+
providers: [
17+
{
18+
provide: NG_VALUE_ACCESSOR,
19+
useExisting: forwardRef(() => EntryActionbarComponent),
20+
multi: true,
21+
},
22+
],
23+
})
24+
export class EntryActionbarComponent extends ActionbarComponent implements OnInit {
25+
entrySelectActions: Action[];
26+
selectedEntries;
27+
/** The event that focuses the input */
28+
@Input() focusEvent: EventEmitter<boolean> = new EventEmitter();
29+
/** The model to pick from, alternative to field with model property set. */
30+
@Input() model: string;
31+
/** The config that should be merged into the generated config */
32+
// tslint:disable-next-line:no-input-rename
33+
@Input('config') crudConfig: CrudConfig<EntryResource>;
34+
lightModel: any;
35+
36+
constructor(
37+
public sdk: SdkService,
38+
public resourceConfig: ResourceConfig,
39+
public elementRef: ElementRef,
40+
public cdr: ChangeDetectorRef,
41+
public modelConfig: ModelConfigService,
42+
) {
43+
super(elementRef, cdr);
44+
}
45+
46+
ngOnInit() {
47+
if (!this.model) {
48+
this.loadModelActions();
49+
} else {
50+
this.loadEntryActions(this.model);
51+
}
52+
}
53+
54+
getEntryAction(entry: EntryResource | LiteEntryResource) {
55+
return {
56+
id: entry._id,
57+
title: entry._entryTitle,
58+
};
59+
}
60+
61+
async loadModelActions() {
62+
delete this.model;
63+
this.placeholder = 'Select Model...';
64+
const modelActions = await this.getModelActions();
65+
this.loadActions(modelActions);
66+
}
67+
68+
async getModelActions(): Promise<Action[]> {
69+
await this.sdk.ready;
70+
return this.sdk.api.models.map((model) => ({
71+
id: 'model',
72+
select: false,
73+
title: model.title,
74+
action: () => this.loadEntryActions(model.title),
75+
}));
76+
}
77+
78+
async loadEntryActions(model, filterOptions = {}) {
79+
this.model = model;
80+
this.lightModel = await this.modelConfig.getLightModel(this.model);
81+
const entryList = await this.sdk.api.entryList(model, filterOptions);
82+
const entryActions: any[] = entryList.getAllItems().map((entry) => this.getEntryAction(entry));
83+
this.placeholder = 'Select Entry from "' + model + '"...';
84+
entryActions.unshift({
85+
id: 'select-another-model',
86+
title: 'Select another model...',
87+
select: false,
88+
action: () => this.loadModelActions(),
89+
});
90+
this.loadActions(entryActions);
91+
}
92+
93+
writeValue(value) {
94+
if (!value) {
95+
value = [];
96+
}
97+
this.selection.replaceWith(value.map((liteEntry) => new Item(this.getEntryAction(liteEntry), this.config)));
98+
}
99+
100+
filterDropdownList(listComponent: ListComponent<any>, query) {
101+
if (this.lightModel && this.model) {
102+
this.loadEntryActions(this.model, { [this.lightModel.titleField]: { search: query } }); // title: { search: query }
103+
} else {
104+
return super.filterDropdownList(listComponent, query);
105+
}
106+
}
107+
}

packages/data/src/lib/entry-form/default-entry-input.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<ec-entry-select [focusEvent]="focusEvent" [placeholder]="field.placeholder" [formControl]="group.get(field.property)"
99
[model]="field['relation']" [id]="field.id" [config]="field.nestedCrudConfig"></ec-entry-select>
1010
</div>
11+
<div *ngSwitchCase="'entries-actionbar'">
12+
<ec-entry-actionbar [focusEvent]="focusEvent" [placeholder]="field.placeholder" [formControl]="group.get(field.property)"
13+
[model]="field['relation']" [id]="field.id" [config]="field.nestedCrudConfig"></ec-entry-actionbar>
14+
</div>
1115
<div *ngSwitchCase="'entry-list-select'">
1216
<!-- [placeholder]="field.placeholder" -->
1317
<!-- [config]="field.nestedCrudConfig" -->

packages/data/src/lib/model-config/type-config.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ export class TypeConfigService {
126126
},
127127
entries: {
128128
view: 'tags',
129-
inputView: 'entries-select',
129+
inputView: 'entries-actionbar',
130130
inputViews: [
131131
{
132132
name: 'entries-select',
133133
},
134+
{
135+
name: 'entries-actionbar',
136+
},
134137
{
135138
name: 'entry-list-select',
136139
levels: 2,

packages/ui/src/lib/actionbar/actionbar.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit, Input, ElementRef, ChangeDetectorRef } from '@angular/core';
22
import { SelectComponent } from '../select/select.component';
3+
import { selectTemplate } from '../select/select.component.html';
34
import { ListConfig, List, Item } from '@ec.components/core';
45

56
export type ActionFunction = (item?: Item<Action> | any, actionbar?: ActionbarComponent) => any;
@@ -17,7 +18,7 @@ export interface ActionbarConfig extends ListConfig<Action> {}
1718

1819
@Component({
1920
selector: 'ec-actionbar',
20-
templateUrl: '../select/select.component.html',
21+
template: selectTemplate,
2122
})
2223
export class ActionbarComponent extends SelectComponent<Action> implements OnInit {
2324
@Input() root = 'ROOT'; // id of root stack item

packages/ui/src/lib/list/searchbar/searchbar.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export class SearchbarComponent implements AfterViewInit, Focus, OnInit, OnChang
161161
if (query !== this.latestQuery) {
162162
this.query = query;
163163
}
164+
/* this.queryValue.next(query); */
164165
this.cdr.markForCheck();
165166
}
166167

src/app/resource-select/resource-select-demo.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ <h3>Asset Select</h3>
3535
[config]="assetSelectConfig"
3636
></ec-resource-select>
3737

38+
<h3>Entry Actionbar</h3>
39+
<ec-entry-actionbar [(ngModel)]="selectedEntries"></ec-entry-actionbar>
40+
{{ selectedEntries | json}}
41+
3842
<!-- <h3>Resource Actionbar</h3>
3943
<ec-resource-actionbar></ec-resource-actionbar> -->
4044

0 commit comments

Comments
 (0)