playlist_edit_ctrl.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 = '';
  10. loading = false;
  11. playlist: any = {
  12. interval: '5m',
  13. };
  14. playlistItems: any = [];
  15. dashboardresult: any = [];
  16. tagresult: any = [];
  17. navModel: any;
  18. /** @ngInject */
  19. constructor(
  20. private $scope,
  21. private playlistSrv,
  22. private backendSrv,
  23. private $location,
  24. private $route,
  25. private navModelSrv
  26. ) {
  27. this.navModel = navModelSrv.getPlaylistsNav(0);
  28. if ($route.current.params.id) {
  29. var playlistId = $route.current.params.id;
  30. backendSrv.get('/api/playlists/' + playlistId).then(result => {
  31. this.playlist = result;
  32. });
  33. backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
  34. this.playlistItems = result;
  35. });
  36. }
  37. }
  38. filterFoundPlaylistItems() {
  39. this.filteredDashboards = _.reject(this.dashboardresult, (playlistItem) => {
  40. return _.find(this.playlistItems, (listPlaylistItem) => {
  41. return parseInt(listPlaylistItem.value) === playlistItem.id;
  42. });
  43. });
  44. this.filteredTags = _.reject(this.tagresult, (tag) => {
  45. return _.find(this.playlistItems, (listPlaylistItem) => {
  46. return listPlaylistItem.value === tag.term;
  47. });
  48. });
  49. }
  50. addPlaylistItem(playlistItem) {
  51. playlistItem.value = playlistItem.id.toString();
  52. playlistItem.type = 'dashboard_by_id';
  53. playlistItem.order = this.playlistItems.length + 1;
  54. this.playlistItems.push(playlistItem);
  55. this.filterFoundPlaylistItems();
  56. }
  57. addTagPlaylistItem(tag) {
  58. var playlistItem: any = {
  59. value: tag.term,
  60. type: 'dashboard_by_tag',
  61. order: this.playlistItems.length + 1,
  62. title: tag.term
  63. };
  64. this.playlistItems.push(playlistItem);
  65. this.filterFoundPlaylistItems();
  66. }
  67. removePlaylistItem(playlistItem) {
  68. _.remove(this.playlistItems, (listedPlaylistItem) => {
  69. return playlistItem === listedPlaylistItem;
  70. });
  71. this.filterFoundPlaylistItems();
  72. }
  73. savePlaylist(playlist, playlistItems) {
  74. var savePromise;
  75. playlist.items = playlistItems;
  76. savePromise = playlist.id
  77. ? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
  78. : this.backendSrv.post('/api/playlists', playlist);
  79. savePromise
  80. .then(() => {
  81. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  82. this.$location.path('/playlists');
  83. }, () => {
  84. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  85. });
  86. }
  87. isNew() {
  88. return !this.playlist.id;
  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);