Skip to content

Commit

Permalink
Move dispatch to events namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent 2e4c875 commit 0327017
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion spec/dispatch.spec.js → spec/events/dispatch.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dispatch } from "../src";
import { dispatch } from "../../src";

describe("dispatch", () => {
let root;
Expand Down
18 changes: 18 additions & 0 deletions src/events/dispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* @flow */

import type { EventDetails, EventType } from "../types";

function dispatch(
element: Element,
eventType: EventType,
details: EventDetails = {}
): boolean {
const event = (document.createEvent("HTMLEvents"): any);

event.initEvent(eventType, true, true);
(event: any).details = details;

return element.dispatchEvent(event);
}

export default dispatch;
20 changes: 2 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* @flow */

import type { EventType, Selector } from "./types";
import type { Selector } from "./types";

import { html, body } from "./aliases";
import addClass from "./css/add-class";
import hasClass from "./css/has-class";
import removeClass from "./css/remove-class";
import toggleClass from "./css/toggle-class";
import delegate from "./events/delegate";
import dispatch from "./events/dispatch";
import on from "./events/on";
import once from "./events/once";
import query from "./queries/query";
Expand All @@ -20,8 +21,6 @@ import parentsBy from "./traversing/parents-by";

/* Types */

type EventDetails = { [key: string]: mixed };

type PredicateFn = (element: Element) => boolean;

/* Dataset */
Expand Down Expand Up @@ -115,21 +114,6 @@ function remove(element: Element): boolean {
return false;
}

/* Events */

function dispatch(
element: Element,
eventType: EventType,
details: EventDetails = {}
): boolean {
const event = (document.createEvent("HTMLEvents"): any);

event.initEvent(eventType, true, true);
(event: any).details = details;

return element.dispatchEvent(event);
}

/* Exports */

export {
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type CSSClass = string;

export type Elements = Array<Element>;

export type EventDetails = { [key: string]: mixed };

export type EventListener = (event: Event) => mixed;

export type EventType = string;
Expand Down

0 comments on commit 0327017

Please sign in to comment.