Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit cb27a2e

Browse files
authored
Added support for custom options in settings object. #124 (#133)
1 parent dd10513 commit cb27a2e

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

lib/ce/src/hot-settings-resolver.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ const AVAILABLE_HOOKS: string[] = Handsontable.hooks.getRegistered();
77

88
@Injectable()
99
export class HotSettingsResolver {
10-
mergeSettings(component): Handsontable.GridSettings {
11-
const mergedSettings: Handsontable.GridSettings = {};
10+
mergeSettings(component): Handsontable.GridSettings | object {
11+
const isSettingsObject = typeof component['settings'] === 'object';
12+
const mergedSettings: Handsontable.GridSettings = isSettingsObject ? component['settings'] : {};
1213
const options = AVAILABLE_HOOKS.concat(AVAILABLE_OPTIONS);
1314

1415
options.forEach(key => {
16+
const isHook = AVAILABLE_HOOKS.indexOf(key) > -1;
1517
let option;
1618

17-
if (typeof component['settings'] === 'object') {
19+
if (isSettingsObject && isHook) {
1820
option = component['settings'][key];
1921
}
2022

@@ -25,7 +27,7 @@ export class HotSettingsResolver {
2527
if (option === void 0) {
2628
return;
2729

28-
} else if (typeof option === 'function' && AVAILABLE_HOOKS.indexOf(key) > -1) {
30+
} else if (typeof option === 'function' && isHook) {
2931
mergedSettings[key] = function(...args) {
3032
return component._ngZone.run(() => {
3133
return option(this, ...args);

lib/pro/src/hot-settings-resolver.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ const AVAILABLE_HOOKS: string[] = Handsontable.hooks.getRegistered();
77

88
@Injectable()
99
export class HotSettingsResolver {
10-
mergeSettings(component): Handsontable.GridSettings {
11-
const mergedSettings: Handsontable.GridSettings = {};
10+
mergeSettings(component): Handsontable.GridSettings | object {
11+
const isSettingsObject = typeof component['settings'] === 'object';
12+
const mergedSettings: Handsontable.GridSettings = isSettingsObject ? component['settings'] : {};
1213
const options = AVAILABLE_HOOKS.concat(AVAILABLE_OPTIONS);
1314

1415
options.forEach((key) => {
16+
const isHook = AVAILABLE_HOOKS.indexOf(key) > -1;
1517
let option;
1618

17-
if (typeof component['settings'] === 'object') {
19+
if (isSettingsObject && isHook) {
1820
option = component['settings'][key];
1921
}
2022

@@ -25,7 +27,7 @@ export class HotSettingsResolver {
2527
if (option === void 0) {
2628
return;
2729

28-
} else if (typeof option === 'function' && AVAILABLE_HOOKS.indexOf(key) > -1) {
30+
} else if (typeof option === 'function' && isHook) {
2931
mergedSettings[key] = function(...args) {
3032
return component._ngZone.run(() => {
3133
return option(this, ...args);

src/app/hot-table-ce.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ describe('HotTableComponent', () => {
9696
});
9797
});
9898

99+
it(`should be possible to get custom option over to 'settings' defined as bindings`, () => {
100+
TestBed.overrideComponent(TestComponent, {
101+
set: {
102+
template: `<hot-table [hotId]="id" [settings]="prop.settings"></hot-table>`
103+
}
104+
});
105+
TestBed.compileComponents().then(() => {
106+
fixture = TestBed.createComponent(TestComponent);
107+
const app = fixture.componentInstance;
108+
109+
app.prop['settings'] = {
110+
customOption: 'test'
111+
};
112+
113+
fixture.detectChanges();
114+
expect(app.getHotInstance(app.id).getSettings()['customOption']).toBe('test');
115+
});
116+
});
117+
99118
it(`should set activeHeaderClassName defined as bindings`, () => {
100119
TestBed.overrideComponent(TestComponent, {
101120
set: {

src/app/hot-table-pro.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ describe('HotTableComponent', () => {
9696
});
9797
});
9898

99+
it(`should be possible to get custom option over to 'settings' defined as bindings`, () => {
100+
TestBed.overrideComponent(TestComponent, {
101+
set: {
102+
template: `<hot-table [hotId]="id" [settings]="prop.settings"></hot-table>`
103+
}
104+
});
105+
TestBed.compileComponents().then(() => {
106+
fixture = TestBed.createComponent(TestComponent);
107+
const app = fixture.componentInstance;
108+
109+
app.prop['settings'] = {
110+
customOption: 'test'
111+
};
112+
113+
fixture.detectChanges();
114+
expect(app.getHotInstance(app.id).getSettings()['customOption']).toBe('test');
115+
});
116+
});
117+
99118
it(`should set activeHeaderClassName defined as bindings`, () => {
100119
TestBed.overrideComponent(TestComponent, {
101120
set: {

0 commit comments

Comments
 (0)