playlist_srv.ts 3.3 KB

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