From 5f7b4cb5d8061f83f07ce079157d7d98e4257926 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Fri, 21 Apr 2017 13:56:27 -0500 Subject: Use jQuery niceness on blob_fork_suggestion Fix https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10602#note_27871752 --- .../javascripts/blob/blob_fork_suggestion.js | 53 ++++++++++------------ 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'app/assets/javascripts/blob') diff --git a/app/assets/javascripts/blob/blob_fork_suggestion.js b/app/assets/javascripts/blob/blob_fork_suggestion.js index 3baf81905fe..47c431fb809 100644 --- a/app/assets/javascripts/blob/blob_fork_suggestion.js +++ b/app/assets/javascripts/blob/blob_fork_suggestion.js @@ -16,47 +16,44 @@ const defaults = { class BlobForkSuggestion { constructor(options) { this.elementMap = Object.assign({}, defaults, options); - this.onClickWrapper = this.onClick.bind(this); - - document.addEventListener('click', this.onClickWrapper); + this.onOpenButtonClick = this.onOpenButtonClick.bind(this); + this.onCancelButtonClick = this.onCancelButtonClick.bind(this); } - showSuggestionSection(forkPath, action = 'edit') { - [].forEach.call(this.elementMap.suggestionSections, (suggestionSection) => { - suggestionSection.classList.remove('hidden'); - }); + init() { + this.bindEvents(); - [].forEach.call(this.elementMap.forkButtons, (forkButton) => { - forkButton.setAttribute('href', forkPath); - }); + return this; + } - [].forEach.call(this.elementMap.actionTextPieces, (actionTextPiece) => { - // eslint-disable-next-line no-param-reassign - actionTextPiece.textContent = action; - }); + bindEvents() { + $(this.elementMap.openButtons).on('click', this.onOpenButtonClick); + $(this.elementMap.cancelButtons).on('click', this.onCancelButtonClick); } - hideSuggestionSection() { - [].forEach.call(this.elementMap.suggestionSections, (suggestionSection) => { - suggestionSection.classList.add('hidden'); - }); + showSuggestionSection(forkPath, action = 'edit') { + $(this.elementMap.suggestionSections).removeClass('hidden'); + $(this.elementMap.forkButtons).attr('href', forkPath); + $(this.elementMap.actionTextPieces).text(action); } - onClick(e) { - const el = e.target; + hideSuggestionSection() { + $(this.elementMap.suggestionSections).addClass('hidden'); + } - if ([].includes.call(this.elementMap.openButtons, el)) { - const { forkPath, action } = el.dataset; - this.showSuggestionSection(forkPath, action); - } + onOpenButtonClick(e) { + const forkPath = $(e.currentTarget).attr('data-fork-path'); + const action = $(e.currentTarget).attr('data-action'); + this.showSuggestionSection(forkPath, action); + } - if ([].includes.call(this.elementMap.cancelButtons, el)) { - this.hideSuggestionSection(); - } + onCancelButtonClick() { + this.hideSuggestionSection(); } destroy() { - document.removeEventListener('click', this.onClickWrapper); + $(this.elementMap.openButtons).off('click', this.onOpenButtonClick); + $(this.elementMap.cancelButtons).off('click', this.onCancelButtonClick); } } -- cgit v1.2.1