Blame view

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