UNPKG

9.94 kBJavaScriptView Raw
1var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
2
3var _class, _temp;
4
5function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
10
11function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
12
13import React, { Component } from 'react';
14import PropTypes from 'prop-types';
15import Instance from './instance.js';
16
17var Carrousel = (_temp = _class = function (_Component) {
18 _inherits(Carrousel, _Component);
19
20 function Carrousel(props) {
21 _classCallCheck(this, Carrousel);
22
23 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
24
25 _this.componentDidUpdate = function (prevProps) {
26 if (_this.instance && _this.instance.activeIndex !== _this.props.activeIndex) {
27 _this.instance.slideTo(_this.props.activeIndex, _this.props.speed, _this.props.enableOnChange);
28 }
29 if (_this.props.stopPropagation !== prevProps.stopPropagation) {
30 _this.instance.setParams({ stopPropagation: _this.props.stopPropagation });
31 }
32 if (!_this.props.list.equals(prevProps.list)) {
33 _this.update();
34 }
35 };
36
37 _this.componentDidMount = function () {
38 _this.instance();
39 // 轮播图片, 自适应的情况下, 高度需要计算
40 if (!(_this.props.style && _this.props.style.height) && _this.props.list.length && _this.props.delay) {
41 setTimeout(function () {
42 _this.instance.updateContainerSize();
43 }, _this.props.delay);
44 }
45 };
46
47 _this.instance = function () {
48 var instance = new Instance(_this.$el, {
49 height: _this.props.style && _this.props.style.height ? _this.props.style.height : null,
50 width: _this.props.style && _this.props.style.width ? _this.props.style.width : null,
51 stopPropagation: _this.props.stopPropagation,
52 autoplay: _this.props.autoplay,
53 slidesPerView: _this.props.slidesPerView,
54 loop: _this.props.loop,
55 onClick: _this.onClick,
56 onSlideChangeEnd: _this.props.onChange ? _this.props.onChange : null
57 });
58 _this.instance = instance;
59 };
60
61 _this.onClick = function (s, e) {
62 var index = s.activeIndex;
63 if (_this.props.onClick) _this.props.onClick(_this.props.list[index], index, s, e);
64 };
65
66 _this.getCarrouselClassName = function () {
67 var _this$props = _this.props,
68 className = _this$props.className,
69 list = _this$props.list;
70
71 if (className) {
72 if (className.indexOf('carrousel-container') !== -1 || className.indexOf('carrousel-page') !== -1) {
73 return className;
74 }
75 }
76 return (list.length > 0 ? 'carrousel-container' : 'carrousel-page') + (className ? ' ' + className : '');
77 };
78
79 _this.getSlideStyle = function (item) {
80 var slideParams = _this.props.slideParams;
81
82 if (item.bg) {
83 return Object.assign({ backgroundImage: 'url(' + _this.props.defaultSrc + ')' }, slideParams.style);
84 }
85 return slideParams.style;
86 };
87
88 _this.update = function () {
89 // 更新为默认图片
90 var imgs = _this.$el.querySelectorAll('.carrousel-lazy');
91 for (var i = 0; i < imgs.length; i++) {
92 var imgTarget = imgs[i];
93 if (!imgTarget) continue;
94 if (imgTarget.tagName === 'IMG') {
95 imgTarget.src = _this.props.defaultSrc;
96 } else {
97 imgTarget.style.backgroundImage = 'url(' + _this.props.defaultSrc + ')';
98 }
99 }
100 // 更新Carrousel
101 if (_this.instance) _this.instance.update();
102 };
103
104 return _this;
105 }
106
107 Carrousel.prototype.render = function render() {
108 var _this2 = this;
109
110 var _props = this.props,
111 className = _props.className,
112 style = _props.style,
113 slideParams = _props.slideParams,
114 pagination = _props.pagination,
115 paginationParams = _props.paginationParams,
116 prevParams = _props.prevParams,
117 nextParams = _props.nextParams,
118 stopPropagation = _props.stopPropagation,
119 activeIndex = _props.activeIndex,
120 loop = _props.loop,
121 autoplay = _props.autoplay,
122 slidesPerView = _props.slidesPerView,
123 defaultSrc = _props.defaultSrc,
124 list = _props.list,
125 enableOnChange = _props.enableOnChange,
126 speed = _props.speed,
127 onClick = _props.onClick,
128 onChange = _props.onChange,
129 delay = _props.delay,
130 children = _props.children,
131 others = _objectWithoutProperties(_props, ['className', 'style', 'slideParams', 'pagination', 'paginationParams', 'prevParams', 'nextParams', 'stopPropagation', 'activeIndex', 'loop', 'autoplay', 'slidesPerView', 'defaultSrc', 'list', 'enableOnChange', 'speed', 'onClick', 'onChange', 'delay', 'children']);
132
133 var childrenArr = React.Children.toArray(children);
134 return React.createElement(
135 'div',
136 _extends({ ref: function ref(el) {
137 _this2.$el = el;
138 }, className: this.getCarrouselClassName(), style: style }, others),
139 React.createElement(
140 'div',
141 { className: 'carrousel-wrapper' },
142 list.length > 0 && list.map(function (item, index) {
143 return React.createElement(
144 'div',
145 { className: 'carrousel-slide' + (slideParams.className ? ' ' + slideParams.className : '') + (item.bg ? ' carrousel-lazy' : ''), style: _this2.getSlideStyle(item), key: index, 'data-load-src': item.bg },
146 item.img && React.createElement('img', { className: 'carrousel-slide-img carrousel-lazy', alt: '', src: defaultSrc, 'data-load-src': item.img }),
147 item.caption && React.createElement(
148 'div',
149 { className: 'carrousel-summary' },
150 item.iconParams && item.iconParams.className && React.createElement('i', _extends({}, item.iconParams, { className: 'icon carrousel-summary-icon' + (item.iconParams.className ? ' ' + item.iconParams.className : '') })),
151 React.createElement(
152 'span',
153 { className: 'nowrap carrousel-summary-caption', style: { marginRight: '20px' } },
154 item.caption
155 )
156 )
157 );
158 }),
159 list.length === 0 && childrenArr && childrenArr.map(function (item, index) {
160 return React.createElement(
161 'div',
162 { className: 'carrousel-slide', key: index },
163 item
164 );
165 })
166 ),
167 pagination === true && React.createElement('div', _extends({}, paginationParams, { className: 'carrousel-pagination' + (paginationParams.className ? ' ' + paginationParams.className : '') })),
168 pagination && pagination !== true && pagination,
169 list.length > 1 && React.createElement('div', _extends({}, prevParams, { className: 'carrousel-prev' + (prevParams.className ? ' ' + prevParams.className : '') })),
170 list.length > 1 && React.createElement('div', _extends({}, nextParams, { className: 'carrousel-next' + (nextParams.className ? ' ' + nextParams.className : '') }))
171 );
172 };
173
174 return Carrousel;
175}(Component), _class.defaultProps = {
176 slideParams: {},
177 paginationParams: {},
178 prevParams: {},
179 nextParams: {},
180 stopPropagation: false, // 设置为false解决与FastClick插件touch事件冲突的问题
181 activeIndex: 0,
182 page: 0,
183 loop: false,
184 pagination: false,
185 autoplay: 0,
186 slidesPerView: 1,
187 list: [],
188 defaultSrc: '//res.waiqin365.com/d/seedsui/carrousel/default.png',
189 enableOnChange: true,
190 speed: 300,
191 delay: 500
192}, _temp);
193export { Carrousel as default };
194Carrousel.propTypes = process.env.NODE_ENV !== "production" ? {
195 style: PropTypes.object, // 设置容器Style
196 className: PropTypes.string, // 设置容器className
197
198 slideParams: PropTypes.object,
199
200 pagination: PropTypes.oneOfType([// 是否显示小点点
201 PropTypes.bool, PropTypes.node]),
202 paginationParams: PropTypes.object,
203
204 prevParams: PropTypes.object,
205 nextParams: PropTypes.object,
206
207 stopPropagation: PropTypes.bool, // 是否阻止点击事件的传播, 设置为false解决与FastClick插件touch事件冲突的问题
208 activeIndex: PropTypes.number, // 默认选中第几块
209
210 loop: PropTypes.bool, // 是否循环显示
211 autoplay: PropTypes.number, // 是否自动播放
212 slidesPerView: PropTypes.number, // 一屏显示几块,默认1块
213 defaultSrc: PropTypes.string, // 默认图片
214 list: PropTypes.array, // [{bg: 'xx', img: 'xx', iconParams: {}, caption: 'xx'}]
215 enableOnChange: PropTypes.bool, // 手动调用slideTo方法是否触发onChange事件回调
216 speed: PropTypes.number, // 动画过渡的速度
217 onClick: PropTypes.func, // func(s, e)
218 onChange: PropTypes.func,
219 delay: PropTypes.number, // 延迟初始化秒数
220
221 children: PropTypes.node // 轮播页,例<Carrousel><div>第1页</div></Carrousel>
222} : {};
\No newline at end of file