| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- define([
- 'angular',
- 'lodash',
- 'config',
- 'store',
- 'filesaver'
- ],
- function (angular, _) {
- 'use strict';
- var module = angular.module('grafana.controllers');
- module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
- $scope.init = function() {
- $scope.onAppEvent('save-dashboard', $scope.saveDashboard);
- $scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
- };
- $scope.openEditView = function(editview) {
- var search = _.extend($location.search(), {editview: editview});
- $location.search(search);
- };
- $scope.starDashboard = function() {
- if ($scope.dashboardMeta.isStarred) {
- backendSrv.delete('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
- $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', {
- src: './app/features/dashboard/partials/shareDashboard.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);
- backendSrv.saveDashboard(clone, options).then(function(data) {
- $scope.dashboard.version = data.version;
- $scope.appEvent('dashboard-saved', $scope.dashboard);
- 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) {
- 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});
- }
- });
- }
- 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});
- }
- });
- }
- };
- $scope.deleteDashboard = function() {
- $scope.appEvent('confirm-modal', {
- title: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
- 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('/');
- });
- };
- $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,
- });
- };
- $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());
- };
- $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);
- };
- });
- });
|