playlist_srv.ts 2.3 KB

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