Blame view

src/components/base/popper.js 3.54 KB
7570318b   梁灏   fixed Tooltip pla...
1
2
3
  /**
   * https://github.com/freeze-component/vue-popper
   * */
a68c11a5   梁灏   support Nuxt.js
4
5
6
  import Vue from 'vue';
  const isServer = Vue.prototype.$isServer;
  const Popper = isServer ? function() {} : require('popper.js');  // eslint-disable-line
dce3e753   梁灏   add Tooltip compo...
7
  
dce3e753   梁灏   add Tooltip compo...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  export default {
      props: {
          placement: {
              type: String,
              default: 'bottom'
          },
          boundariesPadding: {
              type: Number,
              default: 5
          },
          reference: Object,
          popper: Object,
          offset: {
              default: 0
          },
d6f644e1   梁灏   support Tooltip
23
24
25
26
          value: {
              type: Boolean,
              default: false
          },
dce3e753   梁灏   add Tooltip compo...
27
28
29
30
          transition: String,
          options: {
              type: Object,
              default () {
7570318b   梁灏   fixed Tooltip pla...
31
                  return {
755df66c   梁灏   update Tooltip
32
                      gpuAcceleration: false,
1c803cdf   梁灏   support Slider
33
                      boundariesElement: 'body'    // todo 暂时注释,发现在 vue 2 里方向暂时可以自动识别了,待验证(还是有问题的)
b0893113   jingsam   :art: add eslint
34
                  };
dce3e753   梁灏   add Tooltip compo...
35
              }
9699c270   梁灏   add Poptip component
36
          },
d6f644e1   梁灏   support Tooltip
37
38
39
40
41
42
43
44
          // visible: {
          //     type: Boolean,
          //     default: false
          // }
      },
      data () {
          return {
              visible: this.value
79288d43   梁灏   support Poptip & ...
45
          };
dce3e753   梁灏   add Tooltip compo...
46
      },
dce3e753   梁灏   add Tooltip compo...
47
48
49
50
      watch: {
          value: {
              immediate: true,
              handler(val) {
9699c270   梁灏   add Poptip component
51
                  this.visible = val;
dce3e753   梁灏   add Tooltip compo...
52
53
54
                  this.$emit('input', val);
              }
          },
9699c270   梁灏   add Poptip component
55
          visible(val) {
7f1edb6a   梁灏   Poptip、Tooltip ad...
56
57
58
59
60
61
              if (val) {
                  this.updatePopper();
              } else {
                  this.destroyPopper();
                  this.$emit('on-popper-hide');
              }
dce3e753   梁灏   add Tooltip compo...
62
63
64
65
66
              this.$emit('input', val);
          }
      },
      methods: {
          createPopper() {
a68c11a5   梁灏   support Nuxt.js
67
              if (isServer) return;
dce3e753   梁灏   add Tooltip compo...
68
69
70
71
72
              if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
                  return;
              }
  
              const options = this.options;
d6f644e1   梁灏   support Tooltip
73
74
              const popper = this.popper || this.$refs.popper;
              const reference = this.reference || this.$refs.reference;
dce3e753   梁灏   add Tooltip compo...
75
76
  
              if (!popper || !reference) return;
dce3e753   梁灏   add Tooltip compo...
77
78
79
80
81
82
83
84
  
              if (this.popperJS && this.popperJS.hasOwnProperty('destroy')) {
                  this.popperJS.destroy();
              }
  
              options.placement = this.placement;
              options.offset = this.offset;
  
755df66c   梁灏   update Tooltip
85
86
87
88
89
              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...
90
91
92
              });
          },
          updatePopper() {
a68c11a5   梁灏   support Nuxt.js
93
              if (isServer) return;
755df66c   梁灏   update Tooltip
94
              this.popperJS ? this.popperJS.update() : this.createPopper();
dce3e753   梁灏   add Tooltip compo...
95
96
          },
          doDestroy() {
a68c11a5   梁灏   support Nuxt.js
97
              if (isServer) return;
9699c270   梁灏   add Poptip component
98
              if (this.visible) return;
dce3e753   梁灏   add Tooltip compo...
99
100
101
102
              this.popperJS.destroy();
              this.popperJS = null;
          },
          destroyPopper() {
a68c11a5   梁灏   support Nuxt.js
103
              if (isServer) return;
dce3e753   梁灏   add Tooltip compo...
104
105
106
107
108
              if (this.popperJS) {
                  this.resetTransformOrigin(this.popperJS);
              }
          },
          resetTransformOrigin(popper) {
a68c11a5   梁灏   support Nuxt.js
109
              if (isServer) return;
dce3e753   梁灏   add Tooltip compo...
110
111
112
113
              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...
114
115
116
          }
      },
      beforeDestroy() {
a68c11a5   梁灏   support Nuxt.js
117
          if (isServer) return;
dce3e753   梁灏   add Tooltip compo...
118
119
120
121
          if (this.popperJS) {
              this.popperJS.destroy();
          }
      }
b0893113   jingsam   :art: add eslint
122
  };