diff-view.ts 1.7 KB

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