Blame view

src/components/select/dropdown.vue 3.78 KB
e355dd49   梁灏   add Select Component
1
  <template>
8105945f   梁灏   update ColorPicker
2
      <div class="ivu-select-dropdown" :class="className" :style="styles"><slot></slot></div>
e355dd49   梁灏   add Select Component
3
4
  </template>
  <script>
a68c11a5   梁灏   support Nuxt.js
5
6
      import Vue from 'vue';
      const isServer = Vue.prototype.$isServer;
8f5b1686   梁灏   fixed #196
7
      import { getStyle } from '../../utils/assist';
11d38676   Aresn   SSR support
8
      const Popper = isServer ? function() {} : require('popper.js/dist/umd/popper.js');  // eslint-disable-line
e355dd49   梁灏   add Select Component
9
10
  
      export default {
fd1582c5   梁灏   support Menu & La...
11
          name: 'Drop',
ab8aaf95   梁灏   add Dropdown comp...
12
13
14
15
          props: {
              placement: {
                  type: String,
                  default: 'bottom-start'
8105945f   梁灏   update ColorPicker
16
17
18
              },
              className: {
                  type: String
ab8aaf95   梁灏   add Dropdown comp...
19
20
              }
          },
e355dd49   梁灏   add Select Component
21
22
          data () {
              return {
8f5b1686   梁灏   fixed #196
23
                  popper: null,
e6edc172   miomio-xiao   fix select dropdo...
24
25
                  width: '',
                  popperStatus: false
b0893113   jingsam   :art: add eslint
26
              };
e355dd49   梁灏   add Select Component
27
          },
8f5b1686   梁灏   fixed #196
28
29
30
          computed: {
              styles () {
                  let style = {};
74ef0a6a   梁灏   fixed #4194
31
                  if (this.width) style.minWidth = `${this.width}px`;
8f5b1686   梁灏   fixed #196
32
33
34
                  return style;
              }
          },
e355dd49   梁灏   add Select Component
35
36
          methods: {
              update () {
a68c11a5   梁灏   support Nuxt.js
37
                  if (isServer) return;
e355dd49   梁灏   add Select Component
38
39
40
                  if (this.popper) {
                      this.$nextTick(() => {
                          this.popper.update();
e6edc172   miomio-xiao   fix select dropdo...
41
                          this.popperStatus = true;
e355dd49   梁灏   add Select Component
42
43
44
                      });
                  } else {
                      this.$nextTick(() => {
b1c118d8   梁灏   support Dropdown
45
                          this.popper = new Popper(this.$parent.$refs.reference, this.$el, {
ab8aaf95   梁灏   add Dropdown comp...
46
                              placement: this.placement,
130ea92a   huanghong   update popper.js
47
48
                              modifiers: {
                                  computeStyle:{
61339d63   梁灏   update dropdown.v...
49
50
51
                                      gpuAcceleration: false
                                  },
                                  preventOverflow :{
354254b4   梁灏   change popper.js ...
52
                                      boundariesElement: 'window'
130ea92a   huanghong   update popper.js
53
                                  }
e09b07b7   huanghong   解决drop弹出动画异常
54
55
56
                              },
                              onCreate:()=>{
                                  this.resetTransformOrigin();
347815f7   huanghong   update dropdown
57
                                  this.$nextTick(this.popper.update());
3d1fa034   huanghong   解决dropdown弹出来之后改变...
58
59
60
                              },
                              onUpdate:()=>{
                                  this.resetTransformOrigin();
130ea92a   huanghong   update popper.js
61
                              }
e355dd49   梁灏   add Select Component
62
63
64
                          });
                      });
                  }
8f5b1686   梁灏   fixed #196
65
66
67
68
                  // set a height for parent is Modal and Select's width is 100%
                  if (this.$parent.$options.name === 'iSelect') {
                      this.width = parseInt(getStyle(this.$parent.$el, 'width'));
                  }
e355dd49   梁灏   add Select Component
69
70
71
              },
              destroy () {
                  if (this.popper) {
e355dd49   梁灏   add Select Component
72
                      setTimeout(() => {
e6edc172   miomio-xiao   fix select dropdo...
73
                          if (this.popper && !this.popperStatus) {
e845e84d   陈峰   Update dropdown.vue
74
75
76
                              this.popper.destroy();
                              this.popper = null;
                          }
e6edc172   miomio-xiao   fix select dropdo...
77
                          this.popperStatus = false;
e355dd49   梁灏   add Select Component
78
79
                      }, 300);
                  }
e09b07b7   huanghong   解决drop弹出动画异常
80
81
              },
              resetTransformOrigin() {
8c82b6f2   梁灏   update Select
82
83
84
                  // 不判断,Select 会报错,不知道为什么
                  if (!this.popper) return;
  
dab9c3f6   huanghong   update popper ani...
85
86
87
88
89
90
91
                  let x_placement = this.popper.popper.getAttribute('x-placement');
                  let placementStart = x_placement.split('-')[0];
                  let placementEnd = x_placement.split('-')[1];
                  const leftOrRight = x_placement === 'left' || x_placement === 'right';
                  if(!leftOrRight){
                      this.popper.popper.style.transformOrigin = placementStart==='bottom' || ( placementStart !== 'top' && placementEnd === 'start') ? 'center top' : 'center bottom';
                  }
e355dd49   梁灏   add Select Component
92
93
              }
          },
b1c118d8   梁灏   support Dropdown
94
          created () {
e355dd49   梁灏   add Select Component
95
96
97
98
99
100
101
102
              this.$on('on-update-popper', this.update);
              this.$on('on-destroy-popper', this.destroy);
          },
          beforeDestroy () {
              if (this.popper) {
                  this.popper.destroy();
              }
          }
b0893113   jingsam   :art: add eslint
103
104
      };
  </script>