Skip to content

Commit d068283

Browse files
committed
Add back some admin files still needed for now
1 parent 070a409 commit d068283

21 files changed

Lines changed: 483 additions & 0 deletions

src/lib/admin/src/ActionParams.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Field from './Field';
2+
3+
export default class ActionParams {
4+
private _params: Array<Field> = [];
5+
6+
get params(): Array<Field> {
7+
return this._params;
8+
}
9+
10+
public static id(): ActionParams {
11+
return new ActionParams().and('id');
12+
}
13+
14+
public static with(field: string | Field): ActionParams {
15+
return new ActionParams().and(field);
16+
}
17+
18+
public and(field: string | Field): ActionParams {
19+
if (field instanceof Field) {
20+
this._params.push(field);
21+
} else {
22+
this._params.push(new Field(field));
23+
}
24+
25+
return this;
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Field from './Field';
2+
import FieldHtmlProperties from './FieldHtmlProperties';
3+
4+
export default class AssociatedField extends Field {
5+
constructor(
6+
name: string,
7+
associated_field: Field,
8+
field_html_properties: FieldHtmlProperties = FieldHtmlProperties.defaults()
9+
) {
10+
super(name, '', associated_field, field_html_properties);
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type Field from './Field';
2+
3+
export default class AssociatedItem<T> {
4+
public readonly item: T;
5+
public readonly fields: Field[];
6+
7+
constructor(item: T, fields: Field[]) {
8+
this.item = item;
9+
this.fields = fields;
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import DefaultAction from './DefaultAction';
2+
3+
export default class CallbackAction extends DefaultAction {
4+
private readonly _callback: Function;
5+
6+
constructor(name: string, callback: Function) {
7+
super(name);
8+
this._callback = callback;
9+
}
10+
11+
public call(item: object): string {
12+
return this._callback.call(null, item);
13+
}
14+
}

src/lib/admin/src/Collection.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default class Collection<T> extends Array<T> {
2+
constructor(items: T[] = []) {
3+
super();
4+
this.addItems(items);
5+
}
6+
7+
private addItems(items: T[] = []) {
8+
items.forEach((item) => this.push(item));
9+
}
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type AssociatedField from './AssociatedField';
2+
import Field from './Field';
3+
import FieldOptions from './FieldOptions';
4+
5+
export default class CollectionField extends Field {
6+
constructor(name: string, text: string, item_field: Field) {
7+
super(name, text, new FieldOptions(item_field));
8+
}
9+
10+
public displayFromItem(item: object | any): any {
11+
let field: Field | AssociatedField;
12+
13+
const items = item[this.name];
14+
15+
if (items.length === 0 || !(items instanceof Array)) {
16+
return null;
17+
}
18+
19+
field = this._associated_field;
20+
21+
return items.map((singleItem) => field.displayFromItem(singleItem));
22+
}
23+
}

src/lib/admin/src/ConfigFilter.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import FilterType from './FilterType';
2+
import type FilterWithValue from './FilterWithValue';
3+
import type Filter from '../components/PaginatedTable/Filter.svelte';
4+
5+
export default class ConfigFilter {
6+
public readonly name: string;
7+
public readonly title: string;
8+
public readonly type: FilterType;
9+
public readonly options: { [key: string]: any };
10+
public element: Filter | null;
11+
public value: FilterWithValue | null;
12+
13+
constructor(name: string, title: string, type: FilterType, options?: { [key: string]: any }) {
14+
this.name = name;
15+
this.title = title;
16+
this.type = type;
17+
this.options = options || {};
18+
this.validateOptions();
19+
}
20+
21+
private validateOptions() {
22+
switch (this.type) {
23+
case FilterType.entity:
24+
if (!this.options.entities) {
25+
throw new Error(
26+
'To define a filter of type "entity", you must also specify the "entities" option. This must contain an array that contains elements of type "{name: string, value: string}", or a callable that returns such array, or a promise that resolves to such array.'
27+
);
28+
}
29+
break;
30+
default:
31+
break;
32+
}
33+
}
34+
}

src/lib/admin/src/DefaultAction.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type ItemAction from './ItemAction';
2+
3+
export default abstract class DefaultAction implements ItemAction {
4+
protected readonly _name: string;
5+
6+
protected constructor(name: string) {
7+
this._name = name;
8+
}
9+
10+
get name(): string {
11+
return this._name;
12+
}
13+
}

src/lib/admin/src/Field.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type AssociatedField from './AssociatedField';
2+
import type FieldHtmlProperties from './FieldHtmlProperties';
3+
import FieldOptions from './FieldOptions';
4+
import SortableField from './SortableField';
5+
6+
export const Sortable = true;
7+
8+
export default class Field {
9+
public readonly name: string;
10+
public readonly text: string;
11+
public readonly field_html_properties: FieldHtmlProperties;
12+
public readonly sortable_field: SortableField | null;
13+
protected readonly _associated_field: null | AssociatedField;
14+
15+
constructor(
16+
name: string,
17+
text: string = '',
18+
options: FieldOptions | null = null,
19+
sortable: boolean = false
20+
) {
21+
if (!options) {
22+
options = FieldOptions.defaults();
23+
}
24+
this.name = name;
25+
this.text = text === '' ? name : text;
26+
this.field_html_properties = options.field_html_properties;
27+
this._associated_field = options.associated_field;
28+
this.sortable_field = sortable
29+
? new SortableField(name, null, options.sortable_property_name || name)
30+
: null;
31+
}
32+
33+
public displayFromItem(item: object | any): any {
34+
let field: Field | AssociatedField;
35+
36+
if (this._associated_field) {
37+
item = item[this.name];
38+
field = this._associated_field;
39+
} else {
40+
field = this;
41+
}
42+
43+
return item[field.name];
44+
}
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default class FieldHtmlProperties {
2+
public readonly html_class: string;
3+
4+
constructor(html_class: string = '') {
5+
this.html_class = html_class;
6+
}
7+
8+
static defaults() {
9+
return new FieldHtmlProperties();
10+
}
11+
}

0 commit comments

Comments
 (0)