All files Carousel.vue

76.47% Statements 91/119
67.02% Branches 63/94
81.82% Functions 27/33
75.44% Lines 86/114
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 4403x     3x       3x   3x   3x   3x   3x   3x   9x   3x             19x                                                                                                                                                     26x     10x     37x   37x     21x 17x     4x     40x 40x 40x   40x   40x     19x     38x 38x   38x 2x 2x     36x     19x         5x 2x 3x 3x       19x 19x   19x 19x   19x     4x         4x 4x 4x     4x   4x     19x 19x     20x 20x     21x     7x 7x                                                                           19x   19x 19x                 19x 19x 19x     19x 12x 26x 26x 26x           19x 8x 8x         19x 19x   19x   19x         19x 19x 19x       19x 19x   19x 19x       1x 1x 1x     1x           3x 3x 40x 3x                                                                                                                                                                                                                                                                                  
<template>
  <div class="carousel">
    <div
      class="carousel-inner"
      v-bind:style="`
        width: ${carouselInnerWidth}px;
        transform: translateX(${currentOffset}px);
        transition: ${!mousedown || isAnimating ? transitionStyle : 'none'};
      `"
    >
      <slot></slot>
    </div>
    <navigation v-if="navigationEnabled"></navigation>
    <pagination v-if="paginationEnabled && pageCount > 0"></pagination>
  </div>
</template>
 
<script>
  import debounce from "./utils/debounce"
  import Navigation from "./Navigation.vue"
  import Pagination from "./Pagination.vue"
 
  export default {
    name: "carousel",
    components: {
      Navigation,
      Pagination,
    },
    data() {
      return {
        browserWidth: null,
        carouselWidth: null,
        currentPage: 0,
        dragOffset: 0,
        dragStartX: 0,
        isAnimating: false,
        slideCount: null,
        slideWidth: null,
        mousedown: false,
      }
    },
    props: {
      /**
       * Slide transition easing
       * Any valid CSS transition easing accepted
       */
      easing: {
        type: String,
        default: "ease",
      },
      /**
       * Minimum distance for the swipe to trigger
       * a slide advance
       */
      minSwipeDistance: {
        type: Number,
        default: 8,
      },
      /**
       * Flag to render the navigation component
       * (next/prev buttons)
       */
      navigationEnabled: {
        type: Boolean,
        default: false,
      },
      /**
       * The fill color of the active pagination dot
       * Any valid CSS color is accepted
       */
      paginationActiveColor: {
        type: String,
        default: "#000000",
      },
      /**
       * The fill color of pagination dots
       * Any valid CSS color is accepted
       */
      paginationColor: {
        type: String,
        default: "#efefef",
      },
      /**
       * Flag to render pagination component
       */
      paginationEnabled: {
        type: Boolean,
        default: true,
      },
      /**
       * The padding inside each pagination dot
       * Pixel values are accepted
       */
      paginationPadding: {
        type: Number,
        default: 10,
      },
      /**
       * The size of each pagination dot
       * Pixel values are accepted
       */
      paginationSize: {
        type: Number,
        default: 10,
      },
      /**
       * Maximum number of slides displayed on each page
       */
      perPage: {
        type: Number,
        default: 2,
      },
      /**
       * Configure the number of visible slides with a particular browser width.
       * This will be an array of arrays, ex. [[320, 2], [1199, 4]]
       * Formatted as [x, y] where x=browser width, and y=number of slides displayed.
       * ex. [1199, 4] means if (window <= 1199) then show 4 slides per page
       */
      perPageCustom: {
        type: Array,
      },
      /**
       * Scroll per page, not per item
       */
      scrollPerPage: {
        type: Boolean,
        default: false,
      },
      /**
       * Slide transition speed
       * Number of milliseconds accepted
       */
      speed: {
        type: Number,
        default: 500,
      },
    },
    computed: {
      /**
       * @return {Boolean} Can the slider move forward?
       */
      canAdvanceForward() {
        return (this.currentPage < (this.pageCount - 1))
      },
      /**
       * @return {Boolean} Can the slider move backward?
       */
      canAdvanceBackward() {
        return (this.currentPage > 0)
      },
      /**
       * CalcEulated width of the inner wrapper.
       * This wrapper will animate horizontally while navigating.
       * @return {Number} The width of the wrapper in pixels
       */
      carouselInnerWidth() {
        const innerWidth = this.slideWidth * this.slideCount
 
        return innerWidth
      },
      /**
       * Number of slides to display per page in the current context.
       * This is constant unless responsive perPage option is set.
       * @return {Number} The number of slides per page to display
       */
      currentPerPage() {
        if (!this.perPageCustom || this.$isServer) {
          return this.perPage // If no custom breakpoints specified, use the default perPage prop.
        }
 
        return this.getBreakpointSlidesPerPage(this.perPageCustom, this.browserWidth)
      },
      /**
       * The horizontal distance the inner wrapper is offset while navigating.
       * @return {Number} Pixel value of offset to apply
       */
      currentOffset() {
        const page = this.currentPage
        const width = this.slideWidth
        const dragged = this.dragOffset
 
        // The offset distance depends on whether the scrollPerPage option is active.
        // If this option is active, the offset will be determined per page rather than per item.
        const offset = (this.scrollPerPage) ? (page * width * this.currentPerPage) : (page * width)
 
        return (offset + dragged) * -1
      },
      isHidden() {
        return (this.carouselWidth <= 0)
      },
      /**
      E * Calculate the number of pages of slides
       * @return {Number} Number of pages
       */
      pageCount() {
        const slideCount = this.slideCount
        const perPage = this.currentPerPage

        if (this.scrollPerPage) {
          const pages = Math.ceil(slideCount / perPage)
          return (pages < 1) ? 1 : pages // Be sure to not round down to zero pages
        }
 
        return (slideCount - (this.currentPerPage - 1))
      },
      transitionStyle() {
        return `${this.speed / 1000}s ${this.easing} transform`
      },
    },
    methods: {
      /**
       * Increase/decrease the current page value
       * @param  {String} direction (Optional) The direction to advance
       */
      advancePage(direction) {
        if (direction && direction === "backward" && this.canAdvanceBackward) {
          this.goToPage(this.currentPage - 1)
        } else if (
          (
            !direction
            || (direction && direction !== "backward"))
            && this.canAdvanceForward
          ) {
          this.goToPage(this.currentPage + 1)
        }
      },
      /**
       * Calculate the width of each slide
       * @return {Number} Slide width
       */
      calculateSlideWidth() {
        const width = this.carouselWidth
      E  const perPage = this.currentPerPage

        this.slideWidth = width / perPage
        this.setChildSlideWidth(this.slideWidth)

        return this.slideWidth
      },
      /**
       * Given a viewport width, find the number of slides to display
       * @param  {Number} width Current viewport width in pixels
       * @return {Number}       Number of slides to display
       */
      getBreakpointSlidesPerPage(breakpointArray, width) {
        const breakpoints = breakpointArray.sort((a, b) => {
          const isMatching = (a[0] > b[0]) ? -1 : 1
          return isMatching
        })
 
        //E Reduce the breakpoints to entries where the width is in range
        // The breakpoint arrays are formatted as [widthToMatch, numberOfSlides]
        const matches = breakpoints.filter((breakpoint) => {
          const isMatching = (width >= breakpoint[0])
          return isMatching
        })
 
        // If there is a match, the result should return only
        // the slide count from the first matching breakpoint
        const match = matches[0] && matches[0][1]
 
        return match || this.perPage
      },
      /**
    E   * Get the current browser viewport width
       * @return {Number} Browser"s width in pixels
       */
      getBrowserWidth() {
        this.browserWidth = window.innerWidth
      I  return this.browserWidth
      },
      /**
       * Get the width of the carousel DOM element
       * @return {Number} Width of the carousel in pixels
       */
      getCarouselWidth() {
        this.carouselWidth = (this.$el && this.$el.clientWidth) || 0 // Assign globally
        return this.carouselWidth
      },
      /**
       * Get the number of slides
       * @return {Number} Number of slides
       */
    E  getSlideCount() {
        return (this.$slots && this.$slots.default && this.$slots.default.length) || 0
      },
      /**
       * Set the current page to a specific value
    E   * This function will only apply the change if the value is within the carousel bounds
       * @param  {Number} page The value of the new page number
      I */
      goToPage(page) {
        if ((page >= 0) && (page <= this.pageCount)) {
          this.currentPage = page
        }
      },
      /**
       * Trigger actions when mouse is pressed
       * @param  {Object} e The event object
E       */
      /* istanbul ignore next */
      handleMousedown(e) {
        if (!e.touches) { e.preventDefault() }
 
        this.mousedown = true
        this.dragStartX = ("ontouchstart" in window) ? e.touches[0].clientX : e.clientX
      },
      /**
       * Trigger actions when mouse is released
       * @param  {Object} e The event object
       */
      handleMouseup() {
        this.mousedown = false
        this.dragOffset = 0
      },
      /**
       * Trigger actions when mouse is pressed and then moved (mouse drag)
       * @param  {Object} e The event object
       */
      handleMousemove(e) {
        if (!this.mousedown) {
          return
        }
 
        const eventPosX = ("ontouchstart" in window) ? e.touches[0].clientX : e.clientX
        const deltaX = (this.dragStartX - eventPosX)
 
        this.dragOffset = deltaX
 
        if (this.dragOffset > this.minSwipeDistance) {
          this.handleMouseup()
          this.advancePage()
        } else if (this.dragOffset < -this.minSwipeDistance) {
          this.handleMouseup()
          this.advancePage("backward")
        }
      },
      /**
       * Trigger actions caused by window resizing
       */
      handleResize() {
        this.getBrowserWidth()
        this.recomputeCarouselWidth()
      },
      /**
       * If the carousel is hidden on init, slide widths cannot be calculated.
       * Dirty checking is applied in this case.
       * Once a width is found, the polling is stopped and the carousel is recalculated.
       */
      pollForWidth() {
        if (!this.pollInterval) {
          this.pollInterval = setInterval(() => {
            if (this.getCarouselWidth() > 0) {
              this.recomputeCarouselWidth()
              clearInterval(this.pollInterval)
            }
          }, 100)
        }
      },
      /**
       * Re-compute the width of the carousel and its slides
       */
      recomputeCarouselWidth() {
        this.getCarouselWidth()
        this.calculateSlideWidth()
        this.setCurrentPageInBounds()
      },
      /**
       * Assign widths to child slides within slots
       * @param {Number} width Width to set on slides
       */
      setChildSlideWidth(width) {
        if (this.$slots.default) {
          this.$slots.default.forEach((child) => {
            const slotChild = child
            if (slotChild && slotChild.child) {
              slotChild.child.width = width
            }
          })
        }
      },
      /**
       * When the current page exceeds the carousel bounds, reset it to the maximum allowed
       */
      setCurrentPageInBounds() {
        if (!this.canAdvanceForward) {
          const setPage = (this.pageCount - 1)
          this.currentPage = (setPage >= 0) ? setPage : 0
        }
      },
    },
    mounted() {
      if (!this.$isServer) {
        this.getBrowserWidth()
 
        window.addEventListener("resize", debounce(this.handleResize, 16))
 
        if ("ontouchstart" in window) {
          this.$el.addEventListener("touchstart", this.handleMousedown)
          this.$el.addEventListener("touchend", this.handleMouseup)
          this.$el.addEventListener("touchmove", this.handleMousemove)
        } else {
          this.$el.addEventListener("mousedown", this.handleMousedown)
          this.$el.addEventListener("mouseup", this.handleMouseup)
          this.$el.addEventListener("mousemove", this.handleMousemove)
        }
      }
 
      this.slideCount = this.getSlideCount()
      this.recomputeCarouselWidth()
 
      if (this.isHidden) {
        this.pollForWidth()
      }
    },
    destroyed() {
      if (!this.$isServer) {
        window.removeEventListener("resize", this.getBrowserWidth)
        if ("ontouchstart" in window) {
          this.$el.removeEventListener("touchmove", this.handleMousemove)
        } else {
          this.$el.removeEventListener("mousemove", this.handleMousemove)
        }
      }
    },
  }
</script>
 
<style scoped>
.carousel {
  width: 100%;
  position: relative;
  overflow: hidden;
}
 
.carousel-inner {
  backface-visibility: hidden;
}
</style>