playlist_edit_ctrl.ts 3.8 KB

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