2014-11-11 04:48:27 -06:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
],
|
|
|
|
function (angular, _) {
|
|
|
|
'use strict';
|
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
var module = angular.module('grafana.directives');
|
|
|
|
|
2015-05-07 02:35:39 -05:00
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
module.directive('dashLinksEditor', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
controller: 'DashLinkEditorCtrl',
|
|
|
|
templateUrl: 'app/features/dashlinks/editor.html',
|
|
|
|
link: function() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2015-05-06 03:57:32 -05:00
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
module.directive('dashLinksContainer', function() {
|
|
|
|
return {
|
|
|
|
scope: {
|
|
|
|
links: "="
|
|
|
|
},
|
|
|
|
restrict: 'E',
|
|
|
|
controller: 'DashLinksContainerCtrl',
|
|
|
|
template: '<dash-link ng-repeat="link in generatedLinks" link="link"></dash-link>',
|
|
|
|
link: function() { }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-05-07 12:14:13 -05:00
|
|
|
module.directive('dashLink', function($compile, linkSrv) {
|
2015-05-06 09:40:43 -05:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
link: function(scope, elem) {
|
2015-05-07 12:14:13 -05:00
|
|
|
var link = scope.link;
|
|
|
|
var template = '<div class="submenu-item dropdown">' +
|
|
|
|
'<a class="pointer dash-nav-link" data-placement="bottom"' +
|
|
|
|
(link.asDropdown ? ' ng-click="fillDropdown(link)" data-toggle="dropdown"' : "") + '>' +
|
|
|
|
'<i></i> <span></span></a>';
|
|
|
|
|
|
|
|
if (link.asDropdown) {
|
|
|
|
template += '<ul class="dropdown-menu" role="menu">' +
|
|
|
|
'<li ng-repeat="dash in link.searchHits"><a href="{{dash.url}}"><i class="fa fa-th-large"></i> {{dash.title}}</a></li>' +
|
|
|
|
'</ul';
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.html(template);
|
|
|
|
$compile(elem.contents())(scope);
|
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
var anchor = elem.find('a');
|
|
|
|
var icon = elem.find('i');
|
|
|
|
var span = elem.find('span');
|
|
|
|
|
|
|
|
function update() {
|
2015-05-07 12:14:13 -05:00
|
|
|
var linkInfo = linkSrv.getAnchorInfo(link);
|
|
|
|
span.text(linkInfo.title);
|
|
|
|
anchor.attr("href", linkInfo.href);
|
2015-05-05 12:56:49 -05:00
|
|
|
}
|
2014-11-11 04:48:27 -06:00
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
// tooltip
|
|
|
|
elem.find('a').tooltip({ title: scope.link.tooltip, html: true, container: 'body' });
|
2015-05-07 02:35:39 -05:00
|
|
|
icon.attr('class', 'fa fa-fw ' + scope.link.icon);
|
2015-05-07 06:10:04 -05:00
|
|
|
anchor.attr('target', scope.link.target);
|
2015-05-06 03:57:32 -05:00
|
|
|
|
2015-05-08 03:56:54 -05:00
|
|
|
// fix for menus on the far right
|
|
|
|
if (link.asDropdown && scope.$last) {
|
|
|
|
elem.find('.dropdown-menu').addClass('pull-right');
|
|
|
|
}
|
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
update();
|
|
|
|
scope.$on('refresh', update);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2014-11-11 06:38:27 -06:00
|
|
|
|
2015-05-07 12:14:13 -05:00
|
|
|
module.controller("DashLinksContainerCtrl", function($scope, $rootScope, $q, backendSrv, dashboardSrv, linkSrv) {
|
2015-05-07 02:35:39 -05:00
|
|
|
var currentDashId = dashboardSrv.getCurrent().id;
|
2015-05-06 09:40:43 -05:00
|
|
|
|
|
|
|
function buildLinks(linkDef) {
|
|
|
|
if (linkDef.type === 'dashboards') {
|
2015-06-02 03:35:10 -05:00
|
|
|
if (!linkDef.tags) {
|
2015-05-07 02:35:39 -05:00
|
|
|
console.log('Dashboard link missing tag');
|
|
|
|
return $q.when([]);
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:14:13 -05:00
|
|
|
if (linkDef.asDropdown) {
|
|
|
|
return $q.when([{
|
|
|
|
title: linkDef.title,
|
2015-06-02 03:35:10 -05:00
|
|
|
tags: linkDef.tags,
|
2015-05-08 03:56:54 -05:00
|
|
|
keepTime: linkDef.keepTime,
|
|
|
|
includeVars: linkDef.includeVars,
|
2015-05-07 12:14:13 -05:00
|
|
|
icon: "fa fa-bars",
|
|
|
|
asDropdown: true
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:44:42 -05:00
|
|
|
return $scope.searchDashboards(linkDef, 7);
|
2015-05-06 09:40:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (linkDef.type === 'link') {
|
2015-05-07 02:35:39 -05:00
|
|
|
return $q.when([{
|
|
|
|
url: linkDef.url,
|
|
|
|
title: linkDef.title,
|
2015-05-07 06:10:04 -05:00
|
|
|
icon: iconMap[linkDef.icon],
|
|
|
|
tooltip: linkDef.tooltip,
|
|
|
|
target: linkDef.targetBlank ? "_blank" : "",
|
2015-05-08 03:56:54 -05:00
|
|
|
keepTime: linkDef.keepTime,
|
|
|
|
includeVars: linkDef.includeVars,
|
2015-05-07 02:35:39 -05:00
|
|
|
}]);
|
2015-05-06 09:40:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $q.when([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDashLinks() {
|
|
|
|
var promises = _.map($scope.links, buildLinks);
|
2014-11-11 06:38:27 -06:00
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
$q.all(promises).then(function(results) {
|
|
|
|
$scope.generatedLinks = _.flatten(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:44:42 -05:00
|
|
|
$scope.searchDashboards = function(link, limit) {
|
|
|
|
return backendSrv.search({tag: link.tags, limit: limit}).then(function(results) {
|
2015-05-13 06:36:13 -05:00
|
|
|
return _.reduce(results, function(memo, dash) {
|
2015-05-07 12:14:13 -05:00
|
|
|
// do not add current dashboard
|
|
|
|
if (dash.id !== currentDashId) {
|
2015-05-08 03:56:54 -05:00
|
|
|
memo.push({
|
|
|
|
title: dash.title,
|
2015-05-13 06:36:13 -05:00
|
|
|
url: 'dashboard/' + dash.uri,
|
2015-05-08 03:56:54 -05:00
|
|
|
icon: 'fa fa-th-large',
|
|
|
|
keepTime: link.keepTime,
|
|
|
|
includeVars: link.includeVars
|
|
|
|
});
|
2015-05-07 12:14:13 -05:00
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, []);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.fillDropdown = function(link) {
|
2015-06-22 10:44:42 -05:00
|
|
|
$scope.searchDashboards(link, 100).then(function(results) {
|
2015-05-07 12:14:13 -05:00
|
|
|
_.each(results, function(hit) {
|
|
|
|
hit.url = linkSrv.getLinkUrl(hit);
|
|
|
|
});
|
|
|
|
link.searchHits = results;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
updateDashLinks();
|
|
|
|
$rootScope.onAppEvent('dash-links-updated', updateDashLinks);
|
|
|
|
});
|
|
|
|
|
2015-05-07 02:35:39 -05:00
|
|
|
module.controller('DashLinkEditorCtrl', function($scope, $rootScope) {
|
2015-05-06 09:40:43 -05:00
|
|
|
|
2015-05-07 02:35:39 -05:00
|
|
|
$scope.iconMap = iconMap;
|
2015-05-06 09:40:43 -05:00
|
|
|
$scope.dashboard.links = $scope.dashboard.links || [];
|
2015-05-08 03:56:54 -05:00
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
$scope.addLink = function() {
|
2015-05-07 02:35:39 -05:00
|
|
|
$scope.dashboard.links.push({ type: 'dashboards', icon: 'external link' });
|
2015-05-08 03:56:54 -05:00
|
|
|
$scope.updateSubmenuVisibility();
|
|
|
|
$scope.updated();
|
2015-05-06 09:40:43 -05:00
|
|
|
};
|
|
|
|
|
2015-05-07 02:35:39 -05:00
|
|
|
$scope.moveLink = function(index, dir) {
|
|
|
|
_.move($scope.dashboard.links, index, index+dir);
|
|
|
|
$scope.updated();
|
2015-05-06 09:40:43 -05:00
|
|
|
};
|
|
|
|
|
2015-05-07 02:35:39 -05:00
|
|
|
$scope.updated = function() {
|
|
|
|
$rootScope.appEvent('dash-links-updated');
|
2015-05-06 09:40:43 -05:00
|
|
|
};
|
|
|
|
|
2015-05-08 03:56:54 -05:00
|
|
|
$scope.deleteLink = function(index) {
|
|
|
|
$scope.dashboard.links.splice(index, 1);
|
|
|
|
$scope.updateSubmenuVisibility();
|
|
|
|
$scope.updated();
|
2015-05-06 09:40:43 -05:00
|
|
|
};
|
2014-11-11 04:48:27 -06:00
|
|
|
|
2015-05-06 09:40:43 -05:00
|
|
|
});
|
2014-11-11 04:48:27 -06:00
|
|
|
});
|