UNPKG

angular-gallery

Version:

Responsive gallery for Angular with touch screen support.

1,129 lines (1,099 loc) 84.9 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('rxjs')) : typeof define === 'function' && define.amd ? define('angular-gallery', ['exports', '@angular/core', '@angular/common', 'rxjs'], factory) : (global = global || self, factory(global['angular-gallery'] = {}, global.ng.core, global.ng.common, global.rxjs)); }(this, (function (exports, core, common, rxjs) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __exportStar(m, exports) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } function __values(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result.default = mod; return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } function __classPrivateFieldGet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); } function __classPrivateFieldSet(receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; } var Touches = /** @class */ (function () { function Touches(properties) { var _this = this; this.eventType = undefined; this.handlers = {}; this.startX = 0; this.startY = 0; this.lastTap = 0; this.doubleTapMinTimeout = 300; this.tapMinTimeout = 200; this.touchstartTime = 0; this.i = 0; this.isMousedown = false; this.touchListeners = { "touchstart": "handleTouchstart", "touchmove": "handleTouchmove", "touchend": "handleTouchend" }; this.mouseListeners = { "mousedown": "handleMousedown", "mousemove": "handleMousemove", "mouseup": "handleMouseup", "wheel": "handleWheel" }; this.otherListeners = { "resize": "handleResize" }; /* * Listeners */ /* Touchstart */ this.handleTouchstart = function (event) { _this.elementPosition = _this.getElementPosition(); _this.touchstartTime = new Date().getTime(); if (_this.eventType === undefined) { _this.getTouchstartPosition(event); } _this.runHandler("touchstart", event); }; /* Touchmove */ this.handleTouchmove = function (event) { var touches = event.touches; // Pan if (_this.detectPan(touches)) { _this.runHandler("pan", event); } // Pinch if (_this.detectPinch(event)) { _this.runHandler("pinch", event); } // Linear swipe switch (_this.detectLinearSwipe(event)) { case "horizontal-swipe": event.swipeType = "horizontal-swipe"; _this.runHandler("horizontal-swipe", event); break; case "vertical-swipe": event.swipeType = "vertical-swipe"; _this.runHandler("vertical-swipe", event); break; } // Linear swipe if (_this.detectLinearSwipe(event) || _this.eventType === 'horizontal-swipe' || _this.eventType === 'vertical-swipe') { _this.handleLinearSwipe(event); } }; /* Touchend */ this.handleTouchend = function (event) { var touches = event.touches; // Double Tap if (_this.detectDoubleTap()) { _this.runHandler("double-tap", event); } // Tap _this.detectTap(); _this.runHandler("touchend", event); _this.eventType = 'touchend'; if (touches && touches.length === 0) { _this.eventType = undefined; _this.i = 0; } }; /* Mousedown */ this.handleMousedown = function (event) { _this.isMousedown = true; _this.elementPosition = _this.getElementPosition(); _this.touchstartTime = new Date().getTime(); if (_this.eventType === undefined) { _this.getMousedownPosition(event); } _this.runHandler("mousedown", event); }; /* Mousemove */ this.handleMousemove = function (event) { //event.preventDefault(); if (!_this.isMousedown) { return; } // Pan _this.runHandler("pan", event); // Linear swipe switch (_this.detectLinearSwipe(event)) { case "horizontal-swipe": event.swipeType = "horizontal-swipe"; _this.runHandler("horizontal-swipe", event); break; case "vertical-swipe": event.swipeType = "vertical-swipe"; _this.runHandler("vertical-swipe", event); break; } // Linear swipe if (_this.detectLinearSwipe(event) || _this.eventType === 'horizontal-swipe' || _this.eventType === 'vertical-swipe') { _this.handleLinearSwipe(event); } }; /* Mouseup */ this.handleMouseup = function (event) { // Tap _this.detectTap(); _this.isMousedown = false; _this.runHandler("mouseup", event); _this.eventType = undefined; _this.i = 0; }; /* Wheel */ this.handleWheel = function (event) { _this.runHandler("wheel", event); }; /* Resize */ this.handleResize = function (event) { _this.runHandler("resize", event); }; this.properties = properties; this.element = this.properties.element; this.elementPosition = this.getElementPosition(); this.toggleEventListeners('addEventListener'); } Touches.prototype.destroy = function () { this.toggleEventListeners('removeEventListener'); }; Touches.prototype.toggleEventListeners = function (action) { var listeners; if (this.properties.listeners === 'mouse and touch') { listeners = Object.assign(this.touchListeners, this.mouseListeners); } else { listeners = this.detectTouchScreen() ? this.touchListeners : this.mouseListeners; } if (this.properties.resize) { listeners = Object.assign(listeners, this.otherListeners); } for (var listener in listeners) { var handler = listeners[listener]; // Window if (listener === "resize") { if (action === 'addEventListener') { window.addEventListener(listener, this[handler], false); } if (action === 'removeEventListener') { window.removeEventListener(listener, this[handler], false); } // Document } else if (listener === 'mouseup' || listener === "mousemove") { if (action === 'addEventListener') { document.addEventListener(listener, this[handler], false); } if (action === 'removeEventListener') { document.removeEventListener(listener, this[handler], false); } // Element } else { if (action === 'addEventListener') { this.element.addEventListener(listener, this[handler], false); } if (action === 'removeEventListener') { this.element.removeEventListener(listener, this[handler], false); } } } }; Touches.prototype.handleLinearSwipe = function (event) { //event.preventDefault(); this.i++; if (this.i > 3) { this.eventType = this.getLinearSwipeType(event); } if (this.eventType === 'horizontal-swipe') { this.runHandler('horizontal-swipe', event); } if (this.eventType === 'vertical-swipe') { this.runHandler('vertical-swipe', event); } }; Touches.prototype.runHandler = function (eventName, response) { if (this.handlers[eventName]) { this.handlers[eventName](response); } }; /* * Detection */ Touches.prototype.detectPan = function (touches) { return touches.length === 1 && !this.eventType || this.eventType === 'pan'; }; Touches.prototype.detectDoubleTap = function () { var _this = this; if (this.eventType != undefined) { return; } var currentTime = new Date().getTime(); var tapLength = currentTime - this.lastTap; clearTimeout(this.doubleTapTimeout); if (tapLength < this.doubleTapMinTimeout && tapLength > 0) { return true; } else { this.doubleTapTimeout = setTimeout(function () { clearTimeout(_this.doubleTapTimeout); }, this.doubleTapMinTimeout); } this.lastTap = currentTime; }; Touches.prototype.detectTap = function () { if (this.eventType != undefined) { return; } var currentTime = new Date().getTime(); var tapLength = currentTime - this.touchstartTime; if (tapLength > 0) { if (tapLength < this.tapMinTimeout) { this.runHandler("tap", event); } else { this.runHandler("longtap", event); } } }; Touches.prototype.detectPinch = function (event) { var touches = event.touches; return (touches.length === 2 && this.eventType === undefined) || this.eventType === 'pinch'; }; Touches.prototype.detectLinearSwipe = function (event) { var touches = event.touches; if (touches) { if (touches.length === 1 && !this.eventType || this.eventType === 'horizontal-swipe' || this.eventType === 'vertical-swipe') { return this.getLinearSwipeType(event); } } else { if (!this.eventType || this.eventType === 'horizontal-swipe' || this.eventType === 'vertical-swipe') { return this.getLinearSwipeType(event); } } }; Touches.prototype.getLinearSwipeType = function (event) { if (this.eventType !== 'horizontal-swipe' && this.eventType !== 'vertical-swipe') { var movementX = Math.abs(this.moveLeft(0, event) - this.startX); var movementY = Math.abs(this.moveTop(0, event) - this.startY); if ((movementY * 3) > movementX) { return 'vertical-swipe'; } else { return 'horizontal-swipe'; } } else { return this.eventType; } }; Touches.prototype.getElementPosition = function () { return this.element.getBoundingClientRect(); }; Touches.prototype.getTouchstartPosition = function (event) { this.startX = event.touches[0].clientX - this.elementPosition.left; this.startY = event.touches[0].clientY - this.elementPosition.top; }; Touches.prototype.getMousedownPosition = function (event) { this.startX = event.clientX - this.elementPosition.left; this.startY = event.clientY - this.elementPosition.top; }; Touches.prototype.moveLeft = function (index, event) { var touches = event.touches; if (touches) { return touches[index].clientX - this.elementPosition.left; } else { return event.clientX - this.elementPosition.left; } }; Touches.prototype.moveTop = function (index, event) { var touches = event.touches; if (touches) { return touches[index].clientY - this.elementPosition.top; } else { return event.clientY - this.elementPosition.top; } }; Touches.prototype.detectTouchScreen = function () { var prefixes = ' -webkit- -moz- -o- -ms- '.split(' '); var mq = function (query) { return window.matchMedia(query).matches; }; if (('ontouchstart' in window)) { return true; } // include the 'heartz' as a way to have a non matching MQ to help terminate the join // https://git.io/vznFH var query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join(''); return mq(query); }; /* Public properties and methods */ Touches.prototype.on = function (event, handler) { if (event) { this.handlers[event] = handler; } }; return Touches; }()); var Carousel = /** @class */ (function () { function Carousel(properties) { var _this = this; this.initialPositionX = 0; this.newContainerPositionIndex = 0; // The index of the new position relative to the active index, for example -1 or +1 this.preliminarySlideCounter = 0; this.slideCounter = 0; // Scrolling Index (counter) this.previousSlideCounter = 0; this.isContentImages = true; this.isLazyLoad = true; this.isContainerLocked = false; this.alignCells = "left"; this.initialContainerPosition = 0; this.handleTouchstart = function (event) { _this.isTouchstart = true; if (_this.isSlideInProgress) { return; } var touches = event.touches; if (touches) { _this.startX = touches[0].clientX - _this.getCarouselElementPosition()['left']; } else { _this.startX = event.clientX - _this.getCarouselElementPosition()['left']; } _this.containerInitialPositionX = _this.getElementPosition()['left'] - _this.getCarouselElementPosition()['left']; _this.isMoveInProgress = true; }; this.handleHorizontalSwipe = function (event) { if (_this.isSlideInProgress) { return; } var touches = event.touches; if (touches) { _this.moveX = touches[0].clientX - _this.getCarouselElementPosition()['left']; } else { _this.moveX = event.clientX - _this.getCarouselElementPosition()['left']; } _this.distanceAbs = _this.getDistanceAbs(); _this.direction = _this.getDirection(); _this.moveContainer(); }; this.handleTouchend = function (event) { if (_this.isSlideInProgress || !_this.isTouchstart) { _this.isTouchstart = false; return; } _this.isMoveInProgress = false; if (_this.detectSlide()) { _this.handleSlide(); } else { _this.newContainerPositionIndex = 0; _this.alignContainer(); } _this.startX = _this.moveX = _this.distanceAbs = undefined; _this.isTouchstart = false; }; this.element = properties.element; this.properties = properties; this.cells = this.element.children; this.visibleWidth = this.properties.visibleWidth || this.element.parentElement.clientWidth; this.setContainerWidth(); this.alignContainer(0); } Object.defineProperty(Carousel.prototype, "isFirstCell", { get: function () { return this.slideCounter === 0; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "lastCellIndex", { get: function () { return this.images.length ? (this.images.length - 1) : (this.cells.length - 1); }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "overflowCellsLimit", { get: function () { return this.properties.overflowCellsLimit; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "cellLimit", { get: function () { return this.visibleCellsCount + this.overflowCellsLimit * 2; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "images", { get: function () { return this.properties.images; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "margin", { get: function () { return this.properties.margin; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "minSwipeDistance", { get: function () { return this.properties.minSwipeDistance; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "transitionDuration", { get: function () { return this.properties.transitionDuration; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "transitionTimingFunction", { get: function () { return this.properties.transitionTimingFunction; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "fullCellWidth", { get: function () { return this.properties.cellWidth + this.margin; }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "visibleCellsCount", { get: function () { return Math.ceil(this.visibleWidth / this.fullCellWidth); }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "lapCounter", { get: function () { return Math.floor(this.slideCounter / this.totalContainerCellsCount); }, enumerable: true, configurable: true }); Object.defineProperty(Carousel.prototype, "visibleCellsOverflowContainer", { get: function () { return (this.visibleCellsCount * this.fullCellWidth - this.margin) > this.visibleWidth; }, enumerable: true, configurable: true }); /* Move */ Carousel.prototype.moveContainer = function () { var positionX = this.getMovePositionX(); this.transformPositionX(positionX, 0); }; Carousel.prototype.getMovePositionX = function () { var distance = this.getDistance(); return this.containerInitialPositionX - distance; }; /* Align */ Carousel.prototype.alignContainer = function (duration) { if (duration === void 0) { duration = this.transitionDuration; } var positionX = this.getContainerPosition(); this.transformPositionX(positionX, duration); this.setInitialContainerPosition(positionX); }; Carousel.prototype.getContainerPosition = function () { var correction = this.getContainerPositionCorrection(); this.isContainerPositionCorrection = correction != 0; return (this.initialContainerPosition + this.newContainerPositionIndex * this.fullCellWidth) + correction; }; Carousel.prototype.setInitialContainerPosition = function (position) { var correction = this.getContainerPositionCorrection(); this.initialContainerPosition = position - correction; }; Carousel.prototype.getContainerPositionCorrection = function () { var correction = 0; if ((this.images.length - this.preliminarySlideCounter) < this.visibleCellsCount || this.isSlideLengthLimited) { if (this.visibleWidth < this.totalContainerCellsCount * this.fullCellWidth) { correction = -(this.visibleCellsCount * this.fullCellWidth - this.visibleWidth - this.margin); } if (correction >= -this.margin) { correction = 0; } } return correction; }; /* Quickly center */ Carousel.prototype.quicklyPositionContainer = function () { var correction = this.getContainerPositionCorrection(); var initialPosition = this.getNewContainerPosition() + correction; this.transformPositionX(initialPosition, 0); this.setInitialContainerPosition(initialPosition); }; Carousel.prototype.getNewContainerPosition = function () { if (this.slideCounter > this.overflowCellsLimit) { if (this.alignCells === "left") { this.lineUpCells(); return -(this.overflowCellsLimit * this.fullCellWidth); } } if (this.slideCounter <= this.overflowCellsLimit) { if (this.previousSlideCounter > this.overflowCellsLimit) { this.lineUpCells(); } return -(this.slideCounter * this.fullCellWidth); } }; Carousel.prototype.setContainerWidth = function () { var containerWidth = this.getContainerWidth(); this.element.style.width = containerWidth + "px"; }; Carousel.prototype.getContainerWidth = function () { this.totalContainerCellsCount = this.visibleCellsCount + this.overflowCellsLimit * 2; var containerWidth = this.totalContainerCellsCount * this.fullCellWidth; var totalImageWidth = this.images.length * this.fullCellWidth; if (totalImageWidth < containerWidth) { containerWidth = totalImageWidth; } return containerWidth; }; Carousel.prototype.getFile = function (cellIndex) { var imageIndex = this.getFileIndex(cellIndex); var file = this.images[imageIndex]; if (file && !file.type) { file.type = 'image'; } return { image: this.images[imageIndex], imageIndex: imageIndex }; }; Carousel.prototype.getFileIndex = function (cellIndex) { var positionIndex = this.getPositionIndex(cellIndex); var numberLeftCells = (this.totalContainerCellsCount - 1) / 2; var imageIndex; if (!this.isContainerLocked) { if (this.slideCounter <= this.overflowCellsLimit) { return cellIndex; } else { var cellLimitOverflow = this.slideCounter - this.overflowCellsLimit; imageIndex = positionIndex + cellLimitOverflow; return imageIndex; } } if (this.alignCells === "left") { if (this.slideCounter > this.overflowCellsLimit) { var cellLimitOverflow = this.slideCounter - this.overflowCellsLimit; imageIndex = positionIndex + cellLimitOverflow; } else { imageIndex = cellIndex; } } if (imageIndex > this.lastCellIndex) { return false; } return imageIndex; }; Carousel.prototype.transformPositionX = function (value, duration) { if (duration === void 0) { duration = this.transitionDuration; } this.element.style.transition = 'transform ' + duration + 'ms ' + this.transitionTimingFunction; this.element.style.transform = 'translateX(' + value + 'px)'; }; Carousel.prototype.detectSlide = function () { return this.distanceAbs >= this.minSwipeDistance; }; Carousel.prototype.next = function () { if (this.isSlideInProgress) { return; } this.direction = 'left'; this.handleSlide(1); }; Carousel.prototype.prev = function () { if (this.isSlideInProgress) { return; } this.direction = 'right'; this.handleSlide(1); }; Carousel.prototype.select = function (index) { this.slideCounter = index; this.quicklyPositionContainer(); }; Carousel.prototype.handleSlide = function (slideLength) { if (slideLength === void 0) { slideLength = undefined; } this.slideLength = this.getSlideLength(); this.slideLength = slideLength ? slideLength : this.limitSlideLength(this.slideLength); if (this.direction === 'left' && !this.isSlideInProgress) { this.preliminarySlideCounter = this.slideCounter + this.slideLength; if (!this.detectLastSlide(this.slideCounter + this.slideLength)) { this.newContainerPositionIndex = this.newContainerPositionIndex - this.slideLength; this.isSlideInProgress = true; if (this.isLazyLoad) { this.isContainerLocked = this.preliminarySlideCounter > this.overflowCellsLimit; if (this.detectContainerUnlock()) { this.isContainerLocked = false; } } } } if (this.direction === 'right' && !this.isSlideInProgress) { if (this.slideCounter - this.slideLength < 0) { this.slideLength = this.slideCounter; } this.preliminarySlideCounter = this.slideCounter - this.slideLength; if (!this.isFirstCell) { this.newContainerPositionIndex = this.newContainerPositionIndex + this.slideLength; this.isSlideInProgress = true; if (this.isLazyLoad) { if (this.preliminarySlideCounter > this.overflowCellsLimit) { this.isContainerLocked = true; } else { this.isContainerLocked = false; } if (this.detectContainerUnlock()) { this.isContainerLocked = false; } } } } this.alignContainer(); }; Carousel.prototype.getSlideLength = function () { var correction = this.getContainerPositionCorrection(); var length = Math.floor((this.distanceAbs + correction) / this.fullCellWidth); if (this.distanceAbs % this.fullCellWidth >= this.minSwipeDistance) { length++; } return length; }; Carousel.prototype.limitSlideLength = function (slideLength) { if (slideLength > 1) { for (var i = 0; i < slideLength; i++) { var newSlideCounter = this.slideCounter + (slideLength - i); if (!this.detectLastSlide(newSlideCounter)) { slideLength = slideLength - i; this.isSlideLengthLimited = i > 0; break; } } } return slideLength; }; Carousel.prototype.detectLastSlide = function (slideCounter) { return (this.images.length - slideCounter) < this.visibleCellsCount; }; Carousel.prototype.isNextArrowDisabled = function () { if (this.visibleCellsOverflowContainer) { return this.detectLastSlide(this.slideCounter + 1) && this.isContainerPositionCorrection; } else { return this.detectLastSlide(this.slideCounter + 1); } }; Carousel.prototype.isPrevArrowDisabled = function () { return this.slideCounter === 0; }; Carousel.prototype.detectContainerUnlock = function () { return (this.images.length - this.preliminarySlideCounter) < (this.visibleCellsCount + this.overflowCellsLimit); }; Carousel.prototype.handleSlideEnd = function () { if (this.isSlideInProgress) { this.transformSlideEnd(); this.isSlideInProgress = false; this.newContainerPositionIndex = 0; this.isSlideLengthLimited = undefined; } }; Carousel.prototype.transformSlideEnd = function () { if (this.isLazyLoad) { this.setSlideCounter(); this.quicklyPositionContainer(); } this.previousSlideCounter = this.slideCounter; }; Carousel.prototype.setSlideCounter = function () { if (this.direction === 'left') { this.slideCounter = this.slideCounter + this.slideLength; } if (this.direction === 'right') { this.slideCounter = this.slideCounter - this.slideLength; } this.direction = undefined; this.slideLength = 0; }; Carousel.prototype.resetTransition = function () { this.element.style.transition = ''; }; Carousel.prototype.getElementPosition = function () { return this.element.getBoundingClientRect(); }; Carousel.prototype.getCarouselElementPosition = function () { return this.properties.container.getBoundingClientRect(); }; Carousel.prototype.getDistance = function () { return this.startX - this.moveX; }; Carousel.prototype.getDistanceAbs = function () { return Math.abs(this.startX - this.moveX); }; Carousel.prototype.getDirection = function () { var direction = Math.sign(this.startX - this.moveX); if (direction === -1) { return 'right'; } if (direction === 1) { return 'left'; } }; Carousel.prototype.lineUpCells = function () { var cells = this.element.children; for (var i = 0; i < cells.length; i++) { var cell = cells[i]; var positionX = this.getCellPositionX(i); cell.style.transform = 'translateX(' + positionX + 'px)'; } ; }; Carousel.prototype.getCellPositionX = function (index) { var positionIndex = this.getPositionIndex(index); return positionIndex * this.fullCellWidth; }; Carousel.prototype.getPositionIndex = function (cellIndex) { var slideCounter = this.slideCounter - this.overflowCellsLimit; var positionIndex; if (slideCounter > this.totalContainerCellsCount) { slideCounter = slideCounter % this.totalContainerCellsCount; } if (slideCounter < 0) { return cellIndex; } else { positionIndex = cellIndex - slideCounter; if (positionIndex < 0) { positionIndex = this.totalContainerCellsCount + positionIndex; } } return positionIndex; }; Object.defineProperty(Carousel.prototype, "containerOverflowRightCount", { get: function () { var totalOverflowCellsCount = this.images.length - this.totalContainerCellsCount; var overflowRight = 0; if (totalOverflowCellsCount > 0) { overflowRight = totalOverflowCellsCount - (this.previousSlideCounter - this.overflowCellsLimit); if (overflowRight > 0) { return overflowRight; } } return overflowRight; }, enumerable: true, configurable: true }); Carousel.prototype.getCenterPositionIndex = function () { return (this.totalContainerCellsCount - 1) / 2; }; return Carousel; }()); var EventService = /** @class */ (function () { function EventService() { this.emitter = new rxjs.ReplaySubject(1); } EventService.prototype.emitChangeEvent = function (data) { this.emitter.next(data); }; EventService = __decorate([ core.Injectable() ], EventService); return EventService; }()); var CarouselComponent = /** @class */ (function () { function CarouselComponent(elementRef, ref, overlayEventService) { var _this = this; this.elementRef = elementRef; this.ref = ref; this.overlayEventService = overlayEventService; this.minTimeout = 30; this._cellWidth = 200; this._transitionDuration = 200; this.events = new core.EventEmitter(); this.height = 200; this.margin = 10; this.objectFit = 'cover'; this.minSwipeDistance = 50; this.transitionTimingFunction = 'ease'; this.counterSeparator = " / "; this.overflowCellsLimit = 3; this.listeners = 'mouse and touch'; this.arrows = true; this.arrowsTheme = 'light'; this.hostClassCarousel = true; /* Touchstart */ this.handleTouchstart = function (event) { event.preventDefault(); _this.carousel.handleTouchstart(event); _this.isMoving = true; _this.events.emit({ type: 'touchstart', event: event }); }; /* Touchmove */ this.handleHorizontalSwipe = function (event) { event.preventDefault(); _this.carousel.handleHorizontalSwipe(event); _this.events.emit({ type: 'swipe', event: event }); }; /* Touchend */ this.handleTouchend = function (event) { var touches = event.touches; _this.carousel.handleTouchend(event); _this.isMoving = false; _this.events.emit({ type: 'touchend', event: event }); }; /* Tap */ this.handleTap = function (event) { var i = _this.carousel.slideCounter; var cellIndex = _this.carousel.currentCellIndex; var fileIndex = _this.carousel.getFileIndex(i); var file = _this.carousel.getFile(cellIndex); _this.events.emit({ type: 'click', file: file, index: fileIndex }); }; } Object.defineProperty(CarouselComponent.prototype, "isLandscape", { get: function () { return window.innerWidth > window.innerHeight; }, enumerable: true, configurable: true }); Object.defineProperty(CarouselComponent.prototype, "isSafari", { get: function () { var ua = navigator.userAgent.toLowerCase(); if (ua.indexOf('safari') !== -1) { return !(ua.indexOf('chrome') > -1); } }, enumerable: true, configurable: true }); Object.defineProperty(CarouselComponent.prototype, "counter", { get: function () { return this.carousel.slideCounter + 1 + this.counterSeparator + this.images.length; }, enumerable: true, configurable: true }); Object.defineProperty(CarouselComponent.prototype, "images", { get: function () { return this._images; }, set: function (images) { this._images = images; }, enumerable: true, configurable: true }); Object.defineProperty(Carou