Skip to content

Commit 26d5971

Browse files
committed
Add CSV exporter, fix #9
1 parent f59670e commit 26d5971

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

utuputki/webui/public/partials/history.html

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
<div class="col-sm-6 col-lg-4">
1+
<div class="col-sm-12">
22
<h2>History</h2>
33
</div>
4+
<div class="col-sm-12 queue_add_block">
5+
<div class="row">
6+
<div class="col-lg-6 col-md-6">
7+
<form class="input-group">
8+
<span class="input-group-btn submit-button">
9+
<button class="btn btn-default" type="submit" ng-click="export()">Export</button>
10+
</span>
11+
</form>
12+
</div>
13+
</div>
14+
</div>
415
<div class="col-sm-12 queue_show_block">
516
<div class="queue_info" ng-if="grid_opts.data">
617
<div ui-grid="grid_opts" ui-grid-auto-resize ng-style="getTableHeight()" class="queue_grid"></div>

utuputki/webui/public/static/custom/controllers/history.js

+23
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ app.controller('HistoryController', ['$scope', '$window', '$rootScope', '$locati
101101
refresh_grid();
102102
}
103103

104+
$scope.export = function() {
105+
var c_player = Player.get_current_player();
106+
if(c_player == null) {
107+
return;
108+
}
109+
110+
var playlist = Playlist.get_playlist();
111+
var out = [];
112+
for(var i = 0; i < playlist.length; i++) {
113+
var field = playlist[i];
114+
var source = field.source;
115+
var duration = moment.duration(source.length_seconds, "seconds").format("hh:mm:ss", { trim: false });
116+
out.push([
117+
source.id,
118+
source.title,
119+
duration,
120+
'http://youtu.be/'+source.youtube_hash,
121+
source.description
122+
]);
123+
}
124+
exportToCsv("playlist.csv", out);
125+
};
126+
104127
function init() {
105128
var c_player = Player.get_current_player();
106129
Playlist.query(c_player.id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
// http://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
4+
function exportToCsv(filename, rows) {
5+
var processRow = function (row) {
6+
var finalVal = '';
7+
for (var j = 0; j < row.length; j++) {
8+
var innerValue = row[j] === null ? '' : row[j].toString();
9+
if (row[j] instanceof Date) {
10+
innerValue = row[j].toLocaleString();
11+
}
12+
var result = innerValue.replace(/"/g, '""');
13+
if (result.search(/("|,|\n)/g) >= 0)
14+
result = '"' + result + '"';
15+
if (j > 0)
16+
finalVal += ',';
17+
finalVal += result;
18+
}
19+
return finalVal + '\n';
20+
};
21+
22+
var csvFile = '';
23+
for (var i = 0; i < rows.length; i++) {
24+
csvFile += processRow(rows[i]);
25+
}
26+
27+
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
28+
if (navigator.msSaveBlob) { // IE 10+
29+
navigator.msSaveBlob(blob, filename);
30+
} else {
31+
var link = document.createElement("a");
32+
if (link.download !== undefined) { // feature detection
33+
// Browsers that support HTML5 download attribute
34+
var url = URL.createObjectURL(blob);
35+
link.setAttribute("href", url);
36+
link.setAttribute("download", filename);
37+
link.style.visibility = 'hidden';
38+
document.body.appendChild(link);
39+
link.click();
40+
document.body.removeChild(link);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)