event_editor.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. import {AnnotationEvent} from './event';
  7. export class EventEditorCtrl {
  8. panelCtrl: MetricsPanelCtrl;
  9. event: AnnotationEvent;
  10. timeRange: {from: number, to: number};
  11. form: any;
  12. close: any;
  13. /** @ngInject **/
  14. constructor(private annotationsSrv) {
  15. this.event.panelId = this.panelCtrl.panel.id;
  16. this.event.dashboardId = this.panelCtrl.dashboard.id;
  17. }
  18. save() {
  19. if (!this.form.$valid) {
  20. return;
  21. }
  22. let saveModel = _.cloneDeep(this.event);
  23. saveModel.time = saveModel.time.valueOf();
  24. saveModel.timeEnd = 0;
  25. if (saveModel.isRegion) {
  26. saveModel.timeEnd = saveModel.timeEnd.valueOf();
  27. if (saveModel.timeEnd < saveModel.time) {
  28. console.log('invalid time');
  29. return;
  30. }
  31. }
  32. this.annotationsSrv.saveAnnotationEvent(saveModel).then(() => {
  33. this.panelCtrl.refresh();
  34. this.close();
  35. });
  36. }
  37. timeChanged() {
  38. this.panelCtrl.render();
  39. }
  40. }
  41. export function eventEditor() {
  42. return {
  43. restrict: 'E',
  44. controller: EventEditorCtrl,
  45. bindToController: true,
  46. controllerAs: 'ctrl',
  47. templateUrl: 'public/app/features/annotations/partials/event_editor.html',
  48. scope: {
  49. "panelCtrl": "=",
  50. "event": "=",
  51. "close": "&",
  52. }
  53. };
  54. }
  55. coreModule.directive('eventEditor', eventEditor);