saveDashboardAsCtrl.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. define([
  2. 'angular',
  3. ],
  4. function (angular) {
  5. 'use strict';
  6. var module = angular.module('grafana.controllers');
  7. module.controller('SaveDashboardAsCtrl', function($scope, backendSrv, $location) {
  8. $scope.init = function() {
  9. $scope.clone.id = null;
  10. $scope.clone.editable = true;
  11. $scope.clone.title = $scope.clone.title + " Copy";
  12. // remove alerts
  13. $scope.clone.rows.forEach(function(row) {
  14. row.panels.forEach(function(panel) {
  15. delete panel.alert;
  16. });
  17. });
  18. // remove auto update
  19. delete $scope.clone.autoUpdate;
  20. };
  21. function saveDashboard(options) {
  22. return backendSrv.saveDashboard($scope.clone, options).then(function(result) {
  23. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + $scope.clone.title]);
  24. $location.url('/dashboard/db/' + result.slug);
  25. $scope.appEvent('dashboard-saved', $scope.clone);
  26. $scope.dismiss();
  27. });
  28. }
  29. $scope.keyDown = function (evt) {
  30. if (evt.keyCode === 13) {
  31. $scope.saveClone();
  32. }
  33. };
  34. $scope.saveClone = function() {
  35. saveDashboard({overwrite: false}).then(null, function(err) {
  36. if (err.data && err.data.status === "name-exists") {
  37. err.isHandled = true;
  38. $scope.appEvent('confirm-modal', {
  39. title: 'Conflict',
  40. text: 'Dashboard with the same name exists.',
  41. text2: 'Would you still like to save this dashboard?',
  42. yesText: "Save & Overwrite",
  43. icon: "fa-warning",
  44. onConfirm: function() {
  45. saveDashboard({overwrite: true});
  46. }
  47. });
  48. }
  49. });
  50. };
  51. });
  52. });