playlistSrv.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. 'store'
  6. ],
  7. function (angular, _, kbn) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.service('playlistSrv', function($location, $rootScope, $timeout) {
  11. var self = this;
  12. this.next = function() {
  13. $timeout.cancel(self.cancelPromise);
  14. angular.element(window).unbind('resize');
  15. var dash = self.dashboards[self.index % self.dashboards.length];
  16. $location.url('dashboard/db/' + dash.slug);
  17. self.index++;
  18. self.cancelPromise = $timeout(self.next, self.interval);
  19. };
  20. this.prev = function() {
  21. self.index = Math.max(self.index - 2, 0);
  22. self.next();
  23. };
  24. this.start = function(dashboards, timespan) {
  25. self.stop();
  26. self.index = 0;
  27. self.interval = kbn.interval_to_ms(timespan);
  28. self.dashboards = dashboards;
  29. $rootScope.playlistSrv = this;
  30. self.cancelPromise = $timeout(self.next, self.interval);
  31. self.next();
  32. };
  33. this.stop = function() {
  34. self.index = 0;
  35. if (self.cancelPromise) {
  36. $timeout.cancel(self.cancelPromise);
  37. }
  38. $rootScope.playlistSrv = null;
  39. };
  40. });
  41. });