playlist_edit_ctrl.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = {text: result.name, icon: this.navModel.node.icon};
  31. this.navModel.breadcrumbs.push(this.navModel.node);
  32. });
  33. backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
  34. this.playlistItems = result;
  35. });
  36. } else {
  37. this.navModel.node = {text: "New playlist", icon: this.navModel.node.icon};
  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
  83. .then(() => {
  84. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  85. this.$location.path('/playlists');
  86. }, () => {
  87. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  88. });
  89. }
  90. isPlaylistEmpty() {
  91. return !this.playlistItems.length;
  92. }
  93. backToList() {
  94. this.$location.path('/playlists');
  95. }
  96. searchStarted(promise) {
  97. promise.then((data) => {
  98. this.dashboardresult = data.dashboardResult;
  99. this.tagresult = data.tagResult;
  100. this.filterFoundPlaylistItems();
  101. });
  102. }
  103. movePlaylistItem(playlistItem, offset) {
  104. var currentPosition = this.playlistItems.indexOf(playlistItem);
  105. var newPosition = currentPosition + offset;
  106. if (newPosition >= 0 && newPosition < this.playlistItems.length) {
  107. this.playlistItems.splice(currentPosition, 1);
  108. this.playlistItems.splice(newPosition, 0, playlistItem);
  109. }
  110. }
  111. movePlaylistItemUp(playlistItem) {
  112. this.movePlaylistItem(playlistItem, -1);
  113. }
  114. movePlaylistItemDown(playlistItem) {
  115. this.movePlaylistItem(playlistItem, 1);
  116. }
  117. }
  118. coreModule.controller('PlaylistEditCtrl', PlaylistEditCtrl);