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. /** @ngInject */
  13. constructor(private $rootScope: any, private $location: any, private $timeout: any, private backendSrv: any) { }
  14. next() {
  15. this.$timeout.cancel(this.cancelPromise);
  16. var playedAllDashboards = this.index > this.dashboards.length - 1;
  17. if (playedAllDashboards) {
  18. window.location.href = this.startUrl;
  19. } else {
  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. }
  26. prev() {
  27. this.index = Math.max(this.index - 2, 0);
  28. this.next();
  29. }
  30. start(playlistId) {
  31. this.stop();
  32. this.startUrl = window.location.href;
  33. this.index = 0;
  34. this.playlistId = playlistId;
  35. this.$rootScope.playlistSrv = this;
  36. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  37. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  38. this.dashboards = dashboards;
  39. this.interval = kbn.interval_to_ms(playlist.interval);
  40. this.next();
  41. });
  42. });
  43. }
  44. stop() {
  45. this.index = 0;
  46. this.playlistId = 0;
  47. if (this.cancelPromise) {
  48. this.$timeout.cancel(this.cancelPromise);
  49. }
  50. this.$rootScope.playlistSrv = null;
  51. }
  52. }
  53. coreModule.service('playlistSrv', PlaylistSrv);