playlist_srv.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Libraries
  2. import _ from 'lodash';
  3. // Utils
  4. import { toUrlParams } from 'app/core/utils/url';
  5. import coreModule from '../../core/core_module';
  6. import appEvents from 'app/core/app_events';
  7. import locationUtil from 'app/core/utils/location_util';
  8. import kbn from 'app/core/utils/kbn';
  9. import { store } from 'app/store/store';
  10. export const queryParamsToPreserve: { [key: string]: boolean } = {
  11. kiosk: true,
  12. autofitpanels: true,
  13. orgId: true,
  14. };
  15. export class PlaylistSrv {
  16. private cancelPromise: any;
  17. private dashboards: Array<{ url: string }>;
  18. private index: number;
  19. private interval: number;
  20. private startUrl: string;
  21. private numberOfLoops = 0;
  22. private storeUnsub: () => void;
  23. private validPlaylistUrl: string;
  24. isPlaying: boolean;
  25. /** @ngInject */
  26. constructor(private $location: any, private $timeout: any, private backendSrv: any) {}
  27. next() {
  28. this.$timeout.cancel(this.cancelPromise);
  29. const playedAllDashboards = this.index > this.dashboards.length - 1;
  30. if (playedAllDashboards) {
  31. this.numberOfLoops++;
  32. // This does full reload of the playlist to keep memory in check due to existing leaks but at the same time
  33. // we do not want page to flicker after each full loop.
  34. if (this.numberOfLoops >= 3) {
  35. window.location.href = this.startUrl;
  36. return;
  37. }
  38. this.index = 0;
  39. }
  40. const dash = this.dashboards[this.index];
  41. const queryParams = this.$location.search();
  42. const filteredParams = _.pickBy(queryParams, (value: any, key: string) => queryParamsToPreserve[key]);
  43. const nextDashboardUrl = locationUtil.stripBaseFromUrl(dash.url);
  44. // this is done inside timeout to make sure digest happens after
  45. // as this can be called from react
  46. this.$timeout(() => {
  47. this.$location.url(nextDashboardUrl + '?' + toUrlParams(filteredParams));
  48. });
  49. this.index++;
  50. this.validPlaylistUrl = nextDashboardUrl;
  51. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  52. }
  53. prev() {
  54. this.index = Math.max(this.index - 2, 0);
  55. this.next();
  56. }
  57. // Detect url changes not caused by playlist srv and stop playlist
  58. storeUpdated() {
  59. const state = store.getState();
  60. if (state.location.path !== this.validPlaylistUrl) {
  61. this.stop();
  62. }
  63. }
  64. start(playlistId) {
  65. this.stop();
  66. this.startUrl = window.location.href;
  67. this.index = 0;
  68. this.isPlaying = true;
  69. // setup location tracking
  70. this.storeUnsub = store.subscribe(() => this.storeUpdated());
  71. this.validPlaylistUrl = this.$location.path();
  72. appEvents.emit('playlist-started');
  73. return this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  74. return this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  75. this.dashboards = dashboards;
  76. this.interval = kbn.interval_to_ms(playlist.interval);
  77. this.next();
  78. });
  79. });
  80. }
  81. stop() {
  82. if (this.isPlaying) {
  83. const queryParams = this.$location.search();
  84. if (queryParams.kiosk) {
  85. appEvents.emit('toggle-kiosk-mode', { exit: true });
  86. }
  87. }
  88. this.index = 0;
  89. this.isPlaying = false;
  90. if (this.storeUnsub) {
  91. this.storeUnsub();
  92. }
  93. if (this.cancelPromise) {
  94. this.$timeout.cancel(this.cancelPromise);
  95. }
  96. appEvents.emit('playlist-stopped');
  97. }
  98. }
  99. coreModule.service('playlistSrv', PlaylistSrv);