2017-12-20 12:33:33 +01:00
|
|
|
import coreModule from '../../core/core_module';
|
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
|
import appEvents from 'app/core/app_events';
|
2018-08-30 11:52:31 +02:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import { toUrlParams } from 'app/core/utils/url';
|
2016-01-18 13:21:48 +01:00
|
|
|
|
|
|
|
|
class PlaylistSrv {
|
2016-01-18 14:05:48 +01:00
|
|
|
private cancelPromise: any;
|
|
|
|
|
private dashboards: any;
|
|
|
|
|
private index: number;
|
|
|
|
|
private interval: any;
|
2016-12-05 11:43:51 +01:00
|
|
|
private startUrl: string;
|
2018-08-31 16:40:23 +02:00
|
|
|
isPlaying: boolean;
|
2016-01-18 13:21:48 +01:00
|
|
|
|
|
|
|
|
/** @ngInject */
|
2018-08-30 11:52:31 +02:00
|
|
|
constructor(private $location: any, private $timeout: any, private backendSrv: any) {}
|
2016-01-18 13:21:48 +01:00
|
|
|
|
|
|
|
|
next() {
|
|
|
|
|
this.$timeout.cancel(this.cancelPromise);
|
|
|
|
|
|
2018-08-26 20:19:23 +02:00
|
|
|
const playedAllDashboards = this.index > this.dashboards.length - 1;
|
2016-01-18 14:58:46 +01:00
|
|
|
if (playedAllDashboards) {
|
2018-08-30 11:52:31 +02:00
|
|
|
window.location.href = this.startUrl;
|
2017-06-28 16:29:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 20:19:23 +02:00
|
|
|
const dash = this.dashboards[this.index];
|
2018-08-30 11:52:31 +02:00
|
|
|
const queryParams = this.$location.search();
|
|
|
|
|
const filteredParams = _.pickBy(queryParams, value => value !== null);
|
|
|
|
|
|
|
|
|
|
this.$location.url('dashboard/' + dash.uri + '?' + toUrlParams(filteredParams));
|
2017-06-28 16:29:46 +02:00
|
|
|
|
|
|
|
|
this.index++;
|
|
|
|
|
this.cancelPromise = this.$timeout(() => this.next(), this.interval);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 16:00:11 +01:00
|
|
|
prev() {
|
2016-01-18 13:21:48 +01:00
|
|
|
this.index = Math.max(this.index - 2, 0);
|
|
|
|
|
this.next();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 13:40:51 +01:00
|
|
|
start(playlistId) {
|
2016-01-18 13:21:48 +01:00
|
|
|
this.stop();
|
|
|
|
|
|
2016-12-05 11:43:51 +01:00
|
|
|
this.startUrl = window.location.href;
|
2016-01-18 13:21:48 +01:00
|
|
|
this.index = 0;
|
2017-06-02 14:00:42 +02:00
|
|
|
this.isPlaying = true;
|
2016-01-18 13:21:48 +01:00
|
|
|
|
2016-01-18 16:00:11 +01:00
|
|
|
this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
|
2017-12-21 08:39:31 +01:00
|
|
|
this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
|
|
|
|
|
this.dashboards = dashboards;
|
|
|
|
|
this.interval = kbn.interval_to_ms(playlist.interval);
|
|
|
|
|
this.next();
|
|
|
|
|
});
|
2016-01-18 16:00:11 +01:00
|
|
|
});
|
2016-01-18 13:21:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stop() {
|
2018-08-30 11:52:31 +02:00
|
|
|
if (this.isPlaying) {
|
|
|
|
|
const queryParams = this.$location.search();
|
|
|
|
|
if (queryParams.kiosk) {
|
|
|
|
|
appEvents.emit('toggle-kiosk-mode', { exit: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 13:21:48 +01:00
|
|
|
this.index = 0;
|
2017-06-02 14:00:42 +02:00
|
|
|
this.isPlaying = false;
|
2016-01-18 13:21:48 +01:00
|
|
|
|
|
|
|
|
if (this.cancelPromise) {
|
2016-01-18 16:00:11 +01:00
|
|
|
this.$timeout.cancel(this.cancelPromise);
|
2016-01-18 13:21:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.service('playlistSrv', PlaylistSrv);
|