Skip to content

Commit a884ba7

Browse files
committed
open source ftw
0 parents  commit a884ba7

7 files changed

+352
-0
lines changed

LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2013, Mozilla Corporation
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the Mozilla Corporation nor the names of its contributors
15+
may be used to endorse or promote products derived from this software without
16+
specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# pointer.js
2+
3+
Normalizes mouse/touch events into 'pointer' events.
4+
5+
## Types of Events
6+
7+
The following events are generated:
8+
9+
* `pointerdown`: based on mousedown/touchstart
10+
* `pointerup`: based on mouseup/touchend
11+
* `pointermove`: based on mousemove/touchmove
12+
* `pointerlave`: based on mouseout/touchleave
13+
* `pointerclick`: a 'fast click' event based on a sequence of the above events. Additional heuristics are applied to determine whether or not to generate a `pointerclick`.
14+
15+
## Event Objects
16+
17+
`pointer` events have the following custom properties:
18+
19+
* maskedEvent: the event that triggered the pointer event.
20+
* touch: boolean- is maskedEvent a touch event?
21+
* mouse: boolean- is maskedEvent a mouse event?
22+
* x: page-normalized x coordinate of the event.
23+
* y: page-normalized y coordinate of the event.

dom.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var dom = (function(win, doc, undefined) {
2+
3+
function dom(sel) {
4+
5+
if (sel === window) return window;
6+
var ret = sel.nodeType ? [sel] : arr(doc.querySelectorAll(sel));
7+
8+
ret.on = function (type, handler) {
9+
ret.forEach(function(el) {
10+
on(el, type, handler)
11+
});
12+
return ret;
13+
};
14+
ret.delegate = function (type, sel, handler) {
15+
ret.forEach(function(delegateEl) {
16+
on(delegateEl, type, function(e,t) {
17+
var matches = delegateEl.querySelectorAll(sel);
18+
if (!matches.length) return;
19+
for (var el = t; el.parentNode && el != delegateEl; el = el.parentNode) {
20+
for (var i = 0; i < matches.length; i++) {
21+
if (matches[i] == el) {
22+
handler.call(el, e);
23+
return;
24+
}
25+
}
26+
}
27+
});
28+
});
29+
return ret;
30+
};
31+
ret.css = function (o) {
32+
if (typeof o == 'object') {
33+
for (var i in o) {
34+
if (!o.hasOwnProperty(i)) continue;
35+
ret.forEach(function(el) {
36+
el.style.setProperty(i, o[i]);
37+
});
38+
}
39+
return ret;
40+
}
41+
return win.getComputedStyle(ret[0]).getPropertyValue(o);
42+
};
43+
ret.attr = function (o) {
44+
if (typeof o == 'object') {
45+
for (var i in o) {
46+
if (!o.hasOwnProperty(i)) continue;
47+
ret.forEach(function(el) {
48+
el.setAttribute(i, o[i]);
49+
});
50+
}
51+
return ret;
52+
}
53+
return ret[0].getAttribute(o);
54+
};
55+
ret.prop = function (o) {
56+
ret.forEach(function(el) {
57+
extend(el, o);
58+
});
59+
return ret;
60+
};
61+
ret.append = function (el) {
62+
ret[0].appendChild(el.length ? el[0] : el);
63+
return ret;
64+
};
65+
ret.empty = function () {
66+
ret.forEach(empty);
67+
return ret;
68+
};
69+
ret.text = function (s) {
70+
ret[0].appendChild(doc.createTextNode(s));
71+
return ret;
72+
};
73+
74+
return ret;
75+
};
76+
77+
var arr = dom.arr = function(a, i) {
78+
return Array.prototype.slice.call(a, i || 0);
79+
};
80+
var extend = dom.extend = function extend(d, s) {
81+
for (p in s) {
82+
if (s.hasOwnProperty(p)) {
83+
d[p] = s[p];
84+
}
85+
}
86+
};
87+
var on = dom.on = function on(obj, type, handler) {
88+
obj.addEventListener(type, function(e) {
89+
handler(e, e.target);
90+
}, false);
91+
};
92+
93+
var empty = dom.empty = function empty(el) {
94+
while (el.childNodes.length) {
95+
el.removeChild(el.firstChild);
96+
}
97+
};
98+
99+
dom.tag = function tag(name) {
100+
return dom(doc.createElement(name));
101+
};
102+
103+
return dom;
104+
105+
})(window, document);

index.html

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
6+
<title>Pointer Events Test</title>
7+
<style>
8+
div {
9+
margin: 0 10px;
10+
border: 1px solid black;
11+
line-height: 40px;
12+
text-align: center;
13+
}
14+
input {
15+
position: fixed;
16+
top: 0;
17+
left: 0;
18+
}
19+
b {
20+
display: block;
21+
position: absolute;
22+
width: 64px;
23+
height: 64px;
24+
border: 1px solid black;
25+
pointer-events: none;
26+
border-radius: 32px;
27+
text-align: center;
28+
line-height: 64px;
29+
}
30+
i {
31+
position: fixed;
32+
width: 0;
33+
height: 0;
34+
top: 0;
35+
left: 0;
36+
pointer-events: none;
37+
}
38+
#lineX {
39+
height: 100%;
40+
border-left: 1px solid black;
41+
}
42+
#lineY {
43+
width: 100%;
44+
position: absolute;
45+
border-top: 1px solid black;
46+
}
47+
</style>
48+
</head>
49+
<body>
50+
<h1>Pointer Events Test</h1>
51+
<b></b>
52+
<i id="lineX"></i>
53+
<i id="lineY"></i>
54+
<script src="pointer.js"></script>
55+
<script src="dom.js"></script>
56+
<script>
57+
dom('body').append(dom.tag('input')
58+
.attr({'id': 'foo', 'data-foo': 'bar'})
59+
.css({'border-color': 'red'}));
60+
for (var i=0; i<100; i++) {
61+
dom('body').append(dom.tag('div').text('foo'));
62+
}
63+
dom('body').on('pointermove', function(e) {
64+
});
65+
var bel = dom('b');
66+
var input = dom('input')[0];
67+
document.body.addEventListener('pointermove', function(e) {
68+
bel.css({'left': e.x-32+'px', 'top': e.y-32+'px'});
69+
dom('#lineX').css({'left': e.x+'px'});
70+
dom('#lineY').css({'top': e.y+'px'});
71+
dom('b').empty().text(e.x);
72+
});
73+
dom('body').delegate('pointerclick', 'div', function(e) {
74+
e.target.innerHTML = Date.now();
75+
});
76+
</script>
77+
</body>
78+
</html>

manifest.webapp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Pointer Test",
3+
"description": "Test Page for Pointer Events Polyfill",
4+
"developer": {
5+
"name": "Potch",
6+
"url": "https://github.com/potch/"
7+
},
8+
"launch_path": "/index.html",
9+
"default_locale": "en"
10+
}

pointer.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
// pointer.js, version 1.0
3+
4+
// Synthesize 'pointer' events using mouse/touch events.
5+
6+
// (c) 2013 Copyright (c) 2013, Mozilla Corporation
7+
// project located at https://github.com/mozilla/pointer.js.
8+
// Licenced under the BSD license (see LICENSE file)
9+
10+
// Pointer events have the following custom properties:
11+
// * maskedEvent: the event that triggered the pointer event.
12+
// * touch: boolean- is maskedEvent a touch event?
13+
// * mouse: boolean- is maskedEvent a mouse event?
14+
// * x: page-normalized x coordinate of the event.
15+
// * y: page-normalized y coordinate of the event.
16+
17+
(function() {
18+
var body = document.body;
19+
20+
var isScrolling = false;
21+
var timeout = false;
22+
var sDistX = 0;
23+
var sDistY = 0;
24+
window.addEventListener('scroll', function() {
25+
if (!isScrolling) {
26+
sDistX = window.pageXOffset;
27+
sDistY = window.pageYOffset;
28+
}
29+
isScrolling = true;
30+
clearTimeout(timeout);
31+
timeout = setTimeout(function() {
32+
isScrolling = false;
33+
sDistX = 0;
34+
sDistY = 0;
35+
}, 100);
36+
});
37+
38+
body.addEventListener('mousedown', pointerDown);
39+
body.addEventListener('touchstart', pointerDown);
40+
body.addEventListener('mouseup', pointerUp);
41+
body.addEventListener('touchend', pointerUp);
42+
body.addEventListener('mousemove', pointerMove);
43+
body.addEventListener('touchmove', pointerMove);
44+
body.addEventListener('mouseout', pointerLeave);
45+
body.addEventListener('touchleave', pointerLeave);
46+
47+
// 'pointerdown' event, triggered by mousedown/touchstart.
48+
function pointerDown(e) {
49+
var evt = makePointerEvent('down', e);
50+
// don't maybeClick if more than one touch is active.
51+
var singleFinger = evt.mouse || (evt.touch && e.touches.length === 1);
52+
if (!isScrolling && singleFinger) {
53+
e.target.maybeClick = true;
54+
e.target.maybeClickX = evt.x;
55+
e.target.maybeClickY = evt.y;
56+
}
57+
}
58+
59+
// 'pointerdown' event, triggered by mouseout/touchleave.
60+
function pointerLeave(e) {
61+
e.target.maybeClick = false;
62+
makePointerEvent('leave', e);
63+
}
64+
65+
// 'pointermove' event, triggered by mousemove/touchmove.
66+
function pointerMove(e) {
67+
var evt = makePointerEvent('move', e);
68+
}
69+
70+
// 'pointerup' event, triggered by mouseup/touchend.
71+
function pointerUp(e) {
72+
var evt = makePointerEvent('up', e);
73+
// Does our target have maybeClick set by pointerdown?
74+
if (e.target.maybeClick) {
75+
// Have we moved too much?
76+
if (Math.abs(e.target.maybeClickX - evt.x) < 5 &&
77+
Math.abs(e.target.maybeClickY - evt.y) < 5) {
78+
// Have we scrolled too much?
79+
if (!isScrolling ||
80+
(Math.abs(sDistX - window.pageXOffset) < 5 &&
81+
Math.abs(sDistY - window.pageYOffset) < 5)) {
82+
makePointerEvent('click', e);
83+
}
84+
}
85+
}
86+
e.target.maybeClick = false;
87+
}
88+
89+
function makePointerEvent(type, e) {
90+
var tgt = e.target;
91+
var evt = document.createEvent('CustomEvent');
92+
evt.initCustomEvent('pointer' + type, true, true, {});
93+
evt.touch = e.type.indexOf('touch') === 0;
94+
evt.mouse = e.type.indexOf('mouse') === 0;
95+
if (evt.touch) {
96+
evt.x = e.changedTouches[0].pageX;
97+
evt.y = e.changedTouches[0].pageY;
98+
}
99+
if (evt.mouse) {
100+
evt.x = e.clientX + window.pageXOffset;
101+
evt.y = e.clientY + window.pageYOffset;
102+
}
103+
evt.maskedEvent = e;
104+
tgt.dispatchEvent(evt);
105+
return evt;
106+
}
107+
108+
})();

pointer.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)