UNPKG

@ohu-mobile/core

Version:
184 lines (183 loc) 8.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props")); var _defineComponent = require("../_utils/defineComponent"); var _Stage = _interopRequireWildcard(require("./Stage")); var _debounce = _interopRequireDefault(require("../_utils/debounce")); var _bindEvent = _interopRequireDefault(require("../_utils/bindEvent")); var _excluded = ["width", "height", "indicatorDarkMode", "autoplay", "indicator", "mode", "interval"]; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Carousel = (0, _defineComponent.defineComponent)('carousel').create({ props: _objectSpread({ autoplay: (0, _defineComponent.props)(Boolean).default(false), interval: (0, _defineComponent.props)(Number).default(3000), indicator: (0, _defineComponent.props)(Boolean).default(true), indicatorDarkMode: (0, _defineComponent.props)(Boolean).default(false), width: String, height: String, resize: (0, _defineComponent.props)(Boolean).default(false) }, _Stage.stageProps), computed: { stage: function stage() { return this.$refs.stage; } }, watch: { autoplay: function autoplay(cur) { cur === true && this.startPlay(); }, resize: function resize(cur) { cur === true && this.handleResize(); } }, data: function data() { return { timer: null, frozen: false }; }, methods: { goTo: function goTo(index) { this.stage.goTo(index); }, prev: function prev() { this.stage.prev(); }, next: function next() { this.stage.next(this.autoplay); }, startPlay: function startPlay() { var _this = this; if (!this.autoplay) return; this.stopPlay(); this.timer = setInterval(function () { _this.next(); }, this.interval); this.$once('hook:beforeDestroy', function () { _this.timer && clearTimeout(_this.timer); }); // when use keepAlive this.$once('hook:activated', function () { if (_this.timer) return; _this.startPlay(); _this.frozen = false; }); this.$once('hook:deactivated', function () { _this.stopPlay(); _this.frozen = true; }); }, stopPlay: function stopPlay() { if (!this.autoplay) return; this.timer && clearTimeout(this.timer); this.timer = null; }, relayout: function relayout() { var _this$stage; this.stopPlay(); (_this$stage = this.stage) === null || _this$stage === void 0 || _this$stage.init(); this.startPlay(); }, handleResize: function handleResize() { var _this2 = this; var handler = (0, _debounce.default)(function () { if (_this2.frozen) return; _this2.relayout(); }, 500); (0, _bindEvent.default)(this, 'resize', handler); }, handleVisibleChange: function handleVisibleChange() { var _this3 = this; var handler = function handler() { if (_this3.frozen) return; if (document.hidden) { _this3.stopPlay(); } else { _this3.startPlay(); } }; (0, _bindEvent.default)(this, 'visibilitychange', handler); } }, mounted: function mounted() { this.startPlay(); if (this.resize) { this.handleResize(); } this.handleVisibleChange(); }, render: function render() { var _this4 = this; var h = arguments[0]; var root = this.$rootCls(); var indicatorClass = root.element('indicator'); var $slots = this.$slots, direction = this.direction; var _this$$props = this.$props, width = _this$$props.width, height = _this$$props.height, indicatorDarkMode = _this$$props.indicatorDarkMode, autoplay = _this$$props.autoplay, indicator = _this$$props.indicator, mode = _this$$props.mode, interval = _this$$props.interval, stageProps = _objectWithoutProperties(_this$$props, _excluded); return h("div", { "class": root, "style": { height: height, width: width } }, [h(_Stage.default, (0, _babelHelperVueJsxMergeProps.default)([{}, { props: stageProps, scopedSlots: { indicator: function indicator(_ref) { var steps = _ref.steps, stepIndex = _ref.stepIndex; if (!_this4.indicator) { return; } return h("div", { "class": indicatorClass.is(direction) }, [Array(steps).fill(0).map(function (_, index) { var dot = indicatorClass.element('dot'); dot.is([stepIndex === index && 'active', indicatorDarkMode && 'dark']); return h("span", { "class": dot, "attrs": { "tabindex": 0, "role": "button" }, "on": { "click": function click() { _this4.goTo(index); } } }); })]); } }, on: { change: function change(e) { _this4.$emit('input', e.to); _this4.$emit('change', e); } }, ref: 'stage' }]), [$slots.default])]); } }); var _default = exports.default = Carousel;