All files / src/components/zd-carousel carousel.ts

100% Statements 41/41
100% Branches 8/8
100% Functions 6/6
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  93x           93x       21x         21x           21x                   21x           21x           21x           21x           21x         21x         21x         21x         21x         21x         21x         21x         21x         21x           21x         21x         21x         21x         21x         21x         21x         21x         21x         21x                   21x                                                                                                                                       9x 9x             3x 3x 2x               3x 2x                     2x                             2x                 42x 42x      
import { IButton } from '../zd-button/interfaces';
import { ComponentRender } from '../zd-component/component-render';
import { ICarousel, ICarouselEvents, ISlideData } from './interfaces';
 
/**
 * Base class for Carousel component.
 */
export class Carousel extends ComponentRender implements ICarousel {
	/**
	 * Configures the carousel to play automatically
	 */
	public autoPlay = false;
 
	/**
	 * Configures the carousel to show buttons outside the slides area
	 */
	public buttonsOutside = true;
 
	/**
	 * Displays the carousel items always in the center (only when using
	 * a non-integer number in slidesPerView, like 1.5)
	 */
	public center = false;
 
	/**
	 * Current slide being displayed
	 */
	public currentSlide: number;
 
	/**
	 * Events registered to the carousel
	 */
	public events: ICarouselEvents = {};
 
	/**
	 * Displays the pagination as a fraction in the following format: currentSlide/slidesCount.
	 * Only works when <code>showPagination</code> property is true
	 */
	public fractionPagination = false;
 
	/**
	 * Sets the carousel height. Example values: 'auto', '100%', '400px',
	 * '30em', '400', 400. Values without measurement unit will be notated in pixels by default
	 */
	public height: number | string = 'auto';
 
	/**
	 * Sets the carousel max height. Example values: 'auto', '100%', '400px',
	 * '30em', '400', 400. Values without measurement unit will be notated in pixels by default
	 */
	public maxHeight: number | string = 'none';
 
	/**
	 * Sets the carousel min height. Example values: 'auto', '100%', '400px',
	 * '30em', '400', 400. Values without measurement unit will be notated in pixels by default
	 */
	public minHeight: number | string = 'auto';
 
	/**
	 * Set component height to fill all space available
	 */
	public fillHeight = false;
 
	/**
	 * Configures the carousel as infinite (the slide after the last one is the first slide, and vice-versa)
	 */
	public infiniteScroll = true;
 
	/**
	 * Configures the carousel's initial slide
	 */
	public initialSlide = 0;
 
	/**
	 * If autoPlay is enabled, sets the time duration of each slide in milliseconds
	 */
	public interval = 4000;
 
	/**
	 * Allows controlling the carousel using the keyboard arrow keys
	 */
	public keysControl = true;
 
	/**
	 * Allows controlling the carousel using mouse drag
	 */
	public mouseControl = false;
 
	/**
	 * Displays the pagination background
	 */
	public paginationBackground = true;
 
	/**
	 * If autoPlay is enabled, it will pause on mouse hover
	 */
	public pauseOnHover = true;
 
	/**
	 * Show the navigation arrows.
	 */
	public showArrows = true;
 
	/**
	 * The navigation arrows will be shown only on mouse hover.
	 * Only works when <code>showArrows</code> property is true
	 */
	public showArrowsOnHover = false;
 
	/**
	 * Show the carousel pagination menu
	 */
	public showPagination = false;
 
	/**
	 * Show a progress bar that fills as the carousel approaches the last slide
	 */
	public showProgress = false;
 
	/**
	 * Css class to be added to slides
	 */
	public slideCssClass = 'zd-pa-4';
 
	/**
	 * Number of slides to be shown for each view (non-integer numbers are allowed)
	 */
	public slidesPerView = 1;
 
	/**
	 * Number of slides to be slided
	 */
	public slidesToSlide = 1;
 
	/**
	 * Allows controlling the carousel using touch for mobile
	 */
	public touchControl = true;
 
	/**
	 * Duration of the slide transition in milliseconds
	 */
	public transitionDuration = 300;
 
	/**
	 * Allows controlling the carousel using the mouse wheel
	 */
	public wheelControl = false;
 
	/**
	 * Carousel's right side button.
	 */
	public nextButton: IButton = {
		name: '<<NAME>>_nextButton',
		component: 'ZdButton',
		iconName: 'next',
		icon: true,
	};
 
	/**
	 * Carousel's left side button
	 */
	public prevButton: IButton = {
		name: '<<NAME>>_prevButton',
		component: 'ZdButton',
		iconName: 'prev',
		icon: true,
	};
 
	/* istanbul ignore next */
	/**
	 * Creates a new Carousel
	 * @param props Carousel properties
	 */
	constructor(props: ICarousel) {
		super(props);
		this.autoPlay = this.getInitValue('autoPlay', props.autoPlay, this.autoPlay);
		this.buttonsOutside = this.getInitValue('buttonsOutside', props.buttonsOutside, this.buttonsOutside);
		this.center = this.getInitValue('center', props.center, this.center);
		this.height = this.getInitValue('height', props.height, this.height);
		this.maxHeight = this.getInitValue('maxHeight', props.maxHeight, this.maxHeight);
		this.minHeight = this.getInitValue('minHeight', props.minHeight, this.minHeight);
		this.fillHeight = this.getInitValue('fillHeight', props.fillHeight, this.fillHeight);
		this.infiniteScroll = this.getInitValue('infiniteScroll', props.infiniteScroll, this.infiniteScroll);
		this.initialSlide = this.getInitValue('initialSlide', props.initialSlide, this.initialSlide);
		this.currentSlide = this.getInitValue('currentSlide', props.currentSlide, this.initialSlide);
		this.interval = this.getInitValue('interval', props.interval, this.interval);
		this.keysControl = this.getInitValue('keysControl', props.keysControl, this.keysControl);
		this.mouseControl = this.getInitValue('mouseControl', props.mouseControl, this.mouseControl);
		this.nextButton = props.nextButton || this.nextButton;
		this.fractionPagination = this.getInitValue(
			'fractionPagination',
			props.fractionPagination,
			this.fractionPagination,
		);
		this.paginationBackground = this.getInitValue(
			'paginationBackground',
			props.paginationBackground,
			this.paginationBackground,
		);
		this.pauseOnHover = this.getInitValue('pauseOnHover', props.pauseOnHover, this.pauseOnHover);
		this.prevButton = props.prevButton || this.prevButton;
		this.showArrowsOnHover = this.getInitValue('showArrowsOnHover', props.showArrowsOnHover, this.showArrowsOnHover);
		this.showArrows = this.getInitValue('showArrows', props.showArrows, this.showArrows);
		this.showPagination = this.getInitValue('showPagination', props.showPagination, this.showPagination);
		this.showProgress = this.getInitValue('showProgress', props.showProgress, this.showProgress);
		this.slideCssClass = this.getInitValue('slideCssClass', props.slideCssClass, this.slideCssClass);
		this.slidesPerView = this.getInitValue('slidesPerView', props.slidesPerView, this.slidesPerView);
		this.slidesToSlide = this.getInitValue('slidesToSlide', props.slidesToSlide, this.slidesToSlide);
		this.touchControl = this.getInitValue('touchControl', props.touchControl, this.touchControl);
		this.transitionDuration = this.getInitValue(
			'transitionDuration',
			props.transitionDuration,
			this.transitionDuration,
		);
		this.wheelControl = this.getInitValue('wheelControl', props.wheelControl, this.wheelControl);
		this.prevButton = this.changeDefaultButtonName(this.prevButton);
		this.nextButton = this.changeDefaultButtonName(this.nextButton);
		this.createAccessors();
 
		if (props.events) {
			this.events = { ...props.events };
		}
	}
 
	/**
	 * Slides the carousel to the defined index
	 * @param index Index of the slide to be slided to
	 */
	public slideTo(index: number) {
		const slideCount = this.children.length;
		this.currentSlide = index < 0 ? (index + slideCount) % slideCount : index % slideCount;
	}
 
	/**
	 * Slides to the next slide
	 */
	public slideNext() {
		const slideCount = this.children.length;
		if (this.infiniteScroll || this.currentSlide < slideCount - 1) {
			this.slideTo(this.currentSlide + this.slidesToSlide);
		}
	}
 
	/**
	 * Slides to the previous slide
	 */
	public slidePrev() {
		if (this.infiniteScroll || this.currentSlide > 0) {
			this.slideTo(this.currentSlide - this.slidesToSlide);
		}
	}
 
	/**
	 * Event triggered before the current slide changes
	 * @param event Event that triggered the beforeSlide event
	 * @param element Carousel element
	 * @param slideData Data related to the beforeSlide event
	 */
	public beforeSlide(element: any, slideData: ISlideData, event?: Event) {
		this.callEvent('beforeSlide', {
			event,
			element,
			slideData,
			component: this,
		});
	}
 
	/**
	 * Event triggered when the current slide changes.
	 * @param event Event that triggered the slide event
	 * @param element Carousel element
	 * @param slideData Data related to the slide event
	 */
	public slide(element: any, slideData: ISlideData, event?: Event) {
		this.callEvent('slide', {
			event,
			element,
			slideData,
			component: this,
		});
	}
 
	private changeDefaultButtonName(button: IButton) {
		button.name = button.name.replace('<<NAME>>', this.name);
		return button;
	}
}