playlist_edit_ctrl.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. define([
  2. 'angular',
  3. 'app/core/config',
  4. 'lodash'
  5. ],
  6. function (angular, config, _) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('PlaylistEditCtrl', function($scope, playlistSrv, backendSrv, $location, $route) {
  10. $scope.timespan = config.playlist_timespan;
  11. $scope.filteredDashboards = [];
  12. $scope.foundDashboards = [];
  13. $scope.searchQuery = '';
  14. $scope.loading = false;
  15. $scope.playlist = {};
  16. $scope.dashboards = [];
  17. if ($route.current.params.id) {
  18. var playlistId = $route.current.params.id;
  19. backendSrv.get('/api/playlists/' + playlistId)
  20. .then(function(result) {
  21. $scope.playlist = result;
  22. });
  23. backendSrv.get('/api/playlists/' + playlistId + '/dashboards')
  24. .then(function(result) {
  25. $scope.dashboards = result;
  26. });
  27. }
  28. $scope.search = function() {
  29. var query = {starred: true, limit: 10};
  30. if ($scope.searchQuery) {
  31. query.query = $scope.searchQuery;
  32. query.starred = false;
  33. }
  34. $scope.loading = true;
  35. backendSrv.search(query)
  36. .then(function(results) {
  37. $scope.foundDashboards = results;
  38. $scope.filterFoundDashboards();
  39. })
  40. .finally(function() {
  41. $scope.loading = false;
  42. });
  43. };
  44. $scope.filterFoundDashboards = function() {
  45. $scope.filteredDashboards = _.reject($scope.foundDashboards, function(dashboard) {
  46. return _.findWhere($scope.dashboards, function(listDashboard) {
  47. return listDashboard.id === dashboard.id;
  48. });
  49. });
  50. };
  51. $scope.addDashboard = function(dashboard) {
  52. $scope.dashboards.push(dashboard);
  53. $scope.filterFoundDashboards();
  54. };
  55. $scope.removeDashboard = function(dashboard) {
  56. _.remove($scope.dashboards, function(listedDashboard) {
  57. return dashboard === listedDashboard;
  58. });
  59. $scope.filterFoundDashboards();
  60. };
  61. $scope.savePlaylist = function(playlist, dashboards) {
  62. var savePromise;
  63. playlist.data = dashboards.map(function(dashboard) {
  64. return dashboard.id;
  65. });
  66. // Hardcoding playlist type for this iteration
  67. playlist.type = "dashboards";
  68. savePromise = playlist.id
  69. ? backendSrv.put('/api/playlists/' + playlist.id, playlist)
  70. : backendSrv.post('/api/playlists', playlist);
  71. savePromise
  72. .then(function() {
  73. $scope.appEvent('alert-success', ['Playlist saved', '']);
  74. $location.path('/playlists');
  75. }, function() {
  76. $scope.appEvent('alert-success', ['Unable to save playlist', '']);
  77. });
  78. };
  79. $scope.isNew = function() {
  80. return !$scope.playlist.id;
  81. };
  82. $scope.startPlaylist = function(playlist, dashboards) {
  83. playlistSrv.start(dashboards, playlist.timespan);
  84. };
  85. $scope.isPlaylistEmpty = function() {
  86. return !$scope.dashboards.length;
  87. };
  88. $scope.isSearchResultsEmpty = function() {
  89. return !$scope.foundDashboards.length;
  90. };
  91. $scope.isSearchQueryEmpty = function() {
  92. return $scope.searchQuery === '';
  93. };
  94. $scope.backToList = function() {
  95. $location.path('/playlists');
  96. };
  97. $scope.isLoading = function() {
  98. return $scope.loading;
  99. };
  100. $scope.moveDashboard = function(dashboard, offset) {
  101. var currentPosition = $scope.dashboards.indexOf(dashboard);
  102. var newPosition = currentPosition + offset;
  103. if (newPosition >= 0 && newPosition < $scope.dashboards.length) {
  104. $scope.dashboards.splice(currentPosition, 1);
  105. $scope.dashboards.splice(newPosition, 0, dashboard);
  106. }
  107. };
  108. $scope.moveDashboardUp = function(dashboard) {
  109. $scope.moveDashboard(dashboard, -1);
  110. };
  111. $scope.moveDashboardDown = function(dashboard) {
  112. $scope.moveDashboard(dashboard, 1);
  113. };
  114. $scope.search();
  115. });
  116. });