playlist_srv.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 playlistId: number;
  11. private startUrl: string;
  12. public isPlaying: boolean;
  13. /** @ngInject */
  14. constructor(
  15. private $location: any,
  16. private $timeout: any,
  17. private backendSrv: any,
  18. private $routeParams: any
  19. ) { }
  20. next() {
  21. this.$timeout.cancel(this.cancelPromise);
  22. var playedAllDashboards = this.index > this.dashboards.length - 1;
  23. if (playedAllDashboards) {
  24. window.location.href = this.getUrlWithKioskMode();
  25. return;
  26. }
  27. var dash = this.dashboards[this.index];
  28. this.$location.url('dashboard/' + dash.uri);
  29. this.index++;
  30. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  31. }
  32. getUrlWithKioskMode() {
  33. const inKioskMode = document.body.classList.contains('page-kiosk-mode');
  34. // check if should add kiosk query param
  35. if (inKioskMode && this.startUrl.indexOf('kiosk') === -1) {
  36. return this.startUrl + '?kiosk=true';
  37. }
  38. // check if should remove kiosk query param
  39. if (!inKioskMode) {
  40. return this.startUrl.split("?")[0];
  41. }
  42. // already has kiosk query param, just return startUrl
  43. return this.startUrl;
  44. }
  45. prev() {
  46. this.index = Math.max(this.index - 2, 0);
  47. this.next();
  48. }
  49. start(playlistId) {
  50. this.stop();
  51. this.startUrl = window.location.href;
  52. this.index = 0;
  53. this.playlistId = playlistId;
  54. this.isPlaying = true;
  55. if (this.$routeParams.kiosk) {
  56. appEvents.emit('toggle-kiosk-mode');
  57. }
  58. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  59. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  60. this.dashboards = dashboards;
  61. this.interval = kbn.interval_to_ms(playlist.interval);
  62. this.next();
  63. });
  64. });
  65. }
  66. stop() {
  67. this.index = 0;
  68. this.isPlaying = false;
  69. this.playlistId = 0;
  70. if (this.cancelPromise) {
  71. this.$timeout.cancel(this.cancelPromise);
  72. }
  73. }
  74. }
  75. coreModule.service('playlistSrv', PlaylistSrv);