Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 2029b06

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
2 parents 2b69f3b + 90b99d3 commit 2029b06

File tree

7 files changed

+420
-58
lines changed

7 files changed

+420
-58
lines changed

src/app/modules/angular-slickgrid/services/collection.service.spec.ts renamed to src/app/modules/angular-slickgrid/services/__tests__/collection.service.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { TestBed } from '@angular/core/testing';
22
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3-
import { CollectionService } from './collection.service';
3+
import { CollectionService } from '../collection.service';
44
import {
55
CollectionFilterBy,
66
CollectionSortBy,
77
FilterMultiplePassType,
88
OperatorType,
99
Column,
1010
FieldType,
11-
} from './../models/index';
11+
} from '../../models/index';
1212

1313
describe('CollectionService', () => {
1414
let collection = [];
1515
let translate: TranslateService;
1616
let service: CollectionService;
1717

18-
// stub some methods of the SlickGrid Grid instance
19-
const gridStub = {
20-
getOptions: jest.fn()
21-
};
22-
2318
beforeEach(() => {
2419
TestBed.configureTestingModule({
2520
providers: [CollectionService],

src/app/modules/angular-slickgrid/services/__tests__/gridState.service.spec.ts

Lines changed: 359 additions & 0 deletions
Large diffs are not rendered by default.

src/app/modules/angular-slickgrid/services/resizer.service.spec.ts renamed to src/app/modules/angular-slickgrid/services/__tests__/resizer.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GridOption } from './../models/gridOption.interface';
2-
import { ResizerService } from './resizer.service';
1+
import { GridOption } from '../../models/gridOption.interface';
2+
import { ResizerService } from '../resizer.service';
33

44
const DATAGRID_MIN_HEIGHT = 180;
55
const DATAGRID_MIN_WIDTH = 300;

src/app/modules/angular-slickgrid/services/utilities.spec.ts renamed to src/app/modules/angular-slickgrid/services/__tests__/utilities.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
uniqueArray,
2121
uniqueObjectArray,
2222
unsubscribeAllObservables,
23-
} from './utilities';
23+
} from '../utilities';
2424
import * as moment from 'moment-mini';
2525
import { of, Subscription } from 'rxjs';
2626

src/app/modules/angular-slickgrid/services/extension.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export class ExtensionService {
312312
}
313313
}
314314

315-
/** Translate the an array of items from an input key and assign to the output key */
315+
/** Translate an array of items from an input key and assign translated value to the output key */
316316
private translateItems(items: any[], inputKey: string, outputKey: string) {
317317
for (const item of items) {
318318
if (item[inputKey]) {

src/app/modules/angular-slickgrid/services/gridState.service.ts

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { SortService } from './sort.service';
1616
import { Subject, Subscription } from 'rxjs';
1717

1818
// using external non-typed js libraries
19-
declare var $: any;
2019
declare var Slick: any;
2120

2221
export class GridStateService {
@@ -63,6 +62,8 @@ export class GridStateService {
6362
}
6463
});
6564
this.subscriptions = [];
65+
this._currentColumns = [];
66+
this._columns = [];
6667
}
6768

6869
/**
@@ -88,7 +89,7 @@ export class GridStateService {
8889
* @return current columns
8990
*/
9091
getColumns(): Column[] {
91-
return this._columns || this._grid.getColumns();
92+
return this._columns;
9293
}
9394

9495
/**
@@ -203,38 +204,6 @@ export class GridStateService {
203204
return null;
204205
}
205206

206-
/**
207-
* Hook a SlickGrid Extension Event to a Grid State change event
208-
* @param extension name
209-
* @param grid
210-
*/
211-
hookExtensionEventToGridStateChange(extensionName: ExtensionName, eventName: string) {
212-
const extension = this.extensionService && this.extensionService.getExtensionByName(extensionName);
213-
214-
if (extension && extension.class && extension.class[eventName] && extension.class[eventName].subscribe) {
215-
this._eventHandler.subscribe(extension.class[eventName], (e: Event, args: any) => {
216-
const columns: Column[] = args && args.columns;
217-
const currentColumns: CurrentColumn[] = this.getAssociatedCurrentColumns(columns);
218-
this.onGridStateChanged.next({ change: { newValues: currentColumns, type: GridStateType.columns }, gridState: this.getCurrentGridState() });
219-
});
220-
}
221-
}
222-
223-
/**
224-
* Hook a Grid Event to a Grid State change event
225-
* @param event name
226-
* @param grid
227-
*/
228-
hookSlickGridEventToGridStateChange(eventName: string, grid: any) {
229-
if (grid && grid[eventName] && grid[eventName].subscribe) {
230-
this._eventHandler.subscribe(grid[eventName], (e: Event, args: any) => {
231-
const columns: Column[] = grid.getColumns();
232-
const currentColumns: CurrentColumn[] = this.getAssociatedCurrentColumns(columns);
233-
this.onGridStateChanged.next({ change: { newValues: currentColumns, type: GridStateType.columns }, gridState: this.getCurrentGridState() });
234-
});
235-
}
236-
}
237-
238207
resetColumns(columnDefinitions?: Column[]) {
239208
const columns: Column[] = columnDefinitions || this._columns;
240209
const currentColumns: CurrentColumn[] = this.getAssociatedCurrentColumns(columns);
@@ -289,11 +258,50 @@ export class GridStateService {
289258
);
290259

291260
// Subscribe to ColumnPicker and/or GridMenu for show/hide Columns visibility changes
292-
this.hookExtensionEventToGridStateChange(ExtensionName.columnPicker, 'onColumnsChanged');
293-
this.hookExtensionEventToGridStateChange(ExtensionName.gridMenu, 'onColumnsChanged');
261+
this.bindExtensionAddonEventToGridStateChange(ExtensionName.columnPicker, 'onColumnsChanged');
262+
this.bindExtensionAddonEventToGridStateChange(ExtensionName.gridMenu, 'onColumnsChanged');
294263

295264
// subscribe to Column Resize & Reordering
296-
this.hookSlickGridEventToGridStateChange('onColumnsReordered', grid);
297-
this.hookSlickGridEventToGridStateChange('onColumnsResized', grid);
265+
this.bindSlickGridEventToGridStateChange('onColumnsReordered', grid);
266+
this.bindSlickGridEventToGridStateChange('onColumnsResized', grid);
267+
}
268+
269+
// --
270+
// private methods
271+
// ------------------
272+
273+
/**
274+
* Bind a SlickGrid Extension Event to a Grid State change event
275+
* @param extension name
276+
* @param grid
277+
*/
278+
private bindExtensionAddonEventToGridStateChange(extensionName: ExtensionName, eventName: string) {
279+
const extension = this.extensionService && this.extensionService.getExtensionByName && this.extensionService.getExtensionByName(extensionName);
280+
const slickEvent = extension && extension.addon && extension.addon[eventName];
281+
282+
if (slickEvent && slickEvent.subscribe) {
283+
this._eventHandler.subscribe(slickEvent, (e: Event, args: any) => {
284+
const columns: Column[] = args && args.columns;
285+
const currentColumns: CurrentColumn[] = this.getAssociatedCurrentColumns(columns);
286+
this.onGridStateChanged.next({ change: { newValues: currentColumns, type: GridStateType.columns }, gridState: this.getCurrentGridState() });
287+
});
288+
}
289+
}
290+
291+
/**
292+
* Bind a Grid Event to a Grid State change event
293+
* @param event name
294+
* @param grid
295+
*/
296+
private bindSlickGridEventToGridStateChange(eventName: string, grid: any) {
297+
const slickGridEvent = grid && grid[eventName];
298+
299+
if (slickGridEvent && slickGridEvent.subscribe) {
300+
this._eventHandler.subscribe(slickGridEvent, (e: Event, args: any) => {
301+
const columns: Column[] = grid.getColumns();
302+
const currentColumns: CurrentColumn[] = this.getAssociatedCurrentColumns(columns);
303+
this.onGridStateChanged.next({ change: { newValues: currentColumns, type: GridStateType.columns }, gridState: this.getCurrentGridState() });
304+
});
305+
}
298306
}
299307
}

src/app/modules/angular-slickgrid/services/utilities.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare var $: any;
1212
* this is used in the CSV export
1313
* @param int nbSpaces: number of white spaces to create
1414
*/
15-
export function addWhiteSpaces(nbSpaces): string {
15+
export function addWhiteSpaces(nbSpaces: number): string {
1616
let result = '';
1717

1818
for (let i = 0; i < nbSpaces; i++) {
@@ -25,7 +25,7 @@ export function addWhiteSpaces(nbSpaces): string {
2525
* Create a in-memory div, set it's inner text(which jQuery automatically encodes)
2626
* then grab the encoded contents back out. The div never exists on the page.
2727
*/
28-
export function htmlDecode(encodedStr) {
28+
export function htmlDecode(encodedStr: string): string {
2929
const parser = new DOMParser;
3030
if (parser && parser.parseFromString) {
3131
const dom = parser.parseFromString(
@@ -42,7 +42,7 @@ export function htmlDecode(encodedStr) {
4242
* Create a in-memory div, set it's inner text(which jQuery automatically encodes)
4343
* then grab the encoded contents back out. The div never exists on the page.
4444
*/
45-
export function htmlEncode(inputValue: string) {
45+
export function htmlEncode(inputValue: string): string {
4646
const entityMap = {
4747
'&': '&amp;',
4848
'<': '&lt;',
@@ -157,9 +157,9 @@ export function findOrDefault(array: any[], logic: (item: any) => boolean, defau
157157
* @param minDecimal
158158
* @param maxDecimal
159159
*/
160-
export function decimalFormatted(input: number | string, minDecimal?: number, maxDecimal?: number) {
160+
export function decimalFormatted(input: number | string, minDecimal?: number, maxDecimal?: number): string {
161161
if (isNaN(+input)) {
162-
return input;
162+
return input as string;
163163
}
164164

165165
const minDec = (minDecimal === undefined) ? 2 : minDecimal;
@@ -204,12 +204,12 @@ export function formatNumber(input: number | string, minDecimal?: number, maxDec
204204
}
205205

206206
/** From a dot (.) notation find and return a property within an object given a path */
207-
export function getDescendantProperty(obj: any, path: string) {
207+
export function getDescendantProperty(obj: any, path: string): any {
208208
return path.split('.').reduce((acc, part) => acc && acc[part], obj);
209209
}
210210

211211
/** Get the browser's scrollbar width, this is different to each browser */
212-
export function getScrollBarWidth() {
212+
export function getScrollBarWidth(): number {
213213
const $outer = $('<div>').css({ visibility: 'hidden', width: 100, overflow: 'scroll' }).appendTo('body');
214214
const widthWithScroll = $('<div>').css({ width: '100%' }).appendTo($outer).outerWidth();
215215
$outer.remove();
@@ -512,7 +512,7 @@ export function mapOperatorByFieldType(fieldType: FieldType | string): OperatorT
512512
}
513513

514514
/** Parse any input (bool, number, string) and return a boolean or False when not possible */
515-
export function parseBoolean(input: boolean | number | string) {
515+
export function parseBoolean(input: boolean | number | string): boolean {
516516
return /(true|1)/i.test(input + '');
517517
}
518518

@@ -553,7 +553,7 @@ export function sanitizeHtmlToText(htmlString: string) {
553553
* @param inputStr
554554
* @returns string
555555
*/
556-
export function titleCase(inputStr: string, caseEveryWords = false) {
556+
export function titleCase(inputStr: string, caseEveryWords = false): string {
557557
if (typeof inputStr === 'string') {
558558
if (caseEveryWords) {
559559
return inputStr.replace(/\w\S*/g, (outputStr) => {

0 commit comments

Comments
 (0)