Skip to content

Commit

Permalink
fix(abc:simple-table): fix invalid reqReName in build optimizer mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Apr 19, 2018
1 parent 6df9bed commit 1a20655
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 60 deletions.
4 changes: 2 additions & 2 deletions packages/abc/simple-table/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ reqMehtod | 请求方法 | `string` | `GET`
reqHeader | 请求 `header` | `any` | -
reqBody | 请求 `body` | `any` | -
reqError | 请求异常时回调 | `EventEmitter` | -
reqReName | 重命名请求参数 `pi``ps` | `Object` | -
resReName | 重命名返回参数 `total``list` | `Object` | -
reqReName | 重命名请求参数 `pi``ps` | `{pi:string;ps:string}` | -
resReName | 重命名返回参数 `total``list`,支持 `a.b.c` 的嵌套写法 | `{total:string;list:string}` | -
columns | 列描述 | `SimpleTableColumn[]` | -
pi | 当前页码 | `number` | `10`
ps | 每页数量,当设置为 `0` 表示不分页,默认:`10` | `number` | `10`
Expand Down
5 changes: 5 additions & 0 deletions packages/abc/simple-table/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ export interface SimpleTableChange {
total: number;
}

export interface ReqReNameType {
pi?: string;
ps?: string;
}

export interface ResReNameType {
total?: string | string[];
list?: string | string[];
Expand Down
74 changes: 40 additions & 34 deletions packages/abc/simple-table/simple-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { of } from 'rxjs/observable/of';
import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
import { CNCurrencyPipe, DatePipe, YNPipe, ModalHelper, ALAIN_I18N_TOKEN, AlainI18NService } from '@delon/theme';

import { SimpleTableColumn, SimpleTableChange, CompareFn, SimpleTableSelection, SimpleTableFilter, SimpleTableData, SimpleTableButton, STExportOptions, ResReNameType } from './interface';
import { AdSimpleTableConfig } from './simple-table.config';
import { deepGet, deepCopy } from '../utils/utils';
import { SimpleTableColumn, SimpleTableChange, CompareFn, SimpleTableSelection, SimpleTableFilter, SimpleTableData, SimpleTableButton, STExportOptions, ResReNameType, ReqReNameType } from './interface';
import { AdSimpleTableConfig } from './simple-table.config';
import { SimpleTableRowDirective } from './simple-table-row.directive';
import { SimpleTableExport } from './simple-table-export';

Expand All @@ -34,7 +34,6 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
_allChecked = false;
_indeterminate = false;
_columns: SimpleTableColumn[] = [];
_resRN: ResReNameType = { total: ['total'], list: ['list'] };

// region: fields

Expand All @@ -55,14 +54,42 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
* 重命名请求参数 `pi`、`ps`
* - `{ pi: 'Page' }` => `pi` 会被替换成 Page
*/
@Input() reqReName: Object;
@Input()
set reqReName(value: ReqReNameType) {
this._reqReName = Object.assign(this._reqReName, value);
}
get reqReName() {
return this._reqReName;
}
private _reqReName: ReqReNameType = { pi: 'pi', ps: 'ps' };
/** 请求异常时回调 */
@Output() reqError: EventEmitter<any> = new EventEmitter<any>();
/**
* 重命名返回参数 `total`、`list`
* - `{ total: 'Total' }` => Total 会被当作 `total`
*/
@Input() resReName: ResReNameType;
@Input()
set resReName(cur: ResReNameType) {
let ret: ResReNameType = {};
if (cur) {
if (cur.list)
ret.list = Array.isArray(cur.list) ? cur.list : cur.list.split('.');
else
ret.list = ['list'];

if (cur.total)
ret.total = Array.isArray(cur.total) ? cur.total : cur.total.split('.');
else
ret.total = ['total'];
} else {
ret = { total: ['total'], list: ['list'] };
}
this._resReName = ret;
}
get resReName() {
return this._resReName;
}
private _resReName: ResReNameType = { total: ['total'], list: ['list'] };
/** 列描述 */
@Input() columns: SimpleTableColumn[] = [];
/** 每页数量,当设置为 `0` 表示不分页,默认:`10` */
Expand Down Expand Up @@ -135,7 +162,7 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
}
private _showQuickJumper = false;
/** 是否显示总数据量 */
_totalTpl = ``;
private _totalTpl = ``;
@Input()
set showTotal(value: any) {
if (typeof value === 'string' && value.length) {
Expand Down Expand Up @@ -226,25 +253,25 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
@Inject(DOCUMENT) private doc: any
) {
Object.assign(this, deepCopy(defConfig));
this.updateResName();
}

// region: data

private getAjaxData(url?: string): Observable<any> {
const params: any = {};
params[this.reqReName && this.reqReName['pi'] || 'pi'] = this.pi;
params[this.reqReName && this.reqReName['ps'] || 'ps'] = this.ps;
const params: any = Object.assign({
[ this.reqReName.pi ]: this.pi,
[ this.reqReName.ps ]: this.ps
}, this.extraParams, this.getReqSortMap(), this.getReqFilterMap());
return this.http.request(this.reqMehtod || 'GET', url || this._url, {
params: Object.assign(params, this.extraParams, this.getReqSortMap(), this.getReqFilterMap()),
params,
body: this.reqBody,
headers: this.reqHeaders
}).pipe(map((res: any) => {
// list
const ret = deepGet(res, this._resRN.list as string[], []);
const ret = deepGet(res, this.resReName.list as string[], []);
if (ret == null || !Array.isArray(ret)) return [];
// total
const retTotal = this._resRN.total && deepGet(res, this._resRN.total as string[], null);
const retTotal = this.resReName.total && deepGet(res, this.resReName.total as string[], null);
this.total = retTotal == null ? this.total || 0 : +retTotal;
return <any[]>ret;
}));
Expand Down Expand Up @@ -784,29 +811,8 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
this._sortMap = sortMap;
}

private updateResName() {
let ret: ResReNameType = {};
const cur = this.resReName;
if (cur) {
if (cur.list)
ret.list = Array.isArray(cur.list) ? cur.list : cur.list.split('.');
else
ret.list = ['list'];

if (cur.total)
ret.total = Array.isArray(cur.total) ? cur.total : cur.total.split('.');
else
ret.total = ['total'];
} else {
ret = { total: ['total'], list: ['list'] };
}
this._resRN = ret;
}

ngOnChanges(changes: { [P in keyof this]?: SimpleChange } & SimpleChanges): void {
if (changes.columns) this.updateColumns();
if (changes.resReName) this.updateResName();

if (changes.data) this.processData();

this.setClass();
Expand Down
27 changes: 3 additions & 24 deletions packages/abc/simple-table/simple-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('abc: simple-table', () => {

beforeEach(() => genModule({ acl: false }));

xdescribe('#data', () => {
describe('#data', () => {
describe('with array', () => {
it(`should render ${PS} rows`, () => {
page.expectCount(PS);
Expand Down Expand Up @@ -165,27 +165,6 @@ describe('abc: simple-table', () => {
expect(console.warn).not.toHaveBeenCalled();
page.expectTotal(1);
});
it('should be wran when invalid rename list', () => {
context.resReName = { total: 'aa', list: 'aa' };
fixture.detectChanges();
httpMock.expectOne(w => true).flush(URLDATA);
expect(console.warn).toHaveBeenCalled();
});
it('should be wran when deep get is a not array', () => {
context.resReName = { total: 'aa', list: 'list' };
fixture.detectChanges();
httpMock.expectOne(w => true).flush({
list: 1,
aa: 1
});
expect(console.warn).toHaveBeenCalled();
});
it('should be wran when invalid rename total', () => {
context.resReName = { total: 'aa', list: 'list' };
fixture.detectChanges();
httpMock.expectOne(w => true).flush(URLDATA);
expect(console.warn).toHaveBeenCalled();
});
it('should be null arguments', () => {
context.resReName = { total: null, list: null };
fixture.detectChanges();
Expand All @@ -205,7 +184,7 @@ describe('abc: simple-table', () => {
});
});

xdescribe('#showTotal', () => {
describe('#showTotal', () => {
it('with false', () => {
context.showTotal = false;
fixture.detectChanges();
Expand All @@ -230,7 +209,7 @@ describe('abc: simple-table', () => {
});
});

xdescribe('acl', () => {
describe('acl', () => {

let acl: ACLService;

Expand Down

0 comments on commit 1a20655

Please sign in to comment.