playlist_edit_ctrl.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.filteredPlaylistItems = [];
  11. $scope.foundPlaylistItems = [];
  12. $scope.searchQuery = '';
  13. $scope.loading = false;
  14. $scope.playlist = {};
  15. $scope.playlistItems = [];
  16. $scope.init = function() {
  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 + '/items')
  24. .then(function(result) {
  25. $scope.playlistItems = result;
  26. });
  27. }
  28. $scope.search();
  29. };
  30. $scope.search = function() {
  31. var query = {limit: 10};
  32. if ($scope.searchQuery) {
  33. query.query = $scope.searchQuery;
  34. }
  35. $scope.loading = true;
  36. backendSrv.search(query)
  37. .then(function(results) {
  38. $scope.foundPlaylistItems = results;
  39. $scope.filterFoundPlaylistItems();
  40. })
  41. .finally(function() {
  42. $scope.loading = false;
  43. });
  44. };
  45. $scope.filterFoundPlaylistItems = function() {
  46. $scope.filteredPlaylistItems = _.reject($scope.foundPlaylistItems, function(playlistItem) {
  47. return _.findWhere($scope.playlistItems, function(listPlaylistItem) {
  48. return parseInt(listPlaylistItem.value) === playlistItem.id;
  49. });
  50. });
  51. };
  52. $scope.addPlaylistItem = function(playlistItem) {
  53. playlistItem.value = playlistItem.id.toString();
  54. playlistItem.type = 'dashboard_by_id';
  55. playlistItem.order = $scope.playlistItems.length + 1;
  56. $scope.playlistItems.push(playlistItem);
  57. $scope.filterFoundPlaylistItems();
  58. };
  59. $scope.removePlaylistItem = function(playlistItem) {
  60. _.remove($scope.playlistItems, function(listedPlaylistItem) {
  61. return playlistItem === listedPlaylistItem;
  62. });
  63. $scope.filterFoundPlaylistItems();
  64. };
  65. $scope.savePlaylist = function(playlist, playlistItems) {
  66. var savePromise;
  67. playlist.items = playlistItems;
  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-error', ['Unable to save playlist', '']);
  77. });
  78. };
  79. $scope.isNew = function() {
  80. return !$scope.playlist.id;
  81. };
  82. $scope.isPlaylistEmpty = function() {
  83. return !$scope.playlistItems.length;
  84. };
  85. $scope.isSearchResultsEmpty = function() {
  86. return !$scope.foundPlaylistItems.length;
  87. };
  88. $scope.isSearchQueryEmpty = function() {
  89. return $scope.searchQuery === '';
  90. };
  91. $scope.backToList = function() {
  92. $location.path('/playlists');
  93. };
  94. $scope.isLoading = function() {
  95. return $scope.loading;
  96. };
  97. $scope.movePlaylistItem = function(playlistItem, offset) {
  98. var currentPosition = $scope.playlistItems.indexOf(playlistItem);
  99. var newPosition = currentPosition + offset;
  100. if (newPosition >= 0 && newPosition < $scope.playlistItems.length) {
  101. $scope.playlistItems.splice(currentPosition, 1);
  102. $scope.playlistItems.splice(newPosition, 0, playlistItem);
  103. }
  104. };
  105. $scope.movePlaylistItemUp = function(playlistItem) {
  106. $scope.moveDashboard(playlistItem, -1);
  107. };
  108. $scope.movePlaylistItemDown = function(playlistItem) {
  109. $scope.moveDashboard(playlistItem, 1);
  110. };
  111. $scope.init();
  112. });
  113. });