forked from googlearchive/swipeable-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipeable-card.html
132 lines (112 loc) · 3.46 KB
/
swipeable-card.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
A card that can be swiped to the left or right to move it out of the way.
@element swipeable-card
@homepage github.io
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="stylesheet" href="swipeable-card.css" shim-shadowdom>
<polymer-element name="swipeable-card" attributes="disableSwipe swiped">
<script>
Polymer('swipeable-card', {
/**
* Fired when the card is swiped away.
*
* @event swipeable-card-swipe-away
*/
/**
* If true, the card can not be swiped.
*
* @attribute disableSwipe
* @type boolean
* @default false
*/
disableSwipe: false,
/**
* Becomes `true` once the card has been swiped. Set
* back to `false` to reset the swipe state.
*
* @attribute swiped
* @type boolean
* @default false
*/
swiped: false,
noCurve: false,
offsetRatio: 0.2,
widthRatio: 3,
eventDelegates: {
webkitTransitionEnd: 'transitionEnd',
transitionend: 'transitionEnd',
trackstart: 'trackStart',
trackx: 'trackx',
trackend: 'trackEnd'
},
animate: function(x) {
var s = this.style;
var d = x > 0 ? 1 : -1;
var w = this._w * this.widthRatio;
var x1 = Math.max(0, Math.abs(x) - this._w * this.offsetRatio);
var r = Math.max(0, (w - x1) / w);
var y = w - Math.sqrt(w * w - x1 * x1);
var deg = (1 - r) * d * 90;
s.opacity = r;
var translate = 'translate3d(' + x + 'px,' +
(this.noCurve ? 0 : y) + 'px,0)';
var rotate = 'rotate(' + deg + 'deg)';
s.transform = s.webkitTransform =
translate + (this.noCurve ? '' : ' ' + rotate);
},
trackStart: function(e) {
if (!this.disableSwipe) {
e.preventTap();
this._w = this.offsetWidth;
this.classList.add('dragging');
}
},
trackx: function(e) {
if (!this.disableSwipe) {
this.animate(e.dx);
}
},
trackEnd: function(e) {
if (!this.disableSwipe) {
this.swipeEnd(Math.abs(e.dx) > this._w / 2 && e.dx * e.xDirection > 0,
e.dx > 0);
}
},
swipeEnd: function(away, dir) {
this.classList.remove('dragging');
this.away = away;
this.direction = dir ? 'right' : 'left';
if (away) {
var w = this._w * this.widthRatio;
this.animate(dir ? w : -w);
} else {
this.animate(0);
}
},
transitionEnd: function(e) {
if (this.away && e.propertyName == 'opacity') {
this.fire('swipeable-card-swipe-away', {direction: this.direction});
this.swiped = true;
}
e.stopPropagation();
},
swipedChanged: function() {
if (this.away != this.swiped) {
this.away = this.swiped;
this.classList.add('dragging');
this._w = this.offsetWidth;
this.animate(this.swiped ? this._w * this.widthRatio : 0);
}
}
});
</script>
</polymer-element>