UNPKG

wux-weapp

Version:

一套组件化、可复用、易扩展的微信小程序 UI 组件库

1 lines 3.88 kB
"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames=_interopRequireDefault(require("../helpers/classNames")),_styleToCssString=_interopRequireDefault(require("../helpers/styleToCssString"));function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _slicedToArray(t,e){return _arrayWithHoles(t)||_iterableToArrayLimit(t,e)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function _iterableToArrayLimit(t,e){var n=[],r=!0,i=!1,a=void 0;try{for(var o,s=t[Symbol.iterator]();!(r=(o=s.next()).done)&&(n.push(o.value),!e||n.length!==e);r=!0);}catch(t){i=!0,a=t}finally{try{r||null==s.return||s.return()}finally{if(i)throw a}}return n}function _arrayWithHoles(t){if(Array.isArray(t))return t}function _objectSpread(e){for(var t=1;t<arguments.length;t++)if(t%2){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),r.forEach(function(t){_defineProperty(e,t,n[t])})}else Object.defineProperties(e,Object.getOwnPropertyDescriptors(arguments[t]));return e}function _defineProperty(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}(0,_baseComponent.default)({relations:{"../index-item/index":{type:"child",observer:function(){this.debounce(this.updated)}}},properties:{prefixCls:{type:String,value:"wux-index"},height:{type:[String,Number],value:300,observer:"updateStyle"},showIndicator:{type:Boolean,value:!0}},data:{scrollTop:0,sections:[],moving:!1,current:0,currentName:"",extStyle:""},computed:{classes:["prefixCls",function(t){return{wrap:(0,_classNames.default)(t),nav:"".concat(t,"__nav"),navItem:"".concat(t,"__nav-item"),indicator:"".concat(t,"__indicator")}}]},methods:{updateStyle:function(t){var e=0<arguments.length&&void 0!==t?t:this.data.height,n=(0,_styleToCssString.default)({height:e});n!==this.data.extStyle&&this.setData({extStyle:n})},updated:function(){var t=this.getRelationNodes("../index-item/index");0<t.length&&(t.forEach(function(t,e){t.updated(e)}),setTimeout(this.getNavPoints.bind(this))),this.data.sections.length!==t.length&&this.setData({sections:t.map(function(t){return t.data})})},setActive:function(e,n){if(e!==this.data.current||n!==this.data.currentName){var t=this.data.sections.filter(function(t){return t.index===e&&t.name===n})[0];t&&this.setData({current:e,currentName:n,scrollTop:t.top})}this.triggerEvent("change",{index:e,name:n})},onTouchStart:function(t){if(!this.data.moving){var e=t.target.dataset,n=e.index,r=e.name;this.setActive(n,r),this.setData({moving:!0})}},onTouchMove:function(t){var e=this.getTargetFromPoint(t.changedTouches[0].pageY);if(void 0!==e){var n=e.dataset,r=n.index,i=n.name;this.setActive(r,i)}},onTouchEnd:function(t){var e=this;this.data.moving&&setTimeout(function(){return e.setData({moving:!1})},300)},onScroll:function(t){var n=this;if(!this.data.moving){var r=t.detail.scrollTop;this.data.sections.forEach(function(t,e){r<t.top+t.height&&r>=t.top&&(e===n.data.current&&t.name===n.data.currentName||n.setData({current:e,currentName:t.name}))})}},getNavPoints:function(){var e=this,t=".".concat(this.data.prefixCls,"__nav-item");wx.createSelectorQuery().in(this).selectAll(t).boundingClientRect(function(t){t.filter(function(t){return!t}).length||e.setData({points:t.map(function(t){return _objectSpread({},t,{offsets:[t.top,t.top+t.height]})})})}).exec()},getTargetFromPoint:function(t){for(var e,n=this.data.points,r=n.length-1;0<=r;r--){var i=_slicedToArray(n[r].offsets,2),a=i[0],o=i[1];if(r===n.length-1&&o<t||0===r&&t<a||a<=t&&t<=o){e=n[r];break}}return e}},ready:function(){this.updateStyle(),this.getNavPoints()}});