Skip to content

Commit

Permalink
Custom events can be unregistered, ref #59
Browse files Browse the repository at this point in the history
  • Loading branch information
ten1seven committed Jul 3, 2017
1 parent 531d01e commit 7f0d595
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 45 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

__A global utility for tracking the current input method (mouse, keyboard or touch).__

## What Input is now v4.2.0
## What Input is now v4.3.0

What Input adds data attributes to the `<html>` tag based on the type of input being used. It also exposes a simple API that can be used for scripting interactions.

### July 3, 2017

* Updated: custom events can now be registered and unregistered.

### June 13, 2017

* Updated: Typing _in_ form inputs does not change input type, but tabbing between inputs _does_ initiate a switch from `mouse` to `keyboard`.
Expand Down Expand Up @@ -178,10 +182,13 @@ var myFunction = function(type) {
};

// fire `myFunction` when the intent changes
whatInput.onChange(myFunction, 'intent');
whatInput.registerOnChange(myFunction, 'intent');

// fire `myFunction` when the input changes
whatInput.onChange(myFunction, 'input');
whatInput.registerOnChange(myFunction, 'input');

// remove custom event
whatInput.unRegisterOnChange(myFunction);
```

## Compatibility
Expand Down
17 changes: 3 additions & 14 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "what-input",
"version": "4.2.0",
"version": "4.3.0",
"homepage": "https://github.com/ten1seven/what-input",
"repository": {
"url": "https://github.com/ten1seven/what-input.git",
Expand All @@ -9,18 +9,7 @@
"author": "Jeremy Fields <[email protected]>",
"description": "A global utility for tracking the current input method (mouse, keyboard or touch).",
"main": "dist/what-input.js",
"keywords": [
"accessibility",
"a11y",
"input",
"javascript"
],
"keywords": ["accessibility", "a11y", "input", "javascript"],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
"ignore": ["**/.*", "node_modules", "bower_components", "test", "tests"]
}
28 changes: 23 additions & 5 deletions dist/what-input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
* @version v4.2.0
* @version v4.3.0
* @link https://github.com/ten1seven/what-input
* @license MIT
*/
Expand Down Expand Up @@ -97,6 +97,7 @@ return /******/ (function(modules) { // webpackBootstrap
// mapping of events to input types
var inputMap = {
keydown: 'keyboard',
keyup: 'keyboard',
mousedown: 'mouse',
mousemove: 'mouse',
MSPointerDown: 'pointer',
Expand Down Expand Up @@ -185,6 +186,7 @@ return /******/ (function(modules) { // webpackBootstrap

// keyboard events
doc.addEventListener('keydown', updateInput);
doc.addEventListener('keyup', updateInput);
};

// checks conditions before updating new input
Expand Down Expand Up @@ -275,7 +277,7 @@ return /******/ (function(modules) { // webpackBootstrap
var fireFunctions = function fireFunctions(type) {
for (var i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].type === type) {
functionList[i].function.call(undefined, currentIntent);
functionList[i].fn.call(undefined, currentIntent);
}
}
};
Expand Down Expand Up @@ -310,6 +312,14 @@ return /******/ (function(modules) { // webpackBootstrap
return wheelType;
};

var objPos = function objPos(match) {
for (var i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].fn === match) {
return i;
}
}
};

/*
* init
*/
Expand Down Expand Up @@ -346,11 +356,19 @@ return /******/ (function(modules) { // webpackBootstrap
// attach functions to input and intent "events"
// funct: function to fire on change
// eventType: 'input'|'intent'
onChange: function onChange(funct, eventType) {
registerOnChange: function registerOnChange(fn, eventType) {
functionList.push({
function: funct,
type: eventType
fn: fn,
type: eventType || 'input'
});
},

unRegisterOnChange: function unRegisterOnChange(fn) {
var position = objPos(fn);

if (position) {
functionList.splice(position, 1);
}
}
};
}();
Expand Down
4 changes: 2 additions & 2 deletions dist/what-input.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ <h1>What Input?</h1>
console.log('intent: ' + type)
};

whatInput.onChange(myIntentFunction, 'intent');
whatInput.onChange(myInputFunction, 'input');
whatInput.registerOnChange(myInputFunction);
whatInput.registerOnChange(myIntentFunction, 'intent');
</script>

</body>
Expand Down
28 changes: 23 additions & 5 deletions docs/scripts/what-input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
* @version v4.2.0
* @version v4.3.0
* @link https://github.com/ten1seven/what-input
* @license MIT
*/
Expand Down Expand Up @@ -97,6 +97,7 @@ return /******/ (function(modules) { // webpackBootstrap
// mapping of events to input types
var inputMap = {
keydown: 'keyboard',
keyup: 'keyboard',
mousedown: 'mouse',
mousemove: 'mouse',
MSPointerDown: 'pointer',
Expand Down Expand Up @@ -185,6 +186,7 @@ return /******/ (function(modules) { // webpackBootstrap

// keyboard events
doc.addEventListener('keydown', updateInput);
doc.addEventListener('keyup', updateInput);
};

// checks conditions before updating new input
Expand Down Expand Up @@ -275,7 +277,7 @@ return /******/ (function(modules) { // webpackBootstrap
var fireFunctions = function fireFunctions(type) {
for (var i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].type === type) {
functionList[i].function.call(undefined, currentIntent);
functionList[i].fn.call(undefined, currentIntent);
}
}
};
Expand Down Expand Up @@ -310,6 +312,14 @@ return /******/ (function(modules) { // webpackBootstrap
return wheelType;
};

var objPos = function objPos(match) {
for (var i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].fn === match) {
return i;
}
}
};

/*
* init
*/
Expand Down Expand Up @@ -346,11 +356,19 @@ return /******/ (function(modules) { // webpackBootstrap
// attach functions to input and intent "events"
// funct: function to fire on change
// eventType: 'input'|'intent'
onChange: function onChange(funct, eventType) {
registerOnChange: function registerOnChange(fn, eventType) {
functionList.push({
function: funct,
type: eventType
fn: fn,
type: eventType || 'input'
});
},

unRegisterOnChange: function unRegisterOnChange(fn) {
var position = objPos(fn);

if (position) {
functionList.splice(position, 1);
}
}
};
}();
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
{
"name": "what-input",
"version": "4.2.0",
"version": "4.3.0",
"description": "A global utility for tracking the current input method (mouse, keyboard or touch).",
"main": "dist/what-input.js",
"repository": {
"url": "https://github.com/ten1seven/what-input.git",
"type": "git"
},
"keywords": [
"accessibility",
"a11y",
"input",
"javascript"
],
"keywords": ["accessibility", "a11y", "input", "javascript"],
"author": "Jeremy Fields <[email protected]>",
"license": "MIT",
"bugs": {
Expand Down
31 changes: 24 additions & 7 deletions src/what-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = (() => {
// mapping of events to input types
const inputMap = {
keydown: 'keyboard',
keyup: 'keyboard',
mousedown: 'mouse',
mousemove: 'mouse',
MSPointerDown: 'pointer',
Expand Down Expand Up @@ -127,6 +128,7 @@ module.exports = (() => {

// keyboard events
doc.addEventListener('keydown', updateInput)
doc.addEventListener('keyup', updateInput)
}

// checks conditions before updating new input
Expand Down Expand Up @@ -225,7 +227,7 @@ module.exports = (() => {
const fireFunctions = type => {
for (let i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].type === type) {
functionList[i].function.call(this, currentIntent)
functionList[i].fn.call(this, currentIntent)
}
}
}
Expand Down Expand Up @@ -254,14 +256,21 @@ module.exports = (() => {
} else {
// Webkit and IE support at least "mousewheel"
// or assume that remaining browsers are older Firefox
wheelType = document.onmousewheel !== undefined
? 'mousewheel'
: 'DOMMouseScroll'
wheelType =
document.onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll'
}

return wheelType
}

const objPos = match => {
for (let i = 0, len = functionList.length; i < len; i++) {
if (functionList[i].fn === match) {
return i
}
}
}

/*
* init
*/
Expand Down Expand Up @@ -298,11 +307,19 @@ module.exports = (() => {
// attach functions to input and intent "events"
// funct: function to fire on change
// eventType: 'input'|'intent'
onChange: (funct, eventType) => {
registerOnChange: (fn, eventType) => {
functionList.push({
function: funct,
type: eventType
fn: fn,
type: eventType || 'input'
})
},

unRegisterOnChange: fn => {
let position = objPos(fn)

if (position) {
functionList.splice(position, 1)
}
}
}
})()

0 comments on commit 7f0d595

Please sign in to comment.