aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/export-json-to-excel
diff options
context:
space:
mode:
authorMichael Lando <ml636r@att.com>2017-06-09 03:19:04 +0300
committerMichael Lando <ml636r@att.com>2017-06-09 03:19:04 +0300
commited64b5edff15e702493df21aa3230b81593e6133 (patch)
treea4cb01fdaccc34930a8db403a3097c0d1e40914b /catalog-ui/src/app/directives/export-json-to-excel
parent280f8015d06af1f41a3ef12e8300801c7a5e0d54 (diff)
[SDC-29] catalog 1707 rebase commit.
Change-Id: I43c3dc5cf44abf5da817649bc738938a3e8388c1 Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'catalog-ui/src/app/directives/export-json-to-excel')
-rw-r--r--catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html7
-rw-r--r--catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts61
2 files changed, 68 insertions, 0 deletions
diff --git a/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html
new file mode 100644
index 0000000000..55696b33fa
--- /dev/null
+++ b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.html
@@ -0,0 +1,7 @@
+<span data-ng-click="export()"
+ class="sprite-new download-btn hand"
+ uib-tooltip="Download"
+ tooltip-class="uib-custom-tooltip"
+ tooltip-placement="bottom"
+ data-tests-id="download-excel"></span>
+
diff --git a/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts
new file mode 100644
index 0000000000..deb1c94936
--- /dev/null
+++ b/catalog-ui/src/app/directives/export-json-to-excel/export-json-to-excel.ts
@@ -0,0 +1,61 @@
+'use strict';
+import {FileUtils} from "app/utils";
+
+export interface IJsonExportExcelScope extends ng.IScope {
+ initExportExcelData:Function;
+ export:()=>void;
+}
+
+export class JsonExportExcelDirective implements ng.IDirective {
+
+ constructor(private fileUtils:FileUtils) {
+ }
+
+ scope = {
+ initExportExcelData: '&'//get function that init and returns Models.ExportExcel
+ };
+
+ public restrict = 'E';
+ replace = true;
+ template = ():string => {
+ return require('./export-json-to-excel.html');
+ };
+
+ private jsonToTableText = (headers:string, dataObj:Array<Object>):string=> {
+ let tableText = headers + "\n";
+ _.each(dataObj, (rowData:any) => {
+ tableText += _.values(rowData).join() + "\n";
+ });
+ return tableText;
+ };
+
+ link = (scope:IJsonExportExcelScope, $elem:any) => {
+ scope.export = ():void => {
+ let exportExcelData = scope.initExportExcelData();
+ exportExcelData.fileName = !!exportExcelData.fileName ? exportExcelData.fileName : 'export-excel';
+
+ let headers = exportExcelData.tableHeaders.join(",");
+ let tableData = "";
+ if (exportExcelData.groupByField) {
+ _.each(_.groupBy(exportExcelData.dataObj, exportExcelData.groupByField), (groupData:Array<Object>) => {
+ tableData += this.jsonToTableText(headers, groupData);
+ tableData += '\n';
+ });
+ } else {
+ tableData = this.jsonToTableText(headers, exportExcelData.dataObj);
+ }
+
+ let blob = new Blob([exportExcelData.metaData.join('\n\n') + '\n\n' + tableData], {type: "text/csv;charset=utf-8"});
+
+ return this.fileUtils.downloadFile(blob, exportExcelData.fileName + '.csv');
+ };
+
+ };
+
+ public static factory = (fileUtils:FileUtils)=> {
+ return new JsonExportExcelDirective( fileUtils);
+ };
+
+}
+
+JsonExportExcelDirective.factory.$inject = ['FileUtils'];