playlist_srv.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import config from 'app/core/config';
  4. import coreModule from '../../core/core_module';
  5. import kbn from 'app/core/utils/kbn';
  6. class PlaylistSrv {
  7. private cancelPromise: any;
  8. private dashboards: any;
  9. private index: number;
  10. private interval: any;
  11. private playlistId: number;
  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 = `${config.appSubUrl}/playlists/play/${this.playlistId}`;
  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.index = 0;
  33. this.playlistId = playlistId;
  34. this.$rootScope.playlistSrv = this;
  35. this.backendSrv.get(`/api/playlists/${playlistId}`).then(playlist => {
  36. this.backendSrv.get(`/api/playlists/${playlistId}/dashboards`).then(dashboards => {
  37. this.dashboards = dashboards;
  38. this.interval = kbn.interval_to_ms(playlist.interval);
  39. this.next();
  40. });
  41. });
  42. }
  43. stop() {
  44. this.index = 0;
  45. this.playlistId = 0;
  46. if (this.cancelPromise) {
  47. this.$timeout.cancel(this.cancelPromise);
  48. }
  49. this.$rootScope.playlistSrv = null;
  50. }
  51. }
  52. coreModule.service('playlistSrv', PlaylistSrv);