playlist_edit_ctrl.ts 3.4 KB

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