From 1b59fb5be9ae776b03c949dd9095191fe82ebca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 14 Mar 2015 16:13:25 -0400 Subject: [PATCH] POC for repeating panels based on template variable options --- src/app/features/dashboard/all.js | 1 + src/app/features/dashboard/dashboardCtrl.js | 3 ++ src/app/features/dashboard/dashboardSrv.js | 1 + .../features/dashboard/dynamicDashboardSrv.js | 48 +++++++++++++++++++ src/app/features/panel/panelSrv.js | 1 + src/app/partials/panelgeneral.html | 7 +++ 6 files changed, 61 insertions(+) create mode 100644 src/app/features/dashboard/dynamicDashboardSrv.js diff --git a/src/app/features/dashboard/all.js b/src/app/features/dashboard/all.js index e9edfe7fcdd..811f48cc464 100644 --- a/src/app/features/dashboard/all.js +++ b/src/app/features/dashboard/all.js @@ -14,5 +14,6 @@ define([ './unsavedChangesSrv', './directives/dashSearchView', './graphiteImportCtrl', + './dynamicDashboardSrv', './importCtrl', ], function () {}); diff --git a/src/app/features/dashboard/dashboardCtrl.js b/src/app/features/dashboard/dashboardCtrl.js index 8430ac18631..c194dcdb7ed 100644 --- a/src/app/features/dashboard/dashboardCtrl.js +++ b/src/app/features/dashboard/dashboardCtrl.js @@ -15,6 +15,7 @@ function (angular, $, config) { dashboardKeybindings, timeSrv, templateValuesSrv, + dynamicDashboardSrv, dashboardSrv, dashboardViewStateSrv, $timeout) { @@ -44,6 +45,8 @@ function (angular, $, config) { // template values service needs to initialize completely before // the rest of the dashboard can load templateValuesSrv.init(dashboard).then(function() { + dynamicDashboardSrv.init(dashboard); + $scope.dashboard = dashboard; $scope.dashboardViewState = dashboardViewStateSrv.create($scope); $scope.dashboardMeta = data.meta; diff --git a/src/app/features/dashboard/dashboardSrv.js b/src/app/features/dashboard/dashboardSrv.js index 90ec885ed41..0eab4f867d9 100644 --- a/src/app/features/dashboard/dashboardSrv.js +++ b/src/app/features/dashboard/dashboardSrv.js @@ -126,6 +126,7 @@ function (angular, $, kbn, _, moment) { var currentRow = this.rows[rowIndex]; currentRow.panels.push(newPanel); + return newPanel; }; p.formatDate = function(date, format) { diff --git a/src/app/features/dashboard/dynamicDashboardSrv.js b/src/app/features/dashboard/dynamicDashboardSrv.js new file mode 100644 index 00000000000..0c54b61419a --- /dev/null +++ b/src/app/features/dashboard/dynamicDashboardSrv.js @@ -0,0 +1,48 @@ +define([ + 'angular', + 'lodash', +], +function (angular, _) { + 'use strict'; + + var module = angular.module('grafana.services'); + + module.service('dynamicDashboardSrv', function() { + + this.init = function(dashboard) { + this.handlePanelRepeats(dashboard); + }; + + this.handlePanelRepeats = function(dashboard) { + var i, j, row, panel; + for (i = 0; i < dashboard.rows.length; i++) { + row = dashboard.rows[i]; + for (j = 0; j < row.panels.length; j++) { + panel = row.panels[j]; + if (panel.repeat) { + this.repeatPanel(panel, row, dashboard); + } + } + } + }; + + this.repeatPanel = function(panel, row, dashboard) { + var variables = dashboard.templating.list; + var variable = _.findWhere(variables, {name: panel.repeat.replace('$', '')}); + if (!variable) { + return; + } + + _.each(variable.options, function(option) { + var copy = dashboard.duplicatePanel(panel, row); + copy.repeat = null; + console.log('duplicatePanel'); + }); + }; + + + }); + +}); + + diff --git a/src/app/features/panel/panelSrv.js b/src/app/features/panel/panelSrv.js index ec3373c3426..4ffebe67f18 100644 --- a/src/app/features/panel/panelSrv.js +++ b/src/app/features/panel/panelSrv.js @@ -11,6 +11,7 @@ function (angular, _, config) { module.service('panelSrv', function($rootScope, $timeout, datasourceSrv, $q) { this.init = function($scope) { + if (!$scope.panel.span) { $scope.panel.span = 12; } $scope.inspector = {}; diff --git a/src/app/partials/panelgeneral.html b/src/app/partials/panelgeneral.html index 21fbf68c68f..75cb2517192 100644 --- a/src/app/partials/panelgeneral.html +++ b/src/app/partials/panelgeneral.html @@ -10,6 +10,13 @@
+ +
+
Templating options
+
+ + +