playlist_edit_ctrl.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import coreModule from '../../core/core_module';
  4. export class PlaylistEditCtrl {
  5. filteredDashboards: any = [];
  6. filteredTags: any = [];
  7. searchQuery = '';
  8. loading = false;
  9. playlist: any = {
  10. interval: '5m',
  11. };
  12. playlistItems: any = [];
  13. dashboardresult: any = [];
  14. tagresult: any = [];
  15. navModel: any;
  16. /** @ngInject */
  17. constructor(
  18. private $scope,
  19. private backendSrv,
  20. private $location,
  21. $route,
  22. navModelSrv
  23. ) {
  24. this.navModel = navModelSrv.getNav('dashboards', 'playlists');
  25. if ($route.current.params.id) {
  26. var playlistId = $route.current.params.id;
  27. backendSrv.get('/api/playlists/' + playlistId).then(result => {
  28. this.playlist = result;
  29. this.navModel.node = {text: result.name, icon: this.navModel.node.icon};
  30. this.navModel.breadcrumbs.push(this.navModel.node);
  31. });
  32. backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
  33. this.playlistItems = result;
  34. });
  35. } else {
  36. this.navModel.node = {text: "New playlist", icon: this.navModel.node.icon};
  37. this.navModel.breadcrumbs.push(this.navModel.node);
  38. }
  39. }
  40. filterFoundPlaylistItems() {
  41. this.filteredDashboards = _.reject(this.dashboardresult, (playlistItem) => {
  42. return _.find(this.playlistItems, (listPlaylistItem) => {
  43. return parseInt(listPlaylistItem.value) === playlistItem.id;
  44. });
  45. });
  46. this.filteredTags = _.reject(this.tagresult, (tag) => {
  47. return _.find(this.playlistItems, (listPlaylistItem) => {
  48. return listPlaylistItem.value === tag.term;
  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. addTagPlaylistItem(tag) {
  60. var playlistItem: any = {
  61. value: tag.term,
  62. type: 'dashboard_by_tag',
  63. order: this.playlistItems.length + 1,
  64. title: tag.term
  65. };
  66. this.playlistItems.push(playlistItem);
  67. this.filterFoundPlaylistItems();
  68. }
  69. removePlaylistItem(playlistItem) {
  70. _.remove(this.playlistItems, (listedPlaylistItem) => {
  71. return playlistItem === listedPlaylistItem;
  72. });
  73. this.filterFoundPlaylistItems();
  74. }
  75. savePlaylist(playlist, playlistItems) {
  76. var savePromise;
  77. playlist.items = playlistItems;
  78. savePromise = playlist.id
  79. ? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
  80. : this.backendSrv.post('/api/playlists', playlist);
  81. savePromise
  82. .then(() => {
  83. this.$scope.appEvent('alert-success', ['Playlist saved', '']);
  84. this.$location.path('/playlists');
  85. }, () => {
  86. this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
  87. });
  88. }
  89. isNew() {
  90. return !this.playlist.id;
  91. }
  92. isPlaylistEmpty() {
  93. return !this.playlistItems.length;
  94. }
  95. backToList() {
  96. this.$location.path('/playlists');
  97. }
  98. searchStarted(promise) {
  99. promise.then((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);