playlistSrv.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn'
  5. ],
  6. function (angular, _, kbn) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.service('playlistSrv', function($location, $rootScope) {
  10. var timerInstance;
  11. var favorites = { dashboards: [] };
  12. this.init = function() {
  13. var existingJson = window.localStorage["grafana-favorites"];
  14. if (existingJson) {
  15. favorites = angular.fromJson(existingJson);
  16. }
  17. };
  18. this._save = function() {
  19. window.localStorage["grafana-favorites"] = angular.toJson(favorites);
  20. };
  21. this._find = function(title) {
  22. return _.findWhere(favorites.dashboards, { title: title });
  23. };
  24. this._remove = function(existing) {
  25. if (existing) {
  26. favorites.dashboards = _.without(favorites.dashboards, existing);
  27. }
  28. };
  29. this.isCurrentFavorite = function(dashboard) {
  30. return this._find(dashboard.title) ? true : false;
  31. };
  32. this.markAsFavorite = function(dashboard) {
  33. var existing = this._find(dashboard.title);
  34. this._remove(existing);
  35. favorites.dashboards.push({
  36. url: $location.path(),
  37. title: dashboard.title
  38. });
  39. this._save();
  40. };
  41. this.removeAsFavorite = function(toRemove) {
  42. var existing = this._find(toRemove.title);
  43. this._remove(existing);
  44. this._save();
  45. };
  46. this.getFavorites = function() {
  47. return favorites;
  48. };
  49. this.start = function(dashboards, timespan) {
  50. var interval = kbn.interval_to_ms(timespan);
  51. var index = 0;
  52. $rootScope.playlist_active = true;
  53. timerInstance = setInterval(function() {
  54. $rootScope.$apply(function() {
  55. angular.element(window).unbind('resize');
  56. $location.path(dashboards[index % dashboards.length].url);
  57. index++;
  58. });
  59. }, interval);
  60. };
  61. this.stop = function() {
  62. clearInterval(timerInstance);
  63. $rootScope.playlist_active = false;
  64. };
  65. this.init();
  66. });
  67. });