vCarousel/src/build/vCarousel.js

122 lines
5.1 KiB
JavaScript
Raw Normal View History

var errors = require("./errors.js");
var vCarousel = (function () {
function vCarousel() {
this._vContainers = [];
this._firstVideoId = undefined;
this._playFirstVideo = false;
this._playNextVideos = false;
this._noStop = false;
this.nbVContainers = 0;
this._currentVideo = undefined;
}
Object.defineProperty(vCarousel.prototype, "vContainers", {
set: function (vContainersIds) {
for (var _i = 0, vContainersIds_1 = vContainersIds; _i < vContainersIds_1.length; _i++) {
var containerId = vContainersIds_1[_i];
var checkContainerExist = document.getElementById(containerId);
if (checkContainerExist === null)
throw new Error(errors.elementNotFound + containerId);
else {
var checkVideoExist = document.querySelector("#" + containerId + " video");
if (checkVideoExist === null)
throw new Error(errors.videoNotFound + containerId);
else
this._vContainers.push({ id: containerId, containerElt: checkContainerExist, videoElt: checkVideoExist });
}
}
this.nbVContainers = this._vContainers.length;
if (this.nbVContainers < 2)
throw new Error(errors.need2Videos);
},
enumerable: true,
configurable: true
});
Object.defineProperty(vCarousel.prototype, "firstVideoId", {
get: function () {
return this._firstVideoId;
},
set: function (firstVideo) {
if ((firstVideo !== "" && this._vContainers.findIndex(function (video) { return video.id === firstVideo; }) !== -1) || (firstVideo === ""))
this._firstVideoId = firstVideo;
},
enumerable: true,
configurable: true
});
Object.defineProperty(vCarousel.prototype, "playFirstVideo", {
set: function (playFirstVideo) {
this._playFirstVideo = playFirstVideo;
},
enumerable: true,
configurable: true
});
Object.defineProperty(vCarousel.prototype, "playNextVideos", {
set: function (playNextVideos) {
this._playNextVideos = playNextVideos;
},
enumerable: true,
configurable: true
});
Object.defineProperty(vCarousel.prototype, "noStop", {
set: function (noStop) {
this._noStop = noStop;
},
enumerable: true,
configurable: true
});
Object.defineProperty(vCarousel.prototype, "currentVideo", {
get: function () {
return this._currentVideo;
},
enumerable: true,
configurable: true
});
vCarousel.prototype.run = function () {
var vCarousel = this;
if (vCarousel.nbVContainers < 2)
throw new Error(errors.needVideosProvided);
var _loop_1 = function (i) {
var vContainer = vCarousel._vContainers[i].containerElt;
var video = vCarousel._vContainers[i].videoElt;
if ((vCarousel._firstVideoId !== undefined && vCarousel._vContainers[i].id !== vCarousel._firstVideoId) || (vCarousel._firstVideoId === undefined && i !== 0) || vCarousel._firstVideoId === "")
vContainer.style.display = "none";
else {
if (vCarousel._currentVideo !== undefined && !vCarousel._currentVideo.paused)
vCarousel._currentVideo.pause();
vContainer.style.display = "block";
vCarousel._currentVideo = video;
if (vCarousel._playFirstVideo === true)
video.play();
}
nbTurn = 0;
video.addEventListener("ended", function () {
if (nbTurn < (vCarousel.nbVContainers - 1) || vCarousel._noStop === true) {
vContainer.style.display = "none";
var nextVContainer = void 0, nextVideo = void 0, nextHash = void 0;
if (i < (vCarousel.nbVContainers - 1)) {
nextVContainer = vCarousel._vContainers[i + 1].containerElt;
nextVideo = vCarousel._vContainers[i + 1].videoElt;
nextHash = vCarousel._vContainers[i + 1].id;
}
else {
nextVContainer = vCarousel._vContainers[0].containerElt;
nextVideo = vCarousel._vContainers[0].videoElt;
nextHash = vCarousel._vContainers[0].id;
}
nextVContainer.style.display = "block";
vCarousel._currentVideo = nextVideo;
window.location.assign("#" + nextHash);
if (vCarousel._playNextVideos === true)
nextVideo.play();
nbTurn++;
}
});
};
var nbTurn;
for (var i = 0; i < vCarousel.nbVContainers; i++) {
_loop_1(i);
}
};
return vCarousel;
}());
export { vCarousel };