grafana/public/app/features/annotations/event_editor.ts
Torkel Ödegaard 8f78b0e7bc
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors

* Added new limit

* Fixed ts issue

* fixed tests

* trying to fix type inference

* Fixing more ts errors

* Revert tsconfig option

* Fix

* Fixed code

* More fixes

* fix tests

* Updated snapshot

* Chore: More ts strict null fixes

* More fixes in some really messed up azure config components

* More fixes, current count: 441

* 419

* More fixes

* Fixed invalid initial state in explore

* Fixing tests

* Fixed tests

* Explore fix

* More fixes

* Progress

* Sub 300

* Now at 218

* Progress

* Update

* Progress

* Updated tests

* at 159

* fixed tests

* Progress

* YAy blow 100! at 94

* 10,9,8,7,6,5,4,3,2,1... lift off

* Fixed tests

* Fixed more type errors

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 12:46:59 +02:00

112 lines
2.6 KiB
TypeScript

import _ from 'lodash';
import { coreModule } from 'app/core/core';
import { MetricsPanelCtrl } from 'app/plugins/sdk';
import { AnnotationEvent } from '@grafana/data';
import { dateTime } from '@grafana/data';
import { AnnotationsSrv } from './all';
export class EventEditorCtrl {
panelCtrl: MetricsPanelCtrl;
event: AnnotationEvent;
timeRange: { from: number; to: number };
form: any;
close: any;
timeFormated: string;
/** @ngInject */
constructor(private annotationsSrv: AnnotationsSrv) {
this.event.panelId = this.panelCtrl.panel.id;
this.event.dashboardId = this.panelCtrl.dashboard.id;
// Annotations query returns time as Unix timestamp in milliseconds
this.event.time = tryEpochToMoment(this.event.time);
if (this.event.isRegion) {
this.event.timeEnd = tryEpochToMoment(this.event.timeEnd);
}
this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time!);
}
save() {
if (!this.form.$valid) {
return;
}
const saveModel = _.cloneDeep(this.event);
saveModel.time = saveModel.time!.valueOf();
saveModel.timeEnd = 0;
if (saveModel.isRegion) {
saveModel.timeEnd = this.event.timeEnd!.valueOf();
if (saveModel.timeEnd < saveModel.time) {
console.log('invalid time');
return;
}
}
if (saveModel.id) {
this.annotationsSrv
.updateAnnotationEvent(saveModel)
.then(() => {
this.panelCtrl.refresh();
this.close();
})
.catch(() => {
this.panelCtrl.refresh();
this.close();
});
} else {
this.annotationsSrv
.saveAnnotationEvent(saveModel)
.then(() => {
this.panelCtrl.refresh();
this.close();
})
.catch(() => {
this.panelCtrl.refresh();
this.close();
});
}
}
delete() {
return this.annotationsSrv
.deleteAnnotationEvent(this.event)
.then(() => {
this.panelCtrl.refresh();
this.close();
})
.catch(() => {
this.panelCtrl.refresh();
this.close();
});
}
}
function tryEpochToMoment(timestamp: any) {
if (timestamp && _.isNumber(timestamp)) {
const epoch = Number(timestamp);
return dateTime(epoch);
} else {
return timestamp;
}
}
export function eventEditor() {
return {
restrict: 'E',
controller: EventEditorCtrl,
bindToController: true,
controllerAs: 'ctrl',
templateUrl: 'public/app/features/annotations/partials/event_editor.html',
scope: {
panelCtrl: '=',
event: '=',
close: '&',
},
};
}
coreModule.directive('eventEditor', eventEditor);