@fusioncharts/core
Version:
JavaScript Data Visualisation Library
1 lines • 14.6 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _extends=require("@babel/runtime/helpers/extends");exports.__esModule=true;exports.default=void 0;var _inheritsLoose2=_interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _componentBase=_interopRequireWildcard(require("./component-base"));var _lib=require("../lib");var _schedular=require("../schedular");function _interopRequireWildcard(e,t){if("function"==typeof WeakMap)var r=new WeakMap,n=new WeakMap;return(_interopRequireWildcard=function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;var o,i,f=_extends({},null,{default:e});if(null===e||"object"!=typeof e&&"function"!=typeof e)return f;if(o=t?n:r){if(o.has(e))return o.get(e);o.set(e,f)}for(var _t in e)"default"!==_t&&{}.hasOwnProperty.call(e,_t)&&((i=(o=Object.defineProperty)&&Object.getOwnPropertyDescriptor(e,_t))&&(i.get||i.set)?o(f,_t,i):f[_t]=e[_t]);return f})(e,t)}var addEventOnAllGraphics=function addEventOnAllGraphics(component,eventType,listener){var callBack;if(eventType&&listener){callBack=function callBack(element){!element._skipEvents&&element.on&&element.on(eventType,listener)};_mapSubFnForward(component.getGraphicalElement(),callBack)}},removeEventFromAllGraphics=function removeEventFromAllGraphics(component,eventType,listener){var callBack;if(eventType&&listener){callBack=function callBack(element){!element._skipEvents&&element.off&&element.off(eventType,listener)};_mapSubFnForward(component.getGraphicalElement(),callBack)}},_mapSubFnBackward=function _mapSubFnBackward(sub,callback){if(sub&&sub.hasOwnProperty&&callback&&callback.call){var currLabel,label,elemStore,i;for(label in sub){currLabel=sub[label];elemStore=currLabel.elemStore;for(i=elemStore&&elemStore.length-1;i>=0;i--){if(callback(elemStore[i],label,i)){break}}}}},_mapSubFnForward=function _mapSubFnForward(sub,callback){if(sub&&sub.hasOwnProperty&&callback&&callback.call){var currLabel,ii,label,elemStore,i;for(label in sub){currLabel=sub[label];elemStore=currLabel.elemStore;for(i=0,ii=elemStore&&elemStore.length;i<ii;i++){if(callback(elemStore[i],label,i)){break}}}}},_getDifferentialAttributes=function _getDifferentialAttributes(newAttrs,existingAttrs,existingCss){if(existingCss===void 0){existingCss={}}var derivedAttrs,currAttr,existingAttr,attr;for(attr in newAttrs){currAttr=newAttrs[attr];existingAttr=(0,_lib.pluck)(existingAttrs[attr],existingCss[attr]);if(existingAttr!==_lib.UNDEF){if(currAttr instanceof Array){if(attr==="text-bound"){derivedAttrs||(derivedAttrs={});derivedAttrs[attr]=currAttr;derivedAttrs.x=newAttrs.x;derivedAttrs.y=newAttrs.y}else if(currAttr.toString().replace(/,/g,"")!==existingAttr.toString().replace(/,/g,"")){derivedAttrs||(derivedAttrs={});derivedAttrs[attr]=currAttr}}else if(typeof currAttr==="string"){if(currAttr.replace(/,/g,"")!==existingAttr.toString().replace(/,/g,"")){derivedAttrs||(derivedAttrs={});derivedAttrs[attr]=currAttr}}else{if(currAttr!==existingAttr){derivedAttrs||(derivedAttrs={});derivedAttrs[attr]=currAttr}}}else{derivedAttrs||(derivedAttrs={});derivedAttrs[attr]=currAttr}}return derivedAttrs};var SmartRenderer=function(_ComponentBase){function SmartRenderer(id){var _this;_this=_ComponentBase.call(this,id)||this;var smartRenderer=_this;smartRenderer._version=2;smartRenderer._id=id;smartRenderer._componentStore={};smartRenderer._oldComponentStore=[];smartRenderer.fireEvent("instantiated");smartRenderer.__setDefaultConfig();return _this}(0,_inheritsLoose2.default)(SmartRenderer,_ComponentBase);var _proto=SmartRenderer.prototype;_proto.isDrawingRequired=function isDrawingRequired(){return this};_proto.syncDraw=function syncDraw(){var component=this,isRemoved=component.getState("removed");if(component.isDrawingRequired()||isRemoved){component.fireEvent("predraw");component.removeJob("draw");component._resetGraphicalStore();isRemoved?component.addJob("dispose-component",(function(){return component._dispose()}),_schedular.priorityList.postRender):component.draw&&component.draw();component.childrenSyncDraw();component._removeUnusedGraphics();component.fireEvent("drawn")}};_proto._remove=function _remove(){var component=this,childComponents;component.onRemove&&component.onRemove();component.setState("removed",true);childComponents=component.getChildren();if(childComponents){component._mapChildren((function(child){child.onRemove&&child.onRemove();child&&child.setState("removed",true)}),true)}};_proto.setDimension=function setDimension(width,height){this.config.width=width;this.config.height=height};_proto.setTranslation=function setTranslation(x,y){var config=this.config;config._translateX=x;config._translateY=y;config.translate="t"+x+","+y};_proto.getTranslation=function getTranslation(){return{x:this.config&&this.config._translateX,y:this.config&&this.config._translateY}};_proto._resetGraphicalStore=function _resetGraphicalStore(){var smartRenderer=this;smartRenderer._oldGraphicalStore=smartRenderer._graphicalStore;smartRenderer._graphicalStore={}};_proto._getLastUsedElem=function _getLastUsedElem(){return this.config.lastElemUsed};_proto.addGraphicalElement=function addGraphicalElement(inputConfiguration,skipEvents){if(inputConfiguration===void 0){inputConfiguration={}}if(skipEvents===void 0){skipEvents=false}var smartRenderer=this,label=inputConfiguration.label,store=smartRenderer._graphicalStore[label]||(smartRenderer._graphicalStore[label]={elemStore:[],idMap:{},indexMap:{},lastIndexUsed:-1}),source,updatedCss,containerConfig,newElem=true,oldStore=smartRenderer._oldGraphicalStore&&smartRenderer._oldGraphicalStore[label],newIndex=store.lastIndexUsed+=1,oldIndex,elem,updatedAttributes=inputConfiguration.attr,id=inputConfiguration.id;if(oldStore){if(id&&oldStore.idMap[id]!==_lib.UNDEF){oldIndex=oldStore.idMap[id];elem=oldStore.elemStore[oldIndex];delete oldStore.elemStore[oldIndex];delete oldStore.indexMap[oldIndex];delete oldStore.idMap[id]}else if(oldStore.elemStore[newIndex]){if(!oldStore.indexMap[newIndex]){elem=oldStore.elemStore[newIndex];delete oldStore.elemStore[newIndex]}}}if(inputConfiguration.el==="text"&&inputConfiguration.css){(0,_lib.removeHtmlAttrs)(inputConfiguration.css)}if(elem){inputConfiguration.el=elem;newElem=false;if(updatedAttributes=_getDifferentialAttributes(inputConfiguration.attr,elem.attrs)){inputConfiguration.attr=updatedAttributes}if(inputConfiguration.css&&(updatedCss=_getDifferentialAttributes(inputConfiguration.css,elem.attrs,elem.styles))){inputConfiguration.css=updatedCss}}if(containerConfig=inputConfiguration.container){source=containerConfig.isParent?smartRenderer.getLinkedParent():smartRenderer;inputConfiguration.container=source.getGraphicalElement(containerConfig.id,containerConfig.label)||source.getChildContainer(containerConfig.id)}else{inputConfiguration.container=smartRenderer.getFromEnv("chart-container")}(updatedAttributes||updatedCss)&&(elem=smartRenderer.getFromEnv("animationManager").setAnimation(inputConfiguration));store.elemStore[newIndex]=elem;if(id){if(store.idMap[id]){delete store.indexMap[store.idMap[id]];delete store.idMap[id]}store.idMap[id]=newIndex;store.indexMap[newIndex]=id}smartRenderer.fireEvent("graphicalelementattached",{element:elem});elem._skipEvents=skipEvents;inputConfiguration.shadow&&elem.shadow(inputConfiguration.shadow);inputConfiguration.outlineText!==_lib.UNDEF&&elem.outlineText(inputConfiguration.outlineText,(0,_lib.convertColor)((0,_lib.pluck)(inputConfiguration.css.fill,inputConfiguration.attr.fill)));inputConfiguration.tooltext!==_lib.UNDEF&&smartRenderer.getFromEnv("toolTipController").enableToolTip(elem,inputConfiguration.tooltext);smartRenderer.config.lastElemUsed=elem;!skipEvents&&newElem&&(0,_componentBase.addAllEventsOnGraphic)(elem,smartRenderer._middleListeners,smartRenderer)};_proto.removeGraphicalElement=function removeGraphicalElement(info,label){var smartRenderer=this,graphics=smartRenderer._graphicalStore,currLabelGraphics,element,found;if(typeof info==="object"){_mapSubFnBackward(graphics,(function(child,currLabel,i){if(info===child){found=true;smartRenderer._setRemoveAnim(child,currLabel);currLabelGraphics=graphics[currLabel];currLabelGraphics.elemStore.splice(i,1);delete currLabelGraphics.idMap[currLabelGraphics.indexMap[i]];delete currLabelGraphics.indexMap[i];return true}}))}else{_mapSubFnBackward(graphics,(function(child,currentLabel,i){currLabelGraphics=graphics[currentLabel];if(currLabelGraphics.indexMap[i]===info&&(label?label===currentLabel:true)){found=true;smartRenderer._setRemoveAnim(child,currentLabel);currLabelGraphics.elemStore.splice(i,1);delete currLabelGraphics.idMap[currLabelGraphics.indexMap[i]];delete currLabelGraphics.indexMap[i];return true}}))}if(found){this.fireEvent("graphicalelementremoved",{element:element})}};_proto.getChildContainer=function getChildContainer(name){return this.getGraphicalElement(_lib.UNDEF,name)};_proto._removeUnusedGraphics=function _removeUnusedGraphics(){var smartRenderer=this,graphics=smartRenderer._oldGraphicalStore,currLabelGraphics;_mapSubFnBackward(graphics,(function(child,currLabel,i){if(child){smartRenderer._setRemoveAnim(child,currLabel);currLabelGraphics=graphics[currLabel];delete currLabelGraphics.idMap[currLabelGraphics.indexMap[i]];delete currLabelGraphics.indexMap[i]}}))};_proto.getGraphicalElement=function getGraphicalElement(id,label){var currLabel,currLabelGraphics,elem,i,graphics=this._graphicalStore;if(!label&&!id){return graphics}if(!label){_mapSubFnForward(graphics,(function(){i=arguments[2];currLabelGraphics=graphics[arguments[1]];if(currLabelGraphics.idMap[i]===id){elem=currLabelGraphics.elemStore[i];return true}}));return elem}currLabel=graphics[label];if(!id){return currLabel&&currLabel.elemStore&&currLabel.elemStore[0]}return currLabel&&currLabel.elemStore&&currLabel.elemStore[currLabel.idMap[id]]};_proto.addEventListener=function addEventListener(eventType,callback,options){var superOutput=_ComponentBase.prototype.addEventListener.call(this,eventType,callback,options);if(superOutput===true){addEventOnAllGraphics(this,eventType,this._middleListeners[eventType]);return callback}else if(superOutput){return callback}return false};_proto.removeEventListener=function removeEventListener(eventType,callback){if(_ComponentBase.prototype.removeEventListener.call(this,eventType,callback)){removeEventFromAllGraphics(this,eventType,this._middleListeners[eventType])}};_proto._dispose=function _dispose(){var component=this,itemName,i,eventType,listenersArr;if(component&&component!==window&&component._disposing!==true){component._disposing=true;component.fireEvent("beforeremove");if(component._extListeners){for(eventType in component._extListeners){listenersArr=component._extListeners[eventType];for(i=listenersArr.length-1;i>=0;i--){if(listenersArr[i].component&&listenersArr[i].component.addEventListener){listenersArr[i].component.removeEventListener(eventType,listenersArr[i].fn)}}}}component.removeAllJobs();if(component._linkedParent&&component._linkedParent.getComponentVersion()===1){component._linkedParent._detachChild(component)}if(component.getFromEnv("paper")&&!component.getFromEnv("paper").removed){_mapSubFnForward(component.getGraphicalElement(),component.__instantRemoveFn)}for(itemName in component){if(component.hasOwnProperty(itemName)){delete component[itemName]}}component.fireEvent("removed")}};_proto.configure=function configure(dataObj){this._resetComponentStore();_ComponentBase.prototype.configure.call(this,dataObj)};_proto._resetComponentStore=function _resetComponentStore(){var smartRenderer=this;smartRenderer._oldComponentStore.push(smartRenderer._componentStore);smartRenderer._componentStore={}};_proto._mapChildren=function _mapChildren(callback,backWord){if(backWord){_mapSubFnBackward(this.getChildren(),callback)}else{_mapSubFnForward(this.getChildren(),callback)}};_proto.attachChild=function attachChild(Component,componentType,id){var smartRenderer=this,store=smartRenderer._componentStore[componentType]||(smartRenderer._componentStore[componentType]={elemStore:[],idMap:{},indexMap:{},lastIndexUsed:-1}),oldStore=smartRenderer._oldComponentStore&&smartRenderer._oldComponentStore.length&&smartRenderer._oldComponentStore[smartRenderer._oldComponentStore.length-1][componentType],newIndex=store.lastIndexUsed+=1,oldIndex,comp;if(oldStore){var oldStoreElems=oldStore.elemStore,oldIndexedElem=oldStoreElems[newIndex];if(id&&oldStore.idMap[id]!==_lib.UNDEF){oldIndex=oldStore.idMap[id];comp=oldStoreElems[oldIndex];delete oldStoreElems[oldIndex];delete oldStore.indexMap[oldIndex];delete oldStore.idMap[id]}else if(oldIndexedElem&&oldIndexedElem.constructor===Component){if(!oldStore.indexMap[newIndex]){comp=oldIndexedElem;delete oldStoreElems[newIndex]}}}if(!comp){comp=new Component(id)}store.elemStore[newIndex]=comp;if(id){store.idMap[id]=newIndex;store.indexMap[newIndex]=id}comp._setLinkedParent(smartRenderer);return comp};_proto.childrenSyncDraw=function childrenSyncDraw(){var component=this;component._mapChildren((function(child){child&&child.draw&&child.syncDraw()}));component._removeUnusedChildren()};_proto._removeUnusedChildren=function _removeUnusedChildren(){var smartRenderer=this,oldStore=smartRenderer._oldComponentStore,l=oldStore.length,i=0,iteratorFn=function iteratorFn(child){if(child){child._remove();child.syncDraw();child._setLinkedParent(_lib.UNDEF)}};for(;i<l;i+=1){_mapSubFnBackward(oldStore[i],iteratorFn)}smartRenderer._oldComponentStore.length=0};_proto.getChild=function getChild(id,componentType){var returnChild;this._searchChildren(id,(function(foundChild){returnChild=foundChild}),componentType);return returnChild};_proto._searchChildren=function _searchChildren(id,cb,_componentType){var componentStore=this._componentStore,foundAt,indexMap,foundComp,componentType=_componentType;if(id){_mapSubFnBackward(componentStore,(function(child,currentComponentType,i){indexMap=componentStore[currentComponentType].indexMap;if(indexMap[i]===id&&(componentType?componentType===currentComponentType:true)){foundComp=child;foundAt=i;componentType=currentComponentType;return true}}))}else{foundComp=componentStore&&componentStore[componentType]&&componentStore[componentType].elemStore}if(foundComp){return cb(foundComp,foundAt,componentType)}};_proto.getChildren=function getChildren(componentType){return componentType&&this._componentStore?this._componentStore[componentType]&&this._componentStore[componentType].elemStore:this._componentStore};_proto._detachChild=function _detachChild(){return this};return SmartRenderer}(_componentBase.default);var _default=exports.default=SmartRenderer;
;