playlist_srv.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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(private $rootScope: any, private $location: any, private $timeout: any, private backendSrv: any) { }
  15. next() {
  16. this.$timeout.cancel(this.cancelPromise);
  17. var playedAllDashboards = this.index > this.dashboards.length - 1;
  18. if (playedAllDashboards) {
  19. window.location.href = this.startUrl;
  20. } else {
  21. var dash = this.dashboards[this.index];
  22. this.$location.url('dashboard/' + dash.uri);
  23. this.index++;
  24. this.cancelPromise = this.$timeout(() => this.next(), this.interval);
  25. }
  26. }
  27. prev() {
  28. this.index = Math.max(this.index - 2, 0);
  29. this.next();
  30. }
  31. start(playlistId) {
  32. this.stop();
  33. this.startUrl = window.location.href;
  34. this.index = 0;
  35. this.playlistId = playlistId;
  36. this.isPlaying = true;
  37. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  38. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  39. this.dashboards = dashboards;
  40. this.interval = kbn.interval_to_ms(playlist.interval);
  41. this.next();
  42. });
  43. });
  44. }
  45. stop() {
  46. this.index = 0;
  47. this.isPlaying = false;
  48. this.playlistId = 0;
  49. if (this.cancelPromise) {
  50. this.$timeout.cancel(this.cancelPromise);
  51. }
  52. }
  53. }
  54. coreModule.service('playlistSrv', PlaylistSrv);