Created
August 14, 2016 01:14
-
-
Save Toddses/fdd862648e10530038550e253212d9c1 to your computer and use it in GitHub Desktop.
Touch directive for AngularJS that allows you to bind touchstart and touchend events.
This file contains hidden or 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
/** | |
* @name fluxTouchDirective | |
* @description | |
* Simple directive for adding touchstart and touchend events to an element | |
* | |
* @author Todd Miller | |
* @version 1.0.0 | |
*/ | |
(function () { | |
'use strict'; | |
angular | |
.module('fluxTouchDirective', []) | |
.directive('fluxTouchstart', fluxTouchstart) | |
.directive('fluxTouchend', fluxTouchend); | |
/** | |
* @name fluxTouchstart | |
* @methodOf fluxTouchDirective | |
* @description | |
* Bind a touchstart event and perform the action given in the attribute | |
*/ | |
function fluxTouchstart () { | |
return { | |
restrict: 'A', | |
link: function (scope, element, attrs) { | |
element.on('touchstart', doTouchstart); | |
/** | |
* @name doTouchstart | |
* @methodOf fluxTouchDirective.fluxTouchstart | |
* @description | |
* Execute the action given in the flux-touchstart attribute | |
* @param {object} event The event object | |
*/ | |
function doTouchstart (event) { | |
event.preventDefault(); | |
// pass along the event object so its accessible in the action | |
scope.$event = event; | |
scope.$apply(attrs.fluxTouchstart); | |
} | |
} | |
}; | |
} | |
/** | |
* @name fluxTouchend | |
* @methodOf fluxTouchDirective | |
* @description | |
* Bind a touchend event and perform that action given in the attribute | |
*/ | |
function fluxTouchend () { | |
return { | |
restrict: 'A', | |
link: function (scope, element, attrs) { | |
element.on('touchend', doTouchend); | |
/** | |
* @name doTouchend | |
* @methodOf fluxTouchDirective.fluxTouchend | |
* @description | |
* Execute the action given in the flux-touchend attribute | |
* @param {object} event The event object | |
*/ | |
function doTouchend (event) { | |
event.preventDefault(); | |
// pass along the event object so its accessible in the action | |
scope.$event = event; | |
scope.$apply(attrs.fluxTouchend); | |
} | |
} | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment