All files / app/assets/javascripts/blob blob_fork_suggestion.js

77.77% Statements 14/18
0% Branches 0/1
75% Functions 6/8
77.77% Lines 14/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 61 62 63    7x                                 2x 2x 2x       2x   2x       2x 2x       1x 1x 1x       1x                           2x 2x          
import $ from 'jquery';
 
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 = { ...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;