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.5KB

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