From 21724892ebeaafd7601313d835f93355a6f08779 Mon Sep 17 00:00:00 2001 From: Elastic Jasper Date: Mon, 10 Oct 2016 15:27:28 -0400 Subject: [PATCH] Backport PR #8566 --------- **Commit 1:** Modifying the histograms interval to support decimals * Original sha: da04e108ded8ae7044ded2bb84f212d9c9eaba32 * Authored by = on 2016-10-05T20:46:52Z **Commit 2:** Removing the input-whole-number, no longer used. * Original sha: cccb3708c6ca0d0663adf6dc54e6e485168d54eb * Authored by = on 2016-10-06T17:29:03Z --- .../agg_types/__tests__/buckets/_histogram.js | 14 ++++++++++++-- .../agg_types/buckets/_interval_options.js | 2 +- src/ui/public/agg_types/buckets/histogram.js | 2 +- src/ui/public/agg_types/controls/interval.html | 2 +- .../{input_whole_number.js => input_number.js} | 16 ++++++++-------- src/ui/public/directives/input_number.js | 18 ++++++++++++++++++ src/ui/public/directives/input_whole_number.js | 18 ------------------ 7 files changed, 41 insertions(+), 31 deletions(-) rename src/ui/public/directives/__tests__/{input_whole_number.js => input_number.js} (70%) create mode 100644 src/ui/public/directives/input_number.js delete mode 100644 src/ui/public/directives/input_whole_number.js diff --git a/src/ui/public/agg_types/__tests__/buckets/_histogram.js b/src/ui/public/agg_types/__tests__/buckets/_histogram.js index 906872e8d3bcf..cf6f8d6ac86f9 100644 --- a/src/ui/public/agg_types/__tests__/buckets/_histogram.js +++ b/src/ui/public/agg_types/__tests__/buckets/_histogram.js @@ -35,12 +35,22 @@ describe('Histogram Agg', function () { describe('interval', function () { // reads aggConfig.params.interval, writes to dsl.interval - it('accepts a number', function () { + it('accepts a whole number', function () { let output = paramWriter.write({ interval: 100 }); expect(output.params).to.have.property('interval', 100); }); - it('accepts a string', function () { + it('accepts a decimal number', function () { + let output = paramWriter.write({ interval: 0.1 }); + expect(output.params).to.have.property('interval', 0.1); + }); + + it('accepts a decimal number string', function () { + let output = paramWriter.write({ interval: '0.1' }); + expect(output.params).to.have.property('interval', 0.1); + }); + + it('accepts a whole number string', function () { let output = paramWriter.write({ interval: '10' }); expect(output.params).to.have.property('interval', 10); }); diff --git a/src/ui/public/agg_types/buckets/_interval_options.js b/src/ui/public/agg_types/buckets/_interval_options.js index f1a8f151898d2..344b7fa30b2c9 100644 --- a/src/ui/public/agg_types/buckets/_interval_options.js +++ b/src/ui/public/agg_types/buckets/_interval_options.js @@ -1,5 +1,5 @@ import moment from 'moment'; -import 'ui/directives/input_whole_number'; + export default function IntervalOptionsService(Private) { // shorthand diff --git a/src/ui/public/agg_types/buckets/histogram.js b/src/ui/public/agg_types/buckets/histogram.js index 38ce8b43eb994..866f4f9462975 100644 --- a/src/ui/public/agg_types/buckets/histogram.js +++ b/src/ui/public/agg_types/buckets/histogram.js @@ -29,7 +29,7 @@ export default function HistogramAggDefinition(Private) { name: 'interval', editor: intervalTemplate, write: function (aggConfig, output) { - output.params.interval = parseInt(aggConfig.params.interval, 10); + output.params.interval = parseFloat(aggConfig.params.interval); } }, diff --git a/src/ui/public/agg_types/controls/interval.html b/src/ui/public/agg_types/controls/interval.html index 829f831ae34ee..858b87150dfdc 100644 --- a/src/ui/public/agg_types/controls/interval.html +++ b/src/ui/public/agg_types/controls/interval.html @@ -35,6 +35,6 @@ class="form-control" name="interval" min="0" - input-whole-number + input-number > diff --git a/src/ui/public/directives/__tests__/input_whole_number.js b/src/ui/public/directives/__tests__/input_number.js similarity index 70% rename from src/ui/public/directives/__tests__/input_whole_number.js rename to src/ui/public/directives/__tests__/input_number.js index 0d97c28dfd767..6592ab76174a0 100644 --- a/src/ui/public/directives/__tests__/input_whole_number.js +++ b/src/ui/public/directives/__tests__/input_number.js @@ -1,12 +1,12 @@ import angular from 'angular'; import expect from 'expect.js'; import ngMock from 'ng_mock'; -import 'ui/directives/input_whole_number'; +import 'ui/directives/input_number'; -describe('Whole number input directive', function () { +describe('Number input directive', function () { let $compile; let $rootScope; - let html = ''; + let html = ''; beforeEach(ngMock.module('kibana')); @@ -22,16 +22,16 @@ describe('Whole number input directive', function () { $rootScope.$digest(); expect(element.hasClass('ng-valid')).to.be.ok(); - $rootScope.value = '1.0'; + $rootScope.value = '1'; $rootScope.$digest(); expect(element.hasClass('ng-valid')).to.be.ok(); - $rootScope.value = '-5.0'; + $rootScope.value = '-5'; $rootScope.$digest(); expect(element.hasClass('ng-valid')).to.be.ok(); }); - it('should disallow numbers with decimals', function () { + it('should allow numbers with decimals', function () { let element = $compile(html)($rootScope); $rootScope.value = '123.0'; @@ -40,10 +40,10 @@ describe('Whole number input directive', function () { $rootScope.value = '1.2'; $rootScope.$digest(); - expect(element.hasClass('ng-invalid')).to.be.ok(); + expect(element.hasClass('ng-valid')).to.be.ok(); $rootScope.value = '-5.5'; $rootScope.$digest(); - expect(element.hasClass('ng-invalid')).to.be.ok(); + expect(element.hasClass('ng-valid')).to.be.ok(); }); }); diff --git a/src/ui/public/directives/input_number.js b/src/ui/public/directives/input_number.js new file mode 100644 index 0000000000000..51b11f0b9b44d --- /dev/null +++ b/src/ui/public/directives/input_number.js @@ -0,0 +1,18 @@ +import uiModules from 'ui/modules'; +let module = uiModules.get('kibana'); + +module.directive('inputNumber', function () { + return { + restrict: 'A', + require: 'ngModel', + link: function ($scope, $elem, attrs, ngModel) { + ngModel.$parsers.push(checkNumber); + ngModel.$formatters.push(checkNumber); + + function checkNumber(value) { + ngModel.$setValidity('number', !isNaN(parseFloat(value))); + return value; + } + } + }; +}); diff --git a/src/ui/public/directives/input_whole_number.js b/src/ui/public/directives/input_whole_number.js deleted file mode 100644 index 5a4f406ea5a9f..0000000000000 --- a/src/ui/public/directives/input_whole_number.js +++ /dev/null @@ -1,18 +0,0 @@ -import uiModules from 'ui/modules'; -let module = uiModules.get('kibana'); - -module.directive('inputWholeNumber', function () { - return { - restrict: 'A', - require: 'ngModel', - link: function ($scope, $elem, attrs, ngModel) { - ngModel.$parsers.push(checkWholeNumber); - ngModel.$formatters.push(checkWholeNumber); - - function checkWholeNumber(value) { - ngModel.$setValidity('whole', value % 1 === 0); - return value; - } - } - }; -});