diff-view.ts 1.6 KB

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