Blame view

src/components/select/dropdown.vue 4.22 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
  
7bafe9d9   梁灏   fixes #4453 #4480...
10
11
      import { transferIndex, transferIncrease } from '../../utils/transfer-queue';
  
e355dd49   梁灏   add Select Component
12
      export default {
fd1582c5   梁灏   support Menu & La...
13
          name: 'Drop',
ab8aaf95   梁灏   add Dropdown comp...
14
15
16
17
          props: {
              placement: {
                  type: String,
                  default: 'bottom-start'
8105945f   梁灏   update ColorPicker
18
19
20
              },
              className: {
                  type: String
7bafe9d9   梁灏   fixes #4453 #4480...
21
22
23
              },
              transfer: {
                  type: Boolean
ab8aaf95   梁灏   add Dropdown comp...
24
25
              }
          },
e355dd49   梁灏   add Select Component
26
27
          data () {
              return {
8f5b1686   梁灏   fixed #196
28
                  popper: null,
e6edc172   miomio-xiao   fix select dropdo...
29
                  width: '',
7bafe9d9   梁灏   fixes #4453 #4480...
30
31
                  popperStatus: false,
                  tIndex: this.handleGetIndex()
b0893113   jingsam   :art: add eslint
32
              };
e355dd49   梁灏   add Select Component
33
          },
8f5b1686   梁灏   fixed #196
34
35
36
          computed: {
              styles () {
                  let style = {};
74ef0a6a   梁灏   fixed #4194
37
                  if (this.width) style.minWidth = `${this.width}px`;
7bafe9d9   梁灏   fixes #4453 #4480...
38
39
40
  
                  if (this.transfer) style['z-index'] = 1060 + this.tIndex;
  
8f5b1686   梁灏   fixed #196
41
42
43
                  return style;
              }
          },
e355dd49   梁灏   add Select Component
44
45
          methods: {
              update () {
a68c11a5   梁灏   support Nuxt.js
46
                  if (isServer) return;
e355dd49   梁灏   add Select Component
47
48
49
                  if (this.popper) {
                      this.$nextTick(() => {
                          this.popper.update();
e6edc172   miomio-xiao   fix select dropdo...
50
                          this.popperStatus = true;
e355dd49   梁灏   add Select Component
51
52
53
                      });
                  } else {
                      this.$nextTick(() => {
b1c118d8   梁灏   support Dropdown
54
                          this.popper = new Popper(this.$parent.$refs.reference, this.$el, {
ab8aaf95   梁灏   add Dropdown comp...
55
                              placement: this.placement,
130ea92a   huanghong   update popper.js
56
57
                              modifiers: {
                                  computeStyle:{
61339d63   梁灏   update dropdown.v...
58
59
60
                                      gpuAcceleration: false
                                  },
                                  preventOverflow :{
354254b4   梁灏   change popper.js ...
61
                                      boundariesElement: 'window'
130ea92a   huanghong   update popper.js
62
                                  }
e09b07b7   huanghong   解决drop弹出动画异常
63
64
65
                              },
                              onCreate:()=>{
                                  this.resetTransformOrigin();
347815f7   huanghong   update dropdown
66
                                  this.$nextTick(this.popper.update());
3d1fa034   huanghong   解决dropdown弹出来之后改变...
67
68
69
                              },
                              onUpdate:()=>{
                                  this.resetTransformOrigin();
130ea92a   huanghong   update popper.js
70
                              }
e355dd49   梁灏   add Select Component
71
72
73
                          });
                      });
                  }
8f5b1686   梁灏   fixed #196
74
75
76
77
                  // 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'));
                  }
7bafe9d9   梁灏   fixes #4453 #4480...
78
                  this.tIndex = this.handleGetIndex();
e355dd49   梁灏   add Select Component
79
80
81
              },
              destroy () {
                  if (this.popper) {
e355dd49   梁灏   add Select Component
82
                      setTimeout(() => {
e6edc172   miomio-xiao   fix select dropdo...
83
                          if (this.popper && !this.popperStatus) {
e845e84d   陈峰   Update dropdown.vue
84
85
86
                              this.popper.destroy();
                              this.popper = null;
                          }
e6edc172   miomio-xiao   fix select dropdo...
87
                          this.popperStatus = false;
e355dd49   梁灏   add Select Component
88
89
                      }, 300);
                  }
e09b07b7   huanghong   解决drop弹出动画异常
90
91
              },
              resetTransformOrigin() {
8c82b6f2   梁灏   update Select
92
93
94
                  // 不判断,Select 会报错,不知道为什么
                  if (!this.popper) return;
  
dab9c3f6   huanghong   update popper ani...
95
96
97
98
99
100
101
                  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';
                  }
7bafe9d9   梁灏   fixes #4453 #4480...
102
103
104
105
106
              },
              handleGetIndex () {
                  transferIncrease();
                  return transferIndex;
              },
e355dd49   梁灏   add Select Component
107
          },
b1c118d8   梁灏   support Dropdown
108
          created () {
e355dd49   梁灏   add Select Component
109
110
111
112
113
114
115
116
              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
117
118
      };
  </script>