click-outside.js 824 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Listen to click outside event
  3. */
  4. import { on, off } from '../utils/dom/event';
  5. export var ClickOutsideMixin = function ClickOutsideMixin(config) {
  6. return {
  7. props: {
  8. closeOnClickOutside: {
  9. type: Boolean,
  10. default: true
  11. }
  12. },
  13. data: function data() {
  14. var _this = this;
  15. var clickOutsideHandler = function clickOutsideHandler(event) {
  16. if (_this.closeOnClickOutside && !_this.$el.contains(event.target)) {
  17. _this[config.method]();
  18. }
  19. };
  20. return {
  21. clickOutsideHandler: clickOutsideHandler
  22. };
  23. },
  24. mounted: function mounted() {
  25. on(document, config.event, this.clickOutsideHandler);
  26. },
  27. beforeDestroy: function beforeDestroy() {
  28. off(document, config.event, this.clickOutsideHandler);
  29. }
  30. };
  31. };