playlist_edit_ctrl.js 3.9 KB

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