UNPKG

@nodata/render

Version:

Allows to dynamically render nodata components

1 lines 6.02 kB
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(e,r)};return function(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function o(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}}();Object.defineProperty(exports,"__esModule",{value:!0}),exports.DatablockRenderer=void 0;var rxjs_1=require("rxjs"),operators_1=require("rxjs/operators"),witchart_1=require("@nodata/witchart"),error_1=require("@nodata/error"),action_1=require("@nodata/action"),utils_1=require("./utils"),datablock_renderer_plugins_1=require("./datablock-renderer-plugins"),waMethods=(0,utils_1.getObjectMethods)(new datablock_renderer_plugins_1.WitchartRendererPlugin),DatablockRenderer=function(t){function e(e,r){var o=t.call(this)||this;return o.executionContext=null,o.datablock=null,o.datablockViewCode=null,o.rendererPlugin=null,o.targetElement=null,o.rendererPlugin=e,o.executionContext=r,o.executionContext||(o.executionContext=new action_1.DatablockExecutionContext),o.forwardWitchartMethodsToPlugin(),o}return __extends(e,t),e.prototype.on=function(t){return this.targetElement="string"==typeof t?document.getElementById(t):t,this.rendererPlugin.on(t),this},e.prototype.disableAnimations=function(){return this.rendererPlugin.disableAnimations(),this},e.prototype.destroy=function(){return this.getRenderingPlugin().destroy(),this},e.prototype.fetch=function(t){var e=this;return this.targetElement&&this.showLoader(),(0,action_1.retrieveDatablockView)(t).pipe((0,operators_1.mergeMap)((function(t){return e.fromDatablockView(t)})),(0,operators_1.catchError)((function(e){return(0,error_1.rethrowError)(e,"NDR-DBLCK-001",'Unable to fetch datablock from view "'.concat(t,'".'))})))},e.prototype.fromDatablockView=function(t){var e=this;return this.targetElement&&this.showLoader(),(0,rxjs_1.combineLatest)((0,rxjs_1.of)(t),(0,action_1.retrieveDatablock)(t.datablock),(0,action_1.executeDatablock)(t.datablock,{executionContext:this.executionContext.with({})})).pipe((0,operators_1.map)((function(t,r){var o=t[0],n=t[1],i=t[2];return e.initializeAndUpdateRenderer(0===r,o,n,i)})),(0,operators_1.tap)((function(){e.targetElement&&e.hideLoader()})),(0,operators_1.catchError)((function(t){return e.targetElement&&e.hideLoader(),(0,error_1.rethrowError)(t,"NDR-DBLCK-002","Unable to update datablock from datablock view.")})))},e.prototype.fromDatablock=function(t){var e=this;return this.targetElement&&this.showLoader(),(0,rxjs_1.combineLatest)([(0,action_1.retrieveDefaultDatablockView)(t.id),(0,action_1.executeDatablockEntity)(t,{executionContext:this.executionContext.with({})})]).pipe((0,operators_1.map)((function(r,o){var n=r[0],i=r[1];return e.initializeAndUpdateRenderer(0===o,n,t,i)})),(0,operators_1.tap)((function(){e.targetElement&&e.hideLoader()})),(0,operators_1.catchError)((function(t){return e.targetElement&&e.hideLoader(),(0,error_1.rethrowError)(t,"NDR-DBLCK-002","Unable to update datablock from entity.")})))},e.prototype.initializeAndUpdateRenderer=function(t,e,r,o){t?(this.datablockViewCode=e.code,this.datablock=r,this.rendererPlugin.initialize(e,r,o)):this.rendererPlugin.updateData(o),this.rendererPlugin.render()},e.prototype.initializeDatablockRenderer=function(t,e){var r=this;return this.on(t).initializeExecutionContext(e.id).pipe((0,operators_1.mergeMap)((function(){return r.fromDatablockView(e)})))},e.prototype.updateDatablockRenderer=function(t,e){var r=this;return this.on(t).initializeExecutionContext(e).pipe((0,operators_1.mergeMap)((function(){return r.fetch(e)})))},e.prototype.getRenderingPlugin=function(){return this.rendererPlugin},e.prototype.showErrorState=function(){this.targetElement&&this.targetElement.classList.add("render-error-placeholder"),this.destroy()},e.prototype.hideErrorState=function(){this.targetElement&&this.targetElement.classList.remove("render-error-placeholder")},e.prototype.showLoader=function(){this.targetElement.classList.add("loader","is-active")},e.prototype.hideLoader=function(){this.targetElement.classList.remove("loader","is-active")},e.prototype.forwardWitchartMethodsToPlugin=function(){for(var t=this,e=(0,utils_1.getObjectMethods)(this,!1),r=function(r){e.includes(r)||(o[r]=function(){for(var e,o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];console.debug("Deprecated. DatablockRenderer.".concat(r," cannot be called directly anymore. Use DatablockRenderer.getRenderingPlugin() then call ").concat(r,"."));var i=(e=t.rendererPlugin)[r].apply(e,o);return i===t.rendererPlugin?t:i})},o=this,n=0,i=waMethods;n<i.length;n++){r(i[n])}},e.prototype.initializeExecutionContext=function(t){var e=this;return(0,rxjs_1.zip)((0,action_1.retrieveDatablockView)(t),(0,action_1.retrievePresetFilters)()).pipe((0,operators_1.mergeMap)((function(t){return(0,action_1.retrieveDatablockViewPresetFiltersByDatablockViewCode)(t[0].code).pipe((0,operators_1.tap)((function(r){r.forEach((function(r){var o=t[1].find((function(t){return t.id===r.idPresetFilter}));e.executionContext.updateMappingEntry(o.code,o.defaultValue),e.executionContext.contextPresetFilters[o.code]=r.filteredField}))})),(0,operators_1.map)((function(){})))})),(0,operators_1.catchError)((function(t){return(0,error_1.rethrowError)(t,"NDR-DBLCK-201","Unable to initialize Datablock Execution Context")})))},e.prototype.updateDecContextPresetFilterEntry=function(t,e){this.executionContext.contextPresetFilters[t]=e},e.prototype.clearExecutionContextCache=function(){this.datablock&&this.executionContext.clearDatablockCacheEntry(this.datablock.code)},e.prototype.deleteDecContextPresetFilterEntry=function(t){delete this.executionContext.contextPresetFilters[t]},e}(witchart_1.WitchArt);exports.DatablockRenderer=DatablockRenderer,exports.default=DatablockRenderer;