playlist_edit_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.getNav('dashboards', 'playlists');
  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. this.navModel.node = {text: result.name, icon: this.navModel.node.icon};
  33. this.navModel.breadcrumbs.push(this.navModel.node);
  34. });
  35. backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
  36. this.playlistItems = result;
  37. });
  38. } else {
  39. this.navModel.node = {text: "New playlist", icon: this.navModel.node.icon};
  40. this.navModel.breadcrumbs.push(this.navModel.node);
  41. }
  42. }
  43. filterFoundPlaylistItems() {
  44. this.filteredDashboards = _.reject(this.dashboardresult, (playlistItem) => {
  45. return _.find(this.playlistItems, (listPlaylistItem) => {
  46. return parseInt(listPlaylistItem.value) === playlistItem.id;
  47. });
  48. });
  49. this.filteredTags = _.reject(this.tagresult, (tag) => {
  50. return _.find(this.playlistItems, (listPlaylistItem) => {
  51. return listPlaylistItem.value === tag.term;
  52. });
  53. });
  54. }
  55. addPlaylistItem(playlistItem) {
  56. playlistItem.value = playlistItem.id.toString();
  57. playlistItem.type = 'dashboard_by_id';
  58. playlistItem.order = this.playlistItems.length + 1;
  59. this.playlistItems.push(playlistItem);
  60. this.filterFoundPlaylistItems();
  61. }
  62. addTagPlaylistItem(tag) {
  63. var playlistItem: any = {
  64. value: tag.term,
  65. type: 'dashboard_by_tag',
  66. order: this.playlistItems.length + 1,
  67. title: tag.term
  68. };
  69. this.playlistItems.push(playlistItem);
  70. this.filterFoundPlaylistItems();
  71. }
  72. removePlaylistItem(playlistItem) {
  73. _.remove(this.playlistItems, (listedPlaylistItem) => {
  74. return playlistItem === listedPlaylistItem;
  75. });
  76. this.filterFoundPlaylistItems();
  77. }
  78. savePlaylist(playlist, playlistItems) {
  79. var savePromise;
  80. playlist.items = playlistItems;
  81. savePromise = playlist.id
  82. ? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
  83. : this.backendSrv.post('/api/playlists', playlist);
  84. savePromise
  85. .then(() => {
  86. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  87. this.$location.path('/playlists');
  88. }, () => {
  89. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  90. });
  91. }
  92. isNew() {
  93. return !this.playlist.id;
  94. }
  95. isPlaylistEmpty() {
  96. return !this.playlistItems.length;
  97. }
  98. backToList() {
  99. this.$location.path('/playlists');
  100. }
  101. searchStarted(promise) {
  102. promise.then((data) => {
  103. this.dashboardresult = data.dashboardResult;
  104. this.tagresult = data.tagResult;
  105. this.filterFoundPlaylistItems();
  106. });
  107. }
  108. movePlaylistItem(playlistItem, offset) {
  109. var currentPosition = this.playlistItems.indexOf(playlistItem);
  110. var newPosition = currentPosition + offset;
  111. if (newPosition >= 0 && newPosition < this.playlistItems.length) {
  112. this.playlistItems.splice(currentPosition, 1);
  113. this.playlistItems.splice(newPosition, 0, playlistItem);
  114. }
  115. }
  116. movePlaylistItemUp(playlistItem) {
  117. this.movePlaylistItem(playlistItem, -1);
  118. }
  119. movePlaylistItemDown(playlistItem) {
  120. this.movePlaylistItem(playlistItem, 1);
  121. }
  122. }
  123. coreModule.controller('PlaylistEditCtrl', PlaylistEditCtrl);