saveDashboardAsCtrl.js 1.5 KB

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