2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
2017-04-10 16:51:24 -04:00
|
|
|
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) {
|
2020-05-07 17:09:26 -04:00
|
|
|
this.elementMap = { ...defaults, ...options };
|
2017-04-21 14:56:27 -04:00
|
|
|
this.onOpenButtonClick = this.onOpenButtonClick.bind(this);
|
|
|
|
this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
|
2017-04-10 16:51:24 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
init() {
|
|
|
|
this.bindEvents();
|
2017-04-10 16:51:24 -04:00
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
return this;
|
|
|
|
}
|
2017-04-10 16:51:24 -04:00
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
bindEvents() {
|
|
|
|
$(this.elementMap.openButtons).on('click', this.onOpenButtonClick);
|
|
|
|
$(this.elementMap.cancelButtons).on('click', this.onCancelButtonClick);
|
2017-04-06 12:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
showSuggestionSection(forkPath, action = 'edit') {
|
|
|
|
$(this.elementMap.suggestionSections).removeClass('hidden');
|
|
|
|
$(this.elementMap.forkButtons).attr('href', forkPath);
|
|
|
|
$(this.elementMap.actionTextPieces).text(action);
|
2017-04-06 12:36:38 -04:00
|
|
|
}
|
2017-04-10 16:51:24 -04:00
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
hideSuggestionSection() {
|
|
|
|
$(this.elementMap.suggestionSections).addClass('hidden');
|
|
|
|
}
|
2017-04-10 16:51:24 -04:00
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
onOpenButtonClick(e) {
|
|
|
|
const forkPath = $(e.currentTarget).attr('data-fork-path');
|
|
|
|
const action = $(e.currentTarget).attr('data-action');
|
|
|
|
this.showSuggestionSection(forkPath, action);
|
|
|
|
}
|
2017-04-10 16:51:24 -04:00
|
|
|
|
2017-04-21 14:56:27 -04:00
|
|
|
onCancelButtonClick() {
|
|
|
|
this.hideSuggestionSection();
|
2017-04-10 16:51:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2017-04-21 14:56:27 -04:00
|
|
|
$(this.elementMap.openButtons).off('click', this.onOpenButtonClick);
|
|
|
|
$(this.elementMap.cancelButtons).off('click', this.onCancelButtonClick);
|
2017-04-10 16:51:24 -04:00
|
|
|
}
|
2017-04-06 12:36:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default BlobForkSuggestion;
|