@sd-angular/core
Version:
Sd Angular Core Lib
2 lines • 15.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@sd-angular/core/common"),require("@angular/core"),require("@sd-angular/core/api"),require("uuid"),require("object-hash"),require("export-to-csv"),require("exceljs"),require("@sd-angular/core/notify")):"function"==typeof define&&define.amd?define("@sd-angular/core/export",["exports","@sd-angular/core/common","@angular/core","@sd-angular/core/api","uuid","object-hash","export-to-csv","exceljs","@sd-angular/core/notify"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self)["sd-angular"]=e["sd-angular"]||{},e["sd-angular"].core=e["sd-angular"].core||{},e["sd-angular"].core.export={}),e["sd-angular"].core.common,e.ng.core,e["sd-angular"].core.api,e.uuid,e["object-hash"],e.exportToCsv,e.exceljs,e["sd-angular"].core.notify)}(this,(function(e,t,r,n,i,o,l,a,s){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=u(o),f={uploadTemplate:"/excel/uploadTemplate",generateUploadTemplate:"/excel/generateUploadTemplate",filesInFolder:"/excel/filesInFolder",removeFile:"/excel/remove",sdExportByTemplate:"/excel/sd-export-by-template",sdMergePDF:"/excel/sd-merge-pdf"};function d(e,t,r,n){return new(r||(r=Promise))((function(i,o){function l(e){try{s(n.next(e))}catch(e){o(e)}}function a(e){try{s(n.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(l,a)}s((n=n.apply(e,t||[])).next())}))}function h(e,t){var r,n,i,o,l={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;l;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return l.label++,{value:o[1],done:!1};case 5:l.label++,n=o[1],o=[0];continue;case 7:o=l.ops.pop(),l.trys.pop();continue;default:if(!(i=l.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){l=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){l.label=o[1];break}if(6===o[0]&&l.label<i[1]){l.label=i[1],i=o;break}if(i&&l.label<i[2]){l.label=i[2],l.ops.push(o);break}i[2]&&l.ops.pop(),l.trys.pop();continue}o=t.call(e,l)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}Object.create;function p(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function y(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),l=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)l.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return l}var v,m,g,b,w,x,C,S;Object.create;function F(e,t,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(e):n?n.value:t.get(e)}var k=function(){function e(e,t,r){var n=this;this.apiService=e,this.notifyService=t,this.firebaseConfiguration=r,v.set(this,{border:{bottom:{style:"thin"},left:{style:"thin"},right:{style:"thin"}},alignment:{vertical:"middle",horizontal:"center",wrapText:!0},font:{bold:!0,size:11,color:{argb:"000000"}},fill:{type:"pattern",pattern:"solid",fgColor:{argb:"FAFAFA"}}}),m.set(this,"FFFFFF"),g.set(this,"143180"),b.set(this,{border:{bottom:{style:"thin"},left:{style:"thin"},right:{style:"thin"}},alignment:{vertical:"middle",horizontal:"center",wrapText:!0},font:{bold:!0,size:11,color:{argb:F(this,m)}},fill:{type:"pattern",pattern:"solid",fgColor:{argb:F(this,g)}}}),w.set(this,{border:{bottom:{style:"thin"},left:{style:"thin"},right:{style:"thin"}},alignment:{vertical:"middle",horizontal:"center",wrapText:!0},font:{italic:!0,size:11,color:{argb:"FFFFFF"}},fill:{type:"pattern",pattern:"solid",fgColor:{argb:"FF1744"}}}),x.set(this,{border:{bottom:{style:"thin"},left:{style:"thin"},right:{style:"thin"}},alignment:{wrapText:!0},font:{italic:!0,size:9,color:{argb:"000000"}},fill:{type:"pattern",pattern:"solid",fgColor:{argb:"CFD8DC"}}}),C.set(this,{alignment:{vertical:"middle",wrapText:!0}}),S.set(this,(function(e,t){var r=c.default({folder:n.folder,userFolder:n.userFolder});return t?(r=c.default({folder:n.folder,group:t}),"dir1="+n.folder+"&dir2=G-"+r+"&dir3="+(e||"")):"dir1="+n.folder+"&dir2=U-"+r+"&dir3="+(e||"")})),this.generateTemplate=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,s,u,c,f,d,m,S,k,j,E,T,A,U,B,P=this;return h(this,(function(h){switch(h.label){case 0:if(t=e.fileName,r=e.columns,n=e.sheets,!Array.isArray(r))throw new Error("Excel template columns must be an array");try{for(i=p(r.entries()),o=i.next();!o.done;o=i.next()){if(l=y(o.value,2),s=l[0],!(u=l[1]).field)throw new Error("Column "+(s+1)+": Field is required");if(!u.title)throw new Error("Column "+(s+1)+": Title is required")}}catch(e){T={error:e}}finally{try{o&&!o.done&&(A=i.return)&&A.call(i)}finally{if(T)throw T.error}}if(c=r.some((function(e){return e.description})),f=new a.Workbook,d=f.addWorksheet("template"),r.forEach((function(e,t){var r,n,i,o,l,a,s=e.required,u=e.fill,f=e.fontColor,h=d.getCell(1,t+1),p=d.getCell(2,t+1),y=d.getCell(3,t+1),m=120;e.width&&e.width.endsWith("px")&&(m=+e.width.replace("px","")),d.getColumn(t+1).width=m/7||20,h.style=F(P,v),h.style=F(P,s?w:v),p.style=Object.assign(Object.assign({},F(P,b)),{font:Object.assign(Object.assign({},null===(r=F(P,b))||void 0===r?void 0:r.font),{color:Object.assign(Object.assign({},null===(i=null===(n=F(P,b))||void 0===n?void 0:n.font)||void 0===i?void 0:i.color),{argb:f||(null===(a=null===(l=null===(o=F(P,b))||void 0===o?void 0:o.font)||void 0===l?void 0:l.color)||void 0===a?void 0:a.argb)})}),fill:{type:"pattern",pattern:"solid",fgColor:{argb:u||F(P,g)}}}),h.value=e.field,p.value=e.title,c&&(y.style=F(P,x),y.value=e.description||"")})),null==n?void 0:n.length){m=function(e){if(e.name&&Array.isArray(e.items)&&Array.isArray(e.fields)){var t=f.addWorksheet(e.name);e.fields.forEach((function(e,r){t.getColumn(r+1).width=30,t.getCell(1,r+1).style=F(P,b),t.getCell(1,r+1).value=e})),e.items.forEach((function(r,n){e.fields.forEach((function(e,i){t.getCell(2+n,1+i).value=r[e],t.getCell(2+n,1+i).style=F(P,C)}))}))}};try{for(S=p(n),k=S.next();!k.done;k=S.next())j=k.value,m(j)}catch(e){U={error:e}}finally{try{k&&!k.done&&(B=S.return)&&B.call(S)}finally{if(U)throw U.error}}}return[4,f.xlsx.writeBuffer()];case 1:return E=h.sent(),SdUtility.downloadBlob(new Blob([E],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),t),[2]}}))}))},this.uploadTemplate=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,a,s,u,c=this;return h(this,(function(d){switch(d.label){case 0:return t=e.key,r=e.group,n=e.validator,i=f.uploadTemplate,o=""+this.functionUrl+i+"?"+F(this,S).call(this,t,r),[4,SdUtility.upload({extensions:["xlsx"],maxSizeInMb:.1,validator:n})];case 1:return(l=d.sent())?[4,this.apiService.upload(o,l).catch((function(e){throw c.notifyService.notify.warning(null==e?void 0:e.message),e}))]:[3,3];case 2:return a=d.sent(),s=a.filePath,u=a.fileName,[2,{filePath:s,fileName:u}];case 3:return[2,null]}}))}))},this.generateUploadTemplate=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,a,s;return h(this,(function(u){switch(u.label){case 0:return t=e.key,r=e.template,n=e.group,i=f.generateUploadTemplate,o=""+this.functionUrl+i+"?"+F(this,S).call(this,t,n),[4,this.apiService.post(o,r)];case 1:return l=u.sent(),a=l.filePath,s=l.fileName,[2,{filePath:a,fileName:s}]}}))}))},this.removeFile=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o;return h(this,(function(l){switch(l.label){case 0:return t=e.key,r=e.group,n=e.fileName,i=f.removeFile,o=""+this.functionUrl+i+"?"+F(this,S).call(this,t,r)+"&name="+n,[4,this.apiService.post(o)];case 1:return l.sent(),[2]}}))}))},this.filesInFolder=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l;return h(this,(function(a){switch(a.label){case 0:return r=e.key,n=e.group,i=f.filesInFolder,o=""+this.functionUrl+i+"?"+F(this,S).call(this,r,n),[4,this.apiService.get(o)];case 1:return l=a.sent(),[2,(null===(t=l.files)||void 0===t?void 0:t.map((function(e){return{filePath:e.publicUrl,fileName:e.name}})))||[]]}}))}))},this.download=function(e,t){var r=document.createElement("a");r.download=(t||i.v4())+".xlsx",r.href=e,document.body.appendChild(r),r.click(),document.body.removeChild(r)},this.exportByTemplate=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,a,s;return h(this,(function(u){switch(u.label){case 0:return t=e.filePath,r=e.columns,n=e.items,i=e.fileName,o=f.sdExportByTemplate,l=""+this.functionUrl+o,[4,this.apiService.post(l,{filePath:t,columns:r,items:n,fileName:i},null,{responseType:"arraybuffer"})];case 1:return a=u.sent(),s=new Blob([a],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,"}),SdUtility.downloadBlob(s,i),[2]}}))}))},this.exportCSV=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,a,s,u,c;return h(this,(function(f){t=e.columns,r=e.items,n=e.fileName,i={};try{for(o=p(t),a=o.next();!a.done;a=o.next())s=a.value,i[s.field]=s.title}catch(e){u={error:e}}finally{try{a&&!a.done&&(c=o.return)&&c.call(o)}finally{if(u)throw u.error}}return new l.ExportToCsv({filename:(n||"CSV")+"_"+Date.toFormat(new Date,"yyyy-MM-dd-HH-mm-ss"),fieldSeparator:",",quoteStrings:'"',decimalSeparator:".",showLabels:!0,showTitle:!1,title:n||"CSV",useBom:!0}).generateCsv(function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(y(arguments[t]));return e}([i],r)),[2]}))}))},this.export=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,s,u,c,f,d,m,g,S,k,j,E,T,A,U,B,P,W=this;return h(this,(function(h){switch(h.label){case 0:t=e.columns,r=e.items,n=e.fileName,i=e.sheets,o=!1;try{for(l=p(t.entries()),s=l.next();!s.done;s=l.next()){if(u=y(s.value,2),c=u[0],!(f=u[1]).field)throw new Error("Column "+(c+1)+": Field is required");if(!f.title)throw new Error("Column "+(c+1)+": Title is required");f.description&&(o=!0)}}catch(e){A={error:e}}finally{try{s&&!s.done&&(U=l.return)&&U.call(l)}finally{if(A)throw A.error}}if(d=new a.Workbook,m=d.addWorksheet("data"),t.forEach((function(e,t){var r=120;e.width&&e.width.endsWith("px")&&(r=+e.width.replace("px","")),m.getColumn(t+1).width=r/7||20,m.getCell(1,t+1).style=F(W,v),e.required?m.getCell(2,t+1).style=F(W,w):m.getCell(2,t+1).style=F(W,b),m.getCell(1,t+1).value=e.field,m.getCell(2,t+1).value=e.title,o&&(m.getCell(3,t+1).style=F(W,x),m.getCell(3,t+1).value=e.description||"")})),null==i?void 0:i.length){g=function(e){if(e.name&&Array.isArray(e.items)&&Array.isArray(e.fields)){var t=d.addWorksheet(e.name);e.fields.forEach((function(e,r){t.getColumn(r+1).width=30,t.getCell(1,r+1).style=F(W,b),t.getCell(1,r+1).value=e})),e.items.forEach((function(r,n){e.fields.forEach((function(e,i){t.getCell(2+n,1+i).value=r[e],t.getCell(2+n,1+i).style=F(W,C)}))}))}};try{for(S=p(i),k=S.next();!k.done;k=S.next())j=k.value,g(j)}catch(e){B={error:e}}finally{try{k&&!k.done&&(P=S.return)&&P.call(S)}finally{if(B)throw B.error}}}return E=o?4:3,r.forEach((function(e,r){t.forEach((function(t,n){"number"==typeof e[t.field]?(m.getCell(E+r,1+n).value=+e[t.field],m.getCell(E+r,1+n).numFmt="#"):m.getCell(E+r,1+n).value=e[t.field],m.getCell(E+r,1+n).style=Object.assign({},F(W,C))}))})),[4,d.xlsx.writeBuffer()];case 1:return T=h.sent(),SdUtility.downloadBlob(new Blob([T],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),n),[2]}}))}))},this.exportCustom=function(e){return d(n,void 0,void 0,(function(){var t,r,n,i,o,l,s,u,c,f,d,v,m,g,b,w,x,C,S,F,k,j,E,T,A,U,B,P;return h(this,(function(h){switch(h.label){case 0:if(t=e.fileName,!(null==(r=e.sheets)?void 0:r.length))return this.notifyService.notify.warning("Phải có ít nhất 1 sheet"),[2];n=new a.Workbook;try{for(i=p(r.entries()),o=i.next();!o.done;o=i.next()){l=y(o.value,2),s=l[0],u=l[1],c=u.name,f=u.columns,d=u.cells,v=n.addWorksheet(c||"Sheet "+(s+1));try{for(A=void 0,m=p(f||[]),g=m.next();!g.done;g=m.next())S=g.value,v.getColumn(S.index).style=S.style,v.getColumn(S.index).width=S.width}catch(e){A={error:e}}finally{try{g&&!g.done&&(U=m.return)&&U.call(m)}finally{if(A)throw A.error}}try{for(B=void 0,b=p(d),w=b.next();!w.done;w=b.next())x=w.value,C=x.row,S=x.column,F=x.value,k=x.style,v.getCell(C,S).style=k||v.getColumn(S).style,v.getCell(C,S).value=F}catch(e){B={error:e}}finally{try{w&&!w.done&&(P=b.return)&&P.call(b)}finally{if(B)throw B.error}}}}catch(e){E={error:e}}finally{try{o&&!o.done&&(T=i.return)&&T.call(i)}finally{if(E)throw E.error}}return[4,n.xlsx.writeBuffer()];case 1:return j=h.sent(),SdUtility.downloadBlob(new Blob([j],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),t),[2]}}))}))},this.mergePDF=function(e){return d(n,void 0,void 0,(function(){var t,r,n;return h(this,(function(i){switch(i.label){case 0:return t=f.sdMergePDF,r=""+this.functionUrl+t,[4,this.apiService.post(r,{urls:e},null,{responseType:"arraybuffer"})];case 1:return n=i.sent(),SdUtility.downloadBlob(new Blob([n],{type:"application/pdf"})),[2]}}))}))},this.uploadXlsx=function(){return d(n,void 0,void 0,(function(){var e=this;return h(this,(function(t){switch(t.label){case 0:return[4,SdUtility.upload({extensions:["xlsx"],maxSizeInMb:10}).then((function(t){return e.parseXlsx(t)}))];case 1:return[2,t.sent()]}}))}))},this.parseXlsx=function(e){return d(n,void 0,void 0,(function(){return h(this,(function(t){return e?[2,new Promise((function(t,r){var n=new a.Workbook,i=new FileReader;i.onload=function(){var o=i.result;o?n.xlsx.load(o).then((function(n){var i=n.worksheets[0];if(i){var o=[],l=i.getRow(1).values;Array.isArray(l)&&(i.eachRow((function(e,t){var r=e.hasValues,n=e.values;if(r&&t>1){var i={};l.forEach((function(e,t){if(e){var r=e.toString();i[r]=n[t],"string"==typeof i[r]&&(i[r]=i[r].trim()),""!==i[r]&&void 0!==i[r]||(i[r]=null)}})),o.push(i)}})),t({items:o,file:e}))}else r("Không tìm thấy dữ liệu")})):r("Có lỗi xảy ra khi đọc file")},i.readAsArrayBuffer(e)}))]:[2,{items:[],file:null}]}))}))}}return Object.defineProperty(e.prototype,"functionUrl",{get:function(){var e;return null===(e=this.firebaseConfiguration)||void 0===e?void 0:e.functionUrl},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"folder",{get:function(){var e=this.firebaseConfiguration;return e.project+"-"+e.env},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"userFolder",{get:function(){var e=this.firebaseConfiguration.folder;if(e)return"string"==typeof e?e:e()},enumerable:!1,configurable:!0}),e}();v=new WeakMap,m=new WeakMap,g=new WeakMap,b=new WeakMap,w=new WeakMap,x=new WeakMap,C=new WeakMap,S=new WeakMap,k.ɵprov=r.ɵɵdefineInjectable({factory:function(){return new k(r.ɵɵinject(n.SdApiService),r.ɵɵinject(s.SdNotifyService),r.ɵɵinject(t.FIREBASE_CONFIG,8))},token:k,providedIn:"root"}),k.decorators=[{type:r.Injectable,args:[{providedIn:"root"}]}],k.ctorParameters=function(){return[{type:n.SdApiService},{type:s.SdNotifyService},{type:void 0,decorators:[{type:r.Inject,args:[t.FIREBASE_CONFIG]},{type:r.Optional}]}]},e.API=f,e.SdExportService=k,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=sd-angular-core-export.umd.min.js.map