Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 827990e

Browse files
gillybtinayuangao
authored andcommitted
feat(swipe): allow accessing the original currentTarget (#10997)
1 parent b4b9369 commit 827990e

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

src/components/swipe/demoBasicUsage/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<div ng-controller="demoSwipeCtrl" ng-cloak layout="row">
22
<div flex>
3-
<div class="demo-swipe" md-swipe-left="onSwipeLeft()">
3+
<div class="demo-swipe" md-swipe-left="onSwipeLeft($event, $target)">
44
<span class="no-select">Swipe me to the left</span>
55
<md-icon md-font-icon="android" aria-label="android"></md-icon>
66
</div>
77
<md-divider></md-divider>
8-
<div class="demo-swipe" md-swipe-right="onSwipeRight()">
8+
<div class="demo-swipe" md-swipe-right="onSwipeRight($event, $target)">
99
<span class="no-select">Swipe me to the right</span>
1010
</div>
1111
</div>
1212
<md-divider></md-divider>
1313
<div flex layout="row">
1414
<div flex layout="row" layout-align="center center"
15-
class="demo-swipe" md-swipe-up="onSwipeUp()">
15+
class="demo-swipe" md-swipe-up="onSwipeUp($event, $target)">
1616
<span class="no-select">Swipe me up</span>
1717
</div>
1818
<md-divider></md-divider>
1919
<div flex layout="row" layout-align="center center"
20-
class="demo-swipe" md-swipe-down="onSwipeDown()">
20+
class="demo-swipe" md-swipe-down="onSwipeDown($event, $target)">
2121
<span class="no-select">Swipe me down</span>
2222
</div>
2323
</div>
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
angular.module('demoSwipe', ['ngMaterial'])
2-
.controller('demoSwipeCtrl', function($scope) {
3-
$scope.onSwipeLeft = function(ev) {
2+
.controller('demoSwipeCtrl', function($scope, $log) {
3+
$scope.onSwipeLeft = function(ev, target) {
44
alert('You swiped left!!');
5+
6+
$log.log('Event Target: ', ev.target);
7+
$log.log('Event Current Target: ', ev.currentTarget);
8+
$log.log('Original Current Target: ', target.current);
59
};
610

7-
$scope.onSwipeRight = function(ev) {
11+
$scope.onSwipeRight = function(ev, target) {
812
alert('You swiped right!!');
13+
14+
$log.log('Event Target: ', ev.target);
15+
$log.log('Event Current Target: ', ev.currentTarget);
16+
$log.log('Original Current Target: ', target.current);
917
};
10-
$scope.onSwipeUp = function(ev) {
18+
$scope.onSwipeUp = function(ev, target) {
1119
alert('You swiped up!!');
20+
21+
$log.log('Event Target: ', ev.target);
22+
$log.log('Event Current Target: ', ev.currentTarget);
23+
$log.log('Original Current Target: ', target.current);
1224
};
1325

14-
$scope.onSwipeDown = function(ev) {
26+
$scope.onSwipeDown = function(ev, target) {
1527
alert('You swiped down!!');
28+
29+
$log.log('Event Target: ', ev.target);
30+
$log.log('Event Current Target: ', ev.currentTarget);
31+
$log.log('Original Current Target: ', target.current);
1632
};
1733
});

src/components/swipe/swipe.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414
* The md-swipe-left directive allows you to specify custom behavior when an element is swiped
1515
* left.
1616
*
17+
* ### Notes
18+
* - The `$event.currentTarget` of the swiped element will be `null`, but you can get a
19+
* reference to the element that actually holds the `md-swipe-left` directive by using `$target.current`
20+
*
21+
* > You can see this in action on the <a ng-href="demo/swipe">demo page</a> (Look at the Developer Tools console while swiping).
22+
*
1723
* @usage
1824
* <hljs lang="html">
19-
* <div md-swipe-left="onSwipeLeft()">Swipe me left!</div>
25+
* <div md-swipe-left="onSwipeLeft($event, $target)">Swipe me left!</div>
2026
* </hljs>
2127
*/
2228
/**
@@ -30,9 +36,15 @@
3036
* The md-swipe-right directive allows you to specify custom behavior when an element is swiped
3137
* right.
3238
*
39+
* ### Notes
40+
* - The `$event.currentTarget` of the swiped element will be `null`, but you can get a
41+
* reference to the element that actually holds the `md-swipe-right` directive by using `$target.current`
42+
*
43+
* > You can see this in action on the <a ng-href="demo/swipe">demo page</a> (Look at the Developer Tools console while swiping).
44+
*
3345
* @usage
3446
* <hljs lang="html">
35-
* <div md-swipe-right="onSwipeRight()">Swipe me right!</div>
47+
* <div md-swipe-right="onSwipeRight($event, $target)">Swipe me right!</div>
3648
* </hljs>
3749
*/
3850
/**
@@ -46,9 +58,15 @@
4658
* The md-swipe-up directive allows you to specify custom behavior when an element is swiped
4759
* up.
4860
*
61+
* ### Notes
62+
* - The `$event.currentTarget` of the swiped element will be `null`, but you can get a
63+
* reference to the element that actually holds the `md-swipe-up` directive by using `$target.current`
64+
*
65+
* > You can see this in action on the <a ng-href="demo/swipe">demo page</a> (Look at the Developer Tools console while swiping).
66+
*
4967
* @usage
5068
* <hljs lang="html">
51-
* <div md-swipe-up="onSwipeUp()">Swipe me up!</div>
69+
* <div md-swipe-up="onSwipeUp($event, $target)">Swipe me up!</div>
5270
* </hljs>
5371
*/
5472
/**
@@ -62,9 +80,15 @@
6280
* The md-swipe-down directive allows you to specify custom behavior when an element is swiped
6381
* down.
6482
*
83+
* ### Notes
84+
* - The `$event.currentTarget` of the swiped element will be `null`, but you can get a
85+
* reference to the element that actually holds the `md-swipe-down` directive by using `$target.current`
86+
*
87+
* > You can see this in action on the <a ng-href="demo/swipe">demo page</a> (Look at the Developer Tools console while swiping).
88+
*
6589
* @usage
6690
* <hljs lang="html">
67-
* <div md-swipe-down="onSwipDown()">Swipe me down!</div>
91+
* <div md-swipe-down="onSwipDown($event, $target)">Swipe me down!</div>
6892
* </hljs>
6993
*/
7094

@@ -86,7 +110,8 @@ function getDirective(name) {
86110
function postLink(scope, element, attr) {
87111
var fn = $parse(attr[directiveName]);
88112
element.on(eventName, function(ev) {
89-
scope.$applyAsync(function() { fn(scope, { $event: ev }); });
113+
var currentTarget = ev.currentTarget;
114+
scope.$applyAsync(function() { fn(scope, { $event: ev, $target: { current: currentTarget } }); });
90115
});
91116
}
92117
}

0 commit comments

Comments
 (0)