event.js 1.4 KB

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