event_editor.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import {coreModule} from 'app/core/core';
  5. import {MetricsPanelCtrl} from 'app/plugins/sdk';
  6. export class AnnotationEvent {
  7. dashboardId: number;
  8. panelId: number;
  9. time: any;
  10. timeEnd: any;
  11. isRegion: boolean;
  12. title: string;
  13. text: string;
  14. }
  15. export class EventEditorCtrl {
  16. panelCtrl: MetricsPanelCtrl;
  17. annotation: AnnotationEvent;
  18. timeRange: {from: number, to: number};
  19. form: any;
  20. close: any;
  21. /** @ngInject **/
  22. constructor(private annotationsSrv) {
  23. this.annotation = new AnnotationEvent();
  24. this.annotation.panelId = this.panelCtrl.panel.id;
  25. this.annotation.dashboardId = this.panelCtrl.dashboard.id;
  26. this.annotation.time = moment(this.timeRange.from);
  27. if (this.timeRange.to) {
  28. this.annotation.timeEnd = moment(this.timeRange.to);
  29. this.annotation.isRegion = true;
  30. }
  31. }
  32. save() {
  33. if (!this.form.$valid) {
  34. return;
  35. }
  36. let saveModel = _.cloneDeep(this.annotation);
  37. saveModel.time = saveModel.time.valueOf();
  38. if (saveModel.isRegion) {
  39. saveModel.timeEnd = saveModel.timeEnd.valueOf();
  40. }
  41. if (saveModel.timeEnd < saveModel.time) {
  42. console.log('invalid time');
  43. return;
  44. }
  45. this.annotationsSrv.saveAnnotationEvent(saveModel).then(() => {
  46. this.panelCtrl.refresh();
  47. this.close();
  48. });
  49. }
  50. }
  51. export function eventEditor() {
  52. return {
  53. restrict: 'E',
  54. controller: EventEditorCtrl,
  55. bindToController: true,
  56. controllerAs: 'ctrl',
  57. templateUrl: 'public/app/features/annotations/partials/event_editor.html',
  58. scope: {
  59. "panelCtrl": "=",
  60. "timeRange": "=",
  61. "close": "&",
  62. }
  63. };
  64. }
  65. coreModule.directive('eventEditor', eventEditor);