Blame view

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