grafana/public/app/core/directives/metric_segment.ts

266 lines
7.5 KiB
TypeScript
Raw Normal View History

2018-04-05 10:10:32 -05:00
import _ from 'lodash';
import $ from 'jquery';
import coreModule from '../core_module';
2018-04-10 02:24:54 -05:00
/** @ngInject */
2018-10-18 08:04:54 -05:00
export function metricSegment($compile, $sce, templateSrv) {
const inputTemplate =
2018-04-05 10:10:32 -05:00
'<input type="text" data-provide="typeahead" ' +
' class="gf-form-input input-medium"' +
' spellcheck="false" style="display:none"></input>';
const linkTemplate =
2018-04-05 10:10:32 -05:00
'<a class="gf-form-label" ng-class="segment.cssClass" ' +
'tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>';
const selectTemplate =
2018-04-05 10:10:32 -05:00
'<a class="gf-form-input gf-form-input--dropdown" ng-class="segment.cssClass" ' +
'tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>';
return {
scope: {
segment: '=',
getOptions: '&',
onChange: '&',
debounce: '@',
},
link: ($scope, elem) => {
const $input = $(inputTemplate);
const segment = $scope.segment;
const $button = $(segment.selectMode ? selectTemplate : linkTemplate);
2018-04-05 10:10:32 -05:00
let options = null;
let cancelBlur = null;
let linkMode = true;
const debounceLookup = $scope.debounce;
2018-04-05 10:10:32 -05:00
$input.appendTo(elem);
$button.appendTo(elem);
$scope.updateVariableValue = value => {
2018-04-05 10:10:32 -05:00
if (value === '' || segment.value === value) {
return;
}
$scope.$apply(() => {
const selected = _.find($scope.altSegments, { value: value });
2018-04-05 10:10:32 -05:00
if (selected) {
segment.value = selected.value;
2018-10-18 08:04:54 -05:00
segment.html = selected.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(selected.value));
2018-04-05 10:10:32 -05:00
segment.fake = false;
segment.expandable = selected.expandable;
if (selected.type) {
segment.type = selected.type;
}
} else if (segment.custom !== 'false') {
segment.value = value;
2018-10-18 08:04:54 -05:00
segment.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(value));
2018-04-05 10:10:32 -05:00
segment.expandable = true;
segment.fake = false;
}
$scope.onChange();
});
};
$scope.switchToLink = fromClick => {
2018-04-05 10:10:32 -05:00
if (linkMode && !fromClick) {
return;
}
clearTimeout(cancelBlur);
cancelBlur = null;
linkMode = true;
$input.hide();
$button.show();
$scope.updateVariableValue($input.val());
};
$scope.inputBlur = () => {
2018-04-05 10:10:32 -05:00
// happens long before the click event on the typeahead options
// need to have long delay because the blur
cancelBlur = setTimeout($scope.switchToLink, 200);
};
$scope.source = (query, callback) => {
$scope.$apply(() => {
$scope.getOptions({ $query: query }).then(altSegments => {
2018-04-05 10:10:32 -05:00
$scope.altSegments = altSegments;
options = _.map($scope.altSegments, alt => {
2018-04-05 10:10:32 -05:00
return _.escape(alt.value);
});
// add custom values
if (segment.custom !== 'false') {
if (!segment.fake && _.indexOf(options, segment.value) === -1) {
options.unshift(_.escape(segment.value));
2018-04-05 10:10:32 -05:00
}
}
callback(options);
});
});
};
$scope.updater = value => {
value = _.unescape(value);
2018-04-05 10:10:32 -05:00
if (value === segment.value) {
clearTimeout(cancelBlur);
$input.focus();
return value;
}
$input.val(value);
$scope.switchToLink(true);
return value;
};
$scope.matcher = function(item) {
if (linkMode) {
return false;
}
2018-04-05 10:10:32 -05:00
let str = this.query;
if (str[0] === '/') {
str = str.substring(1);
}
if (str[str.length - 1] === '/') {
str = str.substring(0, str.length - 1);
}
try {
return item.toLowerCase().match(str.toLowerCase());
} catch (e) {
return false;
}
};
$input.attr('data-provide', 'typeahead');
$input.typeahead({
source: $scope.source,
minLength: 0,
items: 10000,
updater: $scope.updater,
matcher: $scope.matcher,
});
const typeahead = $input.data('typeahead');
2018-04-05 10:10:32 -05:00
typeahead.lookup = function() {
this.query = this.$element.val() || '';
const items = this.source(this.query, $.proxy(this.process, this));
2018-04-05 10:10:32 -05:00
return items ? this.process(items) : items;
};
if (debounceLookup) {
typeahead.lookup = _.debounce(typeahead.lookup, 500, { leading: true });
}
$button.keydown(evt => {
2018-04-05 10:10:32 -05:00
// trigger typeahead on down arrow or enter key
if (evt.keyCode === 40 || evt.keyCode === 13) {
$button.click();
}
});
$button.click(() => {
2018-04-05 10:10:32 -05:00
options = null;
$input.css('width', Math.max($button.width(), 80) + 16 + 'px');
$button.hide();
$input.show();
$input.focus();
linkMode = false;
const typeahead = $input.data('typeahead');
2018-04-05 10:10:32 -05:00
if (typeahead) {
$input.val('');
typeahead.lookup();
}
});
$input.blur($scope.inputBlur);
$compile(elem.contents())($scope);
},
};
}
2018-04-10 02:24:54 -05:00
/** @ngInject */
2018-04-05 10:10:32 -05:00
export function metricSegmentModel(uiSegmentSrv, $q) {
return {
template:
'<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',
restrict: 'E',
scope: {
property: '=',
options: '=',
getOptions: '&',
onChange: '&',
},
link: {
pre: function postLink($scope, elem, attrs) {
let cachedOptions;
$scope.valueToSegment = value => {
const option = _.find($scope.options, { value: value });
const segment = {
2018-04-05 10:10:32 -05:00
cssClass: attrs.cssClass,
custom: attrs.custom,
value: option ? option.text : value,
selectMode: attrs.selectMode,
};
return uiSegmentSrv.newSegment(segment);
};
$scope.getOptionsInternal = () => {
2018-04-05 10:10:32 -05:00
if ($scope.options) {
cachedOptions = $scope.options;
return $q.when(
_.map($scope.options, option => {
2018-10-18 08:04:54 -05:00
return { value: option.text };
2018-04-05 10:10:32 -05:00
})
);
} else {
return $scope.getOptions().then(options => {
2018-04-05 10:10:32 -05:00
cachedOptions = options;
return _.map(options, option => {
2018-04-05 10:10:32 -05:00
if (option.html) {
return option;
}
2018-10-18 08:04:54 -05:00
return { value: option.text };
2018-04-05 10:10:32 -05:00
});
});
}
};
$scope.onSegmentChange = () => {
2018-04-05 10:10:32 -05:00
if (cachedOptions) {
const option = _.find(cachedOptions, { text: $scope.segment.value });
2018-04-05 10:10:32 -05:00
if (option && option.value !== $scope.property) {
$scope.property = option.value;
} else if (attrs.custom !== 'false') {
$scope.property = $scope.segment.value;
}
} else {
$scope.property = $scope.segment.value;
}
// needs to call this after digest so
// property is synced with outerscope
$scope.$$postDigest(() => {
$scope.$apply(() => {
2018-04-05 10:10:32 -05:00
$scope.onChange();
});
});
};
$scope.segment = $scope.valueToSegment($scope.property);
},
},
};
}
coreModule.directive('metricSegment', metricSegment);
coreModule.directive('metricSegmentModel', metricSegmentModel);