From f9ce9bdcec9c3fe231eb5195cc9efd4b3984c2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 5 Sep 2015 08:07:40 +0200 Subject: [PATCH] feat(editor): refactoring and making new editor abstractions --- public/app/directives/metric.segment.js | 57 +++++++++++++++++-- .../datasource/elasticsearch/bucketAgg.js | 15 ++--- .../elasticsearch/partials/bucketAgg.html | 4 +- .../datasource/elasticsearch/queryCtrl.js | 6 ++ 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/public/app/directives/metric.segment.js b/public/app/directives/metric.segment.js index 3b5f473b6dc..b6392ec2a0f 100644 --- a/public/app/directives/metric.segment.js +++ b/public/app/directives/metric.segment.js @@ -20,8 +20,9 @@ function (angular, app, _, $) { return { scope: { segment: "=", + disableCustom: "=", getAltSegments: "&", - onValueChanged: "&" + onValueChanged: "&", }, link: function($scope, elem) { @@ -47,12 +48,13 @@ function (angular, app, _, $) { segment.fake = false; segment.expandable = selected.expandable; } - else { + else if ($scope.disableCustom === false) { segment.value = value; segment.html = $sce.trustAsHtml(value); segment.expandable = true; segment.fake = false; } + $scope.onValueChanged(); }); }; @@ -81,8 +83,10 @@ function (angular, app, _, $) { options = _.map($scope.altSegments, function(alt) { return alt.value; }); // add custom values - if (!segment.fake && _.indexOf(options, segment.value) === -1) { - options.unshift(segment.value); + if ($scope.disableCustom === false) { + if (!segment.fake && _.indexOf(options, segment.value) === -1) { + options.unshift(segment.value); + } } callback(options); @@ -92,7 +96,6 @@ function (angular, app, _, $) { $scope.updater = function(value) { if (value === segment.value) { - console.log('cancel blur'); clearTimeout(cancelBlur); $input.focus(); return value; @@ -153,4 +156,48 @@ function (angular, app, _, $) { } }; }); + + angular + .module('grafana.directives') + .directive('metricSegmentModel', function(uiSegmentSrv, $q) { + return { + template: '', + restrict: 'E', + scope: { + property: "=", + options: "=", + onChange: "&", + }, + link: { + pre: function postLink($scope, elem) { + + $scope.valueToSegment = function(value) { + var option = _.findWhere($scope.options, {value: value}); + if (option) { + return uiSegmentSrv.newSegment({value: option.text}); + } else { + return uiSegmentSrv.newSegment({value: value}); + } + }; + + $scope.getOptions = function() { + var optionSegments = _.map($scope.options, function(option) { + return uiSegmentSrv.newSegment({value: option.text}); + }); + return $q.when(optionSegments); + }; + + $scope.onSegmentChange = function() { + var option = _.findWhere($scope.options, {text: $scope.segment.value}); + if (option && option.value !== $scope.property) { + $scope.property = option.value; + $scope.onChange(); + } + }; + + $scope.segment = $scope.valueToSegment($scope.property); + } + } + }; + }); }); diff --git a/public/app/plugins/datasource/elasticsearch/bucketAgg.js b/public/app/plugins/datasource/elasticsearch/bucketAgg.js index 156f5582aca..7c3954805e8 100644 --- a/public/app/plugins/datasource/elasticsearch/bucketAgg.js +++ b/public/app/plugins/datasource/elasticsearch/bucketAgg.js @@ -13,6 +13,11 @@ function (angular, _, $) { $scope.agg = bucketAggs[$scope.index]; + $scope.bucketAggTypes = [ + {text: "Terms", value: 'terms' }, + {text: "Date Histogram", value: 'date_histogram' }, + ]; + $scope.$watch("index", function() { $scope.isFirst = $scope.index === 0; $scope.isLast = $scope.index === bucketAggs.length - 1; @@ -22,18 +27,10 @@ function (angular, _, $) { $scope.aggOptionsString = "Top 5, Order by: sum @value"; } - $scope.typeSegment = uiSegmentSrv.newSegment($scope.agg.type); $scope.fieldSegment = uiSegmentSrv.newSegment($scope.agg.field); - $scope.getBucketAggTypes = function() { - return $q.when([ - uiSegmentSrv.newSegment({value: 'terms'}), - uiSegmentSrv.newSegment({value: 'date_histogram'}), - ]); - }; - $scope.toggleOptions = function() { - $scope.showOptions = $scope.showOptions; + $scope.showOptions = !$scope.showOptions; } $scope.addBucketAgg = function() { diff --git a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html index 68bea648205..8b0662bad7d 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html +++ b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html @@ -5,7 +5,7 @@ Then by
  • - +
  • @@ -32,7 +32,7 @@ Order
  • - +
  • diff --git a/public/app/plugins/datasource/elasticsearch/queryCtrl.js b/public/app/plugins/datasource/elasticsearch/queryCtrl.js index 27d1d1c0952..49547295160 100644 --- a/public/app/plugins/datasource/elasticsearch/queryCtrl.js +++ b/public/app/plugins/datasource/elasticsearch/queryCtrl.js @@ -10,6 +10,12 @@ function (angular, _, ElasticQueryBuilder) { module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv, $q) { + $scope.metricAggregations = { + "Count": { value: 'count' }, + "Average of": { value: 'avg' }, + "Max of": { value: 'max' }, + }; + $scope.init = function() { var target = $scope.target; if (!target) { return; }