Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ripple.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { consoleWarn } from '../util/console';
  2. function transform(el, value) {
  3. el.style['transform'] = value;
  4. el.style['webkitTransform'] = value;
  5. }
  6. function opacity(el, value) {
  7. el.style['opacity'] = value.toString();
  8. }
  9. function isTouchEvent(e) {
  10. return e.constructor.name === 'TouchEvent';
  11. }
  12. var calculate = function calculate(e, el) {
  13. var value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  14. var offset = el.getBoundingClientRect();
  15. var target = isTouchEvent(e) ? e.touches[e.touches.length - 1] : e;
  16. var localX = target.clientX - offset.left;
  17. var localY = target.clientY - offset.top;
  18. var radius = 0;
  19. var scale = 0.3;
  20. if (el._ripple && el._ripple.circle) {
  21. scale = 0.15;
  22. radius = el.clientWidth / 2;
  23. radius = value.center ? radius : radius + Math.sqrt(Math.pow(localX - radius, 2) + Math.pow(localY - radius, 2)) / 4;
  24. } else {
  25. radius = Math.sqrt(Math.pow(el.clientWidth, 2) + Math.pow(el.clientHeight, 2)) / 2;
  26. }
  27. var centerX = (el.clientWidth - radius * 2) / 2 + 'px';
  28. var centerY = (el.clientHeight - radius * 2) / 2 + 'px';
  29. var x = value.center ? centerX : localX - radius + 'px';
  30. var y = value.center ? centerY : localY - radius + 'px';
  31. return { radius: radius, scale: scale, x: x, y: y, centerX: centerX, centerY: centerY };
  32. };
  33. var ripple = {
  34. /* eslint-disable max-statements */
  35. show: function show(e, el) {
  36. var value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  37. if (!el._ripple || !el._ripple.enabled) {
  38. return;
  39. }
  40. var container = document.createElement('span');
  41. var animation = document.createElement('span');
  42. container.appendChild(animation);
  43. container.className = 'v-ripple__container';
  44. if (value.class) {
  45. container.className += ' ' + value.class;
  46. }
  47. var _calculate = calculate(e, el, value),
  48. radius = _calculate.radius,
  49. scale = _calculate.scale,
  50. x = _calculate.x,
  51. y = _calculate.y,
  52. centerX = _calculate.centerX,
  53. centerY = _calculate.centerY;
  54. var size = radius * 2 + 'px';
  55. animation.className = 'v-ripple__animation';
  56. animation.style.width = size;
  57. animation.style.height = size;
  58. el.appendChild(container);
  59. var computed = window.getComputedStyle(el);
  60. if (computed && computed.position === 'static') {
  61. el.style.position = 'relative';
  62. el.dataset.previousPosition = 'static';
  63. }
  64. animation.classList.add('v-ripple__animation--enter');
  65. animation.classList.add('v-ripple__animation--visible');
  66. transform(animation, 'translate(' + x + ', ' + y + ') scale3d(' + scale + ',' + scale + ',' + scale + ')');
  67. opacity(animation, 0);
  68. animation.dataset.activated = String(performance.now());
  69. setTimeout(function () {
  70. animation.classList.remove('v-ripple__animation--enter');
  71. animation.classList.add('v-ripple__animation--in');
  72. transform(animation, 'translate(' + centerX + ', ' + centerY + ') scale3d(1,1,1)');
  73. opacity(animation, 0.25);
  74. }, 0);
  75. },
  76. hide: function hide(el) {
  77. if (!el || !el._ripple || !el._ripple.enabled) return;
  78. var ripples = el.getElementsByClassName('v-ripple__animation');
  79. if (ripples.length === 0) return;
  80. var animation = ripples[ripples.length - 1];
  81. if (animation.dataset.isHiding) return;else animation.dataset.isHiding = 'true';
  82. var diff = performance.now() - Number(animation.dataset.activated);
  83. var delay = Math.max(250 - diff, 0);
  84. setTimeout(function () {
  85. animation.classList.remove('v-ripple__animation--in');
  86. animation.classList.add('v-ripple__animation--out');
  87. opacity(animation, 0);
  88. setTimeout(function () {
  89. var ripples = el.getElementsByClassName('v-ripple__animation');
  90. if (ripples.length === 1 && el.dataset.previousPosition) {
  91. el.style.position = el.dataset.previousPosition;
  92. delete el.dataset.previousPosition;
  93. }
  94. animation.parentNode && el.removeChild(animation.parentNode);
  95. }, 300);
  96. }, delay);
  97. }
  98. };
  99. function isRippleEnabled(value) {
  100. return typeof value === 'undefined' || !!value;
  101. }
  102. function rippleShow(e) {
  103. var value = {};
  104. var element = e.currentTarget;
  105. if (!element || !element._ripple || element._ripple.touched) return;
  106. if (isTouchEvent(e)) {
  107. element._ripple.touched = true;
  108. }
  109. value.center = element._ripple.centered;
  110. if (element._ripple.class) {
  111. value.class = element._ripple.class;
  112. }
  113. ripple.show(e, element, value);
  114. }
  115. function rippleHide(e) {
  116. var element = e.currentTarget;
  117. if (!element) return;
  118. window.setTimeout(function () {
  119. if (element._ripple) {
  120. element._ripple.touched = false;
  121. }
  122. });
  123. ripple.hide(element);
  124. }
  125. function updateRipple(el, binding, wasEnabled) {
  126. var enabled = isRippleEnabled(binding.value);
  127. if (!enabled) {
  128. ripple.hide(el);
  129. }
  130. el._ripple = el._ripple || {};
  131. el._ripple.enabled = enabled;
  132. var value = binding.value || {};
  133. if (value.center) {
  134. el._ripple.centered = true;
  135. }
  136. if (value.class) {
  137. el._ripple.class = binding.value.class;
  138. }
  139. if (value.circle) {
  140. el._ripple.circle = value.circle;
  141. }
  142. if (enabled && !wasEnabled) {
  143. el.addEventListener('touchstart', rippleShow, { passive: true });
  144. el.addEventListener('touchend', rippleHide, { passive: true });
  145. el.addEventListener('touchcancel', rippleHide);
  146. el.addEventListener('mousedown', rippleShow);
  147. el.addEventListener('mouseup', rippleHide);
  148. el.addEventListener('mouseleave', rippleHide);
  149. // Anchor tags can be dragged, causes other hides to fail - #1537
  150. el.addEventListener('dragstart', rippleHide, { passive: true });
  151. } else if (!enabled && wasEnabled) {
  152. removeListeners(el);
  153. }
  154. }
  155. function removeListeners(el) {
  156. el.removeEventListener('mousedown', rippleShow);
  157. el.removeEventListener('touchstart', rippleHide);
  158. el.removeEventListener('touchend', rippleHide);
  159. el.removeEventListener('touchcancel', rippleHide);
  160. el.removeEventListener('mouseup', rippleHide);
  161. el.removeEventListener('mouseleave', rippleHide);
  162. el.removeEventListener('dragstart', rippleHide);
  163. }
  164. function directive(el, binding, node) {
  165. updateRipple(el, binding, false);
  166. // warn if an inline element is used, waiting for el to be in the DOM first
  167. node.context && node.context.$nextTick(function () {
  168. var computed = window.getComputedStyle(el);
  169. if (computed && computed.display === 'inline') {
  170. var context = node.fnOptions ? [node.fnOptions, node.context] : [node.componentInstance];
  171. consoleWarn.apply(undefined, ['v-ripple can only be used on block-level elements'].concat(context));
  172. }
  173. });
  174. }
  175. function unbind(el) {
  176. delete el._ripple;
  177. removeListeners(el);
  178. }
  179. function update(el, binding) {
  180. if (binding.value === binding.oldValue) {
  181. return;
  182. }
  183. var wasEnabled = isRippleEnabled(binding.oldValue);
  184. updateRipple(el, binding, wasEnabled);
  185. }
  186. export default {
  187. bind: directive,
  188. unbind: unbind,
  189. update: update
  190. };
  191. //# sourceMappingURL=ripple.js.map