From 85e65e61d37ef20be3ef3194cc5fe5950334bb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 7 May 2015 09:35:39 +0200 Subject: [PATCH] More work on dashboards links --- .../app/features/dashboard/dashboardCtrl.js | 3 +- public/app/features/dashboard/dashboardSrv.js | 9 ++- public/app/features/dashlinks/editor.html | 52 ++++++++----- public/app/features/dashlinks/module.js | 74 ++++++++++--------- 4 files changed, 82 insertions(+), 56 deletions(-) diff --git a/public/app/features/dashboard/dashboardCtrl.js b/public/app/features/dashboard/dashboardCtrl.js index 04ef5ae4f2d..ffead046211 100644 --- a/public/app/features/dashboard/dashboardCtrl.js +++ b/public/app/features/dashboard/dashboardCtrl.js @@ -41,6 +41,7 @@ function (angular, $, config) { $rootScope.performance.panelsRendered = 0; var dashboard = dashboardSrv.create(data.dashboard, data.meta); + dashboardSrv.setCurrent(dashboard); // init services timeSrv.init(dashboard); @@ -63,7 +64,7 @@ function (angular, $, config) { $scope.appEvent("dashboard-loaded", $scope.dashboard); }).catch(function(err) { - console.log('Failed to initialize dashboard template variables, error: ', err); + console.log('Failed to initialize dashboard', err); $scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]); }); }; diff --git a/public/app/features/dashboard/dashboardSrv.js b/public/app/features/dashboard/dashboardSrv.js index 303ba32c650..4d79d06675a 100644 --- a/public/app/features/dashboard/dashboardSrv.js +++ b/public/app/features/dashboard/dashboardSrv.js @@ -306,8 +306,13 @@ function (angular, $, kbn, _, moment) { return { create: function(dashboard, meta) { return new DashboardModel(dashboard, meta); - } + }, + setCurrent: function(dashboard) { + this.currentDashboard = dashboard; + }, + getCurrent: function() { + return this.currentDashboard; + }, }; - }); }); diff --git a/public/app/features/dashlinks/editor.html b/public/app/features/dashlinks/editor.html index 8f5570d9fe2..de73ce8d3e9 100644 --- a/public/app/features/dashlinks/editor.html +++ b/public/app/features/dashlinks/editor.html @@ -2,43 +2,61 @@
Links and Dash Navigation
-
+
    -
  • - +
  • +
  • Type
  • - +
  • With tag
  • - +
  • -
  • Url
  • +
  • Url
  • -
  • Title
  • -
  • - +
  • + +
  • -
  • Tooltip
  • -
  • - -
  • - -
  • Icon
  • -
  • - +
  • +
+
+
+
    +
  • + +
  • +
  • Title
  • +
  • + +
  • +
  • Tooltip
  • +
  • + +
  • + +
  • Icon
  • +
  • + +
  • + +
+
+
+
diff --git a/public/app/features/dashlinks/module.js b/public/app/features/dashlinks/module.js index ad7de426587..c446ff2c01a 100644 --- a/public/app/features/dashlinks/module.js +++ b/public/app/features/dashlinks/module.js @@ -7,6 +7,16 @@ function (angular, _) { var module = angular.module('grafana.directives'); + var iconMap = { + "external link": "fa-external-link", + "dashboard": "fa-th-large", + "question": "fa-question", + "info": "fa-info", + "bolt": "fa-bolt", + "doc": "fa-file-text-o", + "cloud": "fa-cloud", + }; + module.directive('dashLinksEditor', function() { return { scope: { @@ -38,7 +48,6 @@ function (angular, _) { link: "=" }, restrict: 'E', - controller: 'DashLinkCtrl', templateUrl: 'app/features/dashlinks/module.html', link: function(scope, elem) { var anchor = elem.find('a'); @@ -52,7 +61,7 @@ function (angular, _) { // tooltip elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' }); - icon.attr('class', scope.link.icon); + icon.attr('class', 'fa fa-fw ' + scope.link.icon); update(); scope.$on('refresh', update); @@ -60,23 +69,33 @@ function (angular, _) { }; }); - module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv) { + module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv, dashboardSrv) { + var currentDashId = dashboardSrv.getCurrent().id; function buildLinks(linkDef) { if (linkDef.type === 'dashboards') { + if (!linkDef.tag) { + console.log('Dashboard link missing tag'); + return $q.when([]); + } + return backendSrv.search({tag: linkDef.tag}).then(function(results) { - return _.map(results.dashboards, function(dash) { - return { - title: dash.title, - url: 'dashboard/db/'+ dash.slug, - icon: 'fa fa-th-large' - }; - }); + return _.reduce(results.dashboards, function(memo, dash) { + // do not add current dashboard + if (dash.id !== currentDashId) { + memo.push({ title: dash.title, url: 'dashboard/db/'+ dash.slug, icon: 'fa fa-th-large' }); + } + return memo; + }, []); }); } if (linkDef.type === 'link') { - return $q.when([{ url: linkDef.url, title: linkDef.title, icon: 'fa fa-external-link', }]); + return $q.when([{ + url: linkDef.url, + title: linkDef.title, + icon: iconMap[linkDef.icon] + }]); } return $q.when([]); @@ -94,40 +113,23 @@ function (angular, _) { $rootScope.onAppEvent('dash-links-updated', updateDashLinks); }); - module.controller("DashLinkCtrl", function($scope) { - - if ($scope.link.type === 'dashboards') { - $scope.searchHits = []; - } - - }); - - module.controller('DashLinkEditorCtrl', function($scope, backendSrv, $rootScope) { + module.controller('DashLinkEditorCtrl', function($scope, $rootScope) { + $scope.iconMap = iconMap; $scope.dashboard.links = $scope.dashboard.links || []; $scope.addLink = function() { - $scope.dashboard.links.push({ - type: 'dashboard', - name: 'Related dashboard' - }); + $scope.dashboard.links.push({ type: 'dashboards', icon: 'external link' }); + }; + + $scope.moveLink = function(index, dir) { + _.move($scope.dashboard.links, index, index+dir); + $scope.updated(); }; $scope.updated = function() { $rootScope.appEvent('dash-links-updated'); }; - $scope.searchDashboards = function(queryStr, callback) { - var query = {query: queryStr}; - - backendSrv.search(query).then(function(result) { - var dashboards = _.map(result.dashboards, function(dash) { - return dash.title; - }); - - callback(dashboards); - }); - }; - $scope.deleteLink = function(link) { $scope.dashboard.links = _.without($scope.dashboard.links, link); };