event.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { isServer } from '..';
  2. // eslint-disable-next-line import/no-mutable-exports
  3. export var supportsPassive = false;
  4. if (!isServer) {
  5. try {
  6. var opts = {};
  7. Object.defineProperty(opts, 'passive', {
  8. // eslint-disable-next-line getter-return
  9. get: function get() {
  10. /* istanbul ignore next */
  11. supportsPassive = true;
  12. }
  13. });
  14. window.addEventListener('test-passive', null, opts); // eslint-disable-next-line no-empty
  15. } catch (e) {}
  16. }
  17. export function on(target, event, handler, passive) {
  18. if (passive === void 0) {
  19. passive = false;
  20. }
  21. if (!isServer) {
  22. target.addEventListener(event, handler, supportsPassive ? {
  23. capture: false,
  24. passive: passive
  25. } : false);
  26. }
  27. }
  28. export function off(target, event, handler) {
  29. if (!isServer) {
  30. target.removeEventListener(event, handler);
  31. }
  32. }
  33. export function stopPropagation(event) {
  34. event.stopPropagation();
  35. }
  36. export function preventDefault(event, isStopPropagation) {
  37. /* istanbul ignore else */
  38. if (typeof event.cancelable !== 'boolean' || event.cancelable) {
  39. event.preventDefault();
  40. }
  41. if (isStopPropagation) {
  42. stopPropagation(event);
  43. }
  44. }