UNPKG

@ohu-mobile/core

Version:
278 lines (277 loc) 11.5 kB
"use strict"; 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); } 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 _List = _interopRequireDefault(require("../List")); var _bom = require("../_utils/bom"); var _variables = require("../_config/variables"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } var createIndexList = (0, _defineComponent.defineAnc)('index-list'); var _default = exports.default = createIndexList.create({ props: { label: (0, _defineComponent.props)(Boolean).default(true), enableIndex: (0, _defineComponent.props)(Boolean).default(true), innerScroll: (0, _defineComponent.props)(Boolean).default(false), indexes: (0, _defineComponent.props)(Array).optional, listProps: _defineComponent.props.ofObject().optional, barClass: _defineComponent.props.ofType().optional }, watch: { enableIndex: function enableIndex(cur) { if (cur === true) { this.detectScollPosition(); } else { this.cleanScroll(); } } }, computed: { currentGroup: function currentGroup() { var map = this.indexMap; var index = this.enterIndex; if (index !== null) { return map[index]; } }, scroller: function scroller() { if (this.innerScroll) { var list = this.$refs.list; if (list) { return list.$el; } } return window; } }, data: function data() { return { children: [], indexMap: {}, enterIndex: null, observer: null, pressed: false, currentSwipeY: -1, handler: null }; }, methods: { removeGroup: function removeGroup(group) { var index = this.children.indexOf(group); if (index >= 0) { this.children.splice(index, 1); delete this.indexMap[group.$props.index]; } }, addGroup: function addGroup(group) { this.children.push(group); this.indexMap[group.$props.index] = group; }, handleIndexBarMouseDown: function handleIndexBarMouseDown(e) { e.preventDefault(); this.pressed = true; this.scrollByPostion(e.clientY); }, handleIndexBarMouseMove: function handleIndexBarMouseMove(e) { e.preventDefault(); if (!this.pressed) return; this.scrollByPostion(e.clientY); }, handleIndexBarTouchStart: function handleIndexBarTouchStart(e) { e.preventDefault(); this.pressed = true; var touch = e.touches[0]; this.scrollByPostion(touch.clientY); }, handleIndexBarTouchMove: function handleIndexBarTouchMove(e) { e.preventDefault(); if (!this.pressed) return; var touch = e.touches[0]; this.scrollByPostion(touch.clientY); }, handleIndexBarEventEnd: function handleIndexBarEventEnd(e) { e.preventDefault(); this.pressed = false; this.currentSwipeY = -1; }, scrollByPostion: function scrollByPostion(y) { var result = this.meetIndex(y); if (result !== undefined) { var index = result.index, offsetY = result.offsetY; if (index === undefined) return; this.currentSwipeY = offsetY; this.scrollToIndex(index); } else { this.currentSwipeY = -1; } }, // ↓ open api scrollToIndex: function scrollToIndex(index) { var group = this.getGroup(index); if (!group) return; this.enterIndex = index; if (this.currentGroup) { this.$emit('select', { index: index, group: this.currentGroup.$props }); this.currentGroup.$el.scrollIntoView(); } }, // ↑ open api meetIndex: function meetIndex(touchTop) { var indexBar = this.$refs.indexBar; if (!indexBar) return; var indexBarItems = indexBar.querySelectorAll('li'); var items = _toConsumableArray(indexBarItems); var currentOffsetY = -1; var indexBarRect = indexBar.getBoundingClientRect(); var meetEl = items.find(function (node, index) { var _node$getBoundingClie = node.getBoundingClientRect(), top = _node$getBoundingClie.top, bottom = _node$getBoundingClie.bottom, height = _node$getBoundingClie.height; currentOffsetY = top - indexBarRect.top + height / 2; if (index === 0 && touchTop < top) { return true; } if (index === items.length - 1 && touchTop > bottom) { return true; } return touchTop >= top && touchTop < bottom; }); if (meetEl && meetEl.dataset.index) { var dataIndex = meetEl.dataset.index; var group = this.getGroup(dataIndex); return { index: group.$props.index, offsetY: currentOffsetY }; } }, meetGroup: function meetGroup() { var _this = this; var group = this.children.find(function (item) { return (0, _bom.isElementSticky)(_this.scroller, item.$el); }); if (group) { return group.$props.index; } }, cleanScroll: function cleanScroll() { if (this.handler) { this.scroller.removeEventListener('scroll', this.handler); this.handler = null; } }, detectScollPosition: function detectScollPosition() { var _this2 = this; this.cleanScroll(); var handler = function handler() { var groupIndex = _this2.meetGroup(); if (groupIndex && !_this2.pressed) { _this2.enterIndex = groupIndex; } }; this.handler = handler; handler(); this.scroller.addEventListener('scroll', handler); this.$once('hook:beforeDestroyed', function () { _this2.cleanScroll(); }); }, getGroup: function getGroup(index) { return this.indexMap[index]; }, renderAnchor: function renderAnchor(index) { var h = this.$createElement; var isEnter = index === this.enterIndex; var group = this.getGroup(index) || {}; return h("li", { "key": index, "attrs": { "data-index": index }, "class": { 'is-active': isEnter, 'is-pressed': this.pressed && isEnter } }, [h("span", [this.$scopedSlots.anchor ? this.$scopedSlots.anchor(_objectSpread({ pressed: this.pressed, active: isEnter }, group.$props)) : index])]); } }, mounted: function mounted() { if (this.enableIndex) { this.detectScollPosition(); } }, render: function render() { var _this3 = this; var h = arguments[0]; var root = this.$rootCls(); var $slots = this.$slots, currentSwipeY = this.currentSwipeY; var _this$$props = this.$props, label = _this$$props.label, listProps = _this$$props.listProps; var indexBar = root.element('bar'); if (!this.innerScroll) { indexBar.is('fixed'); } if (this.barClass) { indexBar.addClasses(this.barClass); } var labelStyle = { transform: "translate(-100%, ".concat(currentSwipeY, "px)") }; return h("div", { "class": root }, [h(_List.default, (0, _babelHelperVueJsxMergeProps.default)([{}, { props: listProps, class: { 'is-scroll': this.innerScroll }, ref: 'list' }]), [$slots.default]), h("transition", { "attrs": { "name": "".concat(_variables.$prefix, "slide-right") } }, [this.enableIndex && h("div", { "class": indexBar, "ref": "indexBar", "on": { "touchstart": this.handleIndexBarTouchStart, "touchmove": this.handleIndexBarTouchMove, "touchend": this.handleIndexBarEventEnd, "mousedown": this.handleIndexBarMouseDown, "mousemove": this.handleIndexBarMouseMove, "mouseup": this.handleIndexBarEventEnd } }, [h("ul", [this.indexes instanceof Array ? this.indexes.map(function (index) { return _this3.renderAnchor(index); }) : this.children.map(function (item) { var index = item.$props.index; return _this3.renderAnchor(index); })]), label && this.pressed && this.currentSwipeY >= 0 && this.currentGroup && h("div", { "style": labelStyle, "class": indexBar.element('label') }, [this.$scopedSlots.label ? this.$scopedSlots.label(this.currentGroup.$props) : h("span", [this.currentGroup.$props.title || this.enterIndex])])])])]); } });