UNPKG

tt-mp

Version:

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

1 lines 4.92 kB
"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames=_interopRequireDefault(require("../helpers/classNames")),_styleToCssString=_interopRequireDefault(require("../helpers/styleToCssString")),_checkIPhoneX=require("../helpers/checkIPhoneX");function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _slicedToArray(t,e){return _arrayWithHoles(t)||_iterableToArrayLimit(t,e)||_unsupportedIterableToArray(t,e)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function _iterableToArrayLimit(t,e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t)){var r=[],n=!0,a=!1,o=void 0;try{for(var i,s=t[Symbol.iterator]();!(n=(i=s.next()).done)&&(r.push(i.value),!e||r.length!==e);n=!0);}catch(t){a=!0,o=t}finally{try{n||null==s.return||s.return()}finally{if(a)throw o}}return r}}function _arrayWithHoles(t){if(Array.isArray(t))return t}function ownKeys(e,t){var r,n=Object.keys(e);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(e),t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)),n}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach(function(t){_defineProperty(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function _defineProperty(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,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,r=(0,_styleToCssString.default)({height:e});r!==this.data.extStyle&&this.setData({extStyle:r})},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,r){var t;e===this.data.current&&r===this.data.currentName||((t=this.data.sections.filter(function(t){return t.index===e&&t.name===r})[0])&&this.setData({current:e,currentName:r,scrollTop:t.top}),this.vibrateShort()),this.triggerEvent("change",{index:e,name:r})},onTouchStart:function(t){var e,r,n;this.data.moving||(r=(e=t.target.dataset).index,n=e.name,this.setActive(r,n),this.setData({moving:!0}))},onTouchMove:function(t){var e,r,n,a=this.getTargetFromPoint(t.changedTouches[0].pageY);void 0!==a&&(r=(e=a.dataset).index,n=e.name,this.setActive(r,n))},onTouchEnd:function(){var t=this;this.data.moving&&setTimeout(function(){return t.setData({moving:!1})},300)},onScroll:function(t){var r,n=this;this.data.moving||(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(_objectSpread({},t),{},{offsets:[t.top,t.top+t.height]})})})}).exec()},getTargetFromPoint:function(t){for(var e,r=this.data.points,n=r.length-1;0<=n;n--){var a=_slicedToArray(r[n].offsets,2),o=a[0],i=a[1];if(n===r.length-1&&i<t||0===n&&t<o||o<=t&&t<=i){e=r[n];break}}return e}},created:function(){var t=(0,_checkIPhoneX.getSystemInfo)();this.vibrateShort=function(){"devtools"!==t.platform&&tt.vibrateShort()}},ready:function(){this.updateStyle(),this.getNavPoints()}});