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