Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit a7231948f3a18dd21b5d850d71ed7b54b30b2b6a @jpdery committed Jan 6, 2013
@@ -0,0 +1,9 @@
+all: build build-compress
+
+build:
+ @wrup -r touch-util ./ > touch-util.js
+ @echo "File written to 'touch-util.js'"
+
+build-compress:
+ @wrup -r touch-util ./ > touch-util.min.js --compress
+ @echo "File written to 'touch-util.min.js'"
No changes.
@@ -0,0 +1,3 @@
+"use strict"
+require('./touch')
+require('./tap')
@@ -0,0 +1,92 @@
+"use strict"
+
+var defineCustomEvent = require('event-util/lib/define-custom-event')
+
+var onDispatch = function(custom, data) {
+ custom.view = data.view
+ custom.touches = data.touches
+ custom.targetTouches = data.targetTouches
+ custom.changedTouches = data.changedTouches
+ custom.ctrlKey = data.ctrlKey
+ custom.shiftKey = data.shiftKey
+ custom.altKey = data.altKey
+ custom.metaKey = data.metaKey
+}
+
+var is = function(parent, node) {
+ return parent === node || parent.contains(node)
+}
+
+var inside = function(x, y, node) {
+ var element = document.elementFromPoint(x, y)
+ if (element) return is(node, element)
+ return false
+}
+
+var outside = function(x, y, node) {
+ var element = document.elementFromPoint(x, y)
+ if (element) return !is(node, elment)
+ return true
+}
+
+var append = function(parent, object) {
+ var merge = {}
+ for (var k in parent) merge[k] = parent[k]
+ for (var k in object) merge[k] = object[k]
+ return merge
+}
+
+var custom = {onDispatch: onDispatch}
+
+defineCustomEvent('tapstart', append(custom, {
+ base: 'touchstart',
+ condition: function(e) {
+ return e.targetTouches.length === 1
+ }
+}))
+
+defineCustomEvent('tapmove', append(custom, {
+ base: 'touchmove',
+ condition: function(e) {
+ return e.targetTouches[0] === e.changedTouches[0]
+ }
+}))
+
+defineCustomEvent('tapend', append(custom, {
+ base: 'touchend',
+ condition: function(e) {
+ return e.targetTouches.length === 0
+ }
+}))
+
+defineCustomEvent('tapcancel', append(custom, {
+ base: 'touchcancel',
+ condition: function(e) {
+ return true
+ }
+}))
+
+defineCustomEvent('tap', append(custom, {
+ base: 'tapend',
+ condition: function(e) {
+ var touch = e.changedTouches[0];
+ return inside(touch.pageX, touch.pageY, this)
+ }
+}))
+
+defineCustomEvent('tapinside', append(custom, {
+ base: 'tapmove',
+ condition: function(e) {
+ var touch = e.targetTouches[0];
+ return inside(touch.pageX, touch.pageY, this)
+ }
+}))
+
+defineCustomEvent('tapoutside', append(custom, {
+ base: 'tapmove',
+ condition: function(e) {
+ var touch = e.targetTouches[0];
+ return outside(touch.pageX, touch.pageY, this)
+ }
+}))
+
@@ -0,0 +1,117 @@
+"use strict"
+
+var hasTouchEvent = 'ontouchstart' in global
+var hasTouchList = 'TouchList' in global
+var hasTouch = 'Touch' in global
+
+if (!hasTouchList) {
+
+ var TouchList = function() {
+ this.length = 0
+ }
+
+ TouchList.prototype.identifiedTouch = function(id) {
+ return this[0] && this[0].identifier === id ? this[0] : null
+ }
+
+ TouchList.prototype.item = function(index) {
+ return this[index] || null;
+ }
+}
+
+if (!hasTouch) {
+ var Touch = function() {}
+}
+
+var touch = null
+var target = null
+
+var onDocumentMouseDown = function(e) {
+
+ if (target === null) {
+ target = e.target
+
+ touch = new Touch()
+ touch.identifier = Date.now()
+ touch.screenX = e.screenX
+ touch.screenY = e.screenY
+ touch.clientX = e.clientX
+ touch.clientY = e.clientY
+ touch.pageX = e.pageX
+ touch.pageY = e.pageY
+ touch.radiusX = 0
+ touch.radiusY = 0
+ touch.rotationAngle = 0
+ touch.force = 0
+ touch.target = target
+
+ var list = new TouchList
+ list.length = 1
+ list[0] = touch
+
+ var event = document.createEvent('CustomEvent')
+ event.initCustomEvent('touchstart', true, true)
+ event.touches = list
+ event.targetTouches = list
+ event.changedTouches = list
+
+ target.dispatchEvent(event)
+ }
+};
+
+var onDocumentMouseMove = function(e) {
+
+ if (target) {
+
+ touch.screenX = e.screenX
+ touch.screenY = e.screenY
+ touch.clientX = e.clientX
+ touch.clientY = e.clientY
+ touch.pageX = e.pageX
+ touch.pageY = e.pageY
+
+ var list = new TouchList
+ list.length = 1
+ list[0] = touch
+
+ var event = document.createEvent('CustomEvent')
+ event.initCustomEvent('touchmove', true, true)
+ event.touches = list
+ event.targetTouches = list
+ event.changedTouches = list
+
+ target.dispatchEvent(event)
+ }
+};
+
+var onDocumentMouseUp = function(e) {
+
+ if (target) {
+
+ touch.screenX = e.screenX
+ touch.screenY = e.screenY
+ touch.clientX = e.clientX
+ touch.clientY = e.clientY
+ touch.pageX = e.pageX
+ touch.pageY = e.pageY
+
+ var list = new TouchList
+ list.length = 1
+ list[0] = touch
+
+ var event = document.createEvent('CustomEvent')
+ event.initCustomEvent('touchend', true, true)
+ event.touches = new TouchList
+ event.targetTouches = new TouchList
+ event.changedTouches = list
+
+ target.dispatchEvent(event)
+ target = null
+ }
+};
+
+if (!hasTouchEvent) {
+ document.addEventListener('mousedown', onDocumentMouseDown)
+ document.addEventListener('mousemove', onDocumentMouseMove)
+ document.addEventListener('mouseup', onDocumentMouseUp)
+}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit a723194

Please sign in to comment.