summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/blob/blob_fork_suggestion.js
blob: 47c431fb809cbe3bd0bd594622624a22eabba1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const defaults = {
  // Buttons that will show the `suggestionSections`
  // has `data-fork-path`, and `data-action`
  openButtons: [],
  // Update the href(from `openButton` -> `data-fork-path`)
  // whenever a `openButton` is clicked
  forkButtons: [],
  // Buttons to hide the `suggestionSections`
  cancelButtons: [],
  // Section to show/hide
  suggestionSections: [],
  // Pieces of text that need updating depending on the action, `edit`, `replace`, `delete`
  actionTextPieces: [],
};

class BlobForkSuggestion {
  constructor(options) {
    this.elementMap = Object.assign({}, defaults, options);
    this.onOpenButtonClick = this.onOpenButtonClick.bind(this);
    this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
  }

  init() {
    this.bindEvents();

    return this;
  }

  bindEvents() {
    $(this.elementMap.openButtons).on('click', this.onOpenButtonClick);
    $(this.elementMap.cancelButtons).on('click', this.onCancelButtonClick);
  }

  showSuggestionSection(forkPath, action = 'edit') {
    $(this.elementMap.suggestionSections).removeClass('hidden');
    $(this.elementMap.forkButtons).attr('href', forkPath);
    $(this.elementMap.actionTextPieces).text(action);
  }

  hideSuggestionSection() {
    $(this.elementMap.suggestionSections).addClass('hidden');
  }

  onOpenButtonClick(e) {
    const forkPath = $(e.currentTarget).attr('data-fork-path');
    const action = $(e.currentTarget).attr('data-action');
    this.showSuggestionSection(forkPath, action);
  }

  onCancelButtonClick() {
    this.hideSuggestionSection();
  }

  destroy() {
    $(this.elementMap.openButtons).off('click', this.onOpenButtonClick);
    $(this.elementMap.cancelButtons).off('click', this.onCancelButtonClick);
  }
}

export default BlobForkSuggestion;