-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjira_exporter.js
196 lines (180 loc) · 7.02 KB
/
jira_exporter.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
<li class="aui-list-item">'
+ '<a class="aui-list-item-link " id="currentCsvFields"'
+ 'href="/sr/jira.issueviews:searchrequest-csv-current-fields/temp/SearchRequest.csv?jqlQuery=order+by+created+DESC&tempMax=1000">'
+ 'Export Excel CSV (current fields) (plugin)'
+ '</a>'
+'</li>'
*/
(function (chrome) {
'use strict';
var added = false;
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function (mutations, observer) {
var current = document.getElementById('currentCsvFields');
if (!added && current) {
added = true;
var all = document.getElementById('allCsvFields');
current.parentElement.parentElement.appendChild(
createElement('blommish_all', 'JiraExporter CSV (all)', all.getAttribute('href')));
current.parentElement.parentElement.appendChild(
createElement('blommish_current', 'JiraExporter CSV (current)', current.getAttribute('href')));
} else {
added = false;
}
});
observer.observe(document, {
childList: true,
subtree: true
});
function map(str, callback) {
var replace = '""';
var replacement = 'BlommishTemporal';
var first = true;
var headers = [];
var rows = [];
var lines = str.split('\n');
var lookingForEndTag = false;
var i = 0;
var j = 0;
for (var lineId = 0; lineId < lines.length; lineId++) {
var line = lines[lineId];
if (!rows[i]) {
rows[i] = [];
}
if (first) {
headers = line.split(',');
first = false;
} else {
while (line && 0 !== line.length) {
if (!lookingForEndTag && line.charAt('0') == '"') {
lookingForEndTag = true;
line = replaceAll(line.substring(1), replace, replacement);
}
if (lookingForEndTag) {
if (!rows[i][j]) {
rows[i][j] = '';
}
if (line.indexOf('"') === -1) {
rows[i][j] = (rows[i][j] + line + '\n');
line = null;
} else if (line.indexOf('""') !== -1 && line.indexOf('""') <= line.indexOf('"')) {
var split = splitFirst(line, '""', 2);
rows[i][j] = rows[i][j] + split[0] + '""';
line = split[1];
} else {
var split = splitFirst(line, '"', 1);
rows[i][j] = replaceAll((rows[i][j] + split[0]), replacement, replace);
line = split[1];
line = replaceAll(line, replacement, replace);
lookingForEndTag = false;
j++;
}
} else {
var split = splitFirst(line, ',');
rows[i][j++] = split[0];
line = split[1];
}
}
if (!lookingForEndTag && (!line || 0 === line.length)) {
i++;
j = 0;
}
}
}
withOptions(headers, rows, callback);
}
function withOptions(headers, rows, callback) {
chrome.storage.sync.get({
separator: ';',
commaColumns: 'Custom field (Story Points)'
}, function (items) {
var columnNames = items.commaColumns.split(',');
for (var i = 0; i < columnNames.length; i++) {
columnNames[i] = columnNames[i].trim();
}
callback(toResult(headers, rows, items.separator.trim(), columnNames));
});
}
function toResult(headers, rows, separator, columnNames) {
var str = "";
var commaColumns = [];
for (var i = 0; i < headers.length; i++) {
if (i !== 0) {
str += separator;
}
if (columnNames.indexOf(headers[i]) > -1) {
commaColumns.push(i);
}
str += headers[i];
}
for (var i = 0; i < rows.length; i++) {
var rowString = "";
for (var j = 0; j < rows[i].length; j++) {
if (j !== 0) {
rowString += separator;
}
var temp = rows[i][j];
if (commaColumns.indexOf(j) > -1) {
temp = temp.replace('.', ',');
}
if (temp.indexOf(separator) !== -1 || temp.indexOf('"') !== -1 || temp.indexOf('\n') !== -1) {
temp = '"' + temp + '"';
}
rowString += temp;
}
str += '\n' + rowString;
}
return '\ufeff' + str; //Adds BOM, TODO: add checkbox to options in settings to deactivate
}
function getPageContents(callback, url, params) {
var http = new XMLHttpRequest();
http.open("GET", url, true);
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
callback(http.responseText);
}
}
http.send(params);
}
function download(path) {
getPageContents(function (result) {
map(result, function (str) {
var uri = 'data:text/csv;charset=utf-8,' + encodeURI(str);
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "jira_export.csv";
downloadLink.click();
});
}, document.location.origin + path)
}
function splitFirst(str, del, i) {
i = typeof i !== 'undefined' ? i : 0;
if (-1 === str.indexOf(del)) {
return [str, null];
} else {
return [str.substring(0, str.indexOf(del)), str.substring(str.indexOf(del) + 1 + i)];
}
}
function replaceAll(str, search, replacement) {
return str.replace(new RegExp(search, 'g'), replacement);
}
function createElement(id, text, href) {
var li = document.createElement('li');
li.setAttribute('class', 'aui-list-item');
li.innerHTML = '<a class="aui-list-item-link" href="#" id="' + id + '">' + text + '</a>';
li.addEventListener('click', function () {
download(href);
});
li.addEventListener('mouseover', function () {
for (var i = 0; i < li.parentElement.childNodes.length; i++) {
li.parentElement.childNodes[i].className = 'aui-list-item';
}
li.className = 'aui-list-item active';
});
li.addEventListener('mouseout', function () {
li.className = 'aui-list-item';
});
return li;
}
}(chrome));