|
|
@@ -4,88 +4,51 @@ define([
|
|
|
'kbn',
|
|
|
'store'
|
|
|
],
|
|
|
-function (angular, _, kbn, store) {
|
|
|
+function (angular, _, kbn) {
|
|
|
'use strict';
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
- module.service('playlistSrv', function($location, $rootScope) {
|
|
|
- var timerInstance;
|
|
|
- var favorites = { dashboards: [] };
|
|
|
+ module.service('playlistSrv', function($location, $rootScope, $timeout) {
|
|
|
+ var self = this;
|
|
|
|
|
|
- this.init = function() {
|
|
|
- var existingJson = store.get("grafana-favorites");
|
|
|
- if (existingJson) {
|
|
|
- favorites = angular.fromJson(existingJson);
|
|
|
- }
|
|
|
- };
|
|
|
+ this.next = function() {
|
|
|
+ $timeout.cancel(self.cancelPromise);
|
|
|
|
|
|
- this._save = function() {
|
|
|
- store.set('grafana-favorites', angular.toJson(favorites));
|
|
|
- };
|
|
|
-
|
|
|
- this._find = function(title) {
|
|
|
- return _.findWhere(favorites.dashboards, { title: title });
|
|
|
- };
|
|
|
+ angular.element(window).unbind('resize');
|
|
|
+ var dash = self.dashboards[self.index % self.dashboards.length];
|
|
|
+ var relativeUrl = dash.url.substring($location.absUrl().length - $location.url().length);
|
|
|
|
|
|
- this._remove = function(existing) {
|
|
|
- if (existing) {
|
|
|
- favorites.dashboards = _.without(favorites.dashboards, existing);
|
|
|
- }
|
|
|
- };
|
|
|
+ $location.url(relativeUrl);
|
|
|
+ self.index++;
|
|
|
|
|
|
- this.isCurrentFavorite = function(dashboard) {
|
|
|
- return this._find(dashboard.title) ? true : false;
|
|
|
+ self.cancelPromise = $timeout(self.next, self.interval);
|
|
|
};
|
|
|
|
|
|
- this.markAsFavorite = function(dashboard) {
|
|
|
- var existing = this._find(dashboard.title);
|
|
|
- this._remove(existing);
|
|
|
-
|
|
|
- favorites.dashboards.push({
|
|
|
- url: $location.path(),
|
|
|
- title: dashboard.title
|
|
|
- });
|
|
|
-
|
|
|
- this._save();
|
|
|
- };
|
|
|
-
|
|
|
- this.removeAsFavorite = function(toRemove) {
|
|
|
- var existing = this._find(toRemove.title);
|
|
|
- this._remove(existing);
|
|
|
- this._save();
|
|
|
- };
|
|
|
-
|
|
|
- this.getFavorites = function() {
|
|
|
- return favorites;
|
|
|
+ this.prev = function() {
|
|
|
+ self.index = Math.max(self.index - 2, 0);
|
|
|
+ self.next();
|
|
|
};
|
|
|
|
|
|
this.start = function(dashboards, timespan) {
|
|
|
this.stop();
|
|
|
|
|
|
- var interval = kbn.interval_to_ms(timespan);
|
|
|
- var index = 0;
|
|
|
-
|
|
|
- $rootScope.playlist_active = true;
|
|
|
-
|
|
|
- timerInstance = setInterval(function() {
|
|
|
- $rootScope.$apply(function() {
|
|
|
- angular.element(window).unbind('resize');
|
|
|
- var dash = dashboards[index % dashboards.length];
|
|
|
- var relativeUrl = dash.url.substring($location.absUrl().length - $location.url().length);
|
|
|
- $location.url(relativeUrl);
|
|
|
+ self.interval = kbn.interval_to_ms(timespan);
|
|
|
+ self.dashboards = dashboards;
|
|
|
+ $rootScope.playlistSrv = this;
|
|
|
|
|
|
- index++;
|
|
|
- });
|
|
|
- }, interval);
|
|
|
+ self.cancelPromise = $timeout(self.next, self.interval);
|
|
|
};
|
|
|
|
|
|
this.stop = function() {
|
|
|
- clearInterval(timerInstance);
|
|
|
- $rootScope.playlist_active = false;
|
|
|
- };
|
|
|
+ self.index = 0;
|
|
|
+
|
|
|
+ if (self.cancelPromise) {
|
|
|
+ $timeout.cancel(self.cancelPromise);
|
|
|
+ }
|
|
|
|
|
|
- this.init();
|
|
|
+ $rootScope.playlistSrv = null;
|
|
|
+ };
|
|
|
|
|
|
});
|
|
|
|