| | |
| | | import config from '../common/config'; |
| | | const { prefix } = config; |
| | | export default function transition() { |
| | | return Behavior({ |
| | | properties: { |
| | | visible: { |
| | | type: Boolean, |
| | | value: null, |
| | | observer: 'watchVisible', |
| | | }, |
| | | appear: Boolean, |
| | | name: { |
| | | type: String, |
| | | value: 'fade', |
| | | }, |
| | | durations: { |
| | | type: Number, |
| | | optionalTypes: [Array], |
| | | }, |
| | | }, |
| | | data: { |
| | | transitionClass: '', |
| | | transitionDurations: 300, |
| | | className: '', |
| | | realVisible: false, |
| | | }, |
| | | created() { |
| | | this.status = ''; |
| | | this.transitionT = 0; |
| | | }, |
| | | attached() { |
| | | this.durations = this.getDurations(); |
| | | if (this.data.visible) { |
| | | this.enter(); |
| | | } |
| | | this.inited = true; |
| | | }, |
| | | detached() { |
| | | clearTimeout(this.transitionT); |
| | | }, |
| | | methods: { |
| | | watchVisible(curr, prev) { |
| | | if (this.inited && curr !== prev) { |
| | | curr ? this.enter() : this.leave(); |
| | | } |
| | | }, |
| | | getDurations() { |
| | | const { durations } = this.data; |
| | | if (Array.isArray(durations)) { |
| | | return durations.map((item) => Number(item)); |
| | | } |
| | | return [Number(durations), Number(durations)]; |
| | | }, |
| | | enter() { |
| | | const { name } = this.data; |
| | | const [duration] = this.durations; |
| | | this.status = 'entering'; |
| | | this.setData({ |
| | | realVisible: true, |
| | | transitionClass: `${prefix}-${name}-enter ${prefix}-${name}-enter-active`, |
| | | }); |
| | | setTimeout(() => { |
| | | this.setData({ |
| | | transitionClass: `${prefix}-${name}-enter-active ${prefix}-${name}-enter-to`, |
| | | }); |
| | | }, 30); |
| | | if (typeof duration === 'number' && duration > 0) { |
| | | this.transitionT = setTimeout(this.entered.bind(this), duration + 30); |
| | | } |
| | | }, |
| | | entered() { |
| | | this.customDuration = false; |
| | | clearTimeout(this.transitionT); |
| | | this.status = 'entered'; |
| | | this.setData({ |
| | | transitionClass: '', |
| | | }); |
| | | }, |
| | | leave() { |
| | | const { name } = this.data; |
| | | const [, duration] = this.durations; |
| | | this.status = 'leaving'; |
| | | this.setData({ |
| | | transitionClass: `${prefix}-${name}-leave ${prefix}-${name}-leave-active`, |
| | | }); |
| | | clearTimeout(this.transitionT); |
| | | setTimeout(() => { |
| | | this.setData({ |
| | | transitionClass: `${prefix}-${name}-leave-active ${prefix}-${name}-leave-to`, |
| | | }); |
| | | }, 30); |
| | | if (typeof duration === 'number' && duration > 0) { |
| | | this.customDuration = true; |
| | | this.transitionT = setTimeout(this.leaved.bind(this), duration + 30); |
| | | } |
| | | }, |
| | | leaved() { |
| | | this.customDuration = false; |
| | | this.triggerEvent('leaved'); |
| | | clearTimeout(this.transitionT); |
| | | this.status = 'leaved'; |
| | | this.setData({ |
| | | transitionClass: '', |
| | | }); |
| | | }, |
| | | onTransitionEnd() { |
| | | if (this.customDuration) { |
| | | return; |
| | | } |
| | | clearTimeout(this.transitionT); |
| | | if (this.status === 'entering' && this.data.visible) { |
| | | this.entered(); |
| | | } |
| | | else if (this.status === 'leaving' && !this.data.visible) { |
| | | this.leaved(); |
| | | this.setData({ |
| | | realVisible: false, |
| | | }); |
| | | } |
| | | }, |
| | | }, |
| | | }); |
| | | } |
| | | import config from"../common/config";const{prefix:prefix}=config;export default function transition(){return Behavior({properties:{visible:{type:Boolean,value:null,observer:"watchVisible"},appear:Boolean,name:{type:String,value:"fade"},durations:{type:Number,optionalTypes:[Array]}},data:{transitionClass:"",transitionDurations:300,className:"",realVisible:!1},created(){this.status="",this.transitionT=0},attached(){this.durations=this.getDurations(),this.data.visible&&this.enter(),this.inited=!0},detached(){clearTimeout(this.transitionT)},methods:{watchVisible(t,i){this.inited&&t!==i&&(t?this.enter():this.leave())},getDurations(){const{durations:t}=this.data;return Array.isArray(t)?t.map(t=>Number(t)):[Number(t),Number(t)]},enter(){const{name:t,transitionDurations:i}=this.data,[e]=this.durations;this.status="entering",this.setData({realVisible:!0,transitionClass:`${prefix}-${t}-enter ${prefix}-${t}-enter-active`}),clearTimeout(this.transitionT),setTimeout(()=>{this.setData({transitionClass:`${prefix}-${t}-enter-active ${prefix}-${t}-enter-to`})},30),this.transitionT="number"==typeof e&&e>0?setTimeout(this.entered.bind(this),e+30):setTimeout("entering"===this.status?this.entered.bind(this):null,i+30)},entered(){this.customDuration=!1,clearTimeout(this.transitionT),this.status="entered",this.setData({transitionClass:""})},leave(){const{name:t,transitionDurations:i}=this.data,[,e]=this.durations;this.status="leaving",this.setData({transitionClass:`${prefix}-${t}-leave ${prefix}-${t}-leave-active`}),clearTimeout(this.transitionT),setTimeout(()=>{this.setData({transitionClass:`${prefix}-${t}-leave-active ${prefix}-${t}-leave-to`})},30),"number"==typeof e&&e>0?(this.customDuration=!0,this.transitionT=setTimeout(this.leaved.bind(this),e+30)):this.transitionT=setTimeout("leaving"===this.status?this.leaved.bind(this):null,i+30)},leaved(){this.customDuration=!1,this.triggerEvent("leaved"),clearTimeout(this.transitionT),this.status="leaved",this.setData({transitionClass:"",realVisible:!1})},onTransitionEnd(){this.customDuration||(clearTimeout(this.transitionT),"entering"===this.status&&this.data.visible?this.entered():"leaving"!==this.status||this.data.visible||this.leaved())}}})} |