playlist_edit_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from '../../core/core_module';
  5. import config from 'app/core/config';
  6. export class PlaylistEditCtrl {
  7. filteredPlaylistItems: any = [];
  8. foundPlaylistItems: any = [];
  9. searchQuery: string = '';
  10. loading: boolean = false;
  11. playlist: any = {
  12. interval: '10m',
  13. };
  14. playlistItems: any = [];
  15. /** @ngInject */
  16. constructor(private $scope, private playlistSrv, private backendSrv, private $location, private $route) {
  17. if ($route.current.params.id) {
  18. var playlistId = $route.current.params.id;
  19. backendSrv.get('/api/playlists/' + playlistId)
  20. .then((result) => {
  21. this.playlist = result;
  22. });
  23. backendSrv.get('/api/playlists/' + playlistId + '/items')
  24. .then((result) => {
  25. this.playlistItems = result;
  26. });
  27. }
  28. this.search();
  29. }
  30. search() {
  31. var query: any = {limit: 10};
  32. if (this.searchQuery) {
  33. query.query = this.searchQuery;
  34. }
  35. this.loading = true;
  36. this.backendSrv.search(query)
  37. .then((results) => {
  38. this.foundPlaylistItems = results;
  39. this.filterFoundPlaylistItems();
  40. })
  41. .finally(() => {
  42. this.loading = false;
  43. });
  44. };
  45. filterFoundPlaylistItems() {
  46. this.filteredPlaylistItems = _.reject(this.foundPlaylistItems, (playlistItem) => {
  47. return _.findWhere(this.playlistItems, (listPlaylistItem) => {
  48. return parseInt(listPlaylistItem.value) === playlistItem.id;
  49. });
  50. });
  51. };
  52. addPlaylistItem(playlistItem) {
  53. playlistItem.value = playlistItem.id.toString();
  54. playlistItem.type = 'dashboard_by_id';
  55. playlistItem.order = this.playlistItems.length + 1;
  56. this.playlistItems.push(playlistItem);
  57. this.filterFoundPlaylistItems();
  58. };
  59. removePlaylistItem(playlistItem) {
  60. _.remove(this.playlistItems, (listedPlaylistItem) => {
  61. return playlistItem === listedPlaylistItem;
  62. });
  63. this.filterFoundPlaylistItems();
  64. };
  65. savePlaylist(playlist, playlistItems) {
  66. var savePromise;
  67. playlist.items = playlistItems;
  68. savePromise = playlist.id
  69. ? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
  70. : this.backendSrv.post('/api/playlists', playlist);
  71. savePromise
  72. .then(() => {
  73. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  74. this.$location.path('/playlists');
  75. }, () => {
  76. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  77. });
  78. };
  79. isNew() {
  80. return !this.playlist.id;
  81. };
  82. isPlaylistEmpty() {
  83. return !this.playlistItems.length;
  84. };
  85. isSearchResultsEmpty() {
  86. return !this.foundPlaylistItems.length;
  87. };
  88. isSearchQueryEmpty() {
  89. return this.searchQuery === '';
  90. };
  91. backToList() {
  92. this.$location.path('/playlists');
  93. };
  94. isLoading() {
  95. return this.loading;
  96. };
  97. movePlaylistItem(playlistItem, offset) {
  98. var currentPosition = this.playlistItems.indexOf(playlistItem);
  99. var newPosition = currentPosition + offset;
  100. if (newPosition >= 0 && newPosition < this.playlistItems.length) {
  101. this.playlistItems.splice(currentPosition, 1);
  102. this.playlistItems.splice(newPosition, 0, playlistItem);
  103. }
  104. };
  105. movePlaylistItemUp(playlistItem) {
  106. this.movePlaylistItem(playlistItem, -1);
  107. };
  108. movePlaylistItemDown(playlistItem) {
  109. this.movePlaylistItem(playlistItem, 1);
  110. };
  111. }
  112. coreModule.controller('PlaylistEditCtrl', PlaylistEditCtrl);