2014-03-07 12:24:45 -06:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2014-03-07 12:24:45 -06:00
|
|
|
'kbn'
|
|
|
|
],
|
|
|
|
function (angular, _, kbn) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.services');
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-06-10 14:32:38 -05:00
|
|
|
module.service('playlistSrv', function($location, $rootScope) {
|
2014-03-08 09:27:01 -06:00
|
|
|
var timerInstance;
|
|
|
|
var favorites = { dashboards: [] };
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
this.init = function() {
|
|
|
|
var existingJson = window.localStorage["grafana-favorites"];
|
|
|
|
if (existingJson) {
|
|
|
|
favorites = angular.fromJson(existingJson);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this._save = function() {
|
|
|
|
window.localStorage["grafana-favorites"] = angular.toJson(favorites);
|
|
|
|
};
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
this._find = function(title) {
|
|
|
|
return _.findWhere(favorites.dashboards, { title: title });
|
|
|
|
};
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
this._remove = function(existing) {
|
2014-03-07 12:24:45 -06:00
|
|
|
if (existing) {
|
2014-03-08 09:27:01 -06:00
|
|
|
favorites.dashboards = _.without(favorites.dashboards, existing);
|
2014-03-07 12:24:45 -06:00
|
|
|
}
|
2014-03-08 09:27:01 -06:00
|
|
|
};
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-06-10 14:32:38 -05:00
|
|
|
this.isCurrentFavorite = function(dashboard) {
|
|
|
|
return this._find(dashboard.title) ? true : false;
|
2014-03-07 12:24:45 -06:00
|
|
|
};
|
|
|
|
|
2014-06-10 14:32:38 -05:00
|
|
|
this.markAsFavorite = function(dashboard) {
|
|
|
|
var existing = this._find(dashboard.title);
|
2014-03-08 09:27:01 -06:00
|
|
|
this._remove(existing);
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
favorites.dashboards.push({
|
|
|
|
url: $location.path(),
|
2014-06-10 14:32:38 -05:00
|
|
|
title: dashboard.title
|
2014-03-08 09:27:01 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
this._save();
|
|
|
|
};
|
2014-03-07 12:24:45 -06:00
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
this.removeAsFavorite = function(toRemove) {
|
|
|
|
var existing = this._find(toRemove.title);
|
|
|
|
this._remove(existing);
|
|
|
|
this._save();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getFavorites = function() {
|
2014-03-07 12:24:45 -06:00
|
|
|
return favorites;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.start = function(dashboards, timespan) {
|
|
|
|
var interval = kbn.interval_to_ms(timespan);
|
|
|
|
var index = 0;
|
|
|
|
|
|
|
|
$rootScope.playlist_active = true;
|
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
timerInstance = setInterval(function() {
|
2014-03-07 12:24:45 -06:00
|
|
|
$rootScope.$apply(function() {
|
2014-04-16 08:52:29 -05:00
|
|
|
angular.element(window).unbind('resize');
|
2014-08-13 10:20:54 -05:00
|
|
|
$location.search({});
|
2014-03-07 12:24:45 -06:00
|
|
|
$location.path(dashboards[index % dashboards.length].url);
|
|
|
|
index++;
|
|
|
|
});
|
|
|
|
}, interval);
|
|
|
|
};
|
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
this.stop = function() {
|
|
|
|
clearInterval(timerInstance);
|
|
|
|
$rootScope.playlist_active = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.init();
|
|
|
|
|
2014-03-07 12:24:45 -06:00
|
|
|
});
|
|
|
|
|
2014-04-16 08:52:29 -05:00
|
|
|
});
|