playlist_srv.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import coreModule from '../../core/core_module';
  2. import kbn from 'app/core/utils/kbn';
  3. import appEvents from 'app/core/app_events';
  4. import _ from 'lodash';
  5. import { toUrlParams } from 'app/core/utils/url';
  6. class PlaylistSrv {
  7. private cancelPromise: any;
  8. private dashboards: any;
  9. private index: number;
  10. private interval: any;
  11. private startUrl: string;
  12. isPlaying: boolean;
  13. /** @ngInject */
  14. constructor(private $location: any, private $timeout: any, private backendSrv: any) {}
  15. next() {
  16. this.$timeout.cancel(this.cancelPromise);
  17. const playedAllDashboards = this.index > this.dashboards.length - 1;
  18. if (playedAllDashboards) {
  19. window.location.href = this.startUrl;
  20. return;
  21. }
  22. const dash = this.dashboards[this.index];
  23. const queryParams = this.$location.search();
  24. const filteredParams = _.pickBy(queryParams, value => value !== null);
  25. this.$location.url('dashboard/' + dash.uri + '?' + toUrlParams(filteredParams));
  26. this.index++;
  27. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  28. }
  29. prev() {
  30. this.index = Math.max(this.index - 2, 0);
  31. this.next();
  32. }
  33. start(playlistId) {
  34. this.stop();
  35. this.startUrl = window.location.href;
  36. this.index = 0;
  37. this.isPlaying = true;
  38. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  39. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  40. this.dashboards = dashboards;
  41. this.interval = kbn.interval_to_ms(playlist.interval);
  42. this.next();
  43. });
  44. });
  45. }
  46. stop() {
  47. if (this.isPlaying) {
  48. const queryParams = this.$location.search();
  49. if (queryParams.kiosk) {
  50. appEvents.emit('toggle-kiosk-mode', { exit: true });
  51. }
  52. }
  53. this.index = 0;
  54. this.isPlaying = false;
  55. if (this.cancelPromise) {
  56. this.$timeout.cancel(this.cancelPromise);
  57. }
  58. }
  59. }
  60. coreModule.service('playlistSrv', PlaylistSrv);