/* global getFieldsList getFieldsObjList getFieldsDescMap setCurrentDateTime getBlob*/ /*jshint laxcomma: true */ /*** CLASSE RepDocumentText: gestisce la generazione/export del documento (report) in formato csv (standard e non) * Eredita da RepDocument - JSReportDocumentRuntime.js */ RepDocumentText = function (name, uid, format, isStandard, csvSeparator, csvHeader) { RepDocument.call(this); this.format = format.toLowerCase(); this.gcurrpage = null; this.name = name; this.progrid = 0; this.datetime = new Date(); if(Empty(uid)) uid = LibJavascript.AlfaKeyGen(5); this.uid = uid; this.parent = null; this.document = null; this.window = null; this.separator = isStandard?",":";"; csvSeparator = LRTrim(csvSeparator); if(!Empty(csvSeparator) && Len(csvSeparator)==1) this.separator = csvSeparator; this.csvHeader = csvHeader; this.fieldsuid = []; }; RepDocumentText.prototype = new RepDocument(); RepDocumentText.prototype.constructor = RepDocumentText; RepDocumentText.prototype.Ctrl = function(id){ return this.document.getElementById(id); }; RepDocumentText.prototype.initProcess = function(allfields,container,bprint,imgfields,simulatedData){ this.container = container; this.setParent(); this.setDocument(); this.setWindow(); this.allfields = allfields; this.simulatedData = simulatedData; this.exportString = ""; this.fieldsuid = getFieldsList(this.obj); this.fieldsobjs = getFieldsObjList(this.obj); this.fielddesc = getFieldsDescMap(this.obj); this.initLastRow(); this.lastGrp = []; this.newLine = "\n"; this.lastRec = []; this.writeHeader(); }; RepDocumentText.prototype.initLastRow = function(){ this.lastRow = []; var rowobj = {}; for(var i=0; i 1) this.writeRow(); this.writeFooter(); if (this.format.indexOf("xml") < 0 || this.autodownload) this.download(); else { // xml - xmls if (this.autodownload) this.download(); else this.printPreview(); } this.execFncCallback(); }; RepDocumentText.prototype.drawCover = function(/*coverobj*/){ }; RepDocumentText.prototype.startLoading = function(){ } RepDocumentText.prototype.updateLoading = function(){ } RepDocumentText.prototype.appendFontsInDocument = function () { } RepDocumentText.prototype.setLimitPage = function(){ this.gwork.limitpage = Infinity; } RepDocumentText.prototype.newPage = function(k,firstpage){ if(Empty(firstpage) && this.obj.showPageZone){ this.drawZone(k,'pagefooter','',false,true); this.drawZone(k,'pageheader','',false); } } RepDocumentText.prototype.getLastChildElement = function(){ return {}; } RepDocumentText.prototype.addCurrentZone = function(){ } RepDocumentText.prototype.checkLimitPage = function(){ return false; } RepDocumentText.prototype.splitStringCheck = function(){ return false; } RepDocumentText.prototype.updateZoneHeight = function(){ } RepDocumentText.prototype.writeHeader = function(){ if(this.csvHeader && (this.format=="csvs" || this.format=="csv") && (this.fieldsuid.length > 0)){ var tmp = ""; for (var i = 0; i < this.fieldsuid.length - 1; i++) { tmp = "" + this.fielddesc[this.fieldsuid[i]]; this.exportString += convertCsv(tmp, this.separator) + this.separator; } tmp = "" + this.fielddesc[this.fieldsuid[this.fieldsuid.length - 1]]; this.exportString += convertCsv(tmp, this.separator); } else if(this.format.indexOf("xml")>=0){ var txt=""+this.newLine; this.exportString=txt; this.exportString+="=0){ if(this.format=="xml") this.exportString+=""+this.newLine; this.exportString+=""; } } RepDocumentText.prototype.getItemValue = function (k, item, zoneid, repObj, useLastRecord) { if (item.type != "field" || (item.fieldDetail && Empty(item.fieldDetail.alias))) return ""; var value; var mdata = useLastRecord ? repObj.lastRec : repObj.propertyValue[k]; if (item.typeView.indexOf("date") >= 0) { if (mdata[item.fieldDetail.alias + '##pic']) value = mdata[item.fieldDetail.alias + '##pic']; else { value = mdata[item.fieldDetail.alias]; value = setCurrentDateTime(value, item); } } else if (item.typeView == "numeric") { if (item.calculation && !Empty(item.calculation.type) && item.calculation.type != 'none') value = repObj.calcObjs[item.calculation.type][item.fieldDetail.alias][item.uid]; else if (mdata[item.fieldDetail.alias + '##pic']) value = mdata[item.fieldDetail.alias + '##pic']; else { value = mdata[item.fieldDetail.alias]; value = repObj.setFormatNumber(value, item.fieldDetail.len, item.fieldDetail.dec, item.stdPicture); } } else if (mdata[item.fieldDetail.alias + '##pic']) value = mdata[item.fieldDetail.alias + '##pic']; else value = Format(mdata[item.fieldDetail.alias], item.fieldDetail.len, item.fieldDetail.dec, item.stdPicture); return value; }; RepDocumentText.prototype.isEoPreview = function () { return false; } RepDocumentText.prototype.addItemInZone = function(k,item,objzone,zoneid,value/*,yRel*/){ var curindex = this.fieldsuid.indexOf(item.uid); if(curindex>=0 && this.format!="xmls"){ if (this.fieldsuid.length == 1) { this.lastRow[curindex].value = value; this.writeRow(); } else { if(this.grpBreak){ this.resetRow(curindex); this.grpBreak = false; } if(curindex=0) value = ""; this.lastRow[curindex].value = value; this.lastElement = curindex; } } else if(curindex>=0){ this.lastRow[curindex].value = value; if(zoneid.indexOf("report")>=0) this.writeRow(zoneid,curindex); else if(zoneid.indexOf("group")>=0){ if(!this.grpBreak){ if(!this.lastGrp[zoneid]) this.lastGrp[zoneid] = []; if(!this.lastGrp[zoneid][curindex+""]) this.lastGrp[zoneid][curindex+""] = []; this.lastGrp[zoneid][curindex+""].push(value); } else{ this.writeRow(); this.lastGrp = []; this.lastGrp[zoneid] = []; this.lastGrp[zoneid][curindex+""] = []; this.lastGrp[zoneid][curindex+""].push(value); this.grpBreak = false; } } } }; RepDocumentText.prototype.resetRow = function(fromidx){ for(var i=fromidx; i"; if( tagvalue != null ) ret += encodeTextForXML(tagvalue); ret += ""; return ret; } if ((this.format == "csvs" || this.format == "csv") && (this.lastRow.length > 0)) { this.exportString += this.newLine; var tmp = ""; for (i = 0; i < this.lastRow.length - 1; i++) { tmp = "" + this.lastRow[i].value;//Strtran(this.lastRow[i].value," ",""); this.exportString += convertCsv(tmp, this.separator) + this.separator; } if( this.lastRow[this.lastRow.length - 1].value!=null ) tmp = "" + this.lastRow[this.lastRow.length - 1].value;//Strtran(this.lastRow[this.lastRow.length-1].value," ",""); this.exportString += convertCsv(tmp, this.separator); } else if(this.format=="csv2"){ var mstr,mlen,diff; for(i=0; i=0){ this.exportString+=getXMLRow(Strtran(this.fielddesc[this.fieldsuid[curindex]]," ",""),this.lastRow[curindex].value); this.exportString+=this.newLine; } else{ var grp = "
"; this.exportString+=grp+this.newLine; if(this.lastGrp.groupheader){ keys = Object.keys(this.lastGrp.groupheader); for(j=0; j=0) ext+="xml"; var filename = (Empty(this.name) ? LibJavascript.AlfaKeyGen(10) : this.name) + ext, _Blob = getBlob(); if(typeof a.download == "undefined"){ if (navigator.msSaveBlob) { var csvData; if(this.format.indexOf("xml")>=0) { csvData = new _Blob(["\ufeff", this.exportString], {type: 'text/xml;charset=utf-8;'}); } else { csvData = new _Blob(["\ufeff", this.exportString], {type: 'text/csv;charset=utf-8;'}); } //navigator.msSaveBlob("\ufeff", csvData, filename); navigator.msSaveOrOpenBlob(csvData, filename); } else { (window.PSAlert||window).alert('Browser Not Implemented Client Side Download!!!'); } } else { var typeApp = 'text/'; if(this.format.indexOf("csv")>=0) typeApp+="csv"; else // xml typeApp+="xml"; typeApp += ';charset=utf-8;'; var mData = new _Blob(["\ufeff", this.exportString], {type: typeApp}); if (IsEdge()) { // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7260192/ navigator.msSaveOrOpenBlob(mData, filename); } else { mUrl = URL.createObjectURL(mData); a.href = mUrl; a.download = filename; a.click(); } document.body.removeChild(a); } }; RepDocumentText.prototype.drawPreviewValidator = function () {}; RepDocumentText.prototype.printPreview = function () { var _Blob = getBlob(), mData = new _Blob(["\ufeff", this.exportString], {type: 'text/xml;charset=utf-8;'}), mUrl = URL.createObjectURL(mData); windowOpenForeground(mUrl, "", "left=50,top=50,status=no,toolbar=no,menubar=no,location=no,resizable=yes", null, null, null, 2) }; function convertCsv(tmp, separator) { // riporto codice RPGenCSV var newLineWin = "\n", newLineLin = "\r", dblQuote = "\""; tmp = Strtran(tmp, dblQuote, (dblQuote + dblQuote)); tmp = Strtran(tmp, newLineLin, newLineWin); if (tmp.indexOf(separator) >= 0 || tmp.indexOf(newLineWin) >= 0 || tmp.indexOf(dblQuote) >= 0) tmp = JSON.stringify(tmp); return tmp; } function encodeSpecialCharsXML(myString){ var ret = myString.replace(/[^a-zA-Z0-9._-]/g, ""); ret = Strtran(ret, " ", ""); /*ret = Strtran(ret, "&", ""); ret = Strtran(ret, "<", ""); ret = Strtran(ret, ">", ""); ret = Strtran(ret, "\"", ""); ret = Strtran(ret, "'", ""); ret = Strtran(ret, "\\r", ""); ret = Strtran(ret, "(", ""); ret = Strtran(ret, ")", ""); ret = Strtran(ret, "=", ""); ret = Strtran(ret, ",", ""); ret = Strtran(ret, ":", ""); ret = Strtran(ret, "*", ""); ret = Strtran(ret, "%", ""); ret = Strtran(ret, "_", ""); ret = Strtran(ret, "+", ""); ret = Strtran(ret, "/", ""); ret = Strtran(ret, "\\", ""); ret = Strtran(ret, "#", "");*/ return ret; } function encodeTextForXML(myString){ var str = myString; var res = ''; var i; for(i = 0; i < str.length; i++){ if(+str[i] || str[i].toLowerCase() !== str[i].toUpperCase()){ res += str[i]; continue; }; if((str[i] === '\r')) res +=''; else if((str[i] === '<') || (str[i] === '>') || (str[i] === '/') || (str[i] === '&') || (str[i] === '\'') || (str[i] === '\"') || (str[i] === '\n') || (str[i] === ' ') || (str[i].charCodeAt(0) > 128) ) res += '&#'+str[i].charCodeAt(0)+';'; else res += str[i]; }; return res; }