Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor trigger #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"webpack": "^1.14.0"
},
"dependencies": {
"create-event": "^1.0.9",
"lodash": "^4.17.4",
"vue-add-globals": "^2.0.0"
},
Expand Down
23 changes: 9 additions & 14 deletions src/Wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import createEvent from 'create-event';
import findMatchingVNodes from './lib/find-matching-vnodes';
import {
findVueComponents,
Expand Down Expand Up @@ -365,8 +366,8 @@ export default class Wrapper implements WrapperInterface {
* @param {String} type - type of event
* @returns {Boolean}
*/
trigger(type: string) {
if (typeof type !== 'string') {
trigger(eventTypeWithModifier: string, options: object = {}) {
if (typeof eventTypeWithModifier !== 'string') {
error('wrapper.trigger() must be passed a string');
}

Expand All @@ -382,23 +383,17 @@ export default class Wrapper implements WrapperInterface {
right: 39,
};

const event = type.split('.');

let eventObject;
const [type, keyModifier] = eventTypeWithModifier.split('.');

// Fallback for IE10,11 - https://stackoverflow.com/questions/26596123
if (typeof (Event) === 'function') {
eventObject = new window.Event(event[0]);
} else {
eventObject = document.createEvent('Event');
eventObject.initEvent(event[0], true, true);
}

if (event.length === 2) {
// eslint-disable-next-line no-prototype-builtins
if (keyModifier && modifiers.hasOwnProperty(keyModifier)) {
// $FlowIgnore
eventObject.keyCode = modifiers[event[1]];
// eslint-disable-next-line no-param-reassign
options.key = modifiers[keyModifier];
}

const eventObject = createEvent(type, options);
this.element.dispatchEvent(eventObject);
this.update();
}
Expand Down