diff-view.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import angular from 'angular';
  2. import { GrafanaRootScope } from 'app/angular/GrafanaCtrl';
  3. import coreModule from './core_module';
  4. export class DeltaCtrl {
  5. observer: any;
  6. /** @ngInject */
  7. constructor() {
  8. const waitForCompile = () => {};
  9. this.observer = new MutationObserver(waitForCompile);
  10. const observerConfig = {
  11. attributes: true,
  12. attributeFilter: ['class'],
  13. characterData: false,
  14. childList: true,
  15. subtree: false,
  16. };
  17. this.observer.observe(angular.element('.delta-html')[0], observerConfig);
  18. }
  19. $onDestroy() {
  20. this.observer.disconnect();
  21. }
  22. }
  23. export function delta() {
  24. return {
  25. controller: DeltaCtrl,
  26. replace: false,
  27. restrict: 'A',
  28. };
  29. }
  30. coreModule.directive('diffDelta', delta);
  31. // Link to JSON line number
  32. export class LinkJSONCtrl {
  33. /** @ngInject */
  34. constructor(private $scope: any, private $rootScope: GrafanaRootScope, private $anchorScroll: any) {}
  35. goToLine(line: number) {
  36. let unbind: () => void;
  37. const scroll = () => {
  38. this.$anchorScroll(`l${line}`);
  39. unbind();
  40. };
  41. this.$scope.switchView().then(() => {
  42. unbind = this.$rootScope.$on('json-diff-ready', scroll.bind(this));
  43. });
  44. }
  45. }
  46. export function linkJson() {
  47. return {
  48. controller: LinkJSONCtrl,
  49. controllerAs: 'ctrl',
  50. replace: true,
  51. restrict: 'E',
  52. scope: {
  53. line: '@lineDisplay',
  54. link: '@lineLink',
  55. switchView: '&',
  56. },
  57. template: `<a class="diff-linenum btn btn-inverse btn-small" ng-click="ctrl.goToLine(link)">Line {{ line }}</a>`,
  58. };
  59. }
  60. coreModule.directive('diffLinkJson', linkJson);