File

packages/data/src/lib/resource-list/resource-list.ts

Description

Extension of List for SDK ListResource. Each each implementation should implement the load method to call the SDK method for loading the desired list! (see EntryList for example)

Extends

List

Index

Properties
Methods

Constructor

constructor(config: ListConfig, api?: Core, relation?, listResource?: ListResource)

The constructor will init the List and Pagination instances. Make sure the config is already complete when initiating an EntryList instance.

Parameters :
Name Type Optional
config ListConfig<Resource> No
api Core Yes
relation Yes
listResource ListResource Yes

Properties

Public Optional api
Type : Core
Protected error
Type : Subject<Error>
Default value : new Subject()

Subject that should be nexted when an error occurs

Public error$
Type : Observable<Error>
Default value : this.error.asObservable()

Observable that is nexted when the list has an error.

Protected listResource
Type : ListResource

The current loaded ListResource

Public loading
Type : Subject<Promise<any>>
Default value : new Subject()

Subject that should be nexted when loading begins

Public loading$
Default value : this.loading.asObservable()

Observable that is nexted when the list begins loading.

Public promise
Type : Promise<any>

latest loading promise

Public Optional relation
Protected change
Type : Subject<List<T>>
Default value : new Subject()
Inherited from List
Defined in List:33

Subject that should be nexted when loading is finished

Public change$
Type : Observable<List<T>>
Default value : this.change.asObservable()
Inherited from List
Defined in List:35

Observable that is nexted when the list has changed.

Public config
Type : ListConfig<T>
Inherited from List
Defined in List:23

The List Configuration, click on ListConfig for details. Can be given an optional ListConfig.

Public fields
Type : Array<Field>
Inherited from List
Defined in List:19

Array of Properties that are relevant for each item. The fields are populated on construction via getFields method.

groups
Type : []
Default value : []
Inherited from List
Defined in List:27

Current Value Groups (Different Unique Values).

Public page
Type : Array<Item<T>>
Default value : []
Inherited from List
Defined in List:31

The items of the current page

Public pagination
Type : Pagination<T>
Inherited from List
Defined in List:29

The list's pagination (Optional)

Public items
Type : Array<T>
Inherited from List
Defined in List:10

The items must all have the same type T.

Protected update
Type : Subject<Collection<T>>
Default value : new Subject()
Inherited from List
Defined in List:12

Subject that is nexted when the items update

Public update$
Type : Observable<Collection<T>>
Default value : this.update.asObservable()
Inherited from List
Defined in List:14

Subject that is nexted when the items change

Public items
Type : Array<T>
Inherited from Collection
Defined in Collection:10

The items must all have the same type T.

Protected update
Type : Subject<Collection<T>>
Default value : new Subject()
Inherited from Collection
Defined in Collection:12

Subject that is nexted when the items update

Public update$
Type : Observable<Collection<T>>
Default value : this.update.asObservable()
Inherited from Collection
Defined in Collection:14

Subject that is nexted when the items change

Methods

filter
filter(property: string, value: any)

Filters the entry list by a given property value. Triggers load.

Parameters :
Name Type Optional Default value
property string No
value any No ''
Returns : any
filterProperty
filterProperty(property: string, value: any)

Updates the config.filter with the given property filter.

Parameters :
Name Type Optional Default value
property string No
value any No ''
Returns : any
Protected Static getFilterOperator
getFilterOperator(property: string, fields: Array)

Returns the operator to use for filtering the given property. Defaults to search.

Parameters :
Name Type Optional
property string No
fields Array<Field> No
Returns : string
Protected getFilterOptions
getFilterOptions(undefined: ListConfig)

Returns SDK filterOptions from a given ListConfig.

Parameters :
Name Type Optional Default value
ListConfig<Resource> No {}
Returns : filterOptions
Public isRawFilter
isRawFilter(property: string, fields: Array)

Returns true if the field of the given property has rawFilter set to true

Parameters :
Name Type Optional Default value
property string No
fields Array<Field> No this.fields
Returns : boolean
load
load(config?: ListConfig)
Parameters :
Name Type Optional
config ListConfig<Resource> Yes
Returns : any
toggleSort
toggleSort(property: string, desc?: boolean)

Toggles sorting of the given property. Overloads list method to reload with the new sort setup

Parameters :
Name Type Optional
property string No
desc boolean Yes
Returns : void
Protected use
use(listResource)

Takes the listResource and dumps the items into the the current page. Then it applies grouping if present.

Parameters :
Name Optional
listResource No
Returns : void
Protected useConfig
useConfig(config?: ListConfig)

deletes all undefined values from given config and assigns it to this.config

Parameters :
Name Type Optional
config ListConfig<Resource> Yes
Returns : void
add
add(item: Item, unique?: boolean, event: boolean)
Inherited from List
Defined in List:79

Adds the given item to the list and assigns the list config to the item

Parameters :
Name Type Optional Default value
item Item<T> No
unique boolean Yes
event boolean No true
Returns : any
clearFilter
clearFilter(property?: string)
Inherited from List
Defined in List:180

Clears the filter for given property or all properties if none given.

Parameters :
Name Type Optional
property string Yes
Returns : void
Public filter
filter(property: string, value: any, operator: string)
Inherited from List
Defined in List:141

Filters the list after the given property and value

Parameters :
Name Type Optional Default value
property string No
value any No ''
operator string No 'exact'
Returns : void
Public filterableFields
filterableFields()
Inherited from List
Defined in List:303

Returns an array of all sortable fields

Returns : any
Protected getFields
getFields()
Inherited from List
Defined in List:88

Distills Array of item properties. Either uses keys of config.fields or parses the item properties directly.

Returns : Array<Field>
getFilterValue
getFilterValue(property?: string, filterOptions)
Inherited from List
Defined in List:213

Returns the filter

Parameters :
Name Type Optional Default value
property string Yes
filterOptions No this.config.filter
Returns : any
groupBy
groupBy(property)
Inherited from List
Defined in List:266

Returns an Array of all unique values of the given property

Parameters :
Name Optional
property No
Returns : void
Protected hideOverflowFields
hideOverflowFields()
Inherited from List
Defined in List:111

Sets all fields that exceed the maxColumns to hidden

Returns : void
id
id(identifier: any)
Inherited from List
Defined in List:126

Resolves the item with the given Array index or identifier (if configured)

Parameters :
Name Type Optional
identifier any No
Returns : Item<T>
isEmptyFilter
isEmptyFilter(query: null | undefined | string | Array | Object)
Inherited from List
Defined in List:191

Helper function. Returns true if the given query value is empty (also recognizes empty array)

Parameters :
Name Type Optional
query null | undefined | string | Array<any> | Object No
Returns : boolean
isFiltered
isFiltered(property?: string, filterOptions)
Inherited from List
Defined in List:202

Returns true if the given property has a filter set. If no property is given it returns true when no property has a filter.

Parameters :
Name Type Optional Default value
property string Yes
filterOptions No this.config.filter
Returns : boolean
Public isOverTheMax
isOverTheMax(field: Field)
Inherited from List
Defined in List:307

Returns true if the given field index in the visible fields is higher than maxColumns.

Parameters :
Name Type Optional
field Field No
Returns : boolean
Public isSorted
isSorted(property: string, desc?: boolean)
Inherited from List
Defined in List:244

Returns true if the given sort state is active. You can either just check for a property + desc flag

Parameters :
Name Type Optional
property string No
desc boolean Yes
Returns : boolean
Public load
load(config?: ListConfig)
Inherited from List
Defined in List:69

Loads the list page with the given config or, if none given, uses the current config. Reapplies grouping (if any) and calls the change Subject.

Parameters :
Name Type Optional
config ListConfig<T> Yes
Returns : void
setFilter
setFilter(filterOptions: object)
Inherited from List
Defined in List:159
Parameters :
Name Type Optional Default value
filterOptions object No {}
Returns : void
Public sortableFields
sortableFields()
Inherited from List
Defined in List:299

Returns an array of all sortable fields

Returns : any
Protected sortProperty
sortProperty(property: string, desc?: boolean)
Inherited from List
Defined in List:224

Changes the config's sort variables to reflect the given sorting

Parameters :
Name Type Optional
property string No
desc boolean Yes
Returns : void
toggleSelectMode
toggleSelectMode()
Inherited from List
Defined in List:258

Toggles selectMode of list config

Returns : void
toggleSort
toggleSort(property: string, desc?: boolean)
Inherited from List
Defined in List:252

Sorts with given sorting, using the Sorter

Parameters :
Name Type Optional
property string No
desc boolean Yes
Returns : void
Public toggleVisibility
toggleVisibility(field)
Inherited from List
Defined in List:105
Parameters :
Name Optional
field No
Returns : void
Public trackItem
trackItem(index, item)
Inherited from List
Defined in List:295

Item tracking for *ngFor.

Parameters :
Name Optional
index No
item No
Returns : any
add
add(item: T, unique?: boolean, event: boolean)
Inherited from List
Defined in List:71

Adds the given item to the Collection. If the unique flag is set, the item will only be added if it is not contained.

Parameters :
Name Type Optional Default value
item T No
unique boolean Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.add(4);</li>
<li>```</li>
</ul>
Returns : boolean
addAll
addAll(items: Array, unique: boolean, event: boolean)
Inherited from List
Defined in List:89

Adds the given items to the Collection. If the unique flag is set, only items that are not contained will be added.

Parameters :
Name Type Optional Default value
items Array<T> No []
unique boolean No false
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.addAll([5, 6, 7]);</li>
<li>```</li>
</ul>
Returns : void
has
has(item: T)
Inherited from List
Defined in List:42

Checks if the Collection contains the given item.

Parameters :
Name Type Optional
item T No
Example :
```typescript</p>
<ul>
<li>numbers.has(2); //true</li>
<li>```</li>
</ul>
Returns : boolean
hasAll
hasAll(items: Array)
Inherited from List
Defined in List:53

Checks if the Collection contains all given items.

Parameters :
Name Type Optional Default value
items Array<T> No []
Example :
```typescript</p>
<ul>
<li>numbers.has([1,2]); //true</li>
<li>```</li>
</ul>
Returns : boolean
index
index(item: T)
Inherited from List
Defined in List:31

Returns the index of the given item

Parameters :
Name Type Optional
item T No
Returns : number
isEmpty
isEmpty()
Inherited from List
Defined in List:160

Returns true if the collection is empty

Returns : boolean
move
move(item: T, index: number, event: boolean)
Inherited from List
Defined in List:165

Moves the given item to the given array index.

Parameters :
Name Type Optional Default value
item T No
index number No
event boolean No true
Returns : void
remove
remove(item: T, event: boolean)
Inherited from List
Defined in List:106

Removes the given item from the Collection.

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.remove(4);</li>
<li>```</li>
</ul>
Returns : boolean
removeAll
removeAll(items?: Array, event: boolean)
Inherited from List
Defined in List:123

Removes all items from the Collection.

Parameters :
Name Type Optional Default value
items Array<T> Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.removeAll();</li>
<li>```</li>
</ul>
Returns : void
replaceWith
replaceWith(items: Array, event: boolean)
Inherited from List
Defined in List:147

Replaces all current items with the given items.

Parameters :
Name Type Optional Default value
items Array<T> No
event boolean No true
Returns : void
toggle
toggle(item: T, event: boolean)
Inherited from List
Defined in List:138

Toggles the item in and out of collection

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Returns : void
add
add(item: T, unique?: boolean, event: boolean)
Inherited from Collection
Defined in Collection:71

Adds the given item to the Collection. If the unique flag is set, the item will only be added if it is not contained.

Parameters :
Name Type Optional Default value
item T No
unique boolean Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.add(4);</li>
<li>```</li>
</ul>
Returns : boolean
addAll
addAll(items: Array, unique: boolean, event: boolean)
Inherited from Collection
Defined in Collection:89

Adds the given items to the Collection. If the unique flag is set, only items that are not contained will be added.

Parameters :
Name Type Optional Default value
items Array<T> No []
unique boolean No false
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.addAll([5, 6, 7]);</li>
<li>```</li>
</ul>
Returns : void
has
has(item: T)
Inherited from Collection
Defined in Collection:42

Checks if the Collection contains the given item.

Parameters :
Name Type Optional
item T No
Example :
```typescript</p>
<ul>
<li>numbers.has(2); //true</li>
<li>```</li>
</ul>
Returns : boolean
hasAll
hasAll(items: Array)
Inherited from Collection
Defined in Collection:53

Checks if the Collection contains all given items.

Parameters :
Name Type Optional Default value
items Array<T> No []
Example :
```typescript</p>
<ul>
<li>numbers.has([1,2]); //true</li>
<li>```</li>
</ul>
Returns : boolean
index
index(item: T)
Inherited from Collection
Defined in Collection:31

Returns the index of the given item

Parameters :
Name Type Optional
item T No
Returns : number
isEmpty
isEmpty()
Inherited from Collection
Defined in Collection:160

Returns true if the collection is empty

Returns : boolean
move
move(item: T, index: number, event: boolean)
Inherited from Collection
Defined in Collection:165

Moves the given item to the given array index.

Parameters :
Name Type Optional Default value
item T No
index number No
event boolean No true
Returns : void
remove
remove(item: T, event: boolean)
Inherited from Collection
Defined in Collection:106

Removes the given item from the Collection.

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.remove(4);</li>
<li>```</li>
</ul>
Returns : boolean
removeAll
removeAll(items?: Array, event: boolean)
Inherited from Collection
Defined in Collection:123

Removes all items from the Collection.

Parameters :
Name Type Optional Default value
items Array<T> Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.removeAll();</li>
<li>```</li>
</ul>
Returns : void
replaceWith
replaceWith(items: Array, event: boolean)
Inherited from Collection
Defined in Collection:147

Replaces all current items with the given items.

Parameters :
Name Type Optional Default value
items Array<T> No
event boolean No true
Returns : void
toggle
toggle(item: T, event: boolean)
Inherited from Collection
Defined in Collection:138

Toggles the item in and out of collection

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Returns : void
import { Field, Item, List, ListConfig } from '@ec.components/core';
import Core from 'ec.sdk/lib/Core';
import ListResource, { filterOptions } from 'ec.sdk/lib/resources/ListResource';
import Resource from 'ec.sdk/lib/resources/Resource';
import { Observable, Subject } from 'rxjs';

/**
 * Extension of List for SDK ListResource. Each each implementation should implement the load
 * method to call the SDK method for loading the desired list! (see EntryList for example)
 */
export class ResourceList extends List<Resource> {
  /** The current loaded ListResource */
  protected listResource: ListResource;
  /** Subject that should be nexted when loading begins */
  public loading: Subject<Promise<any>> = new Subject();
  /** latest loading promise */
  public promise: Promise<any>;
  /** Observable that is nexted when the list begins loading. */
  public loading$ = this.loading.asObservable();
  /** Subject that should be nexted when an error occurs */
  protected error: Subject<Error> = new Subject();
  /** Observable that is nexted when the list has an error. */
  public error$: Observable<Error> = this.error.asObservable();

  /** Returns the operator to use for filtering the given property. Defaults to search. */
  protected static getFilterOperator(property: string, fields: Array<Field>): string {
    if (!fields) {
      return 'search';
    }
    const field = fields.find((_field) => _field.property === property);
    return field && field.filterOperator ? field.filterOperator : 'search';
  }

  /** Returns true if the field of the given property has rawFilter set to true */
  public isRawFilter(property: string, fields: Array<Field> = this.fields): boolean {
    if (!fields) {
      return false;
    }
    const field = fields.find((_field) => _field.property === property);
    return field && field.rawFilter;
  }

  /** The constructor will init the List and Pagination instances.
   * Make sure the config is already complete when initiating an EntryList instance. */
  constructor(config: ListConfig<Resource>, public api?: Core, public relation?, listResource?: ListResource) {
    super([], config);
    if (listResource) {
      // list was already preloaded outside of this instance
      this.use(listResource);
    } else {
      this.load();
    }
  }

  load(config?: ListConfig<Resource>) {
    if (config) {
      this.config = Object.assign({}, this.config, config);
    }
    if (!this.api || !this.relation) {
      return;
    }
    const options = this.getFilterOptions(this.config);
    /* options._count = options.size;
    delete options.size; */
    this.promise = this.api
      .resourceList(this.relation, options)
      .then((list) => this.use(list))
      .catch((err) => this.error.next(err));
    this.loading.next(this.promise);
    return this.promise;
  }

  /** deletes all undefined values from given config and assigns it to this.config */
  protected useConfig(config?: ListConfig<Resource>) {
    if (config) {
      Object.keys(config).forEach((key) => {
        if (config[key] === undefined) {
          delete config[key];
        }
      });
      Object.assign(this.config, config);
    }
  }

  /** Takes the listResource and dumps the items into the the current page. Then it applies grouping if present. */
  protected use(listResource) {
    this.listResource = listResource;
    this.removeAll();
    this.addAll(
      listResource.getAllItems().map((value) => {
        return new Item(value, this.config);
      }),
      true,
    );
    this.page = this.items;
    if (this.pagination) {
      this.pagination.setTotal(listResource.total);
    }
    this.groupBy(this.config.sortBy);
    this.change.next(this);
  }

  /** Returns SDK filterOptions from a given ListConfig. */
  protected getFilterOptions({
    size = 20,
    page = 1,
    filter,
    sortBy,
    desc,
    sort = [],
  }: ListConfig<Resource> = {}): filterOptions {
    const options = { size, page };
    if (sortBy) {
      Object.assign(options, { sort: [(desc ? '-' : '') + sortBy] });
    }
    if (!filter) {
      return options;
    }
    for (const property in filter) {
      if (filter.hasOwnProperty(property)) {
        Object.assign(options, {
          [property]: this.isRawFilter(property, this.fields)
            ? filter[property]
            : {
              [ResourceList.getFilterOperator(property, this.fields)]: filter[property],
            },
        });
      }
    }
    return options;
  }

  /** Toggles sorting of the given property. Overloads list method to reload with the new sort setup*/
  toggleSort(property: string, desc?: boolean) {
    this.sortProperty(property, desc);
    Object.assign(this.config, {
      sort: [(this.config.desc ? '-' : '') + this.config.sortBy],
    });
    this.load();
  }

  /** Updates the config.filter with the given property filter. */
  filterProperty(property: string, value: any = '') {
    const currentFilter = this.config.filter || {};
    if (this.isEmptyFilter(value)) {
      delete currentFilter[property];
    } else {
      Object.assign(currentFilter, {
        [property]: value,
      });
    }
    return currentFilter;
  }

  /** Filters the entry list by a given property value. Triggers load. */
  filter(property: string, value: any = '') {
    if (this.isEmptyFilter(value) && !this.isFiltered(property)) {
      return; // filter is already empty => no need to load again
    }
    return this.load({
      page: 1, // reset page
      filter: this.filterProperty(property, value),
    });
  }
}

result-matching ""

    No results matching ""