Commit 3b4c07ac authored by Mike Greiling's avatar Mike Greiling

refactor AwardsHandler into es class syntax

parent 442bd265
/* eslint-disable class-methods-use-this */
/* global Flash */ /* global Flash */
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
...@@ -68,7 +69,8 @@ function renderCategory(name, emojiList, opts = {}) { ...@@ -68,7 +69,8 @@ function renderCategory(name, emojiList, opts = {}) {
`; `;
} }
function AwardsHandler() { export default class AwardsHandler {
constructor() {
this.eventListeners = []; this.eventListeners = [];
this.aliases = emojiAliases; this.aliases = emojiAliases;
// If the user shows intent let's pre-build the menu // If the user shows intent let's pre-build the menu
...@@ -110,17 +112,17 @@ function AwardsHandler() { ...@@ -110,17 +112,17 @@ function AwardsHandler() {
$target.closest('.js-awards-block').addClass('current'); $target.closest('.js-awards-block').addClass('current');
this.addAward(this.getVotesBlock(), this.getAwardUrl(), emoji); this.addAward(this.getVotesBlock(), this.getAwardUrl(), emoji);
}); });
} }
AwardsHandler.prototype.registerEventListener = function registerEventListener(method = 'on', element, ...args) { registerEventListener(method = 'on', element, ...args) {
element[method].call(element, ...args); element[method].call(element, ...args);
this.eventListeners.push({ this.eventListeners.push({
element, element,
args, args,
}); });
}; }
AwardsHandler.prototype.showEmojiMenu = function showEmojiMenu($addBtn) { showEmojiMenu($addBtn) {
if ($addBtn.hasClass('js-note-emoji')) { if ($addBtn.hasClass('js-note-emoji')) {
$addBtn.closest('.note').find('.js-awards-block').addClass('current'); $addBtn.closest('.note').find('.js-awards-block').addClass('current');
} else { } else {
...@@ -155,11 +157,11 @@ AwardsHandler.prototype.showEmojiMenu = function showEmojiMenu($addBtn) { ...@@ -155,11 +157,11 @@ AwardsHandler.prototype.showEmojiMenu = function showEmojiMenu($addBtn) {
} }
$thumbsBtn.toggleClass('disabled', $userAuthored); $thumbsBtn.toggleClass('disabled', $userAuthored);
}; }
// Create the emoji menu with the first category of emojis. // Create the emoji menu with the first category of emojis.
// Then render the remaining categories of emojis one by one to avoid jank. // Then render the remaining categories of emojis one by one to avoid jank.
AwardsHandler.prototype.createEmojiMenu = function createEmojiMenu(callback) { createEmojiMenu(callback) {
if (this.isCreatingEmojiMenu) { if (this.isCreatingEmojiMenu) {
return; return;
} }
...@@ -198,11 +200,9 @@ AwardsHandler.prototype.createEmojiMenu = function createEmojiMenu(callback) { ...@@ -198,11 +200,9 @@ AwardsHandler.prototype.createEmojiMenu = function createEmojiMenu(callback) {
if (callback) { if (callback) {
callback(); callback();
} }
}; }
AwardsHandler addRemainingEmojiMenuCategories() {
.prototype
.addRemainingEmojiMenuCategories = function addRemainingEmojiMenuCategories() {
if (this.isAddingRemainingEmojiMenuCategories) { if (this.isAddingRemainingEmojiMenuCategories) {
return; return;
} }
...@@ -243,9 +243,9 @@ AwardsHandler ...@@ -243,9 +243,9 @@ AwardsHandler
emojiContentElement.insertAdjacentHTML('beforeend', '<p>We encountered an error while adding the remaining categories</p>'); emojiContentElement.insertAdjacentHTML('beforeend', '<p>We encountered an error while adding the remaining categories</p>');
throw new Error(`Error occurred in addRemainingEmojiMenuCategories: ${err.message}`); throw new Error(`Error occurred in addRemainingEmojiMenuCategories: ${err.message}`);
}); });
}; }
AwardsHandler.prototype.positionMenu = function positionMenu($menu, $addBtn) { positionMenu($menu, $addBtn) {
const position = $addBtn.data('position'); const position = $addBtn.data('position');
// The menu could potentially be off-screen or in a hidden overflow element // The menu could potentially be off-screen or in a hidden overflow element
// So we position the element absolute in the body // So we position the element absolute in the body
...@@ -260,15 +260,15 @@ AwardsHandler.prototype.positionMenu = function positionMenu($menu, $addBtn) { ...@@ -260,15 +260,15 @@ AwardsHandler.prototype.positionMenu = function positionMenu($menu, $addBtn) {
$menu.removeClass('is-aligned-right'); $menu.removeClass('is-aligned-right');
} }
return $menu.css(css); return $menu.css(css);
}; }
AwardsHandler.prototype.addAward = function addAward( addAward(
votesBlock, votesBlock,
awardUrl, awardUrl,
emoji, emoji,
checkMutuality, checkMutuality,
callback, callback,
) { ) {
const normalizedEmoji = this.normalizeEmojiName(emoji); const normalizedEmoji = this.normalizeEmojiName(emoji);
const $emojiButton = this.findEmojiIcon(votesBlock, normalizedEmoji).parent(); const $emojiButton = this.findEmojiIcon(votesBlock, normalizedEmoji).parent();
this.postEmoji($emojiButton, awardUrl, normalizedEmoji, () => { this.postEmoji($emojiButton, awardUrl, normalizedEmoji, () => {
...@@ -277,13 +277,13 @@ AwardsHandler.prototype.addAward = function addAward( ...@@ -277,13 +277,13 @@ AwardsHandler.prototype.addAward = function addAward(
}); });
$('.emoji-menu').removeClass('is-visible'); $('.emoji-menu').removeClass('is-visible');
$('.js-add-award.is-active').removeClass('is-active'); $('.js-add-award.is-active').removeClass('is-active');
}; }
AwardsHandler.prototype.addAwardToEmojiBar = function addAwardToEmojiBar( addAwardToEmojiBar(
votesBlock, votesBlock,
emoji, emoji,
checkForMutuality, checkForMutuality,
) { ) {
if (checkForMutuality || checkForMutuality === null) { if (checkForMutuality || checkForMutuality === null) {
this.checkMutuality(votesBlock, emoji); this.checkMutuality(votesBlock, emoji);
} }
...@@ -304,9 +304,9 @@ AwardsHandler.prototype.addAwardToEmojiBar = function addAwardToEmojiBar( ...@@ -304,9 +304,9 @@ AwardsHandler.prototype.addAwardToEmojiBar = function addAwardToEmojiBar(
votesBlock.removeClass('hidden'); votesBlock.removeClass('hidden');
this.createEmoji(votesBlock, normalizedEmoji); this.createEmoji(votesBlock, normalizedEmoji);
} }
}; }
AwardsHandler.prototype.getVotesBlock = function getVotesBlock() { getVotesBlock() {
const currentBlock = $('.js-awards-block.current'); const currentBlock = $('.js-awards-block.current');
let resultantVotesBlock = currentBlock; let resultantVotesBlock = currentBlock;
if (currentBlock.length === 0) { if (currentBlock.length === 0) {
...@@ -314,13 +314,13 @@ AwardsHandler.prototype.getVotesBlock = function getVotesBlock() { ...@@ -314,13 +314,13 @@ AwardsHandler.prototype.getVotesBlock = function getVotesBlock() {
} }
return resultantVotesBlock; return resultantVotesBlock;
}; }
AwardsHandler.prototype.getAwardUrl = function getAwardUrl() { getAwardUrl() {
return this.getVotesBlock().data('award-url'); return this.getVotesBlock().data('award-url');
}; }
AwardsHandler.prototype.checkMutuality = function checkMutuality(votesBlock, emoji) { checkMutuality(votesBlock, emoji) {
const awardUrl = this.getAwardUrl(); const awardUrl = this.getAwardUrl();
if (emoji === 'thumbsup' || emoji === 'thumbsdown') { if (emoji === 'thumbsup' || emoji === 'thumbsdown') {
const mutualVote = emoji === 'thumbsup' ? 'thumbsdown' : 'thumbsup'; const mutualVote = emoji === 'thumbsup' ? 'thumbsdown' : 'thumbsup';
...@@ -330,17 +330,17 @@ AwardsHandler.prototype.checkMutuality = function checkMutuality(votesBlock, emo ...@@ -330,17 +330,17 @@ AwardsHandler.prototype.checkMutuality = function checkMutuality(votesBlock, emo
this.addAward(votesBlock, awardUrl, mutualVote, false); this.addAward(votesBlock, awardUrl, mutualVote, false);
} }
} }
}; }
AwardsHandler.prototype.isActive = function isActive($emojiButton) { isActive($emojiButton) {
return $emojiButton.hasClass('active'); return $emojiButton.hasClass('active');
}; }
AwardsHandler.prototype.isUserAuthored = function isUserAuthored($button) { isUserAuthored($button) {
return $button.hasClass('js-user-authored'); return $button.hasClass('js-user-authored');
}; }
AwardsHandler.prototype.decrementCounter = function decrementCounter($emojiButton, emoji) { decrementCounter($emojiButton, emoji) {
const counter = $('.js-counter', $emojiButton); const counter = $('.js-counter', $emojiButton);
const counterNumber = parseInt(counter.text(), 10); const counterNumber = parseInt(counter.text(), 10);
if (counterNumber > 1) { if (counterNumber > 1) {
...@@ -357,22 +357,22 @@ AwardsHandler.prototype.decrementCounter = function decrementCounter($emojiButto ...@@ -357,22 +357,22 @@ AwardsHandler.prototype.decrementCounter = function decrementCounter($emojiButto
this.removeEmoji($emojiButton); this.removeEmoji($emojiButton);
} }
return $emojiButton.removeClass('active'); return $emojiButton.removeClass('active');
}; }
AwardsHandler.prototype.removeEmoji = function removeEmoji($emojiButton) { removeEmoji($emojiButton) {
$emojiButton.tooltip('destroy'); $emojiButton.tooltip('destroy');
$emojiButton.remove(); $emojiButton.remove();
const $votesBlock = this.getVotesBlock(); const $votesBlock = this.getVotesBlock();
if ($votesBlock.find('.js-emoji-btn').length === 0) { if ($votesBlock.find('.js-emoji-btn').length === 0) {
$votesBlock.addClass('hidden'); $votesBlock.addClass('hidden');
} }
}; }
AwardsHandler.prototype.getAwardTooltip = function getAwardTooltip($awardBlock) { getAwardTooltip($awardBlock) {
return $awardBlock.attr('data-original-title') || $awardBlock.attr('data-title') || ''; return $awardBlock.attr('data-original-title') || $awardBlock.attr('data-title') || '';
}; }
AwardsHandler.prototype.toSentence = function toSentence(list) { toSentence(list) {
let sentence; let sentence;
if (list.length <= 2) { if (list.length <= 2) {
sentence = list.join(' and '); sentence = list.join(' and ');
...@@ -381,9 +381,9 @@ AwardsHandler.prototype.toSentence = function toSentence(list) { ...@@ -381,9 +381,9 @@ AwardsHandler.prototype.toSentence = function toSentence(list) {
} }
return sentence; return sentence;
}; }
AwardsHandler.prototype.removeYouFromUserList = function removeYouFromUserList($emojiButton) { removeYouFromUserList($emojiButton) {
const awardBlock = $emojiButton; const awardBlock = $emojiButton;
const originalTitle = this.getAwardTooltip(awardBlock); const originalTitle = this.getAwardTooltip(awardBlock);
const authors = originalTitle.split(FROM_SENTENCE_REGEX); const authors = originalTitle.split(FROM_SENTENCE_REGEX);
...@@ -395,9 +395,9 @@ AwardsHandler.prototype.removeYouFromUserList = function removeYouFromUserList($ ...@@ -395,9 +395,9 @@ AwardsHandler.prototype.removeYouFromUserList = function removeYouFromUserList($
.removeAttr('data-original-title') .removeAttr('data-original-title')
.attr('title', this.toSentence(authors)) .attr('title', this.toSentence(authors))
.tooltip('fixTitle'); .tooltip('fixTitle');
}; }
AwardsHandler.prototype.addYouToUserList = function addYouToUserList(votesBlock, emoji) { addYouToUserList(votesBlock, emoji) {
const awardBlock = this.findEmojiIcon(votesBlock, emoji).parent(); const awardBlock = this.findEmojiIcon(votesBlock, emoji).parent();
const origTitle = this.getAwardTooltip(awardBlock); const origTitle = this.getAwardTooltip(awardBlock);
let users = []; let users = [];
...@@ -408,11 +408,9 @@ AwardsHandler.prototype.addYouToUserList = function addYouToUserList(votesBlock, ...@@ -408,11 +408,9 @@ AwardsHandler.prototype.addYouToUserList = function addYouToUserList(votesBlock,
return awardBlock return awardBlock
.attr('title', this.toSentence(users)) .attr('title', this.toSentence(users))
.tooltip('fixTitle'); .tooltip('fixTitle');
}; }
AwardsHandler createAwardButtonForVotesBlock(votesBlock, emojiName) {
.prototype
.createAwardButtonForVotesBlock = function createAwardButtonForVotesBlock(votesBlock, emojiName) {
const buttonHtml = ` const buttonHtml = `
<button class="btn award-control js-emoji-btn has-tooltip active" title="You" data-placement="bottom"> <button class="btn award-control js-emoji-btn has-tooltip active" title="You" data-placement="bottom">
${glEmojiTag(emojiName)} ${glEmojiTag(emojiName)}
...@@ -424,27 +422,27 @@ AwardsHandler ...@@ -424,27 +422,27 @@ AwardsHandler
this.animateEmoji($emojiButton); this.animateEmoji($emojiButton);
$('.award-control').tooltip(); $('.award-control').tooltip();
votesBlock.removeClass('current'); votesBlock.removeClass('current');
}; }
AwardsHandler.prototype.animateEmoji = function animateEmoji($emoji) { animateEmoji($emoji) {
const className = 'pulse animated once short'; const className = 'pulse animated once short';
$emoji.addClass(className); $emoji.addClass(className);
this.registerEventListener('on', $emoji, animationEndEventString, (e) => { this.registerEventListener('on', $emoji, animationEndEventString, (e) => {
$(e.currentTarget).removeClass(className); $(e.currentTarget).removeClass(className);
}); });
}; }
AwardsHandler.prototype.createEmoji = function createEmoji(votesBlock, emoji) { createEmoji(votesBlock, emoji) {
if ($('.emoji-menu').length) { if ($('.emoji-menu').length) {
this.createAwardButtonForVotesBlock(votesBlock, emoji); this.createAwardButtonForVotesBlock(votesBlock, emoji);
} }
this.createEmojiMenu(() => { this.createEmojiMenu(() => {
this.createAwardButtonForVotesBlock(votesBlock, emoji); this.createAwardButtonForVotesBlock(votesBlock, emoji);
}); });
}; }
AwardsHandler.prototype.postEmoji = function postEmoji($emojiButton, awardUrl, emoji, callback) { postEmoji($emojiButton, awardUrl, emoji, callback) {
if (this.isUserAuthored($emojiButton)) { if (this.isUserAuthored($emojiButton)) {
this.userAuthored($emojiButton); this.userAuthored($emojiButton);
} else { } else {
...@@ -456,13 +454,13 @@ AwardsHandler.prototype.postEmoji = function postEmoji($emojiButton, awardUrl, e ...@@ -456,13 +454,13 @@ AwardsHandler.prototype.postEmoji = function postEmoji($emojiButton, awardUrl, e
} }
}).fail(() => new Flash('Something went wrong on our end.')); }).fail(() => new Flash('Something went wrong on our end.'));
} }
}; }
AwardsHandler.prototype.findEmojiIcon = function findEmojiIcon(votesBlock, emoji) { findEmojiIcon(votesBlock, emoji) {
return votesBlock.find(`.js-emoji-btn [data-name="${emoji}"]`); return votesBlock.find(`.js-emoji-btn [data-name="${emoji}"]`);
}; }
AwardsHandler.prototype.userAuthored = function userAuthored($emojiButton) { userAuthored($emojiButton) {
const oldTitle = this.getAwardTooltip($emojiButton); const oldTitle = this.getAwardTooltip($emojiButton);
const newTitle = 'You cannot vote on your own issue, MR and note'; const newTitle = 'You cannot vote on your own issue, MR and note';
gl.utils.updateTooltipTitle($emojiButton, newTitle).tooltip('show'); gl.utils.updateTooltipTitle($emojiButton, newTitle).tooltip('show');
...@@ -471,29 +469,27 @@ AwardsHandler.prototype.userAuthored = function userAuthored($emojiButton) { ...@@ -471,29 +469,27 @@ AwardsHandler.prototype.userAuthored = function userAuthored($emojiButton) {
$emojiButton.tooltip('hide'); $emojiButton.tooltip('hide');
gl.utils.updateTooltipTitle($emojiButton, oldTitle); gl.utils.updateTooltipTitle($emojiButton, oldTitle);
}, 2800); }, 2800);
}; }
AwardsHandler.prototype.scrollToAwards = function scrollToAwards() { scrollToAwards() {
const options = { const options = {
scrollTop: $('.awards').offset().top - 110, scrollTop: $('.awards').offset().top - 110,
}; };
return $('body, html').animate(options, 200); return $('body, html').animate(options, 200);
}; }
AwardsHandler.prototype.normalizeEmojiName = function normalizeEmojiName(emoji) { normalizeEmojiName(emoji) {
return Object.prototype.hasOwnProperty.call(this.aliases, emoji) ? this.aliases[emoji] : emoji; return Object.prototype.hasOwnProperty.call(this.aliases, emoji) ? this.aliases[emoji] : emoji;
}; }
AwardsHandler addEmojiToFrequentlyUsedList(emoji) {
.prototype
.addEmojiToFrequentlyUsedList = function addEmojiToFrequentlyUsedList(emoji) {
if (isEmojiNameValid(emoji)) { if (isEmojiNameValid(emoji)) {
this.frequentlyUsedEmojis = _.uniq(this.getFrequentlyUsedEmojis().concat(emoji)); this.frequentlyUsedEmojis = _.uniq(this.getFrequentlyUsedEmojis().concat(emoji));
Cookies.set('frequently_used_emojis', this.frequentlyUsedEmojis.join(','), { expires: 365 }); Cookies.set('frequently_used_emojis', this.frequentlyUsedEmojis.join(','), { expires: 365 });
} }
}; }
AwardsHandler.prototype.getFrequentlyUsedEmojis = function getFrequentlyUsedEmojis() { getFrequentlyUsedEmojis() {
return this.frequentlyUsedEmojis || (() => { return this.frequentlyUsedEmojis || (() => {
const frequentlyUsedEmojis = _.uniq((Cookies.get('frequently_used_emojis') || '').split(',')); const frequentlyUsedEmojis = _.uniq((Cookies.get('frequently_used_emojis') || '').split(','));
this.frequentlyUsedEmojis = frequentlyUsedEmojis.filter( this.frequentlyUsedEmojis = frequentlyUsedEmojis.filter(
...@@ -502,9 +498,9 @@ AwardsHandler.prototype.getFrequentlyUsedEmojis = function getFrequentlyUsedEmoj ...@@ -502,9 +498,9 @@ AwardsHandler.prototype.getFrequentlyUsedEmojis = function getFrequentlyUsedEmoj
return this.frequentlyUsedEmojis; return this.frequentlyUsedEmojis;
})(); })();
}; }
AwardsHandler.prototype.setupSearch = function setupSearch() { setupSearch() {
const $search = $('.js-emoji-menu-search'); const $search = $('.js-emoji-menu-search');
this.registerEventListener('on', $search, 'input', (e) => { this.registerEventListener('on', $search, 'input', (e) => {
...@@ -519,9 +515,9 @@ AwardsHandler.prototype.setupSearch = function setupSearch() { ...@@ -519,9 +515,9 @@ AwardsHandler.prototype.setupSearch = function setupSearch() {
this.searchEmojis(''); this.searchEmojis('');
} }
}); });
}; }
AwardsHandler.prototype.searchEmojis = function searchEmojis(term) { searchEmojis(term) {
const $search = $('.js-emoji-menu-search'); const $search = $('.js-emoji-menu-search');
$search.val(term); $search.val(term);
...@@ -537,9 +533,9 @@ AwardsHandler.prototype.searchEmojis = function searchEmojis(term) { ...@@ -537,9 +533,9 @@ AwardsHandler.prototype.searchEmojis = function searchEmojis(term) {
} else { } else {
$('.emoji-menu-content').children().show(); $('.emoji-menu-content').children().show();
} }
}; }
AwardsHandler.prototype.findMatchingEmojiElements = function findMatchingEmojiElements(term) { findMatchingEmojiElements(term) {
const safeTerm = term.toLowerCase(); const safeTerm = term.toLowerCase();
const namesMatchingAlias = []; const namesMatchingAlias = [];
...@@ -555,13 +551,12 @@ AwardsHandler.prototype.findMatchingEmojiElements = function findMatchingEmojiEl ...@@ -555,13 +551,12 @@ AwardsHandler.prototype.findMatchingEmojiElements = function findMatchingEmojiEl
$([]), $([]),
); );
return $matchingElements.closest('li').clone(); return $matchingElements.closest('li').clone();
}; }
AwardsHandler.prototype.destroy = function destroy() { destroy() {
this.eventListeners.forEach((entry) => { this.eventListeners.forEach((entry) => {
entry.element.off.call(entry.element, ...entry.args); entry.element.off.call(entry.element, ...entry.args);
}); });
$('.emoji-menu').remove(); $('.emoji-menu').remove();
}; }
}
export default AwardsHandler;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment