Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaEffect committed Mar 19, 2019
2 parents fdabb52 + 10a97f9 commit e0f32bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/element-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface EventHandler {
* @param element
*/
export class ElementEvents {
static defaultListenerOptions: boolean | AddEventListenerOptions = true;

constructor(element: EventTarget) {
this.element = element;
this.subscriptions = {};
Expand Down Expand Up @@ -51,8 +53,11 @@ export class ElementEvents {
* Adds and Event Listener on the context element.
* @return Returns the eventHandler containing a dispose method
*/
subscribe(eventName: string, handler: Function, captureOrOptions?: boolean = true): EventHandler {
subscribe(eventName: string, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions): EventHandler {
if (typeof handler === 'function') {
if (captureOrOptions === undefined) {
captureOrOptions = ElementEvents.defaultListenerOptions;
}
const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, false);
return eventHandler;
}
Expand All @@ -64,8 +69,11 @@ export class ElementEvents {
* Adds an Event Listener on the context element, that will be disposed on the first trigger.
* @return Returns the eventHandler containing a dispose method
*/
subscribeOnce(eventName: string, handler: Function, captureOrOptions?: boolean = true): EventHandler {
subscribeOnce(eventName: string, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions): EventHandler {
if (typeof handler === 'function') {
if (captureOrOptions === undefined) {
captureOrOptions = ElementEvents.defaultListenerOptions;
}
const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, true);
return eventHandler;
}
Expand Down
42 changes: 42 additions & 0 deletions test/element-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ describe('ElementEvents', () => {
expect(callCount).toBe(1);
});

it("should subscribe and take ElementEvent default listener options into account", () => {
let isCapture;

// we need to track event on a parent of the input, let's take body
const bodyElementEvents = new ElementEvents(document.body);
ElementEvents.defaultListenerOptions = { capture: false };
let eventHandler = bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
});

// input has to be attached for the event to bubble up
document.body.appendChild(input);
input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(false);
eventHandler.dispose();


// set capture back to true and check if it's being used
ElementEvents.defaultListenerOptions = { capture: true };
eventHandler = bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
});

input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(true);
eventHandler.dispose();
});

it("should subscribe and ignore ElementEvent default listener options when argument captureOrOptions is passed", () => {
let isCapture;

const bodyElementEvents = new ElementEvents(document.body);
ElementEvents.defaultListenerOptions = { capture: false };
bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
}, true);

document.body.appendChild(input);
input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(true);
});

it('should dispose single event', () => {
let value;
let callCount = 0;
Expand Down

0 comments on commit e0f32bb

Please sign in to comment.