| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- ///<reference path="../../headers/common.d.ts" />
- import _ from 'lodash';
- import kbn from 'app/core/utils/kbn';
- import {Variable, assignModelProperties} from './variable';
- import {VariableSrv, variableConstructorMap} from './variable_srv';
- export class DatasourceVariable implements Variable {
- regex: any;
- query: string;
- options: any;
- defaults = {
- type: 'datasource',
- name: '',
- hide: 0,
- label: '',
- current: {text: '', value: ''}
- regex: '',
- options: [],
- query: '',
- };
- /** @ngInject */
- constructor(private model, private datasourceSrv, private variableSrv) {
- assignModelProperties(this, model, this.defaults);
- }
- getModel() {
- assignModelProperties(this.model, this, this.defaults);
- return this.model;
- }
- setValue(option) {
- this.variableSrv.setOptionAsCurrent(this, option);
- }
- updateOptions() {
- var options = [];
- var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
- var regex;
- if (this.regex) {
- regex = kbn.stringToJsRegex(this.regex);
- }
- for (var i = 0; i < sources.length; i++) {
- var source = sources[i];
- // must match on type
- if (source.meta.id !== this.query) {
- continue;
- }
- if (regex && !regex.exec(source.name)) {
- continue;
- }
- options.push({text: source.name, value: source.name});
- }
- if (options.length === 0) {
- options.push({text: 'No data sources found', value: ''});
- }
- this.options = options;
- }
- dependsOn(variable) {
- return false;
- }
- setValueFromUrl(urlValue) {
- return this.variableSrv.setOptionFromUrl(this, urlValue);
- }
- }
- variableConstructorMap['datasource'] = DatasourceVariable;
|