metrics_ds_selector.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. var module = angular.module('grafana.directives');
  5. var template = `
  6. <div class="gf-form-group">
  7. <div class="gf-form-inline">
  8. <div class="gf-form">
  9. <label class="gf-form-label">
  10. <i class="icon-gf icon-gf-datasource"></i>
  11. </label>
  12. <label class="gf-form-label">
  13. Panel data source
  14. </label>
  15. <metric-segment segment="ctrl.dsSegment" style-mode="select"
  16. get-options="ctrl.getOptions()"
  17. on-change="ctrl.datasourceChanged()"></metric-segment>
  18. </div>
  19. <div class="gf-form gf-form--offset-1">
  20. <button class="btn btn-inverse gf-form-btn" ng-click="ctrl.addDataQuery()" ng-hide="ctrl.current.meta.mixed">
  21. <i class="fa fa-plus"></i>&nbsp;
  22. Add query
  23. </button>
  24. <div class="dropdown" ng-if="ctrl.current.meta.mixed">
  25. <button class="btn btn-inverse dropdown-toggle gf-form-btn" data-toggle="dropdown">
  26. Add Query&nbsp;<span class="fa fa-caret-down"></span>
  27. </button>
  28. <ul class="dropdown-menu" role="menu">
  29. <li ng-repeat="datasource in ctrl.datasources" role="menuitem" ng-hide="datasource.meta.builtIn">
  30. <a ng-click="ctrl.addDataQuery(datasource);">{{datasource.name}}</a>
  31. </li>
  32. </ul>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. `;
  38. export class MetricsDsSelectorCtrl {
  39. dsSegment: any;
  40. dsName: string;
  41. panelCtrl: any;
  42. datasources: any[];
  43. current: any;
  44. /** @ngInject */
  45. constructor(private uiSegmentSrv, datasourceSrv) {
  46. this.datasources = datasourceSrv.getMetricSources();
  47. var dsValue = this.panelCtrl.panel.datasource || null;
  48. for (let ds of this.datasources) {
  49. if (ds.value === dsValue) {
  50. this.current = ds;
  51. }
  52. }
  53. if (!this.current) {
  54. this.current = {name: dsValue + ' not found', value: null};
  55. }
  56. this.dsSegment = uiSegmentSrv.newSegment(this.current.name);
  57. }
  58. getOptions() {
  59. return Promise.resolve(this.datasources.map(value => {
  60. return this.uiSegmentSrv.newSegment(value.name);
  61. }));
  62. }
  63. datasourceChanged() {
  64. var ds = _.findWhere(this.datasources, {name: this.dsSegment.value});
  65. if (ds) {
  66. this.current = ds;
  67. this.panelCtrl.setDatasource(ds);
  68. }
  69. }
  70. addDataQuery(datasource) {
  71. var target: any = {isNew: true};
  72. if (datasource) {
  73. target.datasource = datasource.name;
  74. }
  75. this.panelCtrl.panel.targets.push(target);
  76. }
  77. }
  78. module.directive('metricsDsSelector', function() {
  79. return {
  80. restrict: 'E',
  81. template: template,
  82. controller: MetricsDsSelectorCtrl,
  83. bindToController: true,
  84. controllerAs: 'ctrl',
  85. transclude: true,
  86. scope: {
  87. panelCtrl: "="
  88. }
  89. };
  90. });