ngx-bootstrap
Version:
Native Angular Bootstrap Components
589 lines (581 loc) • 18.9 kB
JavaScript
import { isBs3, LinkedList } from 'ngx-bootstrap/utils';
import { CommonModule } from '@angular/common';
import { Injectable, Component, EventEmitter, Input, NgZone, Output, HostBinding, NgModule } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var CarouselConfig = /** @class */ (function () {
function CarouselConfig() {
/**
* Default interval of auto changing of slides
*/
this.interval = 5000;
/**
* Is loop of auto changing of slides can be paused
*/
this.noPause = false;
/**
* Is slides can wrap from the last to the first slide
*/
this.noWrap = false;
/**
* Show carousel-indicators
*/
this.showIndicators = true;
}
CarouselConfig.decorators = [
{ type: Injectable }
];
return CarouselConfig;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @enum {number} */
var Direction = {
UNKNOWN: 0,
NEXT: 1,
PREV: 2,
};
Direction[Direction.UNKNOWN] = 'UNKNOWN';
Direction[Direction.NEXT] = 'NEXT';
Direction[Direction.PREV] = 'PREV';
/**
* Base element to create carousel
*/
var CarouselComponent = /** @class */ (function () {
function CarouselComponent(config, ngZone) {
this.ngZone = ngZone;
/**
* Will be emitted when active slide has been changed. Part of two-way-bindable [(activeSlide)] property
*/
this.activeSlideChange = new EventEmitter(false);
this._slides = new LinkedList();
this.destroyed = false;
Object.assign(this, config);
}
Object.defineProperty(CarouselComponent.prototype, "activeSlide", {
get: /**
* @return {?}
*/
function () {
return this._currentActiveSlide;
},
/** Index of currently displayed slide(started for 0) */
set: /**
* Index of currently displayed slide(started for 0)
* @param {?} index
* @return {?}
*/
function (index) {
if (this._slides.length && index !== this._currentActiveSlide) {
this._select(index);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(CarouselComponent.prototype, "interval", {
/**
* Delay of item cycling in milliseconds. If false, carousel won't cycle
* automatically.
*/
get: /**
* Delay of item cycling in milliseconds. If false, carousel won't cycle
* automatically.
* @return {?}
*/
function () {
return this._interval;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._interval = value;
this.restartTimer();
},
enumerable: true,
configurable: true
});
Object.defineProperty(CarouselComponent.prototype, "slides", {
get: /**
* @return {?}
*/
function () {
return this._slides.toArray();
},
enumerable: true,
configurable: true
});
Object.defineProperty(CarouselComponent.prototype, "isBs4", {
get: /**
* @return {?}
*/
function () {
return !isBs3();
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
CarouselComponent.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.destroyed = true;
};
/**
* Adds new slide. If this slide is first in collection - set it as active
* and starts auto changing
* @param slide
*/
/**
* Adds new slide. If this slide is first in collection - set it as active
* and starts auto changing
* @param {?} slide
* @return {?}
*/
CarouselComponent.prototype.addSlide = /**
* Adds new slide. If this slide is first in collection - set it as active
* and starts auto changing
* @param {?} slide
* @return {?}
*/
function (slide) {
this._slides.add(slide);
if (this._slides.length === 1) {
this._currentActiveSlide = void 0;
this.activeSlide = 0;
this.play();
}
};
/**
* Removes specified slide. If this slide is active - will roll to another
* slide
* @param slide
*/
/**
* Removes specified slide. If this slide is active - will roll to another
* slide
* @param {?} slide
* @return {?}
*/
CarouselComponent.prototype.removeSlide = /**
* Removes specified slide. If this slide is active - will roll to another
* slide
* @param {?} slide
* @return {?}
*/
function (slide) {
var _this = this;
/** @type {?} */
var remIndex = this._slides.indexOf(slide);
if (this._currentActiveSlide === remIndex) {
// removing of active slide
/** @type {?} */
var nextSlideIndex_1 = void 0;
if (this._slides.length > 1) {
// if this slide last - will roll to first slide, if noWrap flag is
// FALSE or to previous, if noWrap is TRUE in case, if this slide in
// middle of collection, index of next slide is same to removed
nextSlideIndex_1 = !this.isLast(remIndex)
? remIndex
: this.noWrap ? remIndex - 1 : 0;
}
this._slides.remove(remIndex);
// prevents exception with changing some value after checking
setTimeout(function () {
_this._select(nextSlideIndex_1);
}, 0);
}
else {
this._slides.remove(remIndex);
/** @type {?} */
var currentSlideIndex_1 = this.getCurrentSlideIndex();
setTimeout(function () {
// after removing, need to actualize index of current active slide
_this._currentActiveSlide = currentSlideIndex_1;
_this.activeSlideChange.emit(_this._currentActiveSlide);
}, 0);
}
};
/**
* Rolling to next slide
* @param force: {boolean} if true - will ignore noWrap flag
*/
/**
* Rolling to next slide
* @param {?=} force
* @return {?}
*/
CarouselComponent.prototype.nextSlide = /**
* Rolling to next slide
* @param {?=} force
* @return {?}
*/
function (force) {
if (force === void 0) { force = false; }
this.activeSlide = this.findNextSlideIndex(Direction.NEXT, force);
};
/**
* Rolling to previous slide
* @param force: {boolean} if true - will ignore noWrap flag
*/
/**
* Rolling to previous slide
* @param {?=} force
* @return {?}
*/
CarouselComponent.prototype.previousSlide = /**
* Rolling to previous slide
* @param {?=} force
* @return {?}
*/
function (force) {
if (force === void 0) { force = false; }
this.activeSlide = this.findNextSlideIndex(Direction.PREV, force);
};
/**
* Rolling to specified slide
* @param index: {number} index of slide, which must be shown
*/
/**
* Rolling to specified slide
* @param {?} index
* @return {?}
*/
CarouselComponent.prototype.selectSlide = /**
* Rolling to specified slide
* @param {?} index
* @return {?}
*/
function (index) {
this.activeSlide = index;
};
/**
* Starts a auto changing of slides
*/
/**
* Starts a auto changing of slides
* @return {?}
*/
CarouselComponent.prototype.play = /**
* Starts a auto changing of slides
* @return {?}
*/
function () {
if (!this.isPlaying) {
this.isPlaying = true;
this.restartTimer();
}
};
/**
* Stops a auto changing of slides
*/
/**
* Stops a auto changing of slides
* @return {?}
*/
CarouselComponent.prototype.pause = /**
* Stops a auto changing of slides
* @return {?}
*/
function () {
if (!this.noPause) {
this.isPlaying = false;
this.resetTimer();
}
};
/**
* Finds and returns index of currently displayed slide
*/
/**
* Finds and returns index of currently displayed slide
* @return {?}
*/
CarouselComponent.prototype.getCurrentSlideIndex = /**
* Finds and returns index of currently displayed slide
* @return {?}
*/
function () {
return this._slides.findIndex(function (slide) { return slide.active; });
};
/**
* Defines, whether the specified index is last in collection
* @param index
*/
/**
* Defines, whether the specified index is last in collection
* @param {?} index
* @return {?}
*/
CarouselComponent.prototype.isLast = /**
* Defines, whether the specified index is last in collection
* @param {?} index
* @return {?}
*/
function (index) {
return index + 1 >= this._slides.length;
};
/**
* Defines next slide index, depending of direction
* @param direction: Direction(UNKNOWN|PREV|NEXT)
* @param force: {boolean} if TRUE - will ignore noWrap flag, else will
* return undefined if next slide require wrapping
*/
/**
* Defines next slide index, depending of direction
* @private
* @param {?} direction
* @param {?} force
* @return {?}
*/
CarouselComponent.prototype.findNextSlideIndex = /**
* Defines next slide index, depending of direction
* @private
* @param {?} direction
* @param {?} force
* @return {?}
*/
function (direction, force) {
/** @type {?} */
var nextSlideIndex = 0;
if (!force &&
(this.isLast(this.activeSlide) &&
direction !== Direction.PREV &&
this.noWrap)) {
return void 0;
}
switch (direction) {
case Direction.NEXT:
// if this is last slide, not force, looping is disabled
// and need to going forward - select current slide, as a next
nextSlideIndex = !this.isLast(this._currentActiveSlide)
? this._currentActiveSlide + 1
: !force && this.noWrap ? this._currentActiveSlide : 0;
break;
case Direction.PREV:
// if this is first slide, not force, looping is disabled
// and need to going backward - select current slide, as a next
nextSlideIndex =
this._currentActiveSlide > 0
? this._currentActiveSlide - 1
: !force && this.noWrap
? this._currentActiveSlide
: this._slides.length - 1;
break;
default:
throw new Error('Unknown direction');
}
return nextSlideIndex;
};
/**
* Sets a slide, which specified through index, as active
* @param index
*/
/**
* Sets a slide, which specified through index, as active
* @private
* @param {?} index
* @return {?}
*/
CarouselComponent.prototype._select = /**
* Sets a slide, which specified through index, as active
* @private
* @param {?} index
* @return {?}
*/
function (index) {
if (isNaN(index)) {
this.pause();
return;
}
/** @type {?} */
var currentSlide = this._slides.get(this._currentActiveSlide);
if (currentSlide) {
currentSlide.active = false;
}
/** @type {?} */
var nextSlide = this._slides.get(index);
if (nextSlide) {
this._currentActiveSlide = index;
nextSlide.active = true;
this.activeSlide = index;
this.activeSlideChange.emit(index);
}
};
/**
* Starts loop of auto changing of slides
*/
/**
* Starts loop of auto changing of slides
* @private
* @return {?}
*/
CarouselComponent.prototype.restartTimer = /**
* Starts loop of auto changing of slides
* @private
* @return {?}
*/
function () {
var _this = this;
this.resetTimer();
/** @type {?} */
var interval = +this.interval;
if (!isNaN(interval) && interval > 0) {
this.currentInterval = this.ngZone.runOutsideAngular(function () {
return setInterval(function () {
/** @type {?} */
var nInterval = +_this.interval;
_this.ngZone.run(function () {
if (_this.isPlaying &&
!isNaN(_this.interval) &&
nInterval > 0 &&
_this.slides.length) {
_this.nextSlide();
}
else {
_this.pause();
}
});
}, interval);
});
}
};
/**
* Stops loop of auto changing of slides
*/
/**
* Stops loop of auto changing of slides
* @private
* @return {?}
*/
CarouselComponent.prototype.resetTimer = /**
* Stops loop of auto changing of slides
* @private
* @return {?}
*/
function () {
if (this.currentInterval) {
clearInterval(this.currentInterval);
this.currentInterval = void 0;
}
};
CarouselComponent.decorators = [
{ type: Component, args: [{
selector: 'carousel',
template: "<div (mouseenter)=\"pause()\" (mouseleave)=\"play()\" (mouseup)=\"play()\" class=\"carousel slide\">\n <ol class=\"carousel-indicators\" *ngIf=\"showIndicators && slides.length > 1\">\n <li *ngFor=\"let slidez of slides; let i = index;\" [class.active]=\"slidez.active === true\" (click)=\"selectSlide(i)\"></li>\n </ol>\n <div class=\"carousel-inner\"><ng-content></ng-content></div>\n <a class=\"left carousel-control carousel-control-prev\" [class.disabled]=\"activeSlide === 0 && noWrap\" (click)=\"previousSlide()\" *ngIf=\"slides.length > 1\">\n <span class=\"icon-prev carousel-control-prev-icon\" aria-hidden=\"true\"></span>\n <span *ngIf=\"isBs4\" class=\"sr-only\">Previous</span>\n </a>\n <a class=\"right carousel-control carousel-control-next\" (click)=\"nextSlide()\" [class.disabled]=\"isLast(activeSlide) && noWrap\" *ngIf=\"slides.length > 1\">\n <span class=\"icon-next carousel-control-next-icon\" aria-hidden=\"true\"></span>\n <span class=\"sr-only\">Next</span>\n </a>\n</div>\n"
}] }
];
/** @nocollapse */
CarouselComponent.ctorParameters = function () { return [
{ type: CarouselConfig },
{ type: NgZone }
]; };
CarouselComponent.propDecorators = {
noWrap: [{ type: Input }],
noPause: [{ type: Input }],
showIndicators: [{ type: Input }],
activeSlideChange: [{ type: Output }],
activeSlide: [{ type: Input }],
interval: [{ type: Input }]
};
return CarouselComponent;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var SlideComponent = /** @class */ (function () {
function SlideComponent(carousel) {
/**
* Wraps element by appropriate CSS classes
*/
this.addClass = true;
this.carousel = carousel;
}
/** Fires changes in container collection after adding a new slide instance */
/**
* Fires changes in container collection after adding a new slide instance
* @return {?}
*/
SlideComponent.prototype.ngOnInit = /**
* Fires changes in container collection after adding a new slide instance
* @return {?}
*/
function () {
this.carousel.addSlide(this);
};
/** Fires changes in container collection after removing of this slide instance */
/**
* Fires changes in container collection after removing of this slide instance
* @return {?}
*/
SlideComponent.prototype.ngOnDestroy = /**
* Fires changes in container collection after removing of this slide instance
* @return {?}
*/
function () {
this.carousel.removeSlide(this);
};
SlideComponent.decorators = [
{ type: Component, args: [{
selector: 'slide',
template: "\n <div [class.active]=\"active\" class=\"item\">\n <ng-content></ng-content>\n </div>\n ",
host: {
'[attr.aria-hidden]': '!active'
}
}] }
];
/** @nocollapse */
SlideComponent.ctorParameters = function () { return [
{ type: CarouselComponent }
]; };
SlideComponent.propDecorators = {
active: [{ type: HostBinding, args: ['class.active',] }, { type: Input }],
addClass: [{ type: HostBinding, args: ['class.item',] }, { type: HostBinding, args: ['class.carousel-item',] }]
};
return SlideComponent;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var CarouselModule = /** @class */ (function () {
function CarouselModule() {
}
/**
* @return {?}
*/
CarouselModule.forRoot = /**
* @return {?}
*/
function () {
return { ngModule: CarouselModule, providers: [] };
};
CarouselModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
declarations: [SlideComponent, CarouselComponent],
exports: [SlideComponent, CarouselComponent],
providers: [CarouselConfig]
},] }
];
return CarouselModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { CarouselComponent, CarouselModule, SlideComponent, CarouselConfig };
//# sourceMappingURL=ngx-bootstrap-carousel.js.map