quoslibero
Version:
FusionCharts JavaScript charting framework
1 lines • 8.19 kB
JavaScript
import{ComponentInterface}from'../../../fc-core/src/component-interface';import{getDepsByType}from'../../../fc-core/src/dependency-manager';import{raiseWarning}from'../../../fc-core/src/event-api';import Set from'core-js/fn/set';let UNDEF,themer,BLANK='',OBJECTSTRING='object',arrayToStr='[object Array]',objectToStr='[object Object]',isImportantRegEx=/\s+!important$/,importantStrRegEx=/\\!important$/,getValueFromObjProperty=function(a,b){var c,d;for(d in b=(b+'').toLowerCase(),a)if(a.hasOwnProperty(d)&&b===(d+'').toLowerCase()){c=a[d];break}return c},hasFunc=function(a){var b,c;for(b=0,c=a.length;b<c;b++)if('function'==typeof a[b])return!0;return!1},getArrayFromObj=function(a){var b,c=[];for(b in a)c[b]=a[b];return c},trimString=function(a){return a.replace(/^\s\s*/,'').replace(/\s\s*$/,'')},getRegisteredThemes=a=>{let b,c={};for(var d in a)a.hasOwnProperty(d)&&(b=a[d],c[b.name]=b);return c},checkCyclicRef=function(a,b){for(var c=b.length,d=-1;c--;)if(a===b[c])return d=c,d;return d},merge=function(a,b,c,d,e,f){var g,h,i,j,k;if(f?(e.push(a),f.push(b)):(e=[a],f=[b]),b instanceof Array)for(g=0;g<b.length;g+=1){try{h=a[g],i=b[g]}catch(a){continue}typeof i==OBJECTSTRING?((null===h||typeof h!=OBJECTSTRING)&&(h=a[g]=i instanceof Array?[]:{}),k=checkCyclicRef(i,f),-1===k?merge(h,i,c,d,e,f):h=a[g]=e[k]):!(c&&i===UNDEF)&&(a[g]=i)}else for(g in b){try{h=a[g],i=b[g]}catch(a){continue}null!==i&&typeof i==OBJECTSTRING?(j=Object.prototype.toString.call(i),j===objectToStr?((null===h||typeof h!=OBJECTSTRING)&&(h=a[g]={}),k=checkCyclicRef(i,f),-1===k?'data'===g&&i._dataStore?a[g]=d?d(h,i):i:merge(h,i,c,d,e,f):h=a[g]=e[k]):j===arrayToStr?((null===h||!(h instanceof Array))&&(h=a[g]=[]),k=checkCyclicRef(i,f),-1===k?merge(h,i,c,d,e,f):h=a[g]=e[k]):a[g]=d?d(h,i):i):a[g]=d?d(h,i):i}return a},extend2=function(a,b,c,d=null){return typeof a!=OBJECTSTRING&&typeof b!=OBJECTSTRING?null:typeof b!=OBJECTSTRING||null===b?a:(typeof a!=OBJECTSTRING&&(a=b instanceof Array?[]:{}),merge(a,b,c,d),a)},checkImportance=function(a){var b={important:!1,str:BLANK};return a?(a=a.toString(),isImportantRegEx.test(a)?(a=a.replace(isImportantRegEx,BLANK),b.important=!0):(a=a.replace(importantStrRegEx,'!imporant'),b.important=!1),b.str=a,b):b},getImportantStyle=function(a,b){const c=checkImportance(a),d=checkImportance(b);return c.important&&!d.important?c.str:d.str},mergeThemeWithData=function(a,b){var c,d,e,f,g,h=b.getAll();for(c in h)d=c.toLowerCase(),e=h[c].toString(),f=checkImportance(e),g=checkImportance(a[d]),f.important&&!g.important?a[d]=f.str:g.important?a[d]=g.str:a[d]===UNDEF&&'object'!=typeof h[c]?a[d]='object'==typeof h[c]?h[c]:f.str:'object'==typeof h[c]&&('undefined'==typeof a[d]&&(a[d]=h[c]),a[d]=extend2(h[c],a[d],!1,getImportantStyle))},getAllUniqueKeys=function(a,b){const c=[...Object.keys(a)];let d;for(d of b.themeArray)c.push(...Object.keys(d));return Array.from(new Set(c))},recursiveApply=function(a,b){var c,d,e,f,g,h,k,l,m,n=b.type,o=0,p=0,q=getAllUniqueKeys(a,b);for(c of q)if(d=a[c],'undefined'==typeof d&&(d=a[c]={}),l='timeseries'===n&&b.themeComponents[c]&&hasFunc(b.themeComponents[c]),d instanceof Array&&!l)for(h=d.length,g=0;g<h;g+=1)f=d[g],'object'==typeof f&&('category'===c?'true'===f.vline?(e=b.component('vline',o,f),e&&(mergeThemeWithData(f,e),o+=1)):(e=b.component('category',p,f,h),e&&(mergeThemeWithData(f,e),p+=1)):(e=b.component(c,g,f,h),e&&(mergeThemeWithData(f,e),recursiveApply(f,e))));else if('object'==typeof d)if(!l)e=b.component(c,null,d),e&&(mergeThemeWithData(d,e),recursiveApply(d,e));else if(e=b.component(c,null,d),m=e.getAll(),a[c]=m,/^\d+$/.test(Object.keys(m).join('')))for(m=getArrayFromObj(m),a[c]=m,k=0;k<a[c].length;k++)recursiveApply(a[c][k],e);else recursiveApply(d,e)};class ThemeManager extends ComponentInterface{configure(){this.config.themeStore||(this.config.themeStore={})}add(a){for(var b,c=this,d=0,e=a.length;d<e;d+=1)b=a[d].name,b&&(c.config.themeStore[b]=a[d])}themify(a,b,c,d,e){var f,g,h=this,j=b.jsVars,k=a.split(','),l=[],m=k.length;if(h.config.themeStore=getRegisteredThemes(getDepsByType('theme')),m){for(g=0;g<m;g+=1)f=h.config.themeStore[trimString(k[g])],f&&l.push(ThemeManager.evaluateThemeJSON(f.theme,b,c,e));l.length?(j.themeObject=new ThemeInstance(l,b,!1,d,c),ThemeManager.applyTheme(b)):raiseWarning(b,'14051100501','run','api.themes~themify()','The theme "'+a+'" requested has not been registered.')}}static evaluateThemeJSON(a,b,c,d){var e,f,g={},h=b.jsVars,i=function(a){var b,c;for(b in a)c=a[b],g[b]=c instanceof Array?extend2(g[b]||[],c):'object'==typeof c?extend2(g[b]||{},c):c};return c=c||b.chartType(),h.themeObject&&a!==h.themeObject&&(h.themeObject.dispose(),delete h.themeObject),i(getValueFromObjProperty(a,'base')),d&&(e=getValueFromObjProperty(a,d)),e&&i(e),c&&(f=getValueFromObjProperty(a,c)),f&&i(f),g}static applyTheme(a){var b=a.jsVars.themeObject,c=b.getThemedJSONData().data;c&&recursiveApply(c,b)}}class ThemeInstance extends ComponentInterface{constructor(a,b,c,d,e){super();var f,g=0;for(this.themeArray=a,this.themeComponents={},this.base={},this.type=e,this.chartInstance=b,this.isChildInstance=!!c,this.themedData=c?null:d,this.length=a.length,f=a.length;g<f;g+=1)this.parse(a[g])}pushTheme(a,b=!1){a&&(this.themeArray.push(a),this.parse(a,b),this.length+=1)}parse(a,b){var c,d,e=this;if(a instanceof Array)for(c=a.length,d=0;d<c;d++)e._parseSubBlock(a[d],b);else e._parseSubBlock(a,b)}_parseSubBlock(a,b){let c,d,e,f,g=this,h=g.base,i=g.type,j=g.themeComponents,k=g.chartInstance,l=g.isChildInstance;for(c in a)'string'==typeof a[c]||'number'==typeof a[c]?h[c]?(d=checkImportance(a[c]),e=checkImportance(h[c]),(d.important||!e.important)&&(h[c]=a[c])):h[c]=a[c]:'timeseries'===i&&l?a[c]instanceof Array&&'palettecolors'!==c&&!b?(!j[c]&&(j[c]=[]),j[c].push(extend2([],a[c]))):('object'==typeof a[c]||b)&&(h[c]=a[c]):(j[c]||(j[c]=[]),f=j[c],a[c]instanceof Array?f.push(extend2([],a[c])):'object'==typeof a[c]?f.push(new ThemeInstance([a[c]],k,!0,{},g.type)):'function'==typeof a[c]&&f.push(a[c]))}mergeWithThemeInstance(a){var b,c,d,e=this,f=e.base,g=a.base,h=e.themeComponents,i=a.themeComponents;for(d in g)b=checkImportance(f[d]),c=checkImportance(g[d]),(!b.important||c.important)&&(f[d]=g[d]);for(d in i)h[d]=h[d]?h[d].concat(i[d]):[].concat(i[d]);e.length+=a.length}get(a){return this.base[a]}getAll(){return extend2({},this.base)}component(a,b,c,d){var e,f,g,h,j=this,k=j.themeComponents,l=j.chartInstance,m=j.type,n=new ThemeInstance([],l,!0,{},j.type);if(g=k[a],!g)return null;for(e=0,f=g.length;e<f;e+=1)h=g[e],'function'==typeof h?'timeseries'===m?n.mergeWithThemeData(h.call(l,c),b,c,!0):n.mergeWithThemeData(h.call(l,b,c,d),b||0,c):n.mergeWithThemeData(h,b,c);return n}mergeWithThemeData(a,b,c,d=!1){let e,f,g=this;a instanceof Array?(b=b||0,f=a.length,b%=f,e=a[b],e instanceof ThemeInstance?g.mergeWithThemeInstance(e):'function'==typeof e?g.pushTheme(e.call(parent,b,c,length),d):g.pushTheme(e,d)):a instanceof ThemeInstance?g.mergeWithThemeInstance(a):g.pushTheme(a,d)}getThemedJSONData(){return{data:this.themedData}}dispose(){var a,b,c=this,d=c.themeComponents;for(a in d)if(b=d[a].length,b){for(;b--;)d[a][b].dispose&&d[a][b].dispose();delete d[a]}c.themeComponents=null,c.chartInstance=null,c.base=null,c.themeArray=null,c.isChildInstance=null,c.dataWithoutTheme=null}}themer=new ThemeManager,themer.configure();function FCPlugger(a){var b=function(b){var c,d,e,f=b.sender,g=f.getFromEnv('chartInstance'),h=getRegisteredThemes(getDepsByType('theme')),j=f.getFromEnv('dataSource'),k=a.options.defaultTheme,l=j&&(j.chart&&j.chart.theme||j.map&&j.map.theme),m=(k||l)&&`${k||''},${l||''}`,n=m&&m.split(','),o=n&&n.length,p=!1;for(d=0;d<o;d++)e=trimString(n[d]),h&&''!==e&&h.hasOwnProperty(e)&&(p=!0);p?(j=extend2({},f.getFromEnv('dataSource')),c=g.chartType(),j.map&&(j.chart=j.map,delete j.map),themer.themify(m,g,c,j,'geo'===g.apiInstance.defaultSeriesType&&'geo'),f.addToEnv('dataSource',j)):g.jsVars&&g.jsVars.themeObject&&(g.jsVars&&g.jsVars.themeObject.dispose(),g.jsVars&&delete g.jsVars.themeObject)};a.addEventListener('internal.dataSanitized',b)}export default{extension:FCPlugger,name:'ThemeEngine',type:'extension',requiresFusionCharts:!0};