Blame view

src/components/drawer/drawer.vue 10.9 KB
c4d780c0   梁灏   init Drawer compo...
1
  <template>
f59e6e89   梁灏   update Drawer
2
3
      <div v-transfer-dom :data-transfer="transfer">
          <transition name="fade">
8a3c7282   梁灏   add inner prop
4
              <div :class="maskClasses" :style="maskStyle" v-show="visible" v-if="mask" @click="handleMask"></div>
f59e6e89   梁灏   update Drawer
5
6
          </transition>
          <div :class="wrapClasses" @click="handleWrapClick">
ab58648e   梁灏   update Drawer
7
8
              <transition :name="'move-' + placement">
                  <div :class="classes" :style="mainStyles" v-show="visible">
1416321b   梁灏   update Drawer
9
                      <div :class="contentClasses" ref="content">
f59e6e89   梁灏   update Drawer
10
11
12
13
14
                          <a class="ivu-drawer-close" v-if="closable" @click="close">
                              <slot name="close">
                                  <Icon type="ios-close"></Icon>
                              </slot>
                          </a>
1416321b   梁灏   update Drawer
15
                          <div :class="[prefixCls + '-header']" v-if="showHead"><slot name="header"><div :class="[prefixCls + '-header-inner']">{{ title }}</div></slot></div>
f59e6e89   梁灏   update Drawer
16
17
                          <div :class="[prefixCls + '-body']" :style="styles"><slot></slot></div>
                      </div>
a17511c3   梁灏   Drawer add dragga...
18
19
20
21
22
23
24
25
26
                      <div class="ivu-drawer-drag" :class="{ 'ivu-drawer-drag-left': placement === 'left' }" v-if="draggable" @mousedown="handleTriggerMousedown">
                          <slot name="trigger">
                              <div class="ivu-drawer-drag-move-trigger">
                                  <div class="ivu-drawer-drag-move-trigger-point">
                                      <i></i><i></i><i></i><i></i><i></i>
                                  </div>
                              </div>
                          </slot>
                      </div>
f59e6e89   梁灏   update Drawer
27
28
29
30
                  </div>
              </transition>
          </div>
      </div>
c4d780c0   梁灏   init Drawer compo...
31
32
  </template>
  <script>
749665ee   梁灏   add props
33
      import Icon from '../icon';
b0fe4f98   梁灏   fix #4831
34
      import { oneOf, findBrothersComponents, findComponentsUpward } from '../../utils/assist';
749665ee   梁灏   add props
35
      import TransferDom from '../../directives/transfer-dom';
ab58648e   梁灏   update Drawer
36
      import Emitter from '../../mixins/emitter';
749665ee   梁灏   add props
37
38
      import ScrollbarMixins from '../modal/mixins-scrollbar';
  
a17511c3   梁灏   Drawer add dragga...
39
40
      import { on, off } from '../../utils/dom';
  
c4d780c0   梁灏   init Drawer compo...
41
42
43
44
      const prefixCls = 'ivu-drawer';
  
      export default {
          name: 'Drawer',
ab58648e   梁灏   update Drawer
45
          mixins: [ Emitter, ScrollbarMixins ],
749665ee   梁灏   add props
46
47
          components: { Icon },
          directives: { TransferDom },
c4d780c0   梁灏   init Drawer compo...
48
          props: {
749665ee   梁灏   add props
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
              value: {
                  type: Boolean,
                  default: false
              },
              title: {
                  type: String
              },
              width: {
                  type: [Number, String],
                  default: 256
              },
              closable: {
                  type: Boolean,
                  default: true
              },
              maskClosable: {
                  type: Boolean,
                  default: true
              },
              mask: {
                  type: Boolean,
                  default: true
              },
              maskStyle: {
                  type: Object
              },
ab58648e   梁灏   update Drawer
75
76
77
              styles: {
                  type: Object
              },
749665ee   梁灏   add props
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
              scrollable: {
                  type: Boolean,
                  default: false
              },
              placement: {
                  validator (value) {
                      return oneOf(value, ['left', 'right']);
                  },
                  default: 'right'
              },
              zIndex: {
                  type: Number,
                  default: 1000
              },
              transfer: {
                  type: Boolean,
                  default () {
                      return !this.$IVIEW || this.$IVIEW.transfer === '' ? true : this.$IVIEW.transfer;
                  }
              },
f59e6e89   梁灏   update Drawer
98
99
              className: {
                  type: String
8a3c7282   梁灏   add inner prop
100
101
102
103
              },
              inner: {
                  type: Boolean,
                  default: false
a17511c3   梁灏   Drawer add dragga...
104
105
106
107
108
              },
              // Whether drag and drop is allowed to adjust width
              draggable: {
                  type: Boolean,
                  default: false
4a8826fa   梁灏   fixed #4895
109
110
              },
              beforeClose: Function,
749665ee   梁灏   add props
111
112
113
114
115
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  visible: this.value,
f59e6e89   梁灏   update Drawer
116
117
                  wrapShow: false,
                  showHead: true,
a17511c3   梁灏   Drawer add dragga...
118
119
120
121
122
                  canMove: false,
                  dragWidth: this.width,
                  wrapperWidth: this.width,
                  wrapperLeft: 0,
                  minWidth: 256
749665ee   梁灏   add props
123
124
              };
          },
f59e6e89   梁灏   update Drawer
125
126
127
128
129
130
131
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrap`,
                      {
                          [`${prefixCls}-hidden`]: !this.wrapShow,
                          [`${this.className}`]: !!this.className,
8a3c7282   梁灏   add inner prop
132
                          [`${prefixCls}-no-mask`]: !this.mask,
a17511c3   梁灏   Drawer add dragga...
133
134
                          [`${prefixCls}-wrap-inner`]: this.inner,
                          [`${prefixCls}-wrap-dragging`]: this.canMove
f59e6e89   梁灏   update Drawer
135
136
137
138
139
140
                      }
                  ];
              },
              mainStyles () {
                  let style = {};
  
a17511c3   梁灏   Drawer add dragga...
141
                  const width = parseInt(this.dragWidth);
f59e6e89   梁灏   update Drawer
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  
                  const styleWidth = {
                      width: width <= 100 ? `${width}%` : `${width}px`
                  };
  
                  Object.assign(style, styleWidth);
  
                  return style;
              },
              contentClasses () {
                  return [
                      `${prefixCls}-content`,
                      {
                          [`${prefixCls}-content-no-mask`]: !this.mask
                      }
                  ];
              },
ab58648e   梁灏   update Drawer
159
160
161
162
163
164
              classes () {
                  return [
                      `${prefixCls}`,
                      `${prefixCls}-${this.placement}`,
                      {
                          [`${prefixCls}-no-header`]: !this.showHead,
8a3c7282   梁灏   add inner prop
165
166
167
168
169
170
171
172
173
                          [`${prefixCls}-inner`]: this.inner
                      }
                  ];
              },
              maskClasses () {
                  return [
                      `${prefixCls}-mask`,
                      {
                          [`${prefixCls}-mask-inner`]: this.inner
ab58648e   梁灏   update Drawer
174
175
176
                      }
                  ];
              }
f59e6e89   梁灏   update Drawer
177
          },
749665ee   梁灏   add props
178
          methods: {
f59e6e89   梁灏   update Drawer
179
              close () {
4a8826fa   梁灏   fixed #4895
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
                  if (!this.beforeClose) {
                      return this.handleClose();
                  }
  
                  const before = this.beforeClose();
  
                  if (before && before.then) {
                      before.then(() => {
                          this.handleClose();
                      });
                  } else {
                      this.handleClose();
                  }
              },
              handleClose () {
f59e6e89   梁灏   update Drawer
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                  this.visible = false;
                  this.$emit('input', false);
                  this.$emit('on-close');
              },
              handleMask () {
                  if (this.maskClosable && this.mask) {
                      this.close();
                  }
              },
              handleWrapClick (event) {
                  // use indexOf,do not use === ,because ivu-modal-wrap can have other custom className
                  const className = event.target.getAttribute('class');
                  if (className && className.indexOf(`${prefixCls}-wrap`) > -1) this.handleMask();
              },
a17511c3   梁灏   Drawer add dragga...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
              handleMousemove (event) {
                  if (!this.canMove || !this.draggable) return;
                  // 更新容器宽度和距离左侧页面距离,如果是window则距左侧距离为0
                  this.handleSetWrapperWidth();
                  const left = event.pageX - this.wrapperLeft;
                  // 如果抽屉方向为右边,宽度计算需用容器宽度减去left
                  let width = this.placement === 'right' ? this.wrapperWidth - left : left;
                  // 限定最小宽度
                  width = Math.max(width, parseFloat(this.minWidth));
                  event.atMin = width === parseFloat(this.minWidth);
                  // 如果当前width不大于100,视为百分比
                  if (width <= 100) width = (width / this.wrapperWidth) * 100;
                  this.dragWidth = width;
                  this.$emit('on-resize-width', parseInt(this.dragWidth));
              },
              handleSetWrapperWidth () {
                  const {
                      width,
                      left
                  } = this.$el.getBoundingClientRect();
                  this.wrapperWidth = width;
                  this.wrapperLeft = left;
              },
              handleMouseup () {
                  if (!this.draggable) return;
                  this.canMove = false;
              },
              handleTriggerMousedown () {
                  this.canMove = true;
                  // 防止鼠标选中抽屉中文字,造成拖动trigger触发浏览器原生拖动行为
                  window.getSelection().removeAllRanges();
              },
f59e6e89   梁灏   update Drawer
241
242
243
244
245
246
247
          },
          mounted () {
              if (this.visible) {
                  this.wrapShow = true;
              }
  
              let showHead = true;
c4d780c0   梁灏   init Drawer compo...
248
  
f59e6e89   梁灏   update Drawer
249
250
251
252
253
              if (this.$slots.header === undefined && !this.title) {
                  showHead = false;
              }
  
              this.showHead = showHead;
a17511c3   梁灏   Drawer add dragga...
254
255
256
257
  
              on(document, 'mousemove', this.handleMousemove);
              on(document, 'mouseup', this.handleMouseup);
              this.handleSetWrapperWidth();
f59e6e89   梁灏   update Drawer
258
259
          },
          beforeDestroy () {
a17511c3   梁灏   Drawer add dragga...
260
261
              off(document, 'mousemove', this.handleMousemove);
              off(document, 'mouseup', this.handleMouseup);
f59e6e89   梁灏   update Drawer
262
263
264
265
266
267
268
269
270
271
              this.removeScrollEffect();
          },
          watch: {
              value (val) {
                  this.visible = val;
              },
              visible (val) {
                  if (val === false) {
                      this.timer = setTimeout(() => {
                          this.wrapShow = false;
b0fe4f98   梁灏   fix #4831
272
273
274
275
276
277
278
279
280
281
282
                          // #4831 Check if there are any drawers left at the parent level
                          const brotherDrawers = findBrothersComponents(this, 'Drawer') || [];
                          const parentDrawers = findComponentsUpward(this, 'Drawer') || [];
  
                          const otherDrawers = [].concat(brotherDrawers).concat(parentDrawers);
  
                          const isScrollDrawer = otherDrawers.some(item => item.visible && !item.scrollable);
  
                          if (!isScrollDrawer) {
                              this.removeScrollEffect();
                          }
f59e6e89   梁灏   update Drawer
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
                      }, 300);
                  } else {
                      if (this.timer) clearTimeout(this.timer);
                      this.wrapShow = true;
                      if (!this.scrollable) {
                          this.addScrollEffect();
                      }
                  }
                  this.broadcast('Table', 'on-visible-change', val);
                  this.broadcast('Slider', 'on-visible-change', val);  // #2852
                  this.$emit('on-visible-change', val);
              },
              scrollable (val) {
                  if (!val) {
                      this.addScrollEffect();
                  } else {
                      this.removeScrollEffect();
                  }
              },
              title (val) {
                  if (this.$slots.header === undefined) {
                      this.showHead = !!val;
                  }
              }
c4d780c0   梁灏   init Drawer compo...
307
308
309
          }
      };
  </script>