playlist_srv.ts 2.6 KB

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