tt-mp
Version:
一套组件化、可复用、易扩展的头条小程序 UI 组件库
1 lines • 5.99 kB
JavaScript
"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames4=_interopRequireDefault(require("../helpers/classNames")),_gestures=require("../helpers/gestures");function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function ownKeys(e,t){var n,i=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),i.push.apply(i,n)),i}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(n),!0).forEach(function(t){_defineProperty(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):ownKeys(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}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 n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function _iterableToArrayLimit(t,e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t)){var n=[],i=!0,s=!1,o=void 0;try{for(var r,a=t[Symbol.iterator]();!(i=(r=a.next()).done)&&(n.push(r.value),!e||n.length!==e);i=!0);}catch(t){s=!0,o=t}finally{try{i||null==a.return||a.return()}finally{if(s)throw o}}return n}}function _arrayWithHoles(t){if(Array.isArray(t))return t}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:{"../swipe-action-group/index":{type:"ancestor"}},properties:{prefixCls:{type:String,value:"wux-swipe"},autoClose:{type:Boolean,value:!1},disabled:{type:Boolean,value:!1},left:{type:Array,value:[],observer:"updateBtns"},right:{type:Array,value:[],observer:"updateBtns"},useSlots:{type:Boolean,value:!1},data:{type:null,value:null}},data:{index:0,swiping:!1,showCover:!1,offsetStyle:""},computed:{classes:["prefixCls, swiping",function(t,e){return{wrap:(0,_classNames4.default)(t,_defineProperty({},"".concat(t,"--swiping"),e)),cover:"".concat(t,"__cover"),left:(0,_classNames4.default)("".concat(t,"__actions"),_defineProperty({},"".concat(t,"__actions--left"),!0)),right:(0,_classNames4.default)("".concat(t,"__actions"),_defineProperty({},"".concat(t,"__actions--right"),!0)),action:"".concat(t,"__action"),text:"".concat(t,"__text"),content:"".concat(t,"__content")}}]},methods:{updated:function(t){this.data.index!==t&&this.setData({index:t})},onCloseSwipe:function(){var t=this.getRelationNodes("../swipe-action-group/index")[0];t&&t.onCloseSwipe(this.data.index)},getContentEasing:function(t,e){var n=Math.abs(t)-Math.abs(e),i=0<e?1:-1;return 0<n?(t=e+Math.pow(n,.85)*i,Math.abs(t)>Math.abs(e)?e:t):t},setStyle:function(t){var e=0<t?this.btnsLeftWidth:-this.btnsRightWidth,n=this.getContentEasing(t,e),i="left: ".concat(n,"px"),s=0<Math.abs(t);this.data.offsetStyle===i&&this.data.showCover===s||this.setData({offsetStyle:i,showCover:s})},updateBtns:function(){var s=this,t=this.data.prefixCls,e=tt.createSelectorQuery().in(this);e.select(".".concat(t,"__actions--left")).boundingClientRect(),e.select(".".concat(t,"__actions--right")).boundingClientRect(),e.exec(function(t){var e=_slicedToArray(t,2),n=e[0],i=e[1];s.btnsLeftWidth=n?n.width:0,s.btnsRightWidth=i?i.width:0})},onTap:function(t){var e=t.currentTarget.dataset.type,n=_objectSpread(_objectSpread({},t.currentTarget.dataset),{},{buttons:this.data[e],data:this.data.data});this.data.autoClose&&this.onClose(),this.triggerEvent("click",n)},onAcitons:function(){this.data.autoClose&&this.onClose()},onOpen:function(t,e,n){this.openedLeft||this.openedRight||this.triggerEvent("open"),this.openedLeft=e,this.openedRight=n,this.setStyle(t)},onClose:function(){(this.openedLeft||this.openedRight)&&this.triggerEvent("close"),this.openedLeft=!1,this.openedRight=!1,this.setStyle(0)},onOpenLeft:function(){this.onOpen(this.btnsLeftWidth,!0,!1)},onOpenRight:function(){this.onOpen(-this.btnsRightWidth,!0,!1)},onTouchStart:function(t){this.data.disabled||1<(0,_gestures.getPointsNumber)(t)||(this.start=(0,_gestures.getTouchPoints)(t),this.onCloseSwipe())},onTouchMove:function(t){var e,n,i,s,o,r,a,h;this.data.disabled||1<(0,_gestures.getPointsNumber)(t)||(this.move=(0,_gestures.getTouchPoints)(t),e=this.move.x-this.start.x,s="Right"===(n=(0,_gestures.getSwipeDirection)(this.start.x,this.move.x,this.start.y,this.move.y)),((i="Left"===n)||s)&&(r=(o=this.data).left,a=o.right,h=o.useSlots,this.needShowRight=i&&(h||0<a.length),this.needShowLeft=s&&(h||0<r.length),(this.needShowLeft||this.needShowRight)&&(this.swiping=!0,this.setData({swiping:!0}),this.setStyle(e))))},onTouchEnd:function(t){var e,n,i;this.data.disabled||1<(0,_gestures.getPointsNumber)(t)||!this.swiping||(this.end=(0,_gestures.getTouchPoints)(t),e=this.end.x-this.start.x,n=this.needShowRight&&Math.abs(e)>this.btnsRightWidth/2,i=this.needShowLeft&&Math.abs(e)>this.btnsLeftWidth/2,n?this.onOpenRight():i?this.onOpenLeft():this.onClose(),this.swiping=!1,this.setData({swiping:!1}),this.needShowLeft=!1,this.needShowRight=!1)},noop:function(){}},created:function(){this.btnsLeftWidth=0,this.btnsRightWidth=0,this.openedLeft=!1,this.openedRight=!1,this.needShowLeft=!1,this.needShowRight=!1},ready:function(){this.updateBtns()}});