Skip to content

Commit

Permalink
wrong config caching (#251)
Browse files Browse the repository at this point in the history
* wrong config caching

* test fix
  • Loading branch information
ssuperczynski authored Jun 30, 2019
1 parent 19d8cfb commit d653530
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
10 changes: 10 additions & 0 deletions cypress/integration/many-tables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ context('Many tables', () => {
.get('#configurationBasic > tbody > tr:nth-child(1) > td:nth-child(3) > div').contains('ZILLANET')
;
});
it('has rows limit set to 3', () => {
cy
.get('#paginationconfigurationBasic > div > div.ngx-pagination-range > div > div > div')
.contains('3');
});
});

describe('configurationAdvanced', () => {
Expand All @@ -54,5 +59,10 @@ context('Many tables', () => {
.get('#configurationAdvanced > tbody > tr:nth-child(1) > td:nth-child(3) > div').contains('KONGENE')
;
});
it('has rows limit set to 4', () => {
cy
.get('#paginationconfigurationAdvanced > div > div.ngx-pagination-range > div > div > div')
.contains('4');
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint": "ng lint",
"e2e": "npm run cy:ci",
"cy:serve": "http-server dist/demo -p 4201",
"cy:local": "ng serve --port=4201",
"cy:test": "cypress open",
"cy:open": "cypress open",
"cy:run": "cypress run",
Expand Down
44 changes: 18 additions & 26 deletions projects/ngx-easy-table/src/lib/components/base/base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
OnChanges,
OnInit,
Output,
SimpleChange,
SimpleChanges,
TemplateRef,
ViewChild,
Expand Down Expand Up @@ -68,15 +67,7 @@ export class BaseComponent implements OnInit, OnChanges {
public config: Config;
onSelectAllBinded = this.onSelectAll.bind(this);

@Input('configuration')
set configuration(value: Config) {
this.config = value;
}

get configuration(): Config {
return this.config;
}

@Input() configuration: Config;
@Input() data: any[];
@Input() pagination: Pagination;
@Input() groupRowsBy: string;
Expand Down Expand Up @@ -108,10 +99,10 @@ export class BaseComponent implements OnInit, OnChanges {
console.error('[columns] property required!');
}
if (this.configuration) {
DefaultConfigService.config = this.configuration;
this.config = this.configuration;
} else {
this.config = DefaultConfigService.config;
}

this.config = DefaultConfigService.config;
this.limit = this.config.rows;
if (this.groupRowsBy) {
this.grouped = GroupRowsService.doGroupRows(this.data, this.groupRowsBy);
Expand All @@ -120,10 +111,11 @@ export class BaseComponent implements OnInit, OnChanges {
}

ngOnChanges(changes: SimpleChanges) {
const data: SimpleChange = changes.data;
const pagination: SimpleChange = changes.pagination;
const groupRowsBy: SimpleChange = changes.groupRowsBy;
const { configuration, data, pagination, groupRowsBy } = changes;
this.toggleRowIndex = changes.toggleRowIndex;
if (configuration && configuration.currentValue) {
this.config = configuration.currentValue;
}
if (data && data.currentValue) {
this.doApplyData(data);
}
Expand All @@ -141,27 +133,27 @@ export class BaseComponent implements OnInit, OnChanges {

isOrderEnabled(column: Columns) {
const columnOrderEnabled = column.orderEnabled === undefined ? true : !!column.orderEnabled;
return DefaultConfigService.config.orderEnabled && columnOrderEnabled;
return this.config.orderEnabled && columnOrderEnabled;
}

orderBy(column: Columns): void {
if (typeof column.orderEnabled !== 'undefined' && !column.orderEnabled) {
return;
}
this.sortKey = column.key;
if (!DefaultConfigService.config.orderEnabled || this.sortKey === '') {
if (!this.config.orderEnabled || this.sortKey === '') {
return;
}

this.setColumnOrder(this.sortKey);
if (!DefaultConfigService.config.orderEventOnly && !column.orderEventOnly) {
if (!this.config.orderEventOnly && !column.orderEventOnly) {
this.sortBy.key = this.sortKey;
this.sortBy.order = this.sortState.get(this.sortKey);
} else {
this.sortBy.key = '';
this.sortBy.order = '';
}
if (!DefaultConfigService.config.serverPagination) {
if (!this.config.serverPagination) {
this.data = [...this.data];
}
this.sortBy = { ...this.sortBy };
Expand All @@ -173,17 +165,17 @@ export class BaseComponent implements OnInit, OnChanges {
}

onClick($event: MouseEvent, row: object, key: ColumnKeyType, colIndex: number | null, rowIndex: number): void {
if (DefaultConfigService.config.selectRow) {
if (this.config.selectRow) {
this.selectedRow = rowIndex;
}
if (DefaultConfigService.config.selectCol && colIndex) {
if (this.config.selectCol && colIndex) {
this.selectedCol = colIndex;
}
if (DefaultConfigService.config.selectCell && colIndex) {
if (this.config.selectCell && colIndex) {
this.selectedRow = rowIndex;
this.selectedCol = colIndex;
}
if (DefaultConfigService.config.clickEvent) {
if (this.config.clickEvent) {
const value: TableMouseEvent = {
event: $event,
row,
Expand Down Expand Up @@ -221,14 +213,14 @@ export class BaseComponent implements OnInit, OnChanges {
}

onSearch($event: Array<{ key: string; value: string }>): void {
if (!DefaultConfigService.config.serverPagination) {
if (!this.config.serverPagination) {
this.term = $event;
}
this.emitEvent(Event.onSearch, $event);
}

onGlobalSearch(value: string): void {
if (!DefaultConfigService.config.serverPagination) {
if (!this.config.serverPagination) {
this.globalSearchTerm = value;
}
this.emitEvent(Event.onGlobalSearch, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
EventEmitter,
HostListener,
Input,
OnChanges,
Output,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { Config } from '../..';
import { DefaultConfigService } from '../../services/config-service';
import { PaginationControlsDirective } from 'ngx-pagination';

export interface PaginationRange {
Expand All @@ -21,15 +22,15 @@ export interface PaginationRange {
templateUrl: './pagination.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PaginationComponent {
export class PaginationComponent implements OnChanges {
@ViewChild('paginationDirective', { static: true }) paginationDirective: PaginationControlsDirective;
@ViewChild('paginationRange', { static: false }) paginationRange;
@Input() pagination;
@Input() config: Config;
@Input() id;
@Output() readonly updateRange: EventEmitter<PaginationRange> = new EventEmitter();
public ranges: number[] = [5, 10, 25, 50, 100];
public selectedLimit: number = DefaultConfigService.config.rows;
public selectedLimit: number;
public showRange = false;
public screenReaderPaginationLabel = 'Pagination';
public screenReaderPageLabel = 'page';
Expand All @@ -46,6 +47,13 @@ export class PaginationComponent {
}
}

ngOnChanges(changes: SimpleChanges) {
const { config } = changes;
if (config && config.currentValue) {
this.selectedLimit = this.config.rows;
}
}

onPageChange(page: number): void {
this.updateRange.emit({
page,
Expand Down
2 changes: 1 addition & 1 deletion src/app/demo/many-tables/configuration-advanced.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ConfigurationAdvancedService {
selectRow: true,
selectCol: false,
selectCell: false,
rows: 10,
rows: 4,
additionalActions: false,
serverPagination: false,
isLoading: false,
Expand Down
2 changes: 1 addition & 1 deletion src/app/demo/many-tables/configuration-basic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ConfigurationBasicService {
selectRow: true,
selectCol: false,
selectCell: false,
rows: 10,
rows: 3,
additionalActions: false,
serverPagination: false,
isLoading: false,
Expand Down

0 comments on commit d653530

Please sign in to comment.