-
Notifications
You must be signed in to change notification settings - Fork 3
/
angular-export.js
132 lines (113 loc) · 5.27 KB
/
angular-export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
angular
.module('export.csv', [])
.directive('export', function() {
return {
restrict: 'AE',
scope: {
data: '=exportData',
title: '=exportTitle',
keys: '=exportKeys',
headers: '=exportHeaders',
filename: '=exportFilename'
},
link: function(scope, el, attrs) {
el.bind('click', function() {
var data = scope.data;
var title = scope.title;
var keys = scope.keys;
var headers = scope.headers;
var filename = scope.filename;
if (data && !Array.isArray(data)) throwError("Data must be a valid javascript array");
if (keys && !Array.isArray(keys)) throwError("Keys must be a valid javascript array");
if (headers && !Array.isArray(headers)) throwError("Headers must be a valid javascript array");
// Remove any angular added keys
var json_data = angular.toJson(data);
data = JSON.parse(json_data);
if (!data.length) throwError("No data available to export");
// Get keys & headers to be exported
if (!keys) {
var sample_data = data[0];
keys = Object.keys(sample_data);
}
if (headers && (headers.length != keys.length)) {
throwError("Headers must be the same length as the " + (keys ? "keys": "data") + " to export");
} else if (!headers) {
headers = convertToUppercase(keys);
}
data = filterArrayKeys(data, keys);
//////////////////////////
//// Export Functions ////
//////////////////////////
var csv = '';
if (title) {
csv += title + '\r\n\n';
}
csv += headers.join(",") + '\r\n';
for (var i = 0; i < data.length; i++) {
var row = "";
for (var index in data[i]) {
row += '"' + data[i][index] + '",';
}
row.slice(0, row.length - 1);
csv += row + '\r\n';
}
if (csv == '') {
throwError("Invalid Data");
}
if (!filename) filename = "Export " + getDate();
filename = filename.replace(/ /g, "_");
var uri = 'data:text/csv;charset=utf-8,' + escape(csv);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = filename + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
/////////////////////////
/// Utility Functions ///
/////////////////////////
function throwError(message) {
throw message;
return;
}
function filterArrayKeys(array, keys) {
var result = [];
if (!array.length || !keys.length) return result;
for (var i = 0; i < array.length; i++) {
var array_object = array[i];
var new_object = {};
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var value = array_object[key];
new_object[key] = value;
}
result.push(new_object);
}
return result;
};
function convertToUppercase(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
var key = array[i];
result.push(key.charAt(0).toUpperCase() + key.slice(1));
}
return result;
}
function getDate() {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var date = new Date();
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + ' ' + monthNames[monthIndex] + ' ' + year;
}
});
}
};
});