playlist_srv.ts 2.1 KB

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