Skip to content

Commit

Permalink
feat(NgTableParams): nested paramater values never undefined
Browse files Browse the repository at this point in the history
This is part of supporting typescript strictNullChecks compiler option.

BREAKING CHANGES

`NgTableParams.parameters`: any param value supplied as undefined will now be ignored

Instead of `undefined` you will need to supply a suitable value as described below:

* `count`, `page` - `0`
* `filter`, `group`, `sorting` - an empty object to remove existing values
  • Loading branch information
ccrowhurstram committed Dec 22, 2016
1 parent 3ca6852 commit 8f5461f
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/core/ngTableDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
*/

import { ParamValues } from './ngTableParams';
import { ParamValuesPartial } from './ngTableParams';
import { SettingsPartial } from './ngTableSettings';


Expand All @@ -15,7 +15,7 @@ import { SettingsPartial } from './ngTableSettings';
* an instance of `NgTableParams`
*/
export interface Defaults {
params?: ParamValues<any>;
params?: ParamValuesPartial<any>;
settings?: SettingsPartial<any>
}

Expand Down
90 changes: 47 additions & 43 deletions src/core/ngTableParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import * as ng1 from 'angular';
import { ILogService, IPromise, IQService } from 'angular';
import { convertSortToOrderBy, isGroupingFun } from './util';
import { assignPartialDeep } from '../shared';
import { Defaults } from './ngTableDefaults'
import { NgTableEventsChannel } from './ngTableEventsChannel'
import { SettingsPartial, Settings } from './ngTableSettings'
Expand All @@ -25,31 +26,34 @@ export interface InternalTableParams<T> extends NgTableParams<T> {
isNullInstance: boolean
}


export type ParamValuesPartial<T> = Partial<ParamValues<T>>;

/**
* The runtime values for {@link NgTableParams} that determine the set of data rows and
* how they are to be displayed in a table
*/
export interface ParamValues<T> {
export class ParamValues<T> {
/**
* The index of the "slice" of data rows, starting at 1, to be displayed by the table.
*/
page?: number;
page = 1;
/**
* The number of data rows per page
*/
count?: number;
count = 10;
/**
* The filter that should be applied to restrict the set of data rows
*/
filter?: FilterValues;
filter: FilterValues = {};
/**
* The sort order that should be applied to the data rows.
*/
sorting?: SortingValues;
sorting: SortingValues = {};
/**
* The grouping that should be applied to the data rows
*/
group?: string | Grouping<T>;
group: string | Grouping<T> = {};
}


Expand Down Expand Up @@ -85,17 +89,11 @@ export class NgTableParams<T> {
private ngTableDefaults: Defaults
private ngTableEventsChannel: NgTableEventsChannel;
private prevParamsMemento: Memento<T>;
private _params: ParamValues<T> = {
page: 1,
count: 10,
filter: {},
sorting: {},
group: {}
};
private _params = new ParamValues();
private _settings = this.defaultSettings;
private $q: IQService;
private $log: ILogService
constructor(baseParameters?: ParamValues<T> | boolean, baseSettings?: SettingsPartial<T>) {
constructor(baseParameters?: ParamValuesPartial<T> | boolean, baseSettings?: SettingsPartial<T>) {

// the ngTableController "needs" to create a dummy/null instance and it's important to know whether an instance
// is one of these
Expand All @@ -115,7 +113,7 @@ export class NgTableParams<T> {
}
})();

ng1.extend(this._params, this.ngTableDefaults.params);
assignPartialDeep(this._params, this.ngTableDefaults.params);

this.settings(baseSettings);
this.parameters(baseParameters, true);
Expand Down Expand Up @@ -268,7 +266,7 @@ export class NgTableParams<T> {
return this._params.group;
}

const newParameters: ParamValues<T> = {
const newParameters: ParamValuesPartial<T> = {
page: 1
};
if (isGroupingFun(group) && sortDirection !== undefined) {
Expand Down Expand Up @@ -382,39 +380,45 @@ export class NgTableParams<T> {
/**
* Set new parameters
*/
parameters(newParameters?: ParamValues<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): this
parameters(newParameters?: ParamValues<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): ParamValues<T> | this {
parameters(newParameters?: ParamValuesPartial<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): this
parameters(newParameters?: ParamValuesPartial<T> | { [name: string]: string }, parseParamsFromUrl?: boolean): ParamValues<T> | this {
if (newParameters === undefined) {
return this._params;
}

// todo: move parsing of url like parameters into a seperate method

parseParamsFromUrl = parseParamsFromUrl || false;
if (newParameters !== undefined) {
for (const key in newParameters) {
let value = newParameters[key];
if (parseParamsFromUrl && key.indexOf('[') >= 0) {
const keys = key.split(/\[(.*)\]/).reverse()
let lastKey = '';
for (let i = 0, len = keys.length; i < len; i++) {
const name = keys[i];
if (name !== '') {
const v = value;
value = {};
value[lastKey = name] = (isNumber(v) ? parseFloat(v) : v);
}
}
if (lastKey === 'sorting') {
this._params[lastKey] = {};
for (const key in newParameters) {
let value = newParameters[key];
if (parseParamsFromUrl && key.indexOf('[') >= 0) {
const keys = key.split(/\[(.*)\]/).reverse()
let lastKey = '';
for (let i = 0, len = keys.length; i < len; i++) {
const name = keys[i];
if (name !== '') {
const v = value;
value = {};
value[lastKey = name] = (isNumber(v) ? parseFloat(v) : v);
}
this._params[lastKey] = ng1.extend(this._params[lastKey] || {}, value[lastKey]);
}
if (lastKey === 'sorting') {
this._params[lastKey] = {};
}
this._params[lastKey] = ng1.extend(this._params[lastKey] || {}, value[lastKey]);
} else {
if (newParameters[key] === undefined) {
// skip
}
else if (key === 'group') {
this._params[key] = this.parseGroup(newParameters[key]);
} else {
if (key === 'group') {
this._params[key] = this.parseGroup(newParameters[key]);
} else {
this._params[key] = (isNumber(newParameters[key]) ? parseFloat(newParameters[key]) : newParameters[key]);
}
this._params[key] = (isNumber(newParameters[key]) ? parseFloat(newParameters[key]) : newParameters[key]);
}
}
this.log('ngTable: set parameters', this._params);
return this;
}
return this._params;
this.log('ngTable: set parameters', this._params);
return this;
}
/**
* Trigger a reload of the data rows
Expand Down
4 changes: 2 additions & 2 deletions test/specs/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Settings', () => {
beforeAll(() => expect(ngTableCoreModule).toBeDefined());

let defaultOverrides: SettingsPartial<any>;
let standardSettings: SettingsPartial<any>;
let standardSettings: Settings<any>;

beforeEach(ng1.mock.module('ngTable-core'));

Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Settings', () => {
filterDelay: 20
}
};
const originalNewSettings = { ...newSettings, filterOptions: { ... newSettings.filterOptions } };
const originalNewSettings = { ...newSettings, filterOptions: { ...newSettings.filterOptions } };

const actual = Settings.merge(allSettings, newSettings);
expect(actual).not.toBe(allSettings);
Expand Down
6 changes: 3 additions & 3 deletions test/specs/table.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IAugmentedJQuery, ICompileService, IQService, IPromise, IScope, ITimeoutService } from 'angular';
import * as ng1 from 'angular';
import { ngTableModule } from '../../index';
import { NgTableParams, ParamValues, SettingsPartial, SortingValues } from '../../src/core';
import { NgTableParams, ParamValuesPartial, SettingsPartial, SortingValues } from '../../src/core';
import { ColumnDef, FilterTemplateDef, FilterTemplateDefMap, SelectOption } from '../../src/browser'

describe('ng-table', () => {
Expand Down Expand Up @@ -72,10 +72,10 @@ describe('ng-table', () => {
scope.model = {};
}));

function createNgTableParams<T>(initialParams?: ParamValues<T>, settings?: SettingsPartial<T>): NgTableParams<T>;
function createNgTableParams<T>(initialParams?: ParamValuesPartial<T>, settings?: SettingsPartial<T>): NgTableParams<T>;
function createNgTableParams<T>(settings?: SettingsPartial<T>): NgTableParams<T>;
function createNgTableParams<T>(settings?: any): NgTableParams<T> {
let initialParams: ParamValues<T>;
let initialParams: ParamValuesPartial<T>;
if (arguments.length === 2) {
initialParams = arguments[0];
settings = arguments[1];
Expand Down
Loading

0 comments on commit 8f5461f

Please sign in to comment.