playlist_edit_ctrl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. console.log('filter !');
  33. console.log(this.dashboardresult);
  34. this.filteredDashboards = _.reject(this.dashboardresult, (playlistItem) => {
  35. return _.findWhere(this.playlistItems, (listPlaylistItem) => {
  36. return parseInt(listPlaylistItem.value) === playlistItem.id;
  37. });
  38. });
  39. this.filteredTags = this.tagresult;
  40. };
  41. addPlaylistItem(playlistItem) {
  42. playlistItem.value = playlistItem.id.toString();
  43. playlistItem.type = 'dashboard_by_id';
  44. playlistItem.order = this.playlistItems.length + 1;
  45. this.playlistItems.push(playlistItem);
  46. this.filterFoundPlaylistItems();
  47. };
  48. addTagPlaylistItem(tag) {
  49. console.log(tag);
  50. var playlistItem: any = {
  51. value: tag.term,
  52. type: 'dashboard_by_tag',
  53. order: this.playlistItems.length + 1,
  54. title: tag.term
  55. };
  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.dashboardresult.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. searchStarted(promise) {
  98. promise.then((data) => {
  99. console.log('searchStarted: ', 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);