Blame view

components/base/popper.js 2.86 KB
7570318b   梁灏   fixed Tooltip pla...
1
2
3
  /**
   * https://github.com/freeze-component/vue-popper
   * */
dce3e753   梁灏   add Tooltip compo...
4
5
  import Popper from 'popper.js';
  
dce3e753   梁灏   add Tooltip compo...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  export default {
      props: {
          placement: {
              type: String,
              default: 'bottom'
          },
          boundariesPadding: {
              type: Number,
              default: 5
          },
          reference: Object,
          popper: Object,
          offset: {
              default: 0
          },
          value: Boolean,
dce3e753   梁灏   add Tooltip compo...
22
23
24
25
          transition: String,
          options: {
              type: Object,
              default () {
7570318b   梁灏   fixed Tooltip pla...
26
                  return {
755df66c   梁灏   update Tooltip
27
28
29
                      gpuAcceleration: false,
                      boundariesElement: 'body'
                  }
dce3e753   梁灏   add Tooltip compo...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
              }
          }
      },
      data() {
          return {
              showPopper: false
          };
      },
      watch: {
          value: {
              immediate: true,
              handler(val) {
                  this.showPopper = val;
                  this.$emit('input', val);
              }
          },
          showPopper(val) {
              val ? this.updatePopper() : this.destroyPopper();
              this.$emit('input', val);
          }
      },
      methods: {
          createPopper() {
              if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
                  return;
              }
  
              const options = this.options;
              const popper = this.popper || this.$els.popper;
              const reference = this.reference || this.$els.reference;
  
              if (!popper || !reference) return;
dce3e753   梁灏   add Tooltip compo...
62
63
64
65
66
67
68
69
  
              if (this.popperJS && this.popperJS.hasOwnProperty('destroy')) {
                  this.popperJS.destroy();
              }
  
              options.placement = this.placement;
              options.offset = this.offset;
  
755df66c   梁灏   update Tooltip
70
71
72
73
74
              this.popperJS = new Popper(reference, popper, options);
              this.popperJS.onCreate(popper => {
                  this.resetTransformOrigin(popper);
                  this.$nextTick(this.updatePopper);
                  this.$emit('created', this);
dce3e753   梁灏   add Tooltip compo...
75
76
77
              });
          },
          updatePopper() {
755df66c   梁灏   update Tooltip
78
              this.popperJS ? this.popperJS.update() : this.createPopper();
dce3e753   梁灏   add Tooltip compo...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
          },
          doDestroy() {
              if (this.showPopper) return;
              this.popperJS.destroy();
              this.popperJS = null;
          },
          destroyPopper() {
              if (this.popperJS) {
                  this.resetTransformOrigin(this.popperJS);
              }
          },
          resetTransformOrigin(popper) {
              let placementMap = {top: 'bottom', bottom: 'top', left: 'right', right: 'left'};
              let placement = popper._popper.getAttribute('x-placement').split('-')[0];
              let origin = placementMap[placement];
              popper._popper.style.transformOrigin = ['top', 'bottom'].indexOf(placement) > -1 ? `center ${ origin }` : `${ origin } center`;
dce3e753   梁灏   add Tooltip compo...
95
96
97
98
99
100
101
102
          }
      },
      beforeDestroy() {
          if (this.popperJS) {
              this.popperJS.destroy();
          }
      }
  };