blob_fork_suggestion.js 1.76 KB
Newer Older
1 2
import $ from 'jquery';

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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);
21 22
    this.onOpenButtonClick = this.onOpenButtonClick.bind(this);
    this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
23 24
  }

25 26
  init() {
    this.bindEvents();
27

28 29
    return this;
  }
30

31 32 33
  bindEvents() {
    $(this.elementMap.openButtons).on('click', this.onOpenButtonClick);
    $(this.elementMap.cancelButtons).on('click', this.onCancelButtonClick);
34 35
  }

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

42 43 44
  hideSuggestionSection() {
    $(this.elementMap.suggestionSections).addClass('hidden');
  }
45

46 47 48 49 50
  onOpenButtonClick(e) {
    const forkPath = $(e.currentTarget).attr('data-fork-path');
    const action = $(e.currentTarget).attr('data-action');
    this.showSuggestionSection(forkPath, action);
  }
51

52 53
  onCancelButtonClick() {
    this.hideSuggestionSection();
54 55 56
  }

  destroy() {
57 58
    $(this.elementMap.openButtons).off('click', this.onOpenButtonClick);
    $(this.elementMap.cancelButtons).off('click', this.onCancelButtonClick);
59
  }
60 61 62
}

export default BlobForkSuggestion;