saveDashboardAsCtrl.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. };
  13. function saveDashboard(options) {
  14. return backendSrv.saveDashboard($scope.clone, options).then(function(result) {
  15. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + $scope.clone.title]);
  16. $location.url('/dashboard/db/' + result.slug);
  17. $scope.appEvent('dashboard-saved', $scope.clone);
  18. $scope.dismiss();
  19. });
  20. }
  21. $scope.keyDown = function (evt) {
  22. if (evt.keyCode === 13) {
  23. $scope.saveClone();
  24. }
  25. };
  26. $scope.saveClone = function() {
  27. saveDashboard({overwrite: false}).then(null, function(err) {
  28. if (err.data && err.data.status === "name-exists") {
  29. err.isHandled = true;
  30. $scope.appEvent('confirm-modal', {
  31. title: 'Another dashboard with the same name exists',
  32. text: "Would you still like to save this dashboard?",
  33. yesText: "Save & Overwrite",
  34. icon: "fa-warning",
  35. onConfirm: function() {
  36. saveDashboard({overwrite: true});
  37. }
  38. });
  39. }
  40. });
  41. };
  42. });
  43. });