Skip to content

Commit

Permalink
Bug fix close methods (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
dixso committed May 16, 2017
1 parent c3e124e commit d840b22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
33 changes: 24 additions & 9 deletions src/custombox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ function getAfterEach() {
for (let i = 0, t = loaders.length; i < t; i++) {
loaders[i].parentNode.removeChild(loaders[i]);
}

delete Custombox;
}

describe('Custombox', () => {
Expand All @@ -49,8 +47,6 @@ describe('Custombox', () => {
afterEach(()=> {
getAfterEach();

delete Custombox;

jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

Expand Down Expand Up @@ -215,8 +211,6 @@ describe('Custombox', () => {
afterEach(()=> {
getAfterEach();

delete Custombox;

jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

Expand Down Expand Up @@ -304,7 +298,7 @@ describe('Custombox', () => {
content: {
effect: 'fadein',
target: '#foo-1',
escKey: false
close: false
},
}).open();

Expand All @@ -320,6 +314,29 @@ describe('Custombox', () => {
}, 200);
});

it('should not close on clicking the background', (done) => {
new (Custombox as any).modal({
content: {
effect: 'fadein',
target: '#foo-1',
close: false
},
overlay: {
close: false
}
}).open();

setTimeout(() => {
let overlay: any = document.querySelector('.custombox-overlay');
overlay.click();

setTimeout(() => {
expect(hasElement('.custombox-close')).toBe(false);
done();
}, 500);
}, 200);
});

it('should have run the onOpen function', (done) => {
let state: boolean = false;
new (Custombox as any).modal({
Expand Down Expand Up @@ -910,8 +927,6 @@ describe('Custombox', () => {
loaders[i].parentNode.removeChild(loaders[i]);
}

delete Custombox;

jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

Expand Down
20 changes: 11 additions & 9 deletions src/custombox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Custombox {
delay: number;
id: string;
container: string;
escKey: boolean;
close: boolean;
}

interface LoaderSchema {
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace Custombox {
onOpen: null,
onComplete: null,
onClose: null,
escKey: true,
close: true,
};
loader = {
active: true,
Expand Down Expand Up @@ -668,7 +668,7 @@ namespace Custombox {
Promise
.all(close)
.then(() => {
if (this.options.content.escKey) {
if (this.options.content.close) {
document.removeEventListener('keydown', this.action, true);
}

Expand All @@ -693,15 +693,17 @@ namespace Custombox {
}

private listeners(): void {
if (this.options.content.escKey) {
if (this.options.content.close) {
document.addEventListener('keydown', this.action, true);
}

this.content.element.addEventListener('click', (event: Event) => {
if (event.target === this.content.element) {
this._close();
}
}, true);
if (this.options.overlay.close) {
this.content.element.addEventListener('click', (event: Event) => {
if (event.target === this.content.element) {
this._close();
}
}, true);
}

this.content.element.addEventListener(`${CB}:close`, () => {
this._close();
Expand Down

0 comments on commit d840b22

Please sign in to comment.