No Description

swipe-reveal.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  2. import _createClass from 'babel-runtime/helpers/createClass';
  3. /*
  4. Copyright 2013-2015 ASIAL CORPORATION
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. import util from '../../ons/util';
  16. import GestureDetector from '../../ons/gesture-detector';
  17. var widthToPx = function widthToPx(width) {
  18. var _ref = [parseInt(width, 10), /px/.test(width)],
  19. value = _ref[0],
  20. px = _ref[1];
  21. return px ? value : Math.round(document.body.offsetWidth * value / 100);
  22. };
  23. var SwipeReveal = function () {
  24. function SwipeReveal(params) {
  25. var _this = this;
  26. _classCallCheck(this, SwipeReveal);
  27. 'element ignoreSwipe isInitialState onDragCallback swipeMax swipeMin swipeMid'.split(/\s+/).forEach(function (key) {
  28. return _this[key] = params[key];
  29. });
  30. this.elementHandler = params.elementHandler || params.element;
  31. this.getThreshold = params.getThreshold || function () {
  32. return .5;
  33. };
  34. this.getSide = params.getSide || function () {
  35. return 'left';
  36. };
  37. this.handleGesture = this.handleGesture.bind(this);
  38. this._shouldFixScroll = util.globals.actualMobileOS === 'ios';
  39. }
  40. _createClass(SwipeReveal, [{
  41. key: 'update',
  42. value: function update() {
  43. var swipeable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.element.hasAttribute('swipeable');
  44. if (!this.gestureDetector) {
  45. this.gestureDetector = new GestureDetector(this.elementHandler, { dragMinDistance: 1, passive: !this._shouldFixScroll });
  46. }
  47. var action = swipeable ? 'on' : 'off';
  48. this.gestureDetector[action]('drag dragstart dragend', this.handleGesture);
  49. }
  50. }, {
  51. key: 'handleGesture',
  52. value: function handleGesture(e) {
  53. if (e.gesture) {
  54. if (e.type === 'dragstart') {
  55. this.onDragStart(e);
  56. } else if (!this._ignoreDrag) {
  57. e.type === 'dragend' ? this.onDragEnd(e) : this.onDrag(e);
  58. }
  59. }
  60. }
  61. }, {
  62. key: 'onDragStart',
  63. value: function onDragStart(event) {
  64. var _this2 = this;
  65. var getDistance = function getDistance() {
  66. return _this2.getSide() === 'left' ? event.gesture.center.clientX : window.innerWidth - event.gesture.center.clientX;
  67. };
  68. this._ignoreDrag = event.consumed || !util.isValidGesture(event) || this.ignoreSwipe(event, getDistance());
  69. if (!this._ignoreDrag) {
  70. event.consume && event.consume();
  71. event.consumed = true;
  72. this._width = widthToPx(this.element.style.width || '100%');
  73. this._startDistance = this._distance = !(this.isInitialState instanceof Function) || this.isInitialState() ? 0 : this._width;
  74. util.iosPreventScroll(this.gestureDetector);
  75. }
  76. }
  77. }, {
  78. key: 'onDrag',
  79. value: function onDrag(event) {
  80. event.stopPropagation();
  81. var delta = this.getSide() === 'left' ? event.gesture.deltaX : -event.gesture.deltaX;
  82. var distance = Math.max(0, Math.min(this._width, this._startDistance + delta));
  83. if (distance !== this._distance) {
  84. this._distance = distance;
  85. this.swipeMid(this._distance, this._width);
  86. }
  87. }
  88. }, {
  89. key: 'onDragEnd',
  90. value: function onDragEnd(event) {
  91. event.stopPropagation();
  92. var direction = event.gesture.interimDirection;
  93. var isSwipeMax = this.getSide() !== direction && this._distance > this._width * this.getThreshold();
  94. isSwipeMax ? this.swipeMax() : this.swipeMin();
  95. }
  96. }, {
  97. key: 'dispose',
  98. value: function dispose() {
  99. this.gestureDetector && this.gestureDetector.dispose();
  100. this.gestureDetector = this.element = this.elementHandler = null;
  101. }
  102. }]);
  103. return SwipeReveal;
  104. }();
  105. export default SwipeReveal;