UNPKG

cronapp-framework-js

Version:
1 lines 10.2 kB
app,angular.module("report.services",[]).service("ReportService",["$http","$compile","$modal","$translate","$window","$rootScope",function(t,e,a,r,o,n){var i=$("body"),s=angular.element(i.get(0)).scope(),p=["node_modules/cronapp-lib-js/dist/js/stimulsoft/stimulsoft.vendor.css","node_modules/cronapp-lib-js/dist/js/stimulsoft/stimulsoft.vendor.js","node_modules/cronapp-lib-js/dist/js/stimulsoft/stimulsoft-helper.js"],l=[];function c(){return navigator.userAgent.match(/Android/i)||navigator.userAgent.match(/webOS/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/BlackBerry/i)||navigator.userAgent.match(/Windows Phone/i)}function d(t){window.open(t,"_blank")}this.getReport=function(e){var a={url:window.hostApp+"api/rest/report",method:"POST",data:angular.toJson({reportName:e})};return t(a)},this.getPDF=function(e){var a={url:window.hostApp+"api/rest/report/pdf",method:"POST",responseType:"arraybuffer",data:angular.toJson(e)};return t(a)},this.getPDFAsFile=function(e){var a={url:window.hostApp+"api/rest/report/pdfasfile",method:"POST",data:angular.toJson(e)};return t(a)},this.getContentAsString=function(e){var a={url:window.hostApp+"api/rest/report/contentasstring",method:"POST",data:angular.toJson(e)};return t(a)},this.getDataSourcesParams=function(e){var a={url:window.hostApp+"api/rest/report/getdatasourcesparams",method:"POST",data:angular.toJson(e)};return t(a)},this.openURLContent=function(t){if(t&&c())d(t);else{var a=$("#reportViewContext");a.get(0)||(console.log("include[#reportViewContext]"),i.append('<div id="reportViewContext" ng-include="\'node_modules/cronapp-framework-js/components/reports/reports.view.html\'"></div>'),e(i)(s),a=$("#reportViewContext"));var r=function(){var e=$("<iframe/>");e.attr("frameborder",0);var o=parseInt($(window).height());e.attr("height",o-200),e.attr("width","100%"),e.attr("src",t+"?download=false");var n=$("#reportView .modal-body");n.get(0)?(n.html(e),$("#reportViewContext .modal-dialog").css("width","95%"),setTimeout((function(){console.log("open[#reportViewContext]"),$("body").append(a),$("#reportView").modal()}),100)):(console.log("wait[#reportViewContext]"),setTimeout(r,200))};setTimeout(r,200)}},this.initializeStimulsoft=function(t){if(!Stimulsoft.Base.StiLicense.Key){stimulsoftHelper.setLanguage(t);var e=stimulsoftHelper.getLocalization();Stimulsoft.Base.Localization.StiLocalization.loadLocalization(e.xml),Stimulsoft.Base.Localization.StiLocalization.cultureName=e.cultureName,Stimulsoft.Base.StiLicense.Key=stimulsoftHelper.getKey()}},this.openStimulsoftReport=function(t,a,r,o){var p=$("#reportViewContext");p.get(0)||(i.append('<div id="reportViewContext" ng-include="\'node_modules/cronapp-framework-js/components/reports/reports.view.html\'"></div>'),e(i)(s),p=$("#reportViewContext"));var l=parseInt($(window).height())-200+"px",u="StiViewer"+app.common.generateId(),m=new Stimulsoft.Report.StiReport;n.reportTitle=t.ReportAlias,m.load(t),r||(r=stimulsoftHelper.getDatasourcesInBand(m)),a&&a.forEach((function(t){r.datasources.forEach((function(e){for(var a=0;a<e.fieldParams.length;a++)if(e.fieldParams[a].param==t.originalName){e.fieldParams[a].value=t.value;break}}))})),stimulsoftHelper.setParamsInFilter(m.dictionary.dataSources,r.datasources);var f=()=>{var t=new Stimulsoft.Viewer.StiViewerOptions;t.toolbar.showAboutButton=!1,o?(t.toolbar.visible=o.showToolbar,t.appearance.scrollbarsMode=o.showScrollbar,null!=o.height&&(t.height=o.height+"px")):(t.appearance.scrollbarsMode=!0,t.height=l);var e=new Stimulsoft.Viewer.StiViewer(t,u,!1);return e.report=m,Stimulsoft.Viewer.StiJsViewer.prototype.IsTouchDevice()&&(e.options.appearance.interfaceType=Stimulsoft.Viewer.StiInterfaceType.Touch),e};if(o&&o.$element)f().renderHtml(o.$element[0]);else{function h(e){if(e&&c())d(e);else var a=setInterval((function(){var r=$("<div/>");r.attr("id","contentReport"),r.attr("width","100%");var o=$("#reportView .modal-body");o.get(0)&&(o.html(r),$("#reportViewContext .modal-dialog").css("width","95%"),setTimeout((function(){console.log("open[#reportViewContext]"),$("body").append(p),cronapi.screen.showModal("reportView"),t.reportConfig&&"HTML"===t.reportConfig.renderType?f().renderHtml("contentReport"):$("#contentReport").html('<iframe src="'+e+'" width="100%" height="'+l+'"></iframe>'),setTimeout((function(){var t,e;e=setInterval((function(){var a=$("#"+u);a.length?t||(t=a.parent(),t.parent().parent().parent().parent()):(console.log("cleared interval: "+u),clearInterval(e))}),100)}),100)}),100),clearInterval(a))}),200)}if(t.reportConfig&&"PDFSERVER"!==t.reportConfig.renderType){var g=new Stimulsoft.Report.Export.StiPdfExportSettings,v=new Stimulsoft.Report.Export.StiPdfExportService,w=new Stimulsoft.System.IO.MemoryStream;m.renderAsync((function(){t.reportConfig&&"PDF"!==t.reportConfig.renderType&&void 0!==t.reportConfig.renderType?h(null):v.exportToAsync((function(){var t=w.toArray(),e=new Blob([new Uint8Array(t)],{type:"application/pdf"});h(URL.createObjectURL(e))}),m,w,g)}),!1)}else this.getPDF({reportName:t.reportName,parameters:a}).then(function(t){var e=new Blob([new Uint8Array(t.data)],{type:"application/pdf"});h(URL.createObjectURL(e))}.bind(this))}$(`#${u}`).find("img").attr("alt",""),$(`#${u}`).find("input").attr("aria-label",u)},this.showParameters=function(t){var e=t.parameters,r=[],o=0,n=function(){if(o<e.length){var i=e[o++];$.get("node_modules/cronapp-framework-js/components/reports/"+i.type+".parameter.html").done((function(t){var e,a,o;r.push((e=t,a="_field_",o=i.name,e.replace(new RegExp(function(t){return t.replace(/([.*+?^=!:()|\[\]\/\\])/g,"\\$1")}(a),"g"),o))),n()}))}else r.length>0&&a.open({templateUrl:"node_modules/cronapp-framework-js/components/reports/reports.parameters.html",controller:"ParameterController",resolve:{report:function(){return JSON.parse(JSON.stringify(t))},htmlParameters:function(){return JSON.parse(JSON.stringify(r))}}})}.bind(this);n()},this.mergeParam=function(t,e){var a=function(t,e){for(var a in Object.keys(e))if(r(e[a])&&t==Object.keys(e[a])[0])return Object.values(e[a])[0]};let r=t=>t&&{}.constructor===t.constructor;for(var o in Object.keys(t)){var n=t[o].name,i=(t[o].value,a(n,e));i?t[o].value=i:Array.isArray(e)&&!r(e[o])&&(t[o].value=e[o])}return t},this.setVariablesBasedOnParams=function(t,e){for(var a in e){var r=Object.keys(e[a])[0],o=e[a][r];for(var n in t)if(t[n]&&t[n].Name&&t[n].Name===r){t[n].Value=o;break}}},this.hasParameterWithOutValue=function(t){for(var e in Object.keys(t))if(!t[e].value)return!0;return!1},this.getDatasourcesInBandDashBoard=function(t){var e={hasParam:!1,datasources:[]},a=Stimulsoft.Report.StiReport.createNewDashboard();return a.load(t),a.pages&&a.pages.list&&a.pages.list.length>0&&a.pages.toList().forEach((function(t){t.components.toList().forEach((function(t){(t.getDataSources()||[]).forEach((function(t){var a=stimulsoftHelper.getParamsFromFilter(t),r=stimulsoftHelper.getQueryName(t);e.datasources.push({name:t.name,customId:r,fieldParams:a,queryParams:[]}),a.length&&(e.hasParam=!0)}))}))})),e},this.getDatasourcesInBand=function(t){var e=new Stimulsoft.Report.StiReport;return e.load(t),stimulsoftHelper.getDatasourcesInBand(e)},this.loadSriptsStimulsoft=function(t){var e=!0,a=p.length,r=0;Pace.options.initialRate=.7,Pace.options.minTime=1750,Pace.options.maxProgressPerFrame=1,Pace.options.ghostTime=12e4;let o=setInterval((()=>Pace.restart()),2500);p.forEach(function(n,i){this.loadScript(n,(function(n){r++,n||(e=!1),r==a&&(clearInterval(o),Pace.options.initialRate=.03,Pace.options.minTime=250,Pace.options.maxProgressPerFrame=20,Pace.options.ghostTime=10,Pace.stop(),t(e))}))}.bind(this))},this.loadScript=function(t,e){if($.inArray(t,l)>=0)e&&e(!0);else if(-1!=t.indexOf(".css")){var a=document.createElement("link");a.rel="stylesheet",a.type="text/css",a.href=t,a.media="all",a.onload=function(){l.push(t),e&&e(!0)},a.onerror=function(){e&&e(!1)};try{document.getElementsByTagName("head")[0].appendChild(a)}catch(t){console.log(t)}}else{var r=document.createElement("script");r.type="text/javascript",r.readyState?r.onreadystatechange=function(){"loaded"!=r.readyState&&"complete"!=r.readyState||(r.onreadystatechange=null,l.push(t),e&&e(!0))}:r.onload=function(){l.push(t),e&&e(!0)},r.src=t,document.getElementsByTagName("head")[0].appendChild(r)}},this.openReport=function(t,e,a){this.getReport(t).then(function(t){if(t&&t.data){var o=t.data.reportName.endsWith(".report"),n=t.data.reportName.endsWith(".dashboard");o||n?this.loadSriptsStimulsoft(function(o){o?(this.initializeStimulsoft(r.use()),this.getContentAsString(t.data).then(function(o){var i=this.getDatasourcesInBand(o.data);n&&(i=this.getDatasourcesInBandDashBoard(o.data)),this.getDataSourcesParams(i).then(function(n){i=n.data,t.data.parameters=stimulsoftHelper.parseToGroupedParam(i.datasources),t.data.contentData=o.data,t.data.datasourcesInBand=i,e&&(t.data.parameters=this.mergeParam(t.data.parameters,e),t.data.contentData.Dictionary=t.data.contentData.Dictionary||{},this.setVariablesBasedOnParams(t.data.contentData.Dictionary.Variables,e)),this.hasParameterWithOutValue(t.data.parameters)&&!a?(t.data.parameters.forEach((function(t){t.name=r.instant(t.name)})),this.showParameters(JSON.parse(JSON.stringify(t.data)))):this.openStimulsoftReport(o.data,t.data.parameters,t.data.datasourcesInBand,a)}.bind(this))}.bind(this),function(t){var e=cronapi.internal.getErrorMessage(t,t.statusText);s.Notification.error(e)}.bind(this))):s.Notification.error("Error loading report script")}.bind(this)):0==t.data.parameters.length||1==t.data.parameters.length&&"DATA_LIMIT"==t.data.parameters[0].name?this.getPDFAsFile(t.data.reportName).then(function(t){this.openURLContent(t.data)}.bind(this),function(t){var e=cronapi.internal.getErrorMessage(t,t.statusText);s.Notification.error(e)}.bind(this)):(e&&(t.data.parameters=this.mergeParam(t.data.parameters,e)),this.hasParameterWithOutValue(t.data.parameters)?this.showParameters(JSON.parse(JSON.stringify(t.data))):this.getPDFAsFile(t.data).then(function(t){this.openURLContent(t.data)}.bind(this)))}}.bind(this))}}]);