| 1 | import defaultView from "../window.js";
|
|---|
| 2 |
|
|---|
| 3 | function dispatchEvent(node, type, params) {
|
|---|
| 4 | var window = defaultView(node),
|
|---|
| 5 | event = window.CustomEvent;
|
|---|
| 6 |
|
|---|
| 7 | if (typeof event === "function") {
|
|---|
| 8 | event = new event(type, params);
|
|---|
| 9 | } else {
|
|---|
| 10 | event = window.document.createEvent("Event");
|
|---|
| 11 | if (params) event.initEvent(type, params.bubbles, params.cancelable), event.detail = params.detail;
|
|---|
| 12 | else event.initEvent(type, false, false);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | node.dispatchEvent(event);
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | function dispatchConstant(type, params) {
|
|---|
| 19 | return function() {
|
|---|
| 20 | return dispatchEvent(this, type, params);
|
|---|
| 21 | };
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | function dispatchFunction(type, params) {
|
|---|
| 25 | return function() {
|
|---|
| 26 | return dispatchEvent(this, type, params.apply(this, arguments));
|
|---|
| 27 | };
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | export default function(type, params) {
|
|---|
| 31 | return this.each((typeof params === "function"
|
|---|
| 32 | ? dispatchFunction
|
|---|
| 33 | : dispatchConstant)(type, params));
|
|---|
| 34 | }
|
|---|