Blame view

src/components/carousel/carousel.vue 7.34 KB
6c9acb08   Rijn   initialize carousel
1
  <template>
41f83010   Rijn   update props and ...
2
      <div :class="classes">
e9989f2b   Rijn   added horizontal ...
3
4
5
          <button :class="arrowClasses" class="left" @click="add(-1)">
              <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>
e9989f2b   Rijn   added horizontal ...
11
12
13
14
15
16
17
18
19
20
21
22
          <button :class="arrowClasses" class="right" @click="add(1)">
              <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
44
45
46
47
48
49
50
51
52
53
54
              },
              autoplay: {
                  type: Boolean,
                  default: true
              },
              autoplaySpeed: {
                  type: Number,
                  default: 2000
              },
              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
76
77
                  default: 'auto',
                  validator (value) {
                      return value === 'auto' || toString.call(value) === '[object Number]';
                  }
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
178
                  this.$nextTick(() => {
                      this.slides = [];
                      this.slideInstances = [];
  
                      this.updateSlides(true, true);
                      this.updatePos();
                  });
65c64ce9   Rijn   carousel basic po...
179
180
              },
              handleResize () {
9cd69375   Rijn   added height props
181
182
                  this.listWidth = parseInt(getStyle(this.$el, 'width'));
                  this.updatePos();
3e6d6356   Rijn   implement basic t...
183
              },
77d460e8   Rijn   added offset func...
184
185
186
              add (offset) {
                  let index = this.currentIndex;
                  index += offset;
5e8be9b3   Rijn   fixed add bug. ad...
187
188
                  while (index < 0) index += this.slides.length;
                  index = index % this.slides.length;
77d460e8   Rijn   added offset func...
189
190
                  this.currentIndex = index;
              },
e9989f2b   Rijn   added horizontal ...
191
              dotsEvent (event, n) {
c1af3fac   Rijn   added on-change e...
192
                  if (event === this.trigger && this.currentIndex !== n) {
e9989f2b   Rijn   added horizontal ...
193
194
                      this.currentIndex = n;
                  }
bfc11079   Rijn   updated autoplay ...
195
              },
3e6d6356   Rijn   implement basic t...
196
              setAutoplay () {
bfc11079   Rijn   updated autoplay ...
197
                  window.clearInterval(this.timer);
3e6d6356   Rijn   implement basic t...
198
199
                  if (this.autoplay) {
                      this.timer = window.setInterval(() => {
77d460e8   Rijn   added offset func...
200
                          this.add(1);
3e6d6356   Rijn   implement basic t...
201
                      }, this.autoplaySpeed);
3e6d6356   Rijn   implement basic t...
202
                  }
9cd69375   Rijn   added height props
203
204
205
206
207
208
              },
              updateOffset () {
                  this.$nextTick(() => {
                      this.handleResize();
                      this.trackOffset = this.currentIndex * this.listWidth;
                  });
41f83010   Rijn   update props and ...
209
              }
65c64ce9   Rijn   carousel basic po...
210
211
212
          },
          compiled () {
              this.updateSlides(true);
65c64ce9   Rijn   carousel basic po...
213
          },
3e6d6356   Rijn   implement basic t...
214
215
216
217
          watch: {
              autoplay () {
                  this.setAutoplay();
              },
bfc11079   Rijn   updated autoplay ...
218
219
220
              autoplaySpeed () {
                  this.setAutoplay();
              },
c1af3fac   Rijn   added on-change e...
221
              currentIndex (val, oldVal) {
50f4ac70   Rijn   Merge branch 'mas...
222
                  this.$emit('on-change', oldVal, val);
9cd69375   Rijn   added height props
223
224
225
226
                  this.updateOffset();
              },
              height () {
                  this.updatePos();
3e6d6356   Rijn   implement basic t...
227
228
              }
          },
65c64ce9   Rijn   carousel basic po...
229
230
          ready () {
              this.handleResize();
bfc11079   Rijn   updated autoplay ...
231
              this.setAutoplay();
65c64ce9   Rijn   carousel basic po...
232
233
234
235
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
41f83010   Rijn   update props and ...
236
          }
6c9acb08   Rijn   initialize carousel
237
238
      };
  </script>