playlist_edit_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import _ from 'lodash';
  2. import coreModule from '../../core/core_module';
  3. export class PlaylistEditCtrl {
  4. filteredDashboards: any = [];
  5. filteredTags: any = [];
  6. searchQuery = '';
  7. loading = false;
  8. playlist: any = {
  9. interval: '5m',
  10. };
  11. playlistItems: any = [];
  12. dashboardresult: any = [];
  13. tagresult: any = [];
  14. navModel: any;
  15. isNew: boolean;
  16. /** @ngInject */
  17. constructor(
  18. private $scope,
  19. private backendSrv,
  20. private $location,
  21. $route,
  22. navModelSrv
  23. ) {
  24. this.navModel = navModelSrv.getNav('dashboards', 'playlists', 0);
  25. this.isNew = $route.current.params.id;
  26. if ($route.current.params.id) {
  27. var playlistId = $route.current.params.id;
  28. backendSrv.get('/api/playlists/' + playlistId).then(result => {
  29. this.playlist = result;
  30. this.navModel.node = {
  31. text: result.name,
  32. icon: this.navModel.node.icon,
  33. };
  34. this.navModel.breadcrumbs.push(this.navModel.node);
  35. });
  36. backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
  37. this.playlistItems = result;
  38. });
  39. } else {
  40. this.navModel.node = {
  41. text: 'New playlist',
  42. icon: this.navModel.node.icon,
  43. };
  44. this.navModel.breadcrumbs.push(this.navModel.node);
  45. }
  46. }
  47. filterFoundPlaylistItems() {
  48. this.filteredDashboards = _.reject(this.dashboardresult, playlistItem => {
  49. return _.find(this.playlistItems, listPlaylistItem => {
  50. return parseInt(listPlaylistItem.value) === playlistItem.id;
  51. });
  52. });
  53. this.filteredTags = _.reject(this.tagresult, tag => {
  54. return _.find(this.playlistItems, listPlaylistItem => {
  55. return listPlaylistItem.value === tag.term;
  56. });
  57. });
  58. }
  59. addPlaylistItem(playlistItem) {
  60. playlistItem.value = playlistItem.id.toString();
  61. playlistItem.type = 'dashboard_by_id';
  62. playlistItem.order = this.playlistItems.length + 1;
  63. this.playlistItems.push(playlistItem);
  64. this.filterFoundPlaylistItems();
  65. }
  66. addTagPlaylistItem(tag) {
  67. var playlistItem: any = {
  68. value: tag.term,
  69. type: 'dashboard_by_tag',
  70. order: this.playlistItems.length + 1,
  71. title: tag.term,
  72. };
  73. this.playlistItems.push(playlistItem);
  74. this.filterFoundPlaylistItems();
  75. }
  76. removePlaylistItem(playlistItem) {
  77. _.remove(this.playlistItems, listedPlaylistItem => {
  78. return playlistItem === listedPlaylistItem;
  79. });
  80. this.filterFoundPlaylistItems();
  81. }
  82. savePlaylist(playlist, playlistItems) {
  83. var savePromise;
  84. playlist.items = playlistItems;
  85. savePromise = playlist.id
  86. ? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
  87. : this.backendSrv.post('/api/playlists', playlist);
  88. savePromise.then(
  89. () => {
  90. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  91. this.$location.path('/playlists');
  92. },
  93. () => {
  94. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  95. }
  96. );
  97. }
  98. isPlaylistEmpty() {
  99. return !this.playlistItems.length;
  100. }
  101. backToList() {
  102. this.$location.path('/playlists');
  103. }
  104. searchStarted(promise) {
  105. promise.then(data => {
  106. this.dashboardresult = data.dashboardResult;
  107. this.tagresult = data.tagResult;
  108. this.filterFoundPlaylistItems();
  109. });
  110. }
  111. movePlaylistItem(playlistItem, offset) {
  112. var currentPosition = this.playlistItems.indexOf(playlistItem);
  113. var newPosition = currentPosition + offset;
  114. if (newPosition >= 0 && newPosition < this.playlistItems.length) {
  115. this.playlistItems.splice(currentPosition, 1);
  116. this.playlistItems.splice(newPosition, 0, playlistItem);
  117. }
  118. }
  119. movePlaylistItemUp(playlistItem) {
  120. this.movePlaylistItem(playlistItem, -1);
  121. }
  122. movePlaylistItemDown(playlistItem) {
  123. this.movePlaylistItem(playlistItem, 1);
  124. }
  125. }
  126. coreModule.controller('PlaylistEditCtrl', PlaylistEditCtrl);