Blame view

src/components/carousel/carousel.vue 7.96 KB
6c9acb08   Rijn   initialize carousel
1
  <template>
41f83010   Rijn   update props and ...
2
      <div :class="classes">
932db623   Rijn   remove autoplay d...
3
          <button :class="arrowClasses" class="left" @click="arrowEvent(-1)">
e9989f2b   Rijn   added horizontal ...
4
5
              <Icon type="chevron-left"></Icon>
          </button>
65c64ce9   Rijn   carousel basic po...
6
          <div :class="[prefixCls + '-list']">
bb71140e   梁灏   support Carousel
7
              <div :class="[prefixCls + '-track']" :style="trackStyles">
65c64ce9   Rijn   carousel basic po...
8
9
10
                  <slot></slot>
              </div>
          </div>
932db623   Rijn   remove autoplay d...
11
          <button :class="arrowClasses" class="right" @click="arrowEvent(1)">
e9989f2b   Rijn   added horizontal ...
12
13
14
15
              <Icon type="chevron-right"></Icon>
          </button>
          <ul :class="dotsClasses">
              <template v-for="n in slides.length">
bb71140e   梁灏   support Carousel
16
17
18
                  <li :class="[n - 1 === currentIndex ? prefixCls + '-active' : '']"
                      @click="dotsEvent('click', n - 1)"
                      @mouseover="dotsEvent('hover', n - 1)">
e9989f2b   Rijn   added horizontal ...
19
20
21
22
                      <button></button>
                  </li>
              </template>
          </ul>
6c9acb08   Rijn   initialize carousel
23
24
25
      </div>
  </template>
  <script>
41f83010   Rijn   update props and ...
26
      import Icon from '../icon/icon.vue';
53e38f9c   Rijn   added prop valida...
27
      import { getStyle, oneOf } from '../../utils/assist';
1dad8a57   梁灏   update Carousel
28
      import { on, off } from '../../utils/dom';
41f83010   Rijn   update props and ...
29
30
  
      const prefixCls = 'ivu-carousel';
6c9acb08   Rijn   initialize carousel
31
32
  
      export default {
41f83010   Rijn   update props and ...
33
          name: 'Carousel',
50f4ac70   Rijn   Merge branch 'mas...
34
          components: { Icon },
41f83010   Rijn   update props and ...
35
          props: {
e9989f2b   Rijn   added horizontal ...
36
37
              arrow: {
                  type: String,
53e38f9c   Rijn   added prop valida...
38
39
40
41
                  default: 'hover',
                  validator (value) {
                      return oneOf(value, ['hover', 'always', 'never']);
                  }
41f83010   Rijn   update props and ...
42
43
44
              },
              autoplay: {
                  type: Boolean,
5139233b   梁灏   update Carousel
45
                  default: false
41f83010   Rijn   update props and ...
46
47
48
49
50
              },
              autoplaySpeed: {
                  type: Number,
                  default: 2000
              },
41f83010   Rijn   update props and ...
51
52
53
54
55
              easing: {
                  type: String,
                  default: 'ease'
              },
              dots: {
e9989f2b   Rijn   added horizontal ...
56
                  type: String,
53e38f9c   Rijn   added prop valida...
57
58
59
60
                  default: 'inside',
                  validator (value) {
                      return oneOf(value, ['inside', 'outside', 'none']);
                  }
e9989f2b   Rijn   added horizontal ...
61
62
63
              },
              trigger: {
                  type: String,
53e38f9c   Rijn   added prop valida...
64
65
66
67
                  default: 'click',
                  validator (value) {
                      return oneOf(value, ['click', 'hover']);
                  }
41f83010   Rijn   update props and ...
68
              },
bb71140e   梁灏   support Carousel
69
              value: {
3e6d6356   Rijn   implement basic t...
70
71
                  type: Number,
                  default: 0
9cd69375   Rijn   added height props
72
73
74
              },
              height: {
                  type: [String, Number],
53e38f9c   Rijn   added prop valida...
75
76
                  default: 'auto',
                  validator (value) {
0b1b650d   Rijn   fix #269.
77
                      return value === 'auto' || Object.prototype.toString.call(value) === '[object Number]';
53e38f9c   Rijn   added prop valida...
78
                  }
41f83010   Rijn   update props and ...
79
80
              }
          },
65c64ce9   Rijn   carousel basic po...
81
82
83
84
85
          data () {
              return {
                  prefixCls: prefixCls,
                  listWidth: 0,
                  trackWidth: 0,
9cd69375   Rijn   added height props
86
                  trackOffset: 0,
65c64ce9   Rijn   carousel basic po...
87
                  slides: [],
3e6d6356   Rijn   implement basic t...
88
                  slideInstances: [],
9cd69375   Rijn   added height props
89
                  timer: null,
bb71140e   梁灏   support Carousel
90
91
                  ready: false,
                  currentIndex: this.value
50f4ac70   Rijn   Merge branch 'mas...
92
              };
65c64ce9   Rijn   carousel basic po...
93
          },
41f83010   Rijn   update props and ...
94
95
96
          computed: {
              classes () {
                  return [
9cd69375   Rijn   added height props
97
                      `${prefixCls}`
41f83010   Rijn   update props and ...
98
                  ];
65c64ce9   Rijn   carousel basic po...
99
100
101
              },
              trackStyles () {
                  return {
3e6d6356   Rijn   implement basic t...
102
                      width: `${this.trackWidth}px`,
9cd69375   Rijn   added height props
103
                      transform: `translate3d(-${this.trackOffset}px, 0px, 0px)`,
3e6d6356   Rijn   implement basic t...
104
                      transition: `transform 500ms ${this.easing}`
65c64ce9   Rijn   carousel basic po...
105
                  };
e9989f2b   Rijn   added horizontal ...
106
107
108
109
110
              },
              arrowClasses () {
                  return [
                      `${prefixCls}-arrow`,
                      `${prefixCls}-arrow-${this.arrow}`
50f4ac70   Rijn   Merge branch 'mas...
111
                  ];
e9989f2b   Rijn   added horizontal ...
112
113
114
115
              },
              dotsClasses () {
                  return [
                      `${prefixCls}-dots`,
8738f4d3   Rijn   added outside dots
116
                      `${prefixCls}-dots-${this.dots}`
50f4ac70   Rijn   Merge branch 'mas...
117
                  ];
65c64ce9   Rijn   carousel basic po...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
              }
          },
          methods: {
              // find option component
              findChild (cb) {
                  const find = function (child) {
                      const name = child.$options.componentName;
  
                      if (name) {
                          cb(child);
                      } else if (child.$children.length) {
                          child.$children.forEach((innerChild) => {
                              find(innerChild, cb);
                          });
                      }
                  };
  
9cd69375   Rijn   added height props
135
                  if (this.slideInstances.length || !this.$children) {
65c64ce9   Rijn   carousel basic po...
136
137
138
139
140
141
142
143
144
                      this.slideInstances.forEach((child) => {
                          find(child);
                      });
                  } else {
                      this.$children.forEach((child) => {
                          find(child);
                      });
                  }
              },
bb71140e   梁灏   support Carousel
145
              updateSlides (init) {
65c64ce9   Rijn   carousel basic po...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                  let slides = [];
                  let index = 1;
  
                  this.findChild((child) => {
                      slides.push({
                          $el: child.$el
                      });
                      child.index = index++;
  
                      if (init) {
                          this.slideInstances.push(child);
                      }
                  });
  
                  this.slides = slides;
8f4e2cf0   Rijn   update pos when s...
161
162
  
                  this.updatePos();
65c64ce9   Rijn   carousel basic po...
163
164
165
166
              },
              updatePos () {
                  this.findChild((child) => {
                      child.width = this.listWidth;
9cd69375   Rijn   added height props
167
                      child.height = typeof this.height === 'number' ? `${this.height}px` : this.height;
65c64ce9   Rijn   carousel basic po...
168
169
170
171
172
173
                  });
  
                  this.trackWidth = (this.slides.length || 0) * this.listWidth;
              },
              // use when slot changed
              slotChange () {
373dfb3c   Rijn   removed observer
174
175
176
177
178
179
                  this.$nextTick(() => {
                      this.slides = [];
                      this.slideInstances = [];
  
                      this.updateSlides(true, true);
                      this.updatePos();
62808b2b   Rijn   reset autoplay ti...
180
                      this.updateOffset();
373dfb3c   Rijn   removed observer
181
                  });
65c64ce9   Rijn   carousel basic po...
182
183
              },
              handleResize () {
9cd69375   Rijn   added height props
184
185
                  this.listWidth = parseInt(getStyle(this.$el, 'width'));
                  this.updatePos();
62808b2b   Rijn   reset autoplay ti...
186
                  this.updateOffset();
3e6d6356   Rijn   implement basic t...
187
              },
77d460e8   Rijn   added offset func...
188
189
190
              add (offset) {
                  let index = this.currentIndex;
                  index += offset;
5e8be9b3   Rijn   fixed add bug. ad...
191
192
                  while (index < 0) index += this.slides.length;
                  index = index % this.slides.length;
77d460e8   Rijn   added offset func...
193
                  this.currentIndex = index;
bb71140e   梁灏   support Carousel
194
                  this.$emit('input', index);
77d460e8   Rijn   added offset func...
195
              },
932db623   Rijn   remove autoplay d...
196
197
198
199
              arrowEvent (offset) {
                  this.setAutoplay();
                  this.add(offset);
              },
e9989f2b   Rijn   added horizontal ...
200
              dotsEvent (event, n) {
c1af3fac   Rijn   added on-change e...
201
                  if (event === this.trigger && this.currentIndex !== n) {
e9989f2b   Rijn   added horizontal ...
202
                      this.currentIndex = n;
bb71140e   梁灏   support Carousel
203
                      this.$emit('input', n);
62808b2b   Rijn   reset autoplay ti...
204
205
                      // Reset autoplay timer when trigger be activated
                      this.setAutoplay();
e9989f2b   Rijn   added horizontal ...
206
                  }
bfc11079   Rijn   updated autoplay ...
207
              },
3e6d6356   Rijn   implement basic t...
208
              setAutoplay () {
bfc11079   Rijn   updated autoplay ...
209
                  window.clearInterval(this.timer);
3e6d6356   Rijn   implement basic t...
210
211
                  if (this.autoplay) {
                      this.timer = window.setInterval(() => {
932db623   Rijn   remove autoplay d...
212
                          this.add(1);
3e6d6356   Rijn   implement basic t...
213
                      }, this.autoplaySpeed);
3e6d6356   Rijn   implement basic t...
214
                  }
9cd69375   Rijn   added height props
215
216
217
              },
              updateOffset () {
                  this.$nextTick(() => {
9cd69375   Rijn   added height props
218
219
                      this.trackOffset = this.currentIndex * this.listWidth;
                  });
41f83010   Rijn   update props and ...
220
              }
65c64ce9   Rijn   carousel basic po...
221
          },
3e6d6356   Rijn   implement basic t...
222
223
224
225
          watch: {
              autoplay () {
                  this.setAutoplay();
              },
bfc11079   Rijn   updated autoplay ...
226
227
228
              autoplaySpeed () {
                  this.setAutoplay();
              },
c1af3fac   Rijn   added on-change e...
229
              currentIndex (val, oldVal) {
50f4ac70   Rijn   Merge branch 'mas...
230
                  this.$emit('on-change', oldVal, val);
9cd69375   Rijn   added height props
231
232
233
234
                  this.updateOffset();
              },
              height () {
                  this.updatePos();
bb71140e   梁灏   support Carousel
235
236
237
              },
              value (val) {
                  this.currentIndex = val;
3e6d6356   Rijn   implement basic t...
238
239
              }
          },
bb71140e   梁灏   support Carousel
240
241
          mounted () {
              this.updateSlides(true);
65c64ce9   Rijn   carousel basic po...
242
              this.handleResize();
bfc11079   Rijn   updated autoplay ...
243
              this.setAutoplay();
1dad8a57   梁灏   update Carousel
244
245
  //            window.addEventListener('resize', this.handleResize, false);
              on(window, 'resize', this.handleResize);
65c64ce9   Rijn   carousel basic po...
246
247
          },
          beforeDestroy () {
1dad8a57   梁灏   update Carousel
248
249
  //            window.removeEventListener('resize', this.handleResize, false);
              off(window, 'resize', this.handleResize);
41f83010   Rijn   update props and ...
250
          }
6c9acb08   Rijn   initialize carousel
251
252
      };
  </script>