2016-01-17 11:34:51 +01:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
2013-09-13 13:52:13 -07:00
|
|
|
|
2016-01-17 11:34:51 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import moment from 'moment';
|
|
|
|
|
import angular from 'angular';
|
2013-09-13 13:52:13 -07:00
|
|
|
|
2016-01-17 11:34:51 +01:00
|
|
|
export class DashNavCtrl {
|
|
|
|
|
|
|
|
|
|
/** @ngInject */
|
2016-04-03 09:43:33 -04:00
|
|
|
constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
|
2013-09-13 13:52:13 -07:00
|
|
|
|
|
|
|
|
$scope.init = function() {
|
2014-09-04 08:56:50 +02:00
|
|
|
$scope.onAppEvent('save-dashboard', $scope.saveDashboard);
|
|
|
|
|
$scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
|
2016-02-23 00:55:34 -08:00
|
|
|
$scope.onAppEvent('export-dashboard', $scope.snapshot);
|
2016-02-23 08:11:04 -08:00
|
|
|
$scope.onAppEvent('quick-snapshot', $scope.quickSnapshot);
|
2016-01-17 11:34:51 +01:00
|
|
|
|
|
|
|
|
$scope.showSettingsMenu = $scope.dashboardMeta.canEdit || $scope.contextSrv.isEditor;
|
|
|
|
|
|
|
|
|
|
if ($scope.dashboardMeta.isSnapshot) {
|
|
|
|
|
$scope.showSettingsMenu = false;
|
|
|
|
|
var meta = $scope.dashboardMeta;
|
|
|
|
|
$scope.titleTooltip = 'Created: ' + moment(meta.created).calendar();
|
|
|
|
|
if (meta.expires) {
|
|
|
|
|
$scope.titleTooltip += '<br>Expires: ' + moment(meta.expires).fromNow() + '<br>';
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-13 13:52:13 -07:00
|
|
|
};
|
|
|
|
|
|
2015-02-01 15:45:11 +01:00
|
|
|
$scope.openEditView = function(editview) {
|
|
|
|
|
var search = _.extend($location.search(), {editview: editview});
|
|
|
|
|
$location.search(search);
|
2013-09-13 13:52:13 -07:00
|
|
|
};
|
|
|
|
|
|
2015-02-02 11:32:00 +01:00
|
|
|
$scope.starDashboard = function() {
|
|
|
|
|
if ($scope.dashboardMeta.isStarred) {
|
2015-02-28 09:46:37 +01:00
|
|
|
backendSrv.delete('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
|
2015-03-13 09:52:59 +01:00
|
|
|
$scope.dashboardMeta.isStarred = false;
|
2015-02-02 11:32:00 +01:00
|
|
|
});
|
2016-01-17 11:34:51 +01:00
|
|
|
} else {
|
2015-02-28 09:46:37 +01:00
|
|
|
backendSrv.post('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
|
2015-02-02 11:32:00 +01:00
|
|
|
$scope.dashboardMeta.isStarred = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-01-29 11:48:57 -05:00
|
|
|
$scope.shareDashboard = function(tabIndex) {
|
|
|
|
|
var modalScope = $scope.$new();
|
|
|
|
|
modalScope.tabIndex = tabIndex;
|
|
|
|
|
|
2015-02-02 09:45:45 +01:00
|
|
|
$scope.appEvent('show-modal', {
|
2016-02-01 18:19:02 +01:00
|
|
|
src: 'public/app/features/dashboard/partials/shareModal.html',
|
2016-01-29 11:48:57 -05:00
|
|
|
scope: modalScope
|
2015-02-02 09:45:45 +01:00
|
|
|
});
|
2014-06-11 20:22:05 +02:00
|
|
|
};
|
|
|
|
|
|
2016-02-23 08:11:04 -08:00
|
|
|
$scope.quickSnapshot = function() {
|
|
|
|
|
$scope.shareDashboard(1);
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-26 16:42:15 +02:00
|
|
|
$scope.openSearch = function() {
|
2015-02-17 18:20:47 +01:00
|
|
|
$scope.appEvent('show-dash-search');
|
2014-08-26 16:42:15 +02:00
|
|
|
};
|
|
|
|
|
|
2015-09-21 11:29:14 +02:00
|
|
|
$scope.hideTooltip = function(evt) {
|
|
|
|
|
angular.element(evt.currentTarget).tooltip('hide');
|
2015-09-18 02:30:02 -07:00
|
|
|
$scope.appEvent('hide-dash-search');
|
2015-09-18 00:45:29 -07:00
|
|
|
};
|
|
|
|
|
|
2015-12-15 09:06:11 +01:00
|
|
|
$scope.makeEditable = function() {
|
2015-12-15 08:41:16 +01:00
|
|
|
$scope.dashboard.editable = true;
|
|
|
|
|
|
|
|
|
|
var clone = $scope.dashboard.getSaveModelClone();
|
|
|
|
|
|
|
|
|
|
backendSrv.saveDashboard(clone, {overwrite: false}).then(function(data) {
|
|
|
|
|
$scope.dashboard.version = data.version;
|
|
|
|
|
$scope.appEvent('dashboard-saved', $scope.dashboard);
|
|
|
|
|
$scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
|
|
|
|
|
|
2016-01-17 11:34:51 +01:00
|
|
|
// force refresh whole page
|
2015-12-15 08:41:16 +01:00
|
|
|
window.location.href = window.location.href;
|
|
|
|
|
}, $scope.handleSaveDashError);
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-02 22:24:01 +01:00
|
|
|
$scope.saveDashboard = function(options) {
|
2015-04-27 10:59:14 +02:00
|
|
|
if ($scope.dashboardMeta.canSave === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 11:22:35 +02:00
|
|
|
var clone = $scope.dashboard.getSaveModelClone();
|
2014-02-01 10:51:35 +01:00
|
|
|
|
2015-03-02 22:24:01 +01:00
|
|
|
backendSrv.saveDashboard(clone, options).then(function(data) {
|
|
|
|
|
$scope.dashboard.version = data.version;
|
2015-02-28 09:46:37 +01:00
|
|
|
$scope.appEvent('dashboard-saved', $scope.dashboard);
|
2014-02-01 10:51:35 +01:00
|
|
|
|
2015-02-28 09:46:37 +01:00
|
|
|
var dashboardUrl = '/dashboard/db/' + data.slug;
|
2014-06-10 21:32:38 +02:00
|
|
|
|
2015-02-28 09:46:37 +01:00
|
|
|
if (dashboardUrl !== $location.path()) {
|
|
|
|
|
$location.url(dashboardUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
|
2015-03-02 22:24:01 +01:00
|
|
|
}, $scope.handleSaveDashError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.handleSaveDashError = function(err) {
|
2015-03-03 10:20:52 +01:00
|
|
|
if (err.data && err.data.status === "version-mismatch") {
|
2015-03-02 22:24:01 +01:00
|
|
|
err.isHandled = true;
|
|
|
|
|
|
|
|
|
|
$scope.appEvent('confirm-modal', {
|
2016-03-04 20:56:53 +01:00
|
|
|
title: 'Conflict',
|
|
|
|
|
text: 'Someone else has updated this dashboard.',
|
|
|
|
|
text2: 'Would you still like to save this dashboard?',
|
2015-03-05 20:04:54 +01:00
|
|
|
yesText: "Save & Overwrite",
|
2015-03-02 22:24:01 +01:00
|
|
|
icon: "fa-warning",
|
|
|
|
|
onConfirm: function() {
|
|
|
|
|
$scope.saveDashboard({overwrite: true});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 10:20:52 +01:00
|
|
|
if (err.data && err.data.status === "name-exists") {
|
2015-03-02 22:24:01 +01:00
|
|
|
err.isHandled = true;
|
|
|
|
|
|
|
|
|
|
$scope.appEvent('confirm-modal', {
|
2016-03-04 20:56:53 +01:00
|
|
|
title: 'Conflict',
|
|
|
|
|
text: 'Dashboard with the same name exists.',
|
|
|
|
|
text2: 'Would you still like to save this dashboard?',
|
2015-03-05 20:04:54 +01:00
|
|
|
yesText: "Save & Overwrite",
|
2015-03-02 22:24:01 +01:00
|
|
|
icon: "fa-warning",
|
|
|
|
|
onConfirm: function() {
|
|
|
|
|
$scope.saveDashboard({overwrite: true});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-09-13 13:52:13 -07:00
|
|
|
};
|
|
|
|
|
|
2015-02-07 20:25:53 +01:00
|
|
|
$scope.deleteDashboard = function() {
|
2014-11-10 15:01:30 +01:00
|
|
|
$scope.appEvent('confirm-modal', {
|
2016-03-12 10:13:49 +01:00
|
|
|
title: 'Delete',
|
|
|
|
|
text: 'Do you want to delete this dashboard?',
|
2016-03-04 20:56:53 +01:00
|
|
|
text2: $scope.dashboard.title,
|
2015-03-05 21:02:25 +01:00
|
|
|
icon: 'fa-trash',
|
|
|
|
|
yesText: 'Delete',
|
2014-11-10 15:01:30 +01:00
|
|
|
onConfirm: function() {
|
2015-02-07 20:25:53 +01:00
|
|
|
$scope.deleteDashboardConfirmed();
|
2014-11-10 15:01:30 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-07 20:25:53 +01:00
|
|
|
$scope.deleteDashboardConfirmed = function() {
|
2015-02-28 09:46:37 +01:00
|
|
|
backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
|
2015-02-07 20:25:53 +01:00
|
|
|
$scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
|
2015-02-28 09:46:37 +01:00
|
|
|
$location.url('/');
|
2014-06-12 17:40:37 +02:00
|
|
|
});
|
2013-09-13 13:52:13 -07:00
|
|
|
};
|
2014-06-13 13:50:09 +02:00
|
|
|
|
2015-03-06 10:01:18 +01:00
|
|
|
$scope.saveDashboardAs = function() {
|
2015-02-18 10:44:36 +01:00
|
|
|
var newScope = $rootScope.$new();
|
2015-04-06 11:22:35 +02:00
|
|
|
newScope.clone = $scope.dashboard.getSaveModelClone();
|
2015-04-27 10:09:32 +02:00
|
|
|
newScope.clone.editable = true;
|
|
|
|
|
newScope.clone.hideControls = false;
|
2015-02-18 10:44:36 +01:00
|
|
|
|
|
|
|
|
$scope.appEvent('show-modal', {
|
2016-02-01 18:19:02 +01:00
|
|
|
src: 'public/app/features/dashboard/partials/saveDashboardAs.html',
|
2015-02-18 10:44:36 +01:00
|
|
|
scope: newScope,
|
2016-03-02 21:27:08 +01:00
|
|
|
modalClass: 'modal--narrow'
|
2015-02-18 10:44:36 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-13 13:50:09 +02:00
|
|
|
$scope.exportDashboard = function() {
|
2015-04-06 11:22:35 +02:00
|
|
|
var clone = $scope.dashboard.getSaveModelClone();
|
|
|
|
|
var blob = new Blob([angular.toJson(clone, true)], { type: "application/json;charset=utf-8" });
|
2016-01-17 11:34:51 +01:00
|
|
|
var wnd: any = window;
|
|
|
|
|
wnd.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
|
2014-06-13 13:50:09 +02:00
|
|
|
};
|
2013-09-13 13:52:13 -07:00
|
|
|
|
2015-03-20 19:16:59 -04: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);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
$scope.editJson = function() {
|
2015-04-06 11:22:35 +02:00
|
|
|
var clone = $scope.dashboard.getSaveModelClone();
|
|
|
|
|
$scope.appEvent('show-json-editor', { object: clone });
|
2014-08-28 12:44:01 +02:00
|
|
|
};
|
|
|
|
|
|
2014-03-08 16:27:01 +01:00
|
|
|
$scope.stopPlaylist = function() {
|
|
|
|
|
playlistSrv.stop(1);
|
2014-03-07 19:24:45 +01:00
|
|
|
};
|
|
|
|
|
|
2016-01-17 11:34:51 +01:00
|
|
|
$scope.init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dashNavDirective() {
|
|
|
|
|
return {
|
|
|
|
|
restrict: 'E',
|
2016-02-01 18:19:02 +01:00
|
|
|
templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
|
2016-01-17 11:34:51 +01:00
|
|
|
controller: DashNavCtrl,
|
|
|
|
|
transclude: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-09-13 13:52:13 -07:00
|
|
|
|
2016-01-17 11:34:51 +01:00
|
|
|
angular.module('grafana.directives').directive('dashnav', dashNavDirective);
|