close-on-popstate.js 902 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { on, off } from '../utils/dom/event';
  2. import { BindEventMixin } from './bind-event';
  3. export var CloseOnPopstateMixin = {
  4. mixins: [BindEventMixin(function (bind, isBind) {
  5. this.handlePopstate(isBind && this.closeOnPopstate);
  6. })],
  7. props: {
  8. closeOnPopstate: Boolean
  9. },
  10. data: function data() {
  11. return {
  12. bindStatus: false
  13. };
  14. },
  15. watch: {
  16. closeOnPopstate: function closeOnPopstate(val) {
  17. this.handlePopstate(val);
  18. }
  19. },
  20. methods: {
  21. onPopstate: function onPopstate() {
  22. this.close();
  23. this.shouldReopen = false;
  24. },
  25. handlePopstate: function handlePopstate(bind) {
  26. /* istanbul ignore if */
  27. if (this.$isServer) {
  28. return;
  29. }
  30. if (this.bindStatus !== bind) {
  31. this.bindStatus = bind;
  32. var action = bind ? on : off;
  33. action(window, 'popstate', this.onPopstate);
  34. }
  35. }
  36. }
  37. };