playlist_edit_ctrl.ts 4.0 KB

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