Skip to content

Commit

Permalink
refactor(ngrid): move column (some) and datasource symbols to core pa…
Browse files Browse the repository at this point in the history
…ckage

BREAKING CHANGE:
To reduce clutter in the main packge the datasource symbols (all) and some of the column model symbols moved to `@pebula/ngrid/core`.
At this point all symbols we're re-exported from the main module so this no effect there. However, if you've extended on of the symbols using augmentation you will need to update the augmentation module path.
  • Loading branch information
shlomiassaf committed Dec 21, 2020
1 parent 55e8f31 commit 33d2bca
Show file tree
Hide file tree
Showing 55 changed files with 435 additions and 393 deletions.
2 changes: 2 additions & 0 deletions libs/ngrid/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './lib/events/index';
export * from './lib/paginator/index';
export * from './lib/registry/index';
export * from './lib/utils/index';
export * from './lib/models/column';
export * from './lib/data-source/index';
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Observable, Subject, combineLatest, of, from, isObservable, asapScheduler } from 'rxjs';
import { filter, map, switchMap, tap, debounceTime, observeOn } from 'rxjs/operators';

import { PblPaginator, PblPaginatorChangeEvent } from '@pebula/ngrid/core';

import { PblPaginator, PblPaginatorChangeEvent } from '../paginator/types';
import { PblNgridDataSourceSortChange, DataSourceFilter } from './types';
import { filter as filteringFn } from './filtering';
import { applySort } from './sorting';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PblPaginatorChangeEvent } from '@pebula/ngrid/core';
import { PblPaginatorChangeEvent } from '../paginator/types';
import { DataSourceOf } from './data-source';
import { PblNgridDataSourceSortChange, DataSourceFilter } from './types';

/** @internal */
export type RefreshDataWrapper<T> = { data: T };

/**
* Data source event triggers map with triggers that the user can opt in a custom behaviour.
* Data source event triggers map with triggers that the user can opt in a custom behavior.
* The properties represent the trigger name/key and the value represent the event input in it's raw form.
*/
export interface PblDataSourceConfigurableTriggers {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PblDataSource, DataSourceOf, PblDataSourceOptions } from './data-source';
import { PblDataSource, PblDataSourceOptions } from './data-source';
import { PblDataSourceAdapter } from './data-source-adapter';
import {
PblDataSourceConfigurableTriggers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { SelectionModel, CollectionViewer, ListRange } from '@angular/cdk/collec
import { DataSource } from '@angular/cdk/table';
import { moveItemInArray } from '@angular/cdk/drag-drop';

import { PblNgridPaginatorKind, PblPaginator, PblPagingPaginator, PblTokenPaginator, unrx } from '@pebula/ngrid/core';
import { PblNgridPluginContext } from '../ext/plugin-control';
import { PblColumn } from '../grid/column/model';
import { PblNgridComponent } from '../grid/ngrid.component';
import { unrx } from '../utils/unrx';
import { PblNgridPaginatorKind, PblPaginator } from '../paginator/types'
import { PblPagingPaginator } from '../paginator/paging-paginator'
import { PblTokenPaginator } from '../paginator/token-paginator'
import { PblNgridEventEmitter } from '../events/events';
import { PblColumnDefinition } from '../models/column';
import { DataSourcePredicate, DataSourceFilter, DataSourceFilterToken, PblNgridSortDefinition, PblNgridDataSourceSortChange } from './types';
import { createFilter } from './filtering';
import { PblDataSourceAdapter } from './data-source-adapter';
Expand Down Expand Up @@ -159,8 +161,6 @@ export class PblDataSource<T = any,
/** Represents selected items on the data source. */
get selection(): SelectionModel<T> { return this._selection; }

get hostGrid(): PblNgridComponent<T> { return this._gridContext?.grid; }

protected readonly _selection = new SelectionModel<T>(true, []);
protected readonly _tableConnectionChange$ = new Subject<boolean>();
protected readonly _onRenderDataChanging = new Subject<{ event: PblDataSourceTriggerChangedEvent<TData>, data: T[] }>();
Expand All @@ -179,7 +179,7 @@ export class PblDataSource<T = any,
private _lastRefresh: TData;
private _lastRange: ListRange;
private _lastAdapterEvent: PblDataSourceAdapterProcessedResult<T, TData>;
private _gridContext: PblNgridPluginContext;
private _eventEmitter: PblNgridEventEmitter;

constructor(adapter: TDataSourceAdapter, options?: PblDataSourceOptions) {
super();
Expand Down Expand Up @@ -226,15 +226,15 @@ export class PblDataSource<T = any,
* This means that any column specific filter, set in the column definitions is ignored, if you want to take these filters into consideration
* use the column instance provided to identify and use these filters (the `filter` property in `PblColumn`).
*/
setFilter(value: DataSourcePredicate, columns?: PblColumn[]): void;
setFilter(value: DataSourcePredicate, columns?: PblColumnDefinition[]): void;
/**
* Set the filter definition for the current data set using a value to compare with and a list of columns with the values to compare to.
*
* When a column instance has a specific predicate set (`PblColumn.filter`) then it will be used, otherwise
* the `genericColumnPredicate` will be used.
*/
setFilter(value: any, columns: PblColumn[]): void;
setFilter(value?: DataSourceFilterToken, columns?: PblColumn[]): void {
setFilter(value: any, columns: PblColumnDefinition[]): void;
setFilter(value?: DataSourceFilterToken, columns?: PblColumnDefinition[]): void {
if (value && typeof value !== 'function' && (!columns || columns.length === 0)) {
throw new Error('Invalid filter definitions, columns are mandatory when using a single value input.');
}
Expand Down Expand Up @@ -271,8 +271,8 @@ export class PblDataSource<T = any,
* @param skipUpdate When true will not update the datasource, use this when the data comes sorted and you want to sync the definitions with the current data set.
* default to false.
*/
setSort(column: PblColumn, sort: PblNgridSortDefinition, skipUpdate?: boolean): void;
setSort(column?: PblColumn | boolean, sort?: PblNgridSortDefinition, skipUpdate = false): void {
setSort(column: PblColumnDefinition, sort: PblNgridSortDefinition, skipUpdate?: boolean): void;
setSort(column?: PblColumnDefinition | boolean, sort?: PblNgridSortDefinition, skipUpdate = false): void {
if (!column || typeof column === 'boolean') {
this._sort$.next({ column: null, sort: {}, skipUpdate: !!column });
} else {
Expand Down Expand Up @@ -326,7 +326,7 @@ export class PblDataSource<T = any,
}

if (this.length > 0) {
this._gridContext.emitEvent({ source: 'ds', kind: 'onBeforeMoveItem', fromIndex, toIndex });
this._eventEmitter.emitEvent({ source: 'ds', kind: 'onBeforeMoveItem', fromIndex, toIndex });
moveItemInArray(this._source, fromIndex, toIndex)
const data = this._lastRange
? this._source.slice(this._lastRange.start, this._lastRange.end)
Expand All @@ -336,12 +336,12 @@ export class PblDataSource<T = any,
}
}

_attachGrid(context: PblNgridPluginContext): void {
this._gridContext = context;
_attachEmitter(emitter: PblNgridEventEmitter): void {
this._eventEmitter = emitter;
}

_detachGrid(): void {
this._gridContext = undefined;
_detachEmitter(): void {
this._eventEmitter = undefined;
}

private _updateProcessingLogic(cv: CollectionViewer): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PblNgridEvent } from '@pebula/ngrid/core';
import { PblNgridEvent } from '../events/ngrid-events';
import { PblDataSource } from './data-source';

declare module '@pebula/ngrid/core/lib/events/ngrid-events' {
declare module '../events/ngrid-events' {
export interface PblNgridEventSourceMap {
ds: true;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PblColumn } from '../grid/column/model';
import { PblColumnDefinition } from '../models/column';
import { getValue } from '../utils/column';
import { DataSourceFilter, DataSourceFilterToken, DataSourcePredicate, DataSourceColumnPredicate } from './types';

export function createFilter(value: DataSourceFilterToken, columns: PblColumn[]): DataSourceFilter {
export function createFilter(value: DataSourceFilterToken, columns: PblColumnDefinition[]): DataSourceFilter {
return value === undefined
? undefined
: {
Expand All @@ -23,7 +24,7 @@ export function filter<T>(rawData: T[], filter: DataSourceFilter): T[] {
const value = typeof filter.filter.toLowerCase === 'function' ? filter.filter.toLowerCase() : filter.filter;
return rawData.filter( row => cols.some( col => {
const predicate = col.filter || genericColumnPredicate;
return predicate(col.filter ? filter.filter : value, col.getValue(row), row, col);
return predicate(col.filter ? filter.filter : value, getValue(col, row), row, col);
}));
}
}
Expand All @@ -33,6 +34,6 @@ export function filter<T>(rawData: T[], filter: DataSourceFilter): T[] {
/**
* A generic column predicate that compares the inclusion (text) of the value in the column value.
*/
export const genericColumnPredicate: DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumn): boolean => {
export const genericColumnPredicate: DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumnDefinition): boolean => {
return colValue && colValue.toString().toLowerCase().includes(filterValue);
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PblColumn } from '../grid/column/model';
import { PblColumnDefinition } from '../models/column';
import { getValue } from '../utils/column';
import { PblNgridSortDefinition, PblNgridSortInstructions, PblNgridSorter } from './types';

/**
* Apply sorting on a collection, based on column and sort definitions.
* If the sort definition doesn't have a sorting function the default sorter is used.
*/
export function applySort<T>(column: PblColumn, sort: PblNgridSortDefinition, data: T[]): T[] {
export function applySort<T>(column: PblColumnDefinition, sort: PblNgridSortDefinition, data: T[]): T[] {
if (!sort || !sort.order) {
return data;
}
Expand All @@ -23,10 +24,10 @@ export function applySort<T>(column: PblColumn, sort: PblNgridSortDefinition, da
;
}

function defaultSorter<T>(column: PblColumn, sort: PblNgridSortInstructions, data: T[]): T[] {
function defaultSorter<T>(column: PblColumnDefinition, sort: PblNgridSortInstructions, data: T[]): T[] {
return data.sort((a, b) => {
let valueA = column.getValue(a);
let valueB = column.getValue(b);
let valueA = getValue(column, a);
let valueB = getValue(column, b);

valueA = isNaN(+valueA) ? valueA : +valueA;
valueB = isNaN(+valueB) ? valueB : +valueB;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PblColumn } from '../grid/column/model/column';
import { PblColumnDefinition } from '../models/column';

export type PblNgridSortOrder = 'asc' | 'desc';

Expand All @@ -17,11 +17,11 @@ export interface PblNgridSortDefinition extends PblNgridSortInstructions {
* A function that can sort a dataset based on `PblNgridSortInstructions`
*/
export interface PblNgridSorter<T = any> {
(column: PblColumn, sort: PblNgridSortInstructions, data: T[]): T[];
(column: PblColumnDefinition, sort: PblNgridSortInstructions, data: T[]): T[];
}

export interface PblNgridDataSourceSortChange {
column: PblColumn;
column: PblColumnDefinition;
sort: PblNgridSortDefinition;
}

Expand All @@ -32,19 +32,19 @@ export interface PblNgridDataSourceSortChange {
* This is a single column filter predicated, returning false will filter out the entire row but the
* predicate is only intended to filter a specific column.
*/
export type DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumn) => boolean;
export type DataSourceColumnPredicate = (filterValue: any, colValue: any, row?: any, col?: PblColumnDefinition) => boolean;
/**
* A function the return true then the row should be included in the result or false when not.
* @param row The row in the data source that the filter apply on
* @param properties A list of column instances (`PblColumn`) to filter values by.
* @param properties A list of column instances (`PblColumnDefinition`) to filter values by.
*/
export type DataSourcePredicate = (row: any, properties: PblColumn[]) => boolean;
export type DataSourcePredicate = (row: any, properties: PblColumnDefinition[]) => boolean;

export type DataSourceFilterToken = undefined | DataSourcePredicate | any;

export interface DataSourceFilterType {
type: 'value' | 'predicate';
columns: PblColumn[];
columns: PblColumnDefinition[];
filter: any | DataSourcePredicate;
}

Expand Down
5 changes: 5 additions & 0 deletions libs/ngrid/core/src/lib/events/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PblNgridEvents } from './ngrid-events';

export interface PblNgridEventEmitter {
emitEvent(event: PblNgridEvents): void;
}
1 change: 1 addition & 0 deletions libs/ngrid/core/src/lib/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ngrid-events';
export * from './event-pipes';
export * from './events';
Loading

0 comments on commit 33d2bca

Please sign in to comment.