module.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import './link_srv';
  4. import { BackendSrv } from 'app/core/services/backend_srv';
  5. function panelLinksEditor() {
  6. return {
  7. scope: {
  8. panel: '=',
  9. },
  10. restrict: 'E',
  11. controller: 'PanelLinksEditorCtrl',
  12. templateUrl: 'public/app/features/panel/panellinks/module.html',
  13. link: () => {},
  14. };
  15. }
  16. export class PanelLinksEditorCtrl {
  17. /** @ngInject */
  18. constructor($scope: any, backendSrv: BackendSrv) {
  19. $scope.panel.links = $scope.panel.links || [];
  20. $scope.addLink = () => {
  21. $scope.panel.links.push({
  22. type: 'dashboard',
  23. });
  24. };
  25. $scope.searchDashboards = (queryStr: string, callback: Function) => {
  26. backendSrv.search({ query: queryStr }).then(hits => {
  27. const dashboards = _.map(hits, dash => {
  28. return dash.title;
  29. });
  30. callback(dashboards);
  31. });
  32. };
  33. $scope.dashboardChanged = (link: any) => {
  34. backendSrv.search({ query: link.dashboard }).then(hits => {
  35. const dashboard: any = _.find(hits, { title: link.dashboard });
  36. if (dashboard) {
  37. if (dashboard.url) {
  38. link.url = dashboard.url;
  39. } else {
  40. // To support legacy url's
  41. link.dashUri = dashboard.uri;
  42. }
  43. link.title = dashboard.title;
  44. }
  45. });
  46. };
  47. $scope.deleteLink = (link: any) => {
  48. $scope.panel.links = _.without($scope.panel.links, link);
  49. };
  50. }
  51. }
  52. angular
  53. .module('grafana.directives')
  54. .directive('panelLinksEditor', panelLinksEditor)
  55. .controller('PanelLinksEditorCtrl', PanelLinksEditorCtrl);