single_file_diff.js 3.52 KB
Newer Older
1
/* eslint-disable consistent-return */
2

3
import $ from 'jquery';
4
import { spriteIcon } from '~/lib/utils/common_utils';
5 6
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
7
import { deprecatedCreateFlash as createFlash } from './flash';
8
import FilesCommentButton from './files_comment_button';
9
import initImageDiffHelper from './image_diff/helpers/init_image_diff';
10
import syntaxHighlight from './syntax_highlight';
11

12
const WRAPPER = '<div class="diff-content"></div>';
13
const LOADING_HTML = '<span class="spinner"></span>';
14 15 16 17
const ERROR_HTML = `<div class="nothing-here-block">${spriteIcon(
  'warning-solid',
  's16',
)} Could not load diff</div>`;
18 19
const COLLAPSED_HTML =
  '<div class="nothing-here-block diff-collapsed">This diff is collapsed. <button class="click-to-expand btn btn-link">Click to expand it.</button></div>';
Fatih Acet's avatar
Fatih Acet committed
20

21
export default class SingleFileDiff {
22
  constructor(file) {
23 24 25
    this.file = file;
    this.toggleDiff = this.toggleDiff.bind(this);
    this.content = $('.diff-content', this.file);
26 27
    this.$chevronRightIcon = $('.diff-toggle-caret .chevron-right', this.file);
    this.$chevronDownIcon = $('.diff-toggle-caret .chevron-down', this.file);
Jacob Schatz's avatar
Jacob Schatz committed
28
    this.diffForPath = this.content.find('[data-diff-for-path]').data('diffForPath');
29 30 31
    this.isOpen = !this.diffForPath;
    if (this.diffForPath) {
      this.collapsedContent = this.content;
32
      this.loadingContent = $(WRAPPER).addClass('loading').html(LOADING_HTML).hide();
33 34
      this.content = null;
      this.collapsedContent.after(this.loadingContent);
35
      this.$chevronRightIcon.removeClass('gl-display-none');
36
    } else {
37
      this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
38
      this.content.after(this.collapsedContent);
39
      this.$chevronDownIcon.removeClass('gl-display-none');
40 41
    }

42
    $('.js-file-title, .click-to-expand', this.file).on('click', (e) => {
43 44
      this.toggleDiff($(e.target));
    });
45
  }
46

47
  toggleDiff($target, cb) {
48 49 50
    if (
      !$target.hasClass('js-file-title') &&
      !$target.hasClass('click-to-expand') &&
51
      !$target.closest('.diff-toggle-caret').length > 0
52 53
    )
      return;
54 55 56
    this.isOpen = !this.isOpen;
    if (!this.isOpen && !this.hasError) {
      this.content.hide();
57 58
      this.$chevronRightIcon.removeClass('gl-display-none');
      this.$chevronDownIcon.addClass('gl-display-none');
59 60 61 62
      this.collapsedContent.show();
    } else if (this.content) {
      this.collapsedContent.hide();
      this.content.show();
63 64
      this.$chevronDownIcon.removeClass('gl-display-none');
      this.$chevronRightIcon.addClass('gl-display-none');
65
    } else {
66 67
      this.$chevronDownIcon.removeClass('gl-display-none');
      this.$chevronRightIcon.addClass('gl-display-none');
68
      return this.getContentHTML(cb);
Fatih Acet's avatar
Fatih Acet committed
69
    }
70
  }
Fatih Acet's avatar
Fatih Acet committed
71

72
  getContentHTML(cb) {
73 74
    this.collapsedContent.hide();
    this.loadingContent.show();
75

76 77
    axios
      .get(this.diffForPath)
78 79
      .then(({ data }) => {
        this.loadingContent.hide();
80
        if (data.html) {
81 82
          this.content = $(data.html);
          syntaxHighlight(this.content);
83
        } else {
84 85
          this.hasError = true;
          this.content = $(ERROR_HTML);
86
        }
87
        this.collapsedContent.after(this.content);
88

89
        const $file = $(this.file);
Felipe Artur's avatar
Felipe Artur committed
90 91 92
        FilesCommentButton.init($file);

        const canCreateNote = $file.closest('.files').is('[data-can-create-note]');
93
        initImageDiffHelper.initImageDiff($file[0], canCreateNote);
94

95
        if (cb) cb();
96
      })
97 98 99
      .catch(() => {
        createFlash(__('An error occurred while retrieving diff'));
      });
100 101
  }
}