Blame view

src/components/split/split.vue 6.77 KB
8979c734   zhigang.li   add split components
1
  <template>
cd397a6f   梁灏   Split code format
2
3
      <div ref="outerWrapper" :class="wrapperClasses">
          <div v-if="isHorizontal" :class="`${prefix}-horizontal`">
1e9bece0   梁灏   update Split
4
              <div :style="{right: `${anotherOffset}%`}" class="left-pane" :class="paneClasses">
cd397a6f   梁灏   Split code format
5
6
7
8
9
10
11
                  <slot name="left"/>
              </div>
              <div :class="`${prefix}-trigger-con`" :style="{left: `${offset}%`}" @mousedown="handleMousedown">
                  <slot name="trigger">
                      <trigger mode="vertical"/>
                  </slot>
              </div>
1e9bece0   梁灏   update Split
12
              <div :style="{left: `${offset}%`}" class="right-pane" :class="paneClasses">
cd397a6f   梁灏   Split code format
13
14
15
16
                  <slot name="right"/>
              </div>
          </div>
          <div v-else :class="`${prefix}-vertical`">
1e9bece0   梁灏   update Split
17
              <div :style="{bottom: `${anotherOffset}%`}" class="top-pane" :class="paneClasses">
cd397a6f   梁灏   Split code format
18
19
20
21
22
23
24
                  <slot name="top"/>
              </div>
              <div :class="`${prefix}-trigger-con`" :style="{top: `${offset}%`}" @mousedown="handleMousedown">
                  <slot name="trigger">
                      <trigger mode="horizontal"/>
                  </slot>
              </div>
1e9bece0   梁灏   update Split
25
              <div :style="{top: `${offset}%`}" class="bottom-pane" :class="paneClasses">
cd397a6f   梁灏   Split code format
26
27
28
                  <slot name="bottom"/>
              </div>
          </div>
8979c734   zhigang.li   add split components
29
      </div>
8979c734   zhigang.li   add split components
30
31
32
  </template>
  
  <script>
cd397a6f   梁灏   Split code format
33
34
35
36
37
38
39
40
41
42
43
44
      import {oneOf} from '../../utils/assist';
      import {on, off} from '../../utils/dom';
      import Trigger from './trigger.vue';
      export default {
          name: 'Split',
          components: {
              Trigger
          },
          props: {
              value: {
                  type: [Number, String],
                  default: 0.5
77376451   梁灏   fixed #3568
45
              },
cd397a6f   梁灏   Split code format
46
47
48
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
75
76
77
78
79
80
81
              mode: {
                  validator (value) {
                      return oneOf(value, ['horizontal', 'vertical']);
                  },
                  default: 'horizontal'
              },
              min: {
                  type: [Number, String],
                  default: '40px'
              },
              max: {
                  type: [Number, String],
                  default: '40px'
              }
          },
          /**
           * Events
           * @on-move-start
           * @on-moving 返回值:事件对象,但是在事件对象中加入了两个参数:atMin(当前是否在最小值处), atMax(当前是否在最大值处)
           * @on-move-end
           */
          data () {
              return {
                  prefix: 'ivu-split',
                  offset: 0,
                  oldOffset: 0,
                  isMoving: false
              };
          },
          computed: {
              wrapperClasses () {
                  return [
                      `${this.prefix}-wrapper`,
                      this.isMoving ? 'no-select' : ''
                  ];
              },
1e9bece0   梁灏   update Split
82
83
84
85
86
87
88
89
              paneClasses () {
                  return [
                      `${this.prefix}-pane`,
                      {
                          [`${this.prefix}-pane-moving`]: this.isMoving
                      }
                  ];
              },
cd397a6f   梁灏   Split code format
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
              isHorizontal () {
                  return this.mode === 'horizontal';
              },
              anotherOffset () {
                  return 100 - this.offset;
              },
              valueIsPx () {
                  return typeof this.value === 'string';
              },
              offsetSize () {
                  return this.isHorizontal ? 'offsetWidth' : 'offsetHeight';
              },
              computedMin () {
                  return this.getComputedThresholdValue('min');
              },
              computedMax () {
                  return this.getComputedThresholdValue('max');
              }
77376451   梁灏   fixed #3568
108
          },
cd397a6f   梁灏   Split code format
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
          methods: {
              px2percent (numerator, denominator) {
                  return parseFloat(numerator) / parseFloat(denominator);
              },
              getComputedThresholdValue (type) {
                  let size = this.$refs.outerWrapper[this.offsetSize];
                  if (this.valueIsPx) return typeof this[type] === 'string' ? this[type] : size * this[type];
                  else return typeof this[type] === 'string' ? this.px2percent(this[type], size) : this[type];
              },
              getMin (value1, value2) {
                  if (this.valueIsPx) return `${Math.min(parseFloat(value1), parseFloat(value2))}px`;
                  else return Math.min(value1, value2);
              },
              getMax (value1, value2) {
                  if (this.valueIsPx) return `${Math.max(parseFloat(value1), parseFloat(value2))}px`;
                  else return Math.max(value1, value2);
              },
              getAnotherOffset (value) {
                  let res = 0;
                  if (this.valueIsPx) res = `${this.$refs.outerWrapper[this.offsetSize] - parseFloat(value)}px`;
                  else res = 1 - value;
                  return res;
              },
              handleMove (e) {
                  let pageOffset = this.isHorizontal ? e.pageX : e.pageY;
                  let offset = pageOffset - this.initOffset;
                  let outerWidth = this.$refs.outerWrapper[this.offsetSize];
                  let value = this.valueIsPx ? `${parseFloat(this.oldOffset) + offset}px` : (this.px2percent(outerWidth * this.oldOffset + offset, outerWidth));
                  let anotherValue = this.getAnotherOffset(value);
                  if (parseFloat(value) <= parseFloat(this.computedMin)) value = this.getMax(value, this.computedMin);
                  if (parseFloat(anotherValue) <= parseFloat(this.computedMax)) value = this.getAnotherOffset(this.getMax(anotherValue, this.computedMax));
                  e.atMin = this.value === this.computedMin;
                  e.atMax = this.valueIsPx ? this.getAnotherOffset(this.value) === this.computedMax : this.getAnotherOffset(this.value).toFixed(5) === this.computedMax.toFixed(5);
                  this.$emit('input', value);
                  this.$emit('on-moving', e);
              },
              handleUp () {
                  this.isMoving = false;
                  off(document, 'mousemove', this.handleMove);
                  off(document, 'mouseup', this.handleUp);
                  this.$emit('on-move-end');
              },
              handleMousedown (e) {
                  this.initOffset = this.isHorizontal ? e.pageX : e.pageY;
                  this.oldOffset = this.value;
                  this.isMoving = true;
                  on(document, 'mousemove', this.handleMove);
                  on(document, 'mouseup', this.handleUp);
                  this.$emit('on-move-start');
              }
          },
          watch: {
              value () {
                  this.offset = (this.valueIsPx ? this.px2percent(this.value, this.$refs.outerWrapper[this.offsetSize]) : this.value) * 10000 / 100;
              }
          },
          mounted () {
              this.$nextTick(() => {
                  this.offset = (this.valueIsPx ? this.px2percent(this.value, this.$refs.outerWrapper[this.offsetSize]) : this.value) * 10000 / 100;
              });
77376451   梁灏   fixed #3568
169
          }
cd397a6f   梁灏   Split code format
170
      };
8979c734   zhigang.li   add split components
171
  </script>