grafana/public/app/features/dashboard/dashboardNavCtrl.js

157 lines
4.6 KiB
JavaScript
Raw Normal View History

2013-09-13 15:52:13 -05:00
define([
'angular',
2014-08-07 07:35:19 -05:00
'lodash',
'config',
2014-08-16 06:13:26 -05:00
'store',
2014-06-13 06:50:09 -05:00
'filesaver'
2013-09-13 15:52:13 -05:00
],
function (angular, _) {
2013-09-13 15:52:13 -05:00
'use strict';
2014-07-28 11:11:52 -05:00
var module = angular.module('grafana.controllers');
2013-09-13 15:52:13 -05:00
module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
2013-09-13 15:52:13 -05:00
$scope.init = function() {
$scope.onAppEvent('save-dashboard', $scope.saveDashboard);
$scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
2013-09-13 15:52:13 -05:00
};
2015-02-01 08:45:11 -06:00
$scope.openEditView = function(editview) {
var search = _.extend($location.search(), {editview: editview});
$location.search(search);
2013-09-13 15:52:13 -05:00
};
$scope.starDashboard = function() {
if ($scope.dashboardMeta.isStarred) {
backendSrv.delete('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
2015-03-13 03:52:59 -05:00
$scope.dashboardMeta.isStarred = false;
});
}
else {
backendSrv.post('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
$scope.dashboardMeta.isStarred = true;
});
}
};
$scope.shareDashboard = function() {
$scope.appEvent('show-modal', {
2015-03-29 07:30:03 -05:00
src: './app/features/dashboard/partials/shareModal.html',
scope: $scope.$new(),
});
};
$scope.openSearch = function() {
$scope.appEvent('show-dash-search');
};
$scope.dashboardTitleAction = function() {
$scope.appEvent('hide-dash-editor');
$scope.exitFullscreen();
};
$scope.saveDashboard = function(options) {
var clone = angular.copy($scope.dashboard);
2014-02-01 03:51:35 -06:00
backendSrv.saveDashboard(clone, options).then(function(data) {
$scope.dashboard.version = data.version;
$scope.appEvent('dashboard-saved', $scope.dashboard);
2014-02-01 03:51:35 -06:00
var dashboardUrl = '/dashboard/db/' + data.slug;
if (dashboardUrl !== $location.path()) {
$location.url(dashboardUrl);
}
$scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
}, $scope.handleSaveDashError);
};
$scope.handleSaveDashError = function(err) {
2015-03-03 03:20:52 -06:00
if (err.data && err.data.status === "version-mismatch") {
err.isHandled = true;
$scope.appEvent('confirm-modal', {
title: 'Someone else has updated this dashboard!',
text: "Would you still like to save this dashboard?",
yesText: "Save & Overwrite",
icon: "fa-warning",
onConfirm: function() {
$scope.saveDashboard({overwrite: true});
}
});
}
2015-03-03 03:20:52 -06:00
if (err.data && err.data.status === "name-exists") {
err.isHandled = true;
$scope.appEvent('confirm-modal', {
title: 'Another dashboard with the same name exists',
text: "Would you still like to save this dashboard?",
yesText: "Save & Overwrite",
icon: "fa-warning",
onConfirm: function() {
$scope.saveDashboard({overwrite: true});
}
});
}
2013-09-13 15:52:13 -05:00
};
$scope.deleteDashboard = function() {
$scope.appEvent('confirm-modal', {
title: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
2015-03-05 14:02:25 -06:00
icon: 'fa-trash',
yesText: 'Delete',
onConfirm: function() {
$scope.deleteDashboardConfirmed();
}
});
};
$scope.deleteDashboardConfirmed = function() {
backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
$scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
$location.url('/');
2014-06-12 10:40:37 -05:00
});
2013-09-13 15:52:13 -05:00
};
2014-06-13 06:50:09 -05:00
$scope.saveDashboardAs = function() {
var newScope = $rootScope.$new();
newScope.clone = angular.copy($scope.dashboard);
$scope.appEvent('show-modal', {
src: './app/features/dashboard/partials/saveDashboardAs.html',
scope: newScope,
});
};
2014-06-13 06:50:09 -05:00
$scope.exportDashboard = function() {
var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
};
2013-09-13 15:52:13 -05:00
$scope.snapshot = function() {
$scope.dashboard.snapshot = true;
$rootScope.$broadcast('refresh');
$timeout(function() {
$scope.exportDashboard();
$scope.dashboard.snapshot = false;
$scope.appEvent('dashboard-snapshot-cleanup');
}, 1000);
};
$scope.editJson = function() {
$scope.appEvent('show-json-editor', { object: $scope.dashboard });
};
$scope.stopPlaylist = function() {
playlistSrv.stop(1);
};
2013-09-13 15:52:13 -05:00
});
});