playlist_srv.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. export class PlaylistSrv {
  7. private cancelPromise: any;
  8. private dashboards: Array<{ uri: string }>;
  9. private index: number;
  10. private interval: number;
  11. private startUrl: string;
  12. private numberOfLoops = 0;
  13. isPlaying: boolean;
  14. /** @ngInject */
  15. constructor(private $location: any, private $timeout: any, private backendSrv: any) {}
  16. next() {
  17. this.$timeout.cancel(this.cancelPromise);
  18. const playedAllDashboards = this.index > this.dashboards.length - 1;
  19. if (playedAllDashboards) {
  20. this.numberOfLoops++;
  21. // This does full reload of the playlist to keep memory in check due to existing leaks but at the same time
  22. // we do not want page to flicker after each full loop.
  23. if (this.numberOfLoops >= 3) {
  24. window.location.href = this.startUrl;
  25. return;
  26. }
  27. this.index = 0;
  28. }
  29. const dash = this.dashboards[this.index];
  30. const queryParams = this.$location.search();
  31. const filteredParams = _.pickBy(queryParams, value => value !== null);
  32. this.$location.url('dashboard/' + dash.uri + '?' + toUrlParams(filteredParams));
  33. this.index++;
  34. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  35. }
  36. prev() {
  37. this.index = Math.max(this.index - 2, 0);
  38. this.next();
  39. }
  40. start(playlistId) {
  41. this.stop();
  42. this.startUrl = window.location.href;
  43. this.index = 0;
  44. this.isPlaying = true;
  45. return this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  46. return this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  47. this.dashboards = dashboards;
  48. this.interval = kbn.interval_to_ms(playlist.interval);
  49. this.next();
  50. });
  51. });
  52. }
  53. stop() {
  54. if (this.isPlaying) {
  55. const queryParams = this.$location.search();
  56. if (queryParams.kiosk) {
  57. appEvents.emit('toggle-kiosk-mode', { exit: true });
  58. }
  59. }
  60. this.index = 0;
  61. this.isPlaying = false;
  62. if (this.cancelPromise) {
  63. this.$timeout.cancel(this.cancelPromise);
  64. }
  65. }
  66. }
  67. coreModule.service('playlistSrv', PlaylistSrv);