-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Knape/argument-refactor
Argument refactor
- Loading branch information
Showing
11 changed files
with
327 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,110 @@ | ||
// @flow | ||
|
||
import event from './events/event'; | ||
import { isElement, hasKeys, mergeArrayObjects } from './utils/helpers.utils'; | ||
import events, { getEventType } from './events/event'; | ||
import { position } from './utils/position.utils'; | ||
|
||
const fire = ( | ||
import { | ||
isElement, | ||
hasKeys, | ||
mergeArrayObjects, | ||
} from './utils/helpers.utils'; | ||
|
||
|
||
const isOldArgStyle = el => typeof el !== 'string'; | ||
|
||
export const logDeprecationWarning = (method: string): void => { | ||
console.warn(`You are passing a deprecated argument style to ${method}`); | ||
console.warn('Please update your code to the latest API'); | ||
console.warn('This will result in an error in the next version'); | ||
}; | ||
|
||
/** | ||
* | ||
* As we have two ways of passing arguents to both fire and load | ||
* we need to convert them to the same argumentstyle | ||
* | ||
* @param {String} eventName | ||
* @param {String} eventType | ||
* @param {Node} element | ||
* @param {Array} rest | ||
* | ||
**/ | ||
const createEventWrapper = ( | ||
eventName: string = 'MouseEvent', | ||
triggerName: string = 'click', | ||
eventType: string = 'click', | ||
element: HTMLElement | Document = document, | ||
...rest: Array<Object> | ||
...rest: Array<any> | ||
): Event => { | ||
const options = rest.reduce(mergeArrayObjects, {}); | ||
const opts = rest.reduce(mergeArrayObjects, {}); | ||
// Append the correct client and page props to | ||
// the options object if we dont pass any | ||
if (isElement(element) && !hasKeys(options, 'clientX', 'clientY')) { | ||
Object.assign(options, position(element)); | ||
if (isElement(element) && !hasKeys(opts, 'clientX', 'clientY')) { | ||
Object.assign(opts, position(element)); | ||
} | ||
|
||
// Create the custom event, dispatchit and then return the event | ||
const newEvent = event(eventName)(triggerName, element, options); | ||
const newEvent = events(eventName)(eventType, element, opts); | ||
element.dispatchEvent(newEvent); | ||
return newEvent; | ||
}; | ||
|
||
export default fire; | ||
export const fire = ( | ||
eventType: string = 'click', | ||
element: HTMLElement | Document | string = document, | ||
...rest: Array<Object | HTMLElement | Document> | ||
): Event => { | ||
const isNewStyle = isOldArgStyle(element); | ||
|
||
if (!isNewStyle) logDeprecationWarning('fire'); | ||
|
||
// If we are passing arguments as the new style we | ||
// need to find out what constructor we will use | ||
const eventName = (isNewStyle) ? | ||
getEventType(eventType) : eventType; | ||
|
||
// Create the correct event beased on how we suppyly our arguments | ||
return (typeof element !== 'string') ? | ||
createEventWrapper(eventName, eventType, element, rest) : | ||
createEventWrapper(eventName, element, rest[0], rest.slice(1)); | ||
}; | ||
|
||
/** | ||
* Load Instance, works as fire but waiths for the spray method to call it. | ||
* | ||
* @param {String} eventName | ||
* @param {String} eventType | ||
* @param {Node} element | ||
* @param {Object} options | ||
**/ | ||
export const load = ( | ||
eventType: string = 'click', | ||
element: HTMLElement | Document | string = document, | ||
...rest: Array<Object | HTMLElement | Document> | ||
): Function => { | ||
return (opt: Object = {}) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
const isNewStyle = isOldArgStyle(element); | ||
|
||
if (!isNewStyle) logDeprecationWarning('load'); | ||
|
||
// If we are passing arguments as the new style we | ||
// need to find out what constructor we will use | ||
const eventName = (isNewStyle) ? | ||
getEventType(eventType) : eventType; | ||
|
||
// Depending on what kind of arguments style we are using | ||
// we nned to assign the correct argument to options | ||
const options = (isNewStyle) ? | ||
Object.assign({}, rest.reduce(mergeArrayObjects, {}), opt) : | ||
Object.assign({}, rest.slice(1).reduce(mergeArrayObjects, {}), opt); | ||
|
||
// Create the correct event beased on how we suppyly our arguments | ||
const event = (typeof element !== 'string') ? // Otherwise flow compains | ||
createEventWrapper(eventName, eventType, element, options) : | ||
createEventWrapper(eventName, element, rest[0], options); | ||
resolve({ event, eventName }); | ||
}, opt.speed || 0); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.