grafana/public/app/features/playlist/playlist_edit_ctrl.ts

135 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
import coreModule from '../../core/core_module';
2016-01-26 12:03:43 -06:00
export class PlaylistEditCtrl {
filteredDashboards: any = [];
filteredTags: any = [];
2017-12-20 05:33:33 -06:00
searchQuery = '';
2017-01-30 08:04:55 -06:00
loading = false;
2016-01-26 12:03:43 -06:00
playlist: any = {
2017-12-20 05:33:33 -06:00
interval: '5m',
2016-01-26 12:03:43 -06:00
};
2016-01-26 12:03:43 -06:00
playlistItems: any = [];
dashboardresult: any = [];
tagresult: any = [];
navModel: any;
isNew: boolean;
2016-01-26 12:03:43 -06:00
/** @ngInject */
constructor(private $scope, private backendSrv, private $location, $route, navModelSrv) {
2017-12-20 05:33:33 -06:00
this.navModel = navModelSrv.getNav('dashboards', 'playlists', 0);
this.isNew = !$route.current.params.id;
2016-01-26 12:03:43 -06:00
if ($route.current.params.id) {
2018-08-26 13:19:23 -05:00
const playlistId = $route.current.params.id;
2016-01-26 12:03:43 -06:00
2017-12-20 05:33:33 -06:00
backendSrv.get('/api/playlists/' + playlistId).then(result => {
this.playlist = result;
});
2016-01-26 12:03:43 -06:00
2017-12-20 05:33:33 -06:00
backendSrv.get('/api/playlists/' + playlistId + '/items').then(result => {
this.playlistItems = result;
});
2016-01-26 12:03:43 -06:00
}
}
filterFoundPlaylistItems() {
this.filteredDashboards = _.reject(this.dashboardresult, playlistItem => {
return _.find(this.playlistItems, listPlaylistItem => {
return parseInt(listPlaylistItem.value, 10) === playlistItem.id;
2016-01-26 12:03:43 -06:00
});
});
this.filteredTags = _.reject(this.tagresult, tag => {
return _.find(this.playlistItems, listPlaylistItem => {
return listPlaylistItem.value === tag.term;
});
});
}
2016-01-26 12:03:43 -06:00
addPlaylistItem(playlistItem) {
playlistItem.value = playlistItem.id.toString();
2017-12-20 05:33:33 -06:00
playlistItem.type = 'dashboard_by_id';
2016-01-26 12:03:43 -06:00
playlistItem.order = this.playlistItems.length + 1;
this.playlistItems.push(playlistItem);
this.filterFoundPlaylistItems();
}
2016-01-26 12:03:43 -06:00
addTagPlaylistItem(tag) {
2018-08-26 13:19:23 -05:00
const playlistItem: any = {
value: tag.term,
2017-12-20 05:33:33 -06:00
type: 'dashboard_by_tag',
order: this.playlistItems.length + 1,
2017-12-20 05:33:33 -06:00
title: tag.term,
};
this.playlistItems.push(playlistItem);
this.filterFoundPlaylistItems();
}
2016-01-26 12:03:43 -06:00
removePlaylistItem(playlistItem) {
_.remove(this.playlistItems, listedPlaylistItem => {
2016-01-26 12:03:43 -06:00
return playlistItem === listedPlaylistItem;
});
this.filterFoundPlaylistItems();
}
2016-01-26 12:03:43 -06:00
savePlaylist(playlist, playlistItems) {
let savePromise;
2016-01-26 12:03:43 -06:00
playlist.items = playlistItems;
savePromise = playlist.id
2017-12-20 05:33:33 -06:00
? this.backendSrv.put('/api/playlists/' + playlist.id, playlist)
: this.backendSrv.post('/api/playlists', playlist);
savePromise.then(
() => {
2017-12-20 05:33:33 -06:00
this.$scope.appEvent('alert-success', ['Playlist saved', '']);
this.$location.path('/playlists');
},
() => {
2017-12-20 05:33:33 -06:00
this.$scope.appEvent('alert-error', ['Unable to save playlist', '']);
}
);
}
2016-01-26 12:03:43 -06:00
isPlaylistEmpty() {
return !this.playlistItems.length;
}
2016-01-26 12:03:43 -06:00
backToList() {
2017-12-20 05:33:33 -06:00
this.$location.path('/playlists');
}
2016-01-26 12:03:43 -06:00
searchStarted(promise) {
promise.then(data => {
this.dashboardresult = data.dashboardResult;
this.tagresult = data.tagResult;
this.filterFoundPlaylistItems();
});
}
2016-01-26 12:03:43 -06:00
movePlaylistItem(playlistItem, offset) {
2018-08-26 13:19:23 -05:00
const currentPosition = this.playlistItems.indexOf(playlistItem);
const newPosition = currentPosition + offset;
2016-01-26 12:03:43 -06:00
if (newPosition >= 0 && newPosition < this.playlistItems.length) {
this.playlistItems.splice(currentPosition, 1);
this.playlistItems.splice(newPosition, 0, playlistItem);
}
}
2016-01-26 12:03:43 -06:00
movePlaylistItemUp(playlistItem) {
this.movePlaylistItem(playlistItem, -1);
}
2016-01-26 12:03:43 -06:00
movePlaylistItemDown(playlistItem) {
this.movePlaylistItem(playlistItem, 1);
}
2016-01-26 12:03:43 -06:00
}
2017-12-20 05:33:33 -06:00
coreModule.controller('PlaylistEditCtrl', PlaylistEditCtrl);