From 3557a14aaede83292a9b7820a901b10aefedbf9e Mon Sep 17 00:00:00 2001 From: sanke1 <22073083+sankerust@users.noreply.github.com> Date: Fri, 20 Jul 2018 20:21:51 +0300 Subject: [PATCH] fix issue #49 (#52) * remove color index from code use different approach fix issue #49 * remove unneeded space change var to let * add 2 more tests to check correct color choice behaviour after deleting anomalies * use more descriptive word in text description --- src/controllers/analytic_controller.ts | 14 +++++++------- tests/analyticController.jest.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/controllers/analytic_controller.ts b/src/controllers/analytic_controller.ts index 6fca88d..48136d4 100644 --- a/src/controllers/analytic_controller.ts +++ b/src/controllers/analytic_controller.ts @@ -37,11 +37,8 @@ export class AnalyticController { private _savingNewAnalyticUnit: boolean = false; private _tempIdCounted: number = -1; private _graphLocked: boolean = false; - private _currentColorIndex: number = 0; - private _statusRunners: Set = new Set(); - constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { if(_panelObject.anomalyTypes === undefined) { _panelObject.anomalyTypes = []; @@ -50,7 +47,6 @@ export class AnalyticController { this._labelingDataDeletedSegments = new SegmentArray(); this._analyticUnitsSet = new AnalyticUnitsSet(this._panelObject.anomalyTypes); this.analyticUnits.forEach(a => this.runEnabledWaiter(a)); - this._currentColorIndex = this._panelObject.anomalyTypes.length % ANALYTIC_UNIT_COLORS.length; } getSegmentsSearcher(): AnalyticSegmentsSearcher { @@ -72,9 +68,13 @@ export class AnalyticController { this._newAnalyticUnit = new AnalyticUnit(); this._creatingNewAnalyticType = true; this._savingNewAnalyticUnit = false; - this._newAnalyticUnit.color = ANALYTIC_UNIT_COLORS[this._currentColorIndex]; - this._currentColorIndex++; - this._currentColorIndex %= ANALYTIC_UNIT_COLORS.length; + if (this.analyticUnits.length === 0) { + this._newAnalyticUnit.color = ANALYTIC_UNIT_COLORS[0]; + } else { + let colorIndex = ANALYTIC_UNIT_COLORS.indexOf(_.last(this.analyticUnits).color) + 1; + colorIndex %= ANALYTIC_UNIT_COLORS.length; + this._newAnalyticUnit.color = ANALYTIC_UNIT_COLORS[colorIndex]; + } } async saveNew(metricExpanded: MetricExpanded, datasourceRequest: DatasourceRequest, panelId: number) { diff --git a/tests/analyticController.jest.ts b/tests/analyticController.jest.ts index 1271d1f..8423aec 100644 --- a/tests/analyticController.jest.ts +++ b/tests/analyticController.jest.ts @@ -3,6 +3,7 @@ import { MetricExpanded } from '../src/models/metric'; import { DatasourceRequest } from '../src/models/datasource'; import { analyticController } from './setup_tests'; +import { AnalyticUnit } from '../src/models/analytic_unit'; describe('AnalyticController', function () { @@ -25,6 +26,21 @@ describe('AnalyticController', function () { } }); + it('should set different color to newly created Analytic Unit, afer NOT last AU was deleted', async function() { + let auArray = analyticController.analyticUnits; + analyticController.createNew(); + await analyticController.saveNew({} as MetricExpanded, {} as DatasourceRequest, 1); + expect(auArray[auArray.length - 2].panelObject.color).not.toBe(auArray[auArray.length - 1].panelObject.color); + }); + + it('should set different color to newly created Analytic Unit, afer LAST AU was deleted', async function () { + let auArray = analyticController.analyticUnits; + auArray.splice(-1, 1); + analyticController.createNew(); + await analyticController.saveNew({} as MetricExpanded, {} as DatasourceRequest, 1); + expect(auArray[auArray.length - 2].panelObject.color).not.toBe(auArray[auArray.length - 1].panelObject.color); + }); + it('should change color on choosing from palette', function () { analyticController.onAnalyticUnitColorChange('1', 'red'); expect(analyticController.analyticUnits[0].color).toBe('red');