123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- * @license AngularJS v1.5.8
- * (c) 2010-2016 Google, Inc. http://angularjs.org
- * License: MIT
- */
- (function(window, angular) {'use strict';
- */
- * @ngdoc module
- * @name ngTouch
- * @description
- *
- * # ngTouch
- *
- * The `ngTouch` module provides touch events and other helpers for touch-enabled devices.
- * The implementation is based on jQuery Mobile touch event handling
- * ([jquerymobile.com](http://jquerymobile.com/)).
- *
- *
- * See {@link ngTouch.$swipe `$swipe`} for usage.
- *
- * <div doc-module-components="ngTouch"></div>
- *
- */
- var ngTouch = angular.module('ngTouch', []);
- ngTouch.provider('$touch', $TouchProvider);
- function nodeName_(element) {
- return angular.lowercase(element.nodeName || (element[0] && element[0].nodeName));
- }
- * @ngdoc provider
- * @name $touchProvider
- *
- * @description
- * The `$touchProvider` allows enabling / disabling {@link ngTouch.ngClick ngTouch's ngClick directive}.
- */
- $TouchProvider.$inject = ['$provide', '$compileProvider'];
- function $TouchProvider($provide, $compileProvider) {
-
- * @ngdoc method
- * @name $touchProvider#ngClickOverrideEnabled
- *
- * @param {boolean=} enabled update the ngClickOverrideEnabled state if provided, otherwise just return the
- * current ngClickOverrideEnabled state
- * @returns {*} current value if used as getter or itself (chaining) if used as setter
- *
- * @kind function
- *
- * @description
- * Call this method to enable/disable {@link ngTouch.ngClick ngTouch's ngClick directive}. If enabled,
- * the default ngClick directive will be replaced by a version that eliminates the 300ms delay for
- * click events on browser for touch-devices.
- *
- * The default is `false`.
- *
- */
- var ngClickOverrideEnabled = false;
- var ngClickDirectiveAdded = false;
- this.ngClickOverrideEnabled = function(enabled) {
- if (angular.isDefined(enabled)) {
- if (enabled && !ngClickDirectiveAdded) {
- ngClickDirectiveAdded = true;
-
- ngTouchClickDirectiveFactory.$$moduleName = 'ngTouch';
- $compileProvider.directive('ngClick', ngTouchClickDirectiveFactory);
- $provide.decorator('ngClickDirective', ['$delegate', function($delegate) {
- if (ngClickOverrideEnabled) {
-
- $delegate.shift();
- } else {
-
-
- var i = $delegate.length - 1;
- while (i >= 0) {
- if ($delegate[i].$$moduleName === 'ngTouch') {
- $delegate.splice(i, 1);
- break;
- }
- i--;
- }
- }
- return $delegate;
- }]);
- }
- ngClickOverrideEnabled = enabled;
- return this;
- }
- return ngClickOverrideEnabled;
- };
-
- * @ngdoc service
- * @name $touch
- * @kind object
- *
- * @description
- * Provides the {@link ngTouch.$touch#ngClickOverrideEnabled `ngClickOverrideEnabled`} method.
- *
- */
- this.$get = function() {
- return {
-
- * @ngdoc method
- * @name $touch#ngClickOverrideEnabled
- *
- * @returns {*} current value of `ngClickOverrideEnabled` set in the {@link ngTouch.$touchProvider $touchProvider},
- * i.e. if {@link ngTouch.ngClick ngTouch's ngClick} directive is enabled.
- *
- * @kind function
- */
- ngClickOverrideEnabled: function() {
- return ngClickOverrideEnabled;
- }
- };
- };
- }
-
- * @ngdoc service
- * @name $swipe
- *
- * @description
- * The `$swipe` service is a service that abstracts the messier details of hold-and-drag swipe
- * behavior, to make implementing swipe-related directives more convenient.
- *
- * Requires the {@link ngTouch `ngTouch`} module to be installed.
- *
- * `$swipe` is used by the `ngSwipeLeft` and `ngSwipeRight` directives in `ngTouch`.
- *
- * # Usage
- * The `$swipe` service is an object with a single method: `bind`. `bind` takes an element
- * which is to be watched for swipes, and an object with four handler functions. See the
- * documentation for `bind` below.
- */
- ngTouch.factory('$swipe', [function() {
-
- var MOVE_BUFFER_RADIUS = 10;
- var POINTER_EVENTS = {
- 'mouse': {
- start: 'mousedown',
- move: 'mousemove',
- end: 'mouseup'
- },
- 'touch': {
- start: 'touchstart',
- move: 'touchmove',
- end: 'touchend',
- cancel: 'touchcancel'
- },
- 'pointer': {
- start: 'pointerdown',
- move: 'pointermove',
- end: 'pointerup',
- cancel: 'pointercancel'
- }
- };
- function getCoordinates(event) {
- var originalEvent = event.originalEvent || event;
- var touches = originalEvent.touches && originalEvent.touches.length ? originalEvent.touches : [originalEvent];
- var e = (originalEvent.changedTouches && originalEvent.changedTouches[0]) || touches[0];
- return {
- x: e.clientX,
- y: e.clientY
- };
- }
- function getEvents(pointerTypes, eventType) {
- var res = [];
- angular.forEach(pointerTypes, function(pointerType) {
- var eventName = POINTER_EVENTS[pointerType][eventType];
- if (eventName) {
- res.push(eventName);
- }
- });
- return res.join(' ');
- }
- return {
-
- * @ngdoc method
- * @name $swipe#bind
- *
- * @description
- * The main method of `$swipe`. It takes an element to be watched for swipe motions, and an
- * object containing event handlers.
- * The pointer types that should be used can be specified via the optional
- * third argument, which is an array of strings `'mouse'`, `'touch'` and `'pointer'`. By default,
- * `$swipe` will listen for `mouse`, `touch` and `pointer` events.
- *
- * The four events are `start`, `move`, `end`, and `cancel`. `start`, `move`, and `end`
- * receive as a parameter a coordinates object of the form `{ x: 150, y: 310 }` and the raw
- * `event`. `cancel` receives the raw `event` as its single parameter.
- *
- * `start` is called on either `mousedown`, `touchstart` or `pointerdown`. After this event, `$swipe` is
- * watching for `touchmove`, `mousemove` or `pointermove` events. These events are ignored until the total
- * distance moved in either dimension exceeds a small threshold.
- *
- * Once this threshold is exceeded, either the horizontal or vertical delta is greater.
- * - If the horizontal distance is greater, this is a swipe and `move` and `end` events follow.
- * - If the vertical distance is greater, this is a scroll, and we let the browser take over.
- * A `cancel` event is sent.
- *
- * `move` is called on `mousemove`, `touchmove` and `pointermove` after the above logic has determined that
- * a swipe is in progress.
- *
- * `end` is called when a swipe is successfully completed with a `touchend`, `mouseup` or `pointerup`.
- *
- * `cancel` is called either on a `touchcancel` or `pointercancel` from the browser, or when we begin scrolling
- * as described above.
- *
- */
- bind: function(element, eventHandlers, pointerTypes) {
-
- var totalX, totalY;
-
- var startCoords;
-
- var lastPos;
-
- var active = false;
- pointerTypes = pointerTypes || ['mouse', 'touch', 'pointer'];
- element.on(getEvents(pointerTypes, 'start'), function(event) {
- startCoords = getCoordinates(event);
- active = true;
- totalX = 0;
- totalY = 0;
- lastPos = startCoords;
- eventHandlers['start'] && eventHandlers['start'](startCoords, event);
- });
- var events = getEvents(pointerTypes, 'cancel');
- if (events) {
- element.on(events, function(event) {
- active = false;
- eventHandlers['cancel'] && eventHandlers['cancel'](event);
- });
- }
- element.on(getEvents(pointerTypes, 'move'), function(event) {
- if (!active) return;
-
-
-
-
-
- if (!startCoords) return;
- var coords = getCoordinates(event);
- totalX += Math.abs(coords.x - lastPos.x);
- totalY += Math.abs(coords.y - lastPos.y);
- lastPos = coords;
- if (totalX < MOVE_BUFFER_RADIUS && totalY < MOVE_BUFFER_RADIUS) {
- return;
- }
-
- if (totalY > totalX) {
-
- active = false;
- eventHandlers['cancel'] && eventHandlers['cancel'](event);
- return;
- } else {
-
- event.preventDefault();
- eventHandlers['move'] && eventHandlers['move'](coords, event);
- }
- });
- element.on(getEvents(pointerTypes, 'end'), function(event) {
- if (!active) return;
- active = false;
- eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event);
- });
- }
- };
- }]);
- nodeName_: false
- */
- * @ngdoc directive
- * @name ngClick
- * @deprecated
- *
- * @description
- * <div class="alert alert-danger">
- * **DEPRECATION NOTICE**: Beginning with Angular 1.5, this directive is deprecated and by default **disabled**.
- * The directive will receive no further support and might be removed from future releases.
- * If you need the directive, you can enable it with the {@link ngTouch.$touchProvider $touchProvider#ngClickOverrideEnabled}
- * function. We also recommend that you migrate to [FastClick](https://github.com/ftlabs/fastclick).
- * To learn more about the 300ms delay, this [Telerik article](http://developer.telerik.com/featured/300-ms-click-delay-ios-8/)
- * gives a good overview.
- * </div>
- * A more powerful replacement for the default ngClick designed to be used on touchscreen
- * devices. Most mobile browsers wait about 300ms after a tap-and-release before sending
- * the click event. This version handles them immediately, and then prevents the
- * following click event from propagating.
- *
- * Requires the {@link ngTouch `ngTouch`} module to be installed.
- *
- * This directive can fall back to using an ordinary click event, and so works on desktop
- * browsers as well as mobile.
- *
- * This directive also sets the CSS class `ng-click-active` while the element is being held
- * down (by a mouse click or touch) so you can restyle the depressed element if you wish.
- *
- * @element ANY
- * @param {expression} ngClick {@link guide/expression Expression} to evaluate
- * upon tap. (Event object is available as `$event`)
- *
- * @example
- <example module="ngClickExample" deps="angular-touch.js">
- <file name="index.html">
- <button ng-click="count = count + 1" ng-init="count=0">
- Increment
- </button>
- count: {{ count }}
- </file>
- <file name="script.js">
- angular.module('ngClickExample', ['ngTouch']);
- </file>
- </example>
- */
- var ngTouchClickDirectiveFactory = ['$parse', '$timeout', '$rootElement',
- function($parse, $timeout, $rootElement) {
- var TAP_DURATION = 750;
- var MOVE_TOLERANCE = 12;
- var PREVENT_DURATION = 2500;
- var CLICKBUSTER_THRESHOLD = 25;
- var ACTIVE_CLASS_NAME = 'ng-click-active';
- var lastPreventedTime;
- var touchCoordinates;
- var lastLabelClickCoordinates;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function hit(x1, y1, x2, y2) {
- return Math.abs(x1 - x2) < CLICKBUSTER_THRESHOLD && Math.abs(y1 - y2) < CLICKBUSTER_THRESHOLD;
- }
-
-
-
- function checkAllowableRegions(touchCoordinates, x, y) {
- for (var i = 0; i < touchCoordinates.length; i += 2) {
- if (hit(touchCoordinates[i], touchCoordinates[i + 1], x, y)) {
- touchCoordinates.splice(i, i + 2);
- return true;
- }
- }
- return false;
- }
-
-
- function onClick(event) {
- if (Date.now() - lastPreventedTime > PREVENT_DURATION) {
- return;
- }
- var touches = event.touches && event.touches.length ? event.touches : [event];
- var x = touches[0].clientX;
- var y = touches[0].clientY;
-
-
-
-
- if (x < 1 && y < 1) {
- return;
- }
- if (lastLabelClickCoordinates &&
- lastLabelClickCoordinates[0] === x && lastLabelClickCoordinates[1] === y) {
- return;
- }
-
- if (lastLabelClickCoordinates) {
- lastLabelClickCoordinates = null;
- }
-
- if (nodeName_(event.target) === 'label') {
- lastLabelClickCoordinates = [x, y];
- }
-
-
-
- if (checkAllowableRegions(touchCoordinates, x, y)) {
- return;
- }
-
- event.stopPropagation();
- event.preventDefault();
-
- event.target && event.target.blur && event.target.blur();
- }
-
-
- function onTouchStart(event) {
- var touches = event.touches && event.touches.length ? event.touches : [event];
- var x = touches[0].clientX;
- var y = touches[0].clientY;
- touchCoordinates.push(x, y);
- $timeout(function() {
-
- for (var i = 0; i < touchCoordinates.length; i += 2) {
- if (touchCoordinates[i] == x && touchCoordinates[i + 1] == y) {
- touchCoordinates.splice(i, i + 2);
- return;
- }
- }
- }, PREVENT_DURATION, false);
- }
-
-
- function preventGhostClick(x, y) {
- if (!touchCoordinates) {
- $rootElement[0].addEventListener('click', onClick, true);
- $rootElement[0].addEventListener('touchstart', onTouchStart, true);
- touchCoordinates = [];
- }
- lastPreventedTime = Date.now();
- checkAllowableRegions(touchCoordinates, x, y);
- }
-
- return function(scope, element, attr) {
- var clickHandler = $parse(attr.ngClick),
- tapping = false,
- tapElement,
- startTime,
- touchStartX,
- touchStartY;
- function resetState() {
- tapping = false;
- element.removeClass(ACTIVE_CLASS_NAME);
- }
- element.on('touchstart', function(event) {
- tapping = true;
- tapElement = event.target ? event.target : event.srcElement;
-
- if (tapElement.nodeType == 3) {
- tapElement = tapElement.parentNode;
- }
- element.addClass(ACTIVE_CLASS_NAME);
- startTime = Date.now();
-
- var originalEvent = event.originalEvent || event;
- var touches = originalEvent.touches && originalEvent.touches.length ? originalEvent.touches : [originalEvent];
- var e = touches[0];
- touchStartX = e.clientX;
- touchStartY = e.clientY;
- });
- element.on('touchcancel', function(event) {
- resetState();
- });
- element.on('touchend', function(event) {
- var diff = Date.now() - startTime;
-
- var originalEvent = event.originalEvent || event;
- var touches = (originalEvent.changedTouches && originalEvent.changedTouches.length) ?
- originalEvent.changedTouches :
- ((originalEvent.touches && originalEvent.touches.length) ? originalEvent.touches : [originalEvent]);
- var e = touches[0];
- var x = e.clientX;
- var y = e.clientY;
- var dist = Math.sqrt(Math.pow(x - touchStartX, 2) + Math.pow(y - touchStartY, 2));
- if (tapping && diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
-
- preventGhostClick(x, y);
-
-
-
- if (tapElement) {
- tapElement.blur();
- }
- if (!angular.isDefined(attr.disabled) || attr.disabled === false) {
- element.triggerHandler('click', [event]);
- }
- }
- resetState();
- });
-
-
- element.onclick = function(event) { };
-
-
-
-
-
-
- element.on('click', function(event, touchend) {
- scope.$apply(function() {
- clickHandler(scope, {$event: (touchend || event)});
- });
- });
- element.on('mousedown', function(event) {
- element.addClass(ACTIVE_CLASS_NAME);
- });
- element.on('mousemove mouseup', function(event) {
- element.removeClass(ACTIVE_CLASS_NAME);
- });
- };
- }];
- * @ngdoc directive
- * @name ngSwipeLeft
- *
- * @description
- * Specify custom behavior when an element is swiped to the left on a touchscreen device.
- * A leftward swipe is a quick, right-to-left slide of the finger.
- * Though ngSwipeLeft is designed for touch-based devices, it will work with a mouse click and drag
- * too.
- *
- * To disable the mouse click and drag functionality, add `ng-swipe-disable-mouse` to
- * the `ng-swipe-left` or `ng-swipe-right` DOM Element.
- *
- * Requires the {@link ngTouch `ngTouch`} module to be installed.
- *
- * @element ANY
- * @param {expression} ngSwipeLeft {@link guide/expression Expression} to evaluate
- * upon left swipe. (Event object is available as `$event`)
- *
- * @example
- <example module="ngSwipeLeftExample" deps="angular-touch.js">
- <file name="index.html">
- <div ng-show="!showActions" ng-swipe-left="showActions = true">
- Some list content, like an email in the inbox
- </div>
- <div ng-show="showActions" ng-swipe-right="showActions = false">
- <button ng-click="reply()">Reply</button>
- <button ng-click="delete()">Delete</button>
- </div>
- </file>
- <file name="script.js">
- angular.module('ngSwipeLeftExample', ['ngTouch']);
- </file>
- </example>
- */
- * @ngdoc directive
- * @name ngSwipeRight
- *
- * @description
- * Specify custom behavior when an element is swiped to the right on a touchscreen device.
- * A rightward swipe is a quick, left-to-right slide of the finger.
- * Though ngSwipeRight is designed for touch-based devices, it will work with a mouse click and drag
- * too.
- *
- * Requires the {@link ngTouch `ngTouch`} module to be installed.
- *
- * @element ANY
- * @param {expression} ngSwipeRight {@link guide/expression Expression} to evaluate
- * upon right swipe. (Event object is available as `$event`)
- *
- * @example
- <example module="ngSwipeRightExample" deps="angular-touch.js">
- <file name="index.html">
- <div ng-show="!showActions" ng-swipe-left="showActions = true">
- Some list content, like an email in the inbox
- </div>
- <div ng-show="showActions" ng-swipe-right="showActions = false">
- <button ng-click="reply()">Reply</button>
- <button ng-click="delete()">Delete</button>
- </div>
- </file>
- <file name="script.js">
- angular.module('ngSwipeRightExample', ['ngTouch']);
- </file>
- </example>
- */
- function makeSwipeDirective(directiveName, direction, eventName) {
- ngTouch.directive(directiveName, ['$parse', '$swipe', function($parse, $swipe) {
-
- var MAX_VERTICAL_DISTANCE = 75;
-
- var MAX_VERTICAL_RATIO = 0.3;
-
- var MIN_HORIZONTAL_DISTANCE = 30;
- return function(scope, element, attr) {
- var swipeHandler = $parse(attr[directiveName]);
- var startCoords, valid;
- function validSwipe(coords) {
-
-
-
-
-
-
-
-
- if (!startCoords) return false;
- var deltaY = Math.abs(coords.y - startCoords.y);
- var deltaX = (coords.x - startCoords.x) * direction;
- return valid &&
- deltaY < MAX_VERTICAL_DISTANCE &&
- deltaX > 0 &&
- deltaX > MIN_HORIZONTAL_DISTANCE &&
- deltaY / deltaX < MAX_VERTICAL_RATIO;
- }
- var pointerTypes = ['touch'];
- if (!angular.isDefined(attr['ngSwipeDisableMouse'])) {
- pointerTypes.push('mouse');
- }
- $swipe.bind(element, {
- 'start': function(coords, event) {
- startCoords = coords;
- valid = true;
- },
- 'cancel': function(event) {
- valid = false;
- },
- 'end': function(coords, event) {
- if (validSwipe(coords)) {
- scope.$apply(function() {
- element.triggerHandler(eventName);
- swipeHandler(scope, {$event: event});
- });
- }
- }
- }, pointerTypes);
- };
- }]);
- }
- makeSwipeDirective('ngSwipeLeft', -1, 'swipeleft');
- makeSwipeDirective('ngSwipeRight', 1, 'swiperight');
- })(window, window.angular);
|