text: import{pluckNumber,hasSVG}from'../lib';import{priorityList}from'../schedular';import{ComponentInterface}from'../component-interface';import{getDep,getDepsByType}from'../dependency-manager';let UNDEF;const isIE8=!hasSVG,OBJECT='object',CHART='chart',GROUP='group',HTML='html',TEXT='text',PAPER='paper',DISAPPEARING='disappearing',APPEARING='appearing',STRING='string',UPDATING='updating',FUNCTION='function',LINEAR='linear',CHARTSPECIFIC='chartSpecific',GLOBAL='global',DEFAULT='default',STARSTRING='*',durationList={initial:1,update:0,realTimeUpdate:0,resize:0,dispose:0,legendInteraction:0,scroll:0,mouseOut:0,mouseOver:0,default:0,slicing:.25,timenavSqueeze:0,selectedRange:0,timenavScroll:0},rulePriorityList=['default','global','chartSpecific'],animMap={fadeIn:{opacity:{start:0,end:1}},fadeOut:{opacity:{start:1,end:0}}},callbackMap={fadeIn:function(){this&&this.show()},fadeOut:function(){this&&this.hide()}},isNull=a=>null===a,getRegisteredThemes=a=>{let b,c={};for(var d in a)a.hasOwnProperty(d)&&(b=a[d],c[b.name]=b);return c},fetchRulesFromTheme=(a,b='')=>{let c=b.split(','),d=[];return c.forEach(b=>{a[b]&&a[b].animation&&d.push(a[b].animation)}),d},shiftAnimTime=function(a,b,c){var d,e=Math.round,f=Object.assign({},a),g=c/b,h=.2;return g>h&&(g=h),d=f.end-f.start,f.start*=1-g,f.start+=g,f.end=f.start+(1-g)*d,f.start=e(100*f.start)/100,f.end=e(100*f.end)/100,f},_applyAnim=function(a,b,c){var d,e=animMap[b],f='',g={};if(e&&a&&a.el&&a.el[0]){for(f in e)g[f]=a.el.attrs[f],g[f]===UNDEF&&(g[f]=null),a.attrs[f]=e[f].end,a.el.attr(f,e[f].start);d=a.callback,a.callback=function(){c&&callbackMap[b]&&callbackMap[b].call(this),d&&d.call&&d.call(this),this.attr(g)}}},applyType=function(a,b){var c,d=!0;if(b){for(a.attrs=a.attrs||{},b=b.split('-'),~b.indexOf('nc')&&(d=!1),c=b.length;c--;)_applyAnim(a,b[c],d);return a}},animateEl=function(a,b,c,d,e,f,g,h){a.animateWith(e,f,b,g,h,c,d)},execPreAnimFns=function(a){var b=0,c=a.length;for(b=0;b<c;++b)a[b].fn&&a[b].fn.call(a[b].el)},ruleReducer=function(a){let b={};for(let c in a)if(a.hasOwnProperty(c)&&'__esModule'!==c){let d=a[c];merge(b,d)}return b},merge=(a,b)=>{for(let c in b)b.hasOwnProperty(c)&&(a[c]||(a[c]=[]),a[c].push(b[c]))},isFunction=a=>typeof a==FUNCTION;function hasAnimation(a,b){var c,d,e,f,g,h,k=!1;if(!a)return!1;for(e=0,f=a.length;e<f;e+=1)if(d=a[e],!Array.isArray(d))c=!1,-1===b.indexOf(d.name)?d.child&&(c=d.hasAnimation=hasAnimation(d.child,b)):c=!0,k=k||c,d.hasAnimation=c;else for(g=0,h=a[e].length;g<h;g+=1)d=a[e][g],c=!1,-1===b.indexOf(d.name)?d.child&&(c=d.hasAnimation=hasAnimation(d.child,b)):c=!0,k=k||c,d.hasAnimation=c;return k}function buildDynamicRules(a,b,c,d){var f,g,h,j,k,l=a.length,m=0,n=0,o=b;for(g=0,h=0,k=0;k<l;k+=1)f=a[k],f.hasAnimation||!1===f.collapsible?h+=1:g+=f.duration,n+=f.duration;for(g/=h,k=0;k<l;k+=1)(f=a[k],!!f.hasAnimation)&&(m=o,j=c-b,o=m+(f.duration/n+g)*j,d[f.name]={start:m,end:o,effect:f.effect,transition:f.transition},f.child&&buildWorkingRule(f.child,m,o,d))}function buildStaticRules(a,b,c,d){var f,g,h;g=a.start,h=a.end,f=c-b,g=b+g*f,h=b+h*f,d[a.name]={start:g,end:h,effect:a.effect,transition:a.transition},a.child&&buildWorkingRule(a.child,g,h,d)}function buildWorkingRule(a,b,c,d){var e,f,g=a.length;for(e=0;e<g;e++)f=a[e],Array.isArray(f)?buildDynamicRules(f,b,c,d):buildStaticRules(f,b,c,d)}animMap.fadein=animMap.fadeIn,animMap.fadeout=animMap.fadeOut;class AnimationManager extends ComponentInterface{getType(){return'animationManager'}constructor(){super();var a=this;a._removedGraphics=[],a._animCallBack=function(){if(!a.getState('removed')&&!this.removed){let b=a.getAnimationState();a._removeElems(),a.reset(),a.fireEvent('animationComplete',{currAnimState:b})}},a.startAnimation=()=>{a.kickStart(),a.animate()}}getName(){return'animationManager'}stopAnimation(){if(this.getState('chartAnimating')){var a,b=this,c=b.config,d=c.animationObj,e=d&&d.duration;e?(a=b.getGraphicalElement().dummyObj,a&&a.stop(UNDEF,!0,!0)):0===e&&(b.removeJob('anim-callBack'),b._animCallBack())}}kickStart(){let a,b,c,d=this,e=d.config,f=d.getGraphicalElement(),g=d.getFromEnv(PAPER),h=e.animationObj,i=e.animationeffect;b=h.duration,h.animObj=c=getDep('redraphael','plugin').animation({x:0},b,i,d._animCallBack,!0),h.animType=i,(a=f.dummyObj)?a.attr({x:100}):(a=g.rect({x:100,y:0,width:10,height:30}),d.addGraphicalElement('dummyObj',a),a.hide()),h.dummyObj=f.dummyObj,a.stop(UNDEF,!0,!0),d.fireEvent('animationStart',{duration:b}),d.setState('chartAnimating',!0),a.animate(c)}setAnimationState(a=UNDEF){a&&this.stopAnimation(),this.config.__state=a,this._setAnimDuration(a)}getAnimationState(){return this.config&&this.config.__state}_setAnimDuration(a=DEFAULT){var b=this,c=b.config,d=c.animationObj||(c.animationObj={}),e=c.animationDuration||(c.animationDuration={}),f=b.getFromEnv(CHART).getFromEnv('chart-attrib')||{},g=a+'AnimDuration',h=c.killSwitch,i=h?0:1e3*(e[g]||f[g.toLowerCase()]||AnimationManager.getFallbackAnimDuration(a));d&&(d[g]=i,d.duration=+i)}configureAttributes(){var a=this,b=a.getFromEnv(CHART),c=b.config,d=a.config,e=b.getFromEnv('chart-attrib')||{};d.killSwitch=!pluckNumber(e.animation,c.animation,1),d.animationeffect=c.animationeffect||'linear',a.invokeAnimationRules(),d.workingRules||(d.workingRules={}),d.animateArr||(d.animateArr=[]),d.animationName||(d.animationName=[])}invokeAnimationRules(){let a,b=this,c=b.getFromEnv(CHART),d=c.getFromEnv('chart-attrib')||{},e=c.config||{},f=b.config,g=c.getFromEnv('chartInstance'),h=g.args.animation,i=c.getFromEnv('core-options').defaultTheme,j=getRegisteredThemes(getDepsByType('theme')),k=(c.getFromEnv('core-options')._globalAnimationRule||[]).concat(fetchRulesFromTheme(j,i)),l=(h&&[h]||[]).concat(fetchRulesFromTheme(j,e.theme));f.animationRules={global:ruleReducer(k.map(a=>a&&a.rule).filter(a=>a)),chartSpecific:ruleReducer(l.map(a=>a&&a.rule).filter(a=>a)),default:ruleReducer(getDepsByType('animationRule'))},f.animationDuration=a={},k.map(b=>Object.assign(a,b.duration)),l.map(b=>Object.assign(a,b.duration)),a.initialAnimDuration=pluckNumber(a.initialAnimDuration,d.initialanimduration,d.animationduration),b.config.slots=AnimationManager.getFallbackAnimTimeSlots().concat([].concat.apply([],k.map(a=>a.slots)).filter(a=>a)).concat([].concat.apply([],l.map(a=>a.slots)).filter(a=>a))}reInitialize(){var a=this,b=a.config,c=b.animationObj,d=c.duration;a.setState('initialized',!0),d?a.addJob('start-animation',a.startAnimation,priorityList.animation):(a.fireEvent('animationStart',{duration:d}),a.setState('chartAnimating',!0),a.addJob('anim-callBack',a._animCallBack,priorityList.animation))}reset(){var a=this;a.setState('initialized',!1),a.config&&a.config.animateArr&&(a.config.animateArr.length=0),a.setState('chartAnimating',!1),a.setAnimationState()}registerAnimObj(a){for(var b in a)this.registerAnimation(a[b],b)}getFinalSlots(){var a=this,b=a.config,c=b.slots,d=b.workingRules={},e=b.animationName;return hasAnimation(c,e),buildWorkingRule(c,0,1,d),d['default']={start:0,end:1},d}registerOne(a,b){this.registerAnimation([{data:[a]}],b)}registerAnimation(a=[],b){var c,d,e,f=!1;for(c=0,e=a.length;c<e;c+=1)d=a[c],d.data&&d.data.length&&(this.config.animateArr.push((d.component=b,d)),f=!0);f&&this.config.animationName.push(b),this.getState('initialized')||this.reInitialize()}onAnimationComplete(a){var b=this;b.addEventListener('animationComplete',function(b){a(),b.detachHandler()})}static removeOpacityFromGroup(a,b){b&&isIE8&&(a===GROUP||a.type===GROUP)&&delete b.opacity}setAnimation(a={}){var b,c,d,e,f,g,h,j,k,l,m,n,o,p,q=this,r=q.config,s=r.animationObj,t=a.el,u=a.attr||{},v={},w={},x=function(b,c,d,e){return function(){d.callback&&d.callback.call(this),b&&this.attr(b),c&&a.callback&&a.callback.call(this),e&&this.appendTo(e)}},y=function(a,b,c){return function(){b.hookFn&&b.hookFn.call(this),a&&this.attr(a),c&&this.appendTo(c)}},z=[],A=s.duration,B=q.getState('chartAnimating'),C=typeof t==OBJECT&&a.container&&t.parent!==a.container;if(q.getState('chartAnimating')&&r.animationObj&&r.animationObj.duration&&q.setAnimationState('default'),!B&&A&&(k=q.getAnimAttributes(a))){for(j=k.length,p=!1,l=a.state===APPEARING||typeof t==STRING,m=!l&&t.attrs,h={},g=0;g<j;g++){for(e in n=g===j-1,f=z[g]={},o=k[g],b=o.finalAttr,c=o.initialAttr,AnimationManager.removeOpacityFromGroup(a.el,c),AnimationManager.removeOpacityFromGroup(a.el,b),c)h[e]=c[e],w[e]===UNDEF&&(w[e]=c[e]);for(e in b)h[e]=b[e],l&&w[e]===UNDEF&&(w[e]=b[e]);if(f.attrs=b,f.transition=o.transition,f.effect=o.effect,f.animConfig=[o.startEnd],o.callback&&(f.callback=o.callback),(p=C&&o.changeGroup)&&(C=!1),n){if(l)for(e in u)w[e]===UNDEF&&(w[e]=u[e]);for(e in u)d=u[e],(h[e]||!l)&&h[e]!==d&&(b&&b[e]===UNDEF?b[e]=d:v[e]=d);for(e in h)v[e]===UNDEF&&(d=u[e]||m[e]||'',h[e]!==d&&(b&&b[e]===UNDEF&&d!==UNDEF?b[e]=d:v[e]=d));b||(f.attrs=v),f.callback=x((b||c)&&v,n,o,C&&a.container),t=q.setAttributes(t,{attr:w,inputJSON:a})}(c||o.hookFn||p)&&(f.animConfig[0].hookFn=y((!l||g)&&c,o,p&&a.container))}for(g=0;g<j;g++)z[g].el=t.el,q.registerAnimation([{data:[z[g]]}],k[g].slot)}else B?t=q.setAttributes(t,{attr:u,inputJSON:a,groupChangeReq:C,immediate:!0}):(q.registerAnimation(),t=q.setAttributes(t,{attr:u,inputJSON:a,groupChangeReq:C,immediate:!0}));return t.removed?null:t.el}_removeElems(){if(this.getFromEnv(PAPER)&&!this.getFromEnv(PAPER).removed){let a=this;a._removedGraphics.forEach(a=>a.remove&&a.remove()),a._removedGraphics.length=0}}removeElement(a,b){let c=this;return b?a.remove():c._removedGraphics.push(a),null}static _ruleSelectorQueryBuilder(a,b=!0){let{label:c,state:d,component:e}=a,f=e.getType(),g=e.getName();if(b){let a=['*','*.*','*.*.*'],b=[`*.*.${g}`,`*.${f}.*`,`*.${f}.${g}`];return'*'===d?a.concat(b):a.concat(b,[`${d}.*`,`${d}.*.*`,`${d}.*.${g}`,`${d}.${f}.*`,`${d}.${f}.${g}`])}return[`${c}.${d}`,`${c}.*`,`*.${d}`,'*.*','*']}_getRulesByState(a){let b,c,d,e,f=this,g=a.component,h=f.getAnimationState()||STARSTRING,i=AnimationManager._ruleSelectorQueryBuilder(Object.assign(a,{state:h}));return e=g.getState('animRules')||{},(b=e[h])?b:(c=e[h]={},d=f.config.animationRules,rulePriorityList.forEach(a=>{let b=d[a];b&&i.forEach(a=>{b[a]&&(Array.isArray(b[a])?b[a].forEach(a=>{c=e[h]=isNull(a)?null:Object.assign(c||{},isFunction(a)?a.call(g):a)}):c=e[h]=isNull(b[a])?null:Object.assign(c||{},isFunction(b[a])?b[a].call(g):b[a]))})}),g.setState('animRules',e),g.getState('ruleFlushAttached')||g.addExtEventListener('predraw',()=>{g.setState('animRules',UNDEF),g.setState('ruleFlushAttached',!0)},g.getFromEnv(CHART)),c)}static _getRulesByElement(a,b){let c,d=AnimationManager._ruleSelectorQueryBuilder(b,!1);return d.forEach(b=>{void 0===c&&(c=a[b])}),c}getAnimAttributes(a){let b,c,d,e,f,g=this,h=a.component||g.getFromEnv(CHART),j=a.label||('object'==typeof a.el?a.el.type:a.el),k=a.state,l=g._getRulesByState({label:j,component:h}),m=[];if(!isNull(l)&&(k||(k=typeof a.el==STRING?APPEARING:a.attr?UPDATING:DISAPPEARING),b=AnimationManager._getRulesByElement(l,{component:h,state:k,label:j}),!isNull(b))){if(!b)return AnimationManager.getFallbackAnimRule(a);for(isFunction(b)&&(b=b.call(h,a)),f=b.length,e=0;e<f;e++)c=b[e],d={},c.initialAttr&&(d.initialAttr=isFunction(c.initialAttr)?c.initialAttr.call(h,a):Object.assign({},c.initialAttr)),c.finalAttr&&(d.finalAttr=isFunction(c.finalAttr)?c.finalAttr.call(h,a):Object.assign({},c.finalAttr)),d.startEnd=isFunction(c.startEnd)?c.startEnd.call(h,a):c.startEnd?Object.assign({},c.startEnd):{start:0,end:1},d.slot=c.slot||'default',d.callback=c.callback,d.hookFn=c.hookFn,d.transition=c.transition,d.effect=c.effect||'linear',c.groupChange&&(d.groupChange=!0),m.push(d);return m}}static getFallbackAnimRule(a){if(!(a.state===APPEARING||typeof a.el==STRING)){if(a.state===UPDATING||a.attr)return[{startEnd:{start:0,end:1}}];if(a.el.type!==GROUP)return[{initialAttr:{opacity:1},finalAttr:{opacity:0},startEnd:{start:0,end:1}}]}else if(a.el!==GROUP)return[{initialAttr:{opacity:0},finalAttr:{opacity:a.attr&&a.attr.opacity||1},startEnd:{start:0,end:1}}]}static getFallbackAnimDuration(a){return durationList[a]||0}static getFallbackAnimTimeSlots(){return[{name:'_default',start:0,end:1,child:[[{name:'initial',duration:.125},{name:'middle',duration:.75,child:[[{name:'axis',duration:.25},{name:'plot',duration:.75}]]},{name:'final',duration:.125}]]}]}createElement(a,b={},c){var d=this,e=d.getFromEnv(PAPER);if(e||(e=d.getFromEnv(CHART).getFromEnv(PAPER),d.addToEnv(PAPER,e)),a===GROUP)a=e.group(c.attr&&c.attr.name||'',c.container),a.attr(b);else if(a===HTML)a=e.html(c.attr.type,b,c.css,c.container);else{if(a===TEXT&&c.css)return e[c.el](b,c.css,c.container);a=e[c.el](b,c.container)}return a}setAttributes(a,b){var c,d=this,e=b.attr,f=b.inputJSON||{};return'string'==typeof a?a=d.createElement(a,e,f):0===Object.keys(e).length?(!f.attr||f.state===DISAPPEARING)&&(c=!f.doNotRemove):a.attr(e),c?d.removeElement(a,b.immediate):(f.css&&a.css(f.css),b.groupChangeReq&&a.appendTo(f.container),b.immediate&&f.callback&&f.callback.call(a)),{el:a,removed:c}}prepareAnimateArr(a){function b(a,b){var c,e,f,g={start:0,end:1};for(f in a)if(a.hasOwnProperty(f)){if(a[f].syncWith&&!d[a[f].syncWith])continue;c=a[f].syncWith,g.start=a[f].start===UNDEF?0:a[f].start,g.end=a[f].end===UNDEF?1:a[f].end,g.hookFn=a[f].hookFn||a.hookFn,g.smartMorph=a[f].smartMorph||a.smartMorph;break}return c=c&&d[c]?c:b&&d[b]?b:'default',e=d[c].end-d[c].start,g.start=d[c].start+g.start*e,g.end=d[c].start+g.end*e,g}var c,d,e,f,g,h,k,l,m,n=this.config.preAnimArr=[];for(d=this.getFinalSlots(),f=0,g=a.length;f<g;f+=1){for(c=a[f],e=c.component,(h=0,k=c.data.length);h<k;h+=1)m=c.data[h],l=m.transition||d[e]&&d[e].transition,l&&applyType(m,l),m.preAnimFn&&n.push({el:m.el,fn:m.preAnimFn}),m.animConfig&&(m.animConfig=b(m.animConfig,e));c.effect||d[e]&&(c.effect=d[e].effect),c.animConfig||(c.animConfig=[{}]),c.animConfig=b(c.animConfig,e)}}animate(){var a,b,c,d,e,f,g,h,k=this,l=k.config,m=k.config.animateArr,n=l.animationObj,o=n.animObj,p=n.dummyObj,q=n.duration;for(k.prepareAnimateArr(m),execPreAnimFns(k.config.preAnimArr),(e=0,f=m.length);e<f;e+=1)for(b=m[e],g=0,h=b.data.length-1;g<=h;g+=1)c=b.data[g].animConfig||b.animConfig,c=shiftAnimTime(c,q,180),a=b.data[g].effect||b.effect||LINEAR,d=e===f?b.callback||b.data[g].callback:b.data[g].callback,b.data[g].preAttrs&&b.data[g].el.attr(b.data[g].preAttrs),b.data[g].el&&animateEl(b.data[g].el,b.data[g].attrs,d||null,c,p,o,q,a)}remove(a){this.removeAllJobs(),a.instant&&this.getGraphicalElement('dummyObj')&&this.getGraphicalElement('dummyObj').remove(),super.remove(a)}}export default AnimationManager;