debian-mirror-gitlab/app/assets/javascripts/pages/profiles/show/index.js

89 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import emojiRegex from 'emoji-regex';
2018-11-18 11:00:15 +05:30
import $ from 'jquery';
2019-07-31 22:56:46 +05:30
import GfmAutoComplete from 'ee_else_ce/gfm_auto_complete';
2021-03-11 19:13:27 +05:30
import * as Emoji from '~/emoji';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2021-03-11 19:13:27 +05:30
import EmojiMenu from './emoji_menu';
2018-11-18 11:00:15 +05:30
2018-11-20 20:47:30 +05:30
const defaultStatusEmoji = 'speech_balloon';
2021-04-29 21:17:54 +05:30
const toggleEmojiMenuButtonSelector = '.js-toggle-emoji-menu';
const toggleEmojiMenuButton = document.querySelector(toggleEmojiMenuButtonSelector);
const statusEmojiField = document.getElementById('js-status-emoji-field');
const statusMessageField = document.getElementById('js-status-message-field');
2018-11-20 20:47:30 +05:30
2021-04-29 21:17:54 +05:30
const toggleNoEmojiPlaceholder = (isVisible) => {
const placeholderElement = document.getElementById('js-no-emoji-placeholder');
placeholderElement.classList.toggle('hidden', !isVisible);
};
2018-11-18 11:00:15 +05:30
2021-04-29 21:17:54 +05:30
const findStatusEmoji = () => toggleEmojiMenuButton.querySelector('gl-emoji');
const removeStatusEmoji = () => {
const statusEmoji = findStatusEmoji();
if (statusEmoji) {
statusEmoji.remove();
}
};
2018-11-20 20:47:30 +05:30
2021-04-29 21:17:54 +05:30
const selectEmojiCallback = (emoji, emojiTag) => {
statusEmojiField.value = emoji;
toggleNoEmojiPlaceholder(false);
removeStatusEmoji();
toggleEmojiMenuButton.innerHTML += emojiTag;
};
2018-11-18 11:00:15 +05:30
2021-04-29 21:17:54 +05:30
const clearEmojiButton = document.getElementById('js-clear-user-status-button');
clearEmojiButton.addEventListener('click', () => {
statusEmojiField.value = '';
statusMessageField.value = '';
removeStatusEmoji();
toggleNoEmojiPlaceholder(true);
});
2018-11-18 11:00:15 +05:30
2021-04-29 21:17:54 +05:30
const emojiAutocomplete = new GfmAutoComplete();
emojiAutocomplete.setup($(statusMessageField), { emojis: true });
2018-11-18 11:00:15 +05:30
2021-04-29 21:17:54 +05:30
const userNameInput = document.getElementById('user_name');
userNameInput.addEventListener('input', () => {
const EMOJI_REGEX = emojiRegex();
if (EMOJI_REGEX.test(userNameInput.value)) {
// set field to invalid so it gets detected by GlFieldErrors
userNameInput.setCustomValidity(__('Invalid field'));
} else {
userNameInput.setCustomValidity('');
}
});
2019-03-02 22:35:43 +05:30
2021-04-29 21:17:54 +05:30
Emoji.initEmojiMap()
.then(() => {
const emojiMenu = new EmojiMenu(
Emoji,
toggleEmojiMenuButtonSelector,
'js-status-emoji-menu',
selectEmojiCallback,
);
emojiMenu.bindEvents();
2018-11-20 20:47:30 +05:30
2021-04-29 21:17:54 +05:30
const defaultEmojiTag = Emoji.glEmojiTag(defaultStatusEmoji);
statusMessageField.addEventListener('input', () => {
const hasStatusMessage = statusMessageField.value.trim() !== '';
const statusEmoji = findStatusEmoji();
if (hasStatusMessage && statusEmoji) {
return;
}
2018-11-20 20:47:30 +05:30
2021-04-29 21:17:54 +05:30
if (hasStatusMessage) {
toggleNoEmojiPlaceholder(false);
toggleEmojiMenuButton.innerHTML += defaultEmojiTag;
} else if (statusEmoji.dataset.name === defaultStatusEmoji) {
toggleNoEmojiPlaceholder(true);
removeStatusEmoji();
}
});
})
2021-09-04 01:27:46 +05:30
.catch(() =>
createFlash({
message: __('Failed to load emoji list.'),
}),
);