UNPKG

5.51 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(['exports', '../../globals/js/settings', '../../globals/js/misc/mixin', '../../globals/js/mixins/create-component', '../../globals/js/mixins/init-component-by-search'], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require('../../globals/js/settings'), require('../../globals/js/misc/mixin'), require('../../globals/js/mixins/create-component'), require('../../globals/js/mixins/init-component-by-search'));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.settings, global.mixin, global.createComponent, global.initComponentBySearch);
11 global.carousel = mod.exports;
12 }
13})(this, function (exports, _settings, _mixin2, _createComponent, _initComponentBySearch) {
14 'use strict';
15
16 Object.defineProperty(exports, "__esModule", {
17 value: true
18 });
19
20 var _settings2 = _interopRequireDefault(_settings);
21
22 var _mixin3 = _interopRequireDefault(_mixin2);
23
24 var _createComponent2 = _interopRequireDefault(_createComponent);
25
26 var _initComponentBySearch2 = _interopRequireDefault(_initComponentBySearch);
27
28 function _interopRequireDefault(obj) {
29 return obj && obj.__esModule ? obj : {
30 default: obj
31 };
32 }
33
34 function _classCallCheck(instance, Constructor) {
35 if (!(instance instanceof Constructor)) {
36 throw new TypeError("Cannot call a class as a function");
37 }
38 }
39
40 var _createClass = function () {
41 function defineProperties(target, props) {
42 for (var i = 0; i < props.length; i++) {
43 var descriptor = props[i];
44 descriptor.enumerable = descriptor.enumerable || false;
45 descriptor.configurable = true;
46 if ("value" in descriptor) descriptor.writable = true;
47 Object.defineProperty(target, descriptor.key, descriptor);
48 }
49 }
50
51 return function (Constructor, protoProps, staticProps) {
52 if (protoProps) defineProperties(Constructor.prototype, protoProps);
53 if (staticProps) defineProperties(Constructor, staticProps);
54 return Constructor;
55 };
56 }();
57
58 function _possibleConstructorReturn(self, call) {
59 if (!self) {
60 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
61 }
62
63 return call && (typeof call === "object" || typeof call === "function") ? call : self;
64 }
65
66 function _inherits(subClass, superClass) {
67 if (typeof superClass !== "function" && superClass !== null) {
68 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
69 }
70
71 subClass.prototype = Object.create(superClass && superClass.prototype, {
72 constructor: {
73 value: subClass,
74 enumerable: false,
75 writable: true,
76 configurable: true
77 }
78 });
79 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
80 }
81
82 var Carousel = function (_mixin) {
83 _inherits(Carousel, _mixin);
84
85 /**
86 * Carousel.
87 * @extends CreateComponent
88 * @extends InitComponentBySearch
89 * @param {HTMLElement} element The element working as an carousel.
90 */
91 function Carousel(element, options) {
92 _classCallCheck(this, Carousel);
93
94 var _this = _possibleConstructorReturn(this, (Carousel.__proto__ || Object.getPrototypeOf(Carousel)).call(this, element, options));
95
96 _this.handleClick = function (evt) {
97 if (evt.target.matches(_this.options.selectorScrollRight)) {
98 _this.sideScroll('right');
99 } else {
100 _this.sideScroll('left');
101 }
102 };
103
104 _this.sideScroll = function (direction) {
105 var filmstripWidth = _this.filmstrip.getBoundingClientRect().width;
106 var itemWidth = _this.carouselItem.getBoundingClientRect().width + 20;
107 var re = /\.*translateX\((.*)px\)/i;
108
109 var translateXValue = _this.filmstrip.style.transform ? Number(_this.filmstrip.style.transform.split(re)[1]) : 0;
110 var directionValue = direction === 'right' ? -1 : 1;
111
112 var itemWidthDirection = itemWidth * directionValue;
113 var newTranslateValue = itemWidthDirection + translateXValue;
114 if (newTranslateValue > 0) {
115 newTranslateValue = 0;
116 }
117 if (newTranslateValue < filmstripWidth * -1) {
118 newTranslateValue = filmstripWidth * -1;
119 }
120 _this.filmstrip.style.transform = 'translateX(' + newTranslateValue + 'px)';
121 };
122
123 _this.filmstrip = _this.element.querySelector(_this.options.selectorFilmstrip);
124 _this.carouselItem = _this.element.querySelector(_this.options.selectorCarouselItem);
125
126 _this.element.addEventListener('click', function (evt) {
127 return _this.handleClick(evt);
128 });
129 return _this;
130 }
131
132 _createClass(Carousel, null, [{
133 key: 'options',
134 get: function get() {
135 var prefix = _settings2.default.prefix;
136
137 return {
138 selectorInit: '[data-carousel]',
139 selectorFilmstrip: '.' + prefix + '--filmstrip',
140 selectorScrollRight: '[data-scroll-right]',
141 selectorScrollLeft: '[data-scroll-left]',
142 selectorCarouselBtn: '.' + prefix + '--carousel__btn',
143 selectorCarouselItem: '.' + prefix + '--carousel__item'
144 };
145 }
146 }]);
147
148 return Carousel;
149 }((0, _mixin3.default)(_createComponent2.default, _initComponentBySearch2.default));
150
151 Carousel.components = new WeakMap();
152 exports.default = Carousel;
153});
\No newline at end of file