Skip to content

Commit

Permalink
download functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mlison committed Aug 15, 2015
1 parent 061648b commit 5fe652f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ Box.Application.addBehavior('navigation-common', function(context) {
case 'start-terminal':
windowSvc.openTerminalToHost(user, host);
break;

case 'download-configs':
context.broadcast("download-configs");
break;
}
},
onchange: function(event, element, elementType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ Box.Application.addModule('radiator-controller', function(context) {
store.customRadiators.removeRadiatorConfig(radiatorName, graphConfig.chart);
}

function isConfigMarkedForRemoval(config) {
return config.removeAfterUse;
}

function downloadConfigs() {
var configs = host ? metrics.defaultMetrics(host) : store.customRadiators.readConfiguration(radiatorName),
url = 'data:text/json;charset=utf8,' + encodeURIComponent(JSON.stringify(configs)),
filename = (host || radiatorName) + '.json';

$('<a>')
.attr('href', url)
.attr('download', filename)[0]
.click();
}

return {
init: function() {
intercom = context.getGlobal("Intercom").getInstance();
Expand All @@ -241,7 +256,7 @@ Box.Application.addModule('radiator-controller', function(context) {

host = windowSvc.getHashVariable("host");
radiatorName = windowSvc.getHashVariable("name");
var configs = host ? metrics.defaultMetrics(host) : store.customRadiators.readConfiguration(radiatorName);
var configs = host ? metrics.defaultMetrics(host) : store.customRadiators.readConfiguration(radiatorName);

detailsStop = parseInt(new Date().getTime());
var timescale = windowSvc.getTimescale();
Expand All @@ -262,11 +277,16 @@ Box.Application.addModule('radiator-controller', function(context) {
configs[i] = config = config.chart ? config : { chart: config };
initGraph(config);
// wipe config if it is marked for deletion (e.g. single graph)
if (config.removeAfterUse) {
if (isConfigMarkedForRemoval(config)) {
store.customRadiators.removeConfiguration(radiatorName);
}
});

// if all configs were removed there's no configs to download
if (configs.every(isConfigMarkedForRemoval)) {
$('#download-configs').remove();
}

var metric = (configs[0].chart.metric || configs[0].chart.type).toUpperCase();
if (configs.length === 1) {
// we're showing single graph, might as well update title nicely
Expand All @@ -280,13 +300,20 @@ Box.Application.addModule('radiator-controller', function(context) {

onmousedown: isDraggingMouseDown,

messages: ["timescale-changed"],
messages: [
"download-configs",
"timescale-changed"
],

onmessage: function(name, timescale) {
switch (name) {
case 'timescale-changed':
cubismGraphs.resetCubismContext();
break;

case 'download-configs':
downloadConfigs();
break;
}
},

Expand Down

0 comments on commit 5fe652f

Please sign in to comment.