playlist_srv.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ///<reference path="../../headers/common.d.ts" />
  2. import coreModule from '../../core/core_module';
  3. import kbn from 'app/core/utils/kbn';
  4. import appEvents from 'app/core/app_events';
  5. class PlaylistSrv {
  6. private cancelPromise: any;
  7. private dashboards: any;
  8. private index: number;
  9. private interval: any;
  10. private startUrl: string;
  11. public isPlaying: boolean;
  12. /** @ngInject */
  13. constructor(
  14. private $location: any,
  15. private $timeout: any,
  16. private backendSrv: any,
  17. private $routeParams: any
  18. ) { }
  19. next() {
  20. this.$timeout.cancel(this.cancelPromise);
  21. var playedAllDashboards = this.index > this.dashboards.length - 1;
  22. if (playedAllDashboards) {
  23. window.location.href = this.getUrlWithKioskMode();
  24. return;
  25. }
  26. var dash = this.dashboards[this.index];
  27. this.$location.url('dashboard/' + dash.uri);
  28. this.index++;
  29. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  30. }
  31. getUrlWithKioskMode() {
  32. const inKioskMode = document.body.classList.contains('page-kiosk-mode');
  33. // check if should add kiosk query param
  34. if (inKioskMode && this.startUrl.indexOf('kiosk') === -1) {
  35. return this.startUrl + '?kiosk=true';
  36. }
  37. // check if should remove kiosk query param
  38. if (!inKioskMode) {
  39. return this.startUrl.split("?")[0];
  40. }
  41. // already has kiosk query param, just return startUrl
  42. return this.startUrl;
  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. if (this.$routeParams.kiosk) {
  54. appEvents.emit('toggle-kiosk-mode');
  55. }
  56. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  57. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  58. this.dashboards = dashboards;
  59. this.interval = kbn.interval_to_ms(playlist.interval);
  60. this.next();
  61. });
  62. });
  63. }
  64. stop() {
  65. this.index = 0;
  66. this.isPlaying = false;
  67. if (this.cancelPromise) {
  68. this.$timeout.cancel(this.cancelPromise);
  69. }
  70. }
  71. }
  72. coreModule.service('playlistSrv', PlaylistSrv);