OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 |
| 7 <!-- |
| 8 /** |
| 9 * @module Polymer UI Elements |
| 10 */ |
| 11 /** |
| 12 * polymer-ui-card <b>(WIP)</b> |
| 13 * |
| 14 * Example: |
| 15 * |
| 16 * <polymer-ui-card> |
| 17 * ... |
| 18 * </polymer-ui-card> |
| 19 * |
| 20 * @class polymer-ui-card |
| 21 */ |
| 22 /** |
| 23 * Fired when the card is swiped away. |
| 24 * |
| 25 * @event polymer-card-swipe-away |
| 26 */ |
| 27 --> |
| 28 <link rel="import" href="../polymer/polymer.html"> |
| 29 |
| 30 <polymer-element name="polymer-ui-card" attributes="swipeable noCurve" |
| 31 on-trackstart="{{trackStart}}" on-track="{{track}}" on-trackend="{{trackEnd}
}" on-flick="{{flick}}"> |
| 32 <template> |
| 33 <link rel="stylesheet" href="polymer-ui-card.css"> |
| 34 <content></content> |
| 35 </template> |
| 36 <script> |
| 37 Polymer('polymer-ui-card', { |
| 38 /** |
| 39 * If true, the card can be swiped. |
| 40 * |
| 41 * @attribute swipeable |
| 42 * @type boolean |
| 43 * @default false |
| 44 */ |
| 45 swipeable: false, |
| 46 noCurve: false, |
| 47 offsetRatio: 0.2, |
| 48 widthRatio: 3, |
| 49 ready: function() { |
| 50 this.setAttribute('touch-action', 'pan-y'); |
| 51 this.transitionEndListener = this.transitionEnd.bind(this); |
| 52 }, |
| 53 leftView: function() { |
| 54 this.removeListeners(); |
| 55 }, |
| 56 addListeners: function() { |
| 57 this.addEventListener('webkitTransitionEnd', |
| 58 this.transitionEndListener); |
| 59 this.addEventListener('transitionend', this.transitionEndListener); |
| 60 }, |
| 61 removeListeners: function() { |
| 62 this.removeEventListener('webkitTransitionEnd', |
| 63 this.transitionEndListener); |
| 64 this.removeEventListener('transitionend', this.transitionEndListener); |
| 65 }, |
| 66 swipeableChanged: function() { |
| 67 if (this.swipeable) { |
| 68 this.addListeners(); |
| 69 } else { |
| 70 this.removeListeners(); |
| 71 } |
| 72 }, |
| 73 animate: function(x) { |
| 74 var s = this.style; |
| 75 var d = x > 0 ? 1 : -1; |
| 76 var w = this.w * this.widthRatio; |
| 77 var x1 = Math.max(0, Math.abs(x) - this.w * this.offsetRatio); |
| 78 var r = Math.max(0, (w - x1) / w); |
| 79 var y = w - Math.sqrt(w * w - x1 * x1); |
| 80 var deg = (1 - r) * d * 90; |
| 81 s.opacity = r; |
| 82 var translate = 'translate3d(' + x + 'px,' + |
| 83 (this.noCurve ? 0 : y) + 'px,0)'; |
| 84 var rotate = 'rotate(' + deg + 'deg)'; |
| 85 s.webkitTransform = s.mozTransform = s.msTransform = s.transform = |
| 86 translate + (this.noCurve ? '' : ' ' + rotate); |
| 87 }, |
| 88 trackStart: function(e) { |
| 89 if (this.swipeable) { |
| 90 e.preventTap(); |
| 91 this.w = this.offsetWidth; |
| 92 this.classList.add('dragging'); |
| 93 } |
| 94 }, |
| 95 track: function(e) { |
| 96 if (this.swipeable) { |
| 97 this.animate(e.dx); |
| 98 } |
| 99 }, |
| 100 trackEnd: function(e) { |
| 101 if (this.swipeable) { |
| 102 this.swipeEnd(Math.abs(e.dx) > this.w / 2 && e.dx * e.xDirection > 0, |
| 103 e.dx > 0); |
| 104 } |
| 105 }, |
| 106 flick: function(e) { |
| 107 if (this.swipeable && !this.away) { |
| 108 var v = e.xVelocity; |
| 109 this.swipeEnd(Math.abs(v) > 2, v > 0); |
| 110 } |
| 111 }, |
| 112 swipeEnd: function(away, dir) { |
| 113 this.classList.remove('dragging'); |
| 114 this.away = away; |
| 115 if (away) { |
| 116 var w = this.w * this.widthRatio; |
| 117 this.animate(dir ? w : -w); |
| 118 } else { |
| 119 this.animate(0); |
| 120 } |
| 121 }, |
| 122 transitionEnd: function() { |
| 123 if (this.away) { |
| 124 this.fire('polymer-card-swipe-away'); |
| 125 } |
| 126 } |
| 127 }); |
| 128 </script> |
| 129 </polymer-element> |
OLD | NEW |