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

87 lines
3 KiB
JavaScript
Raw Normal View History

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';
2019-03-02 22:35:43 +05:30
import emojiRegex from 'emoji-regex';
2020-01-01 13:55:28 +05:30
import createFlash from '~/flash';
2018-11-18 11:00:15 +05:30
import EmojiMenu from './emoji_menu';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2018-11-18 11:00:15 +05:30
2018-11-20 20:47:30 +05:30
const defaultStatusEmoji = 'speech_balloon';
2018-11-18 11:00:15 +05:30
document.addEventListener('DOMContentLoaded', () => {
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-12-05 23:21:45 +05:30
const toggleNoEmojiPlaceholder = isVisible => {
2018-11-20 20:47:30 +05:30
const placeholderElement = document.getElementById('js-no-emoji-placeholder');
placeholderElement.classList.toggle('hidden', !isVisible);
};
const findStatusEmoji = () => toggleEmojiMenuButton.querySelector('gl-emoji');
2018-11-18 11:00:15 +05:30
const removeStatusEmoji = () => {
2018-11-20 20:47:30 +05:30
const statusEmoji = findStatusEmoji();
2018-11-18 11:00:15 +05:30
if (statusEmoji) {
statusEmoji.remove();
}
};
const selectEmojiCallback = (emoji, emojiTag) => {
statusEmojiField.value = emoji;
2018-11-20 20:47:30 +05:30
toggleNoEmojiPlaceholder(false);
2018-11-18 11:00:15 +05:30
removeStatusEmoji();
toggleEmojiMenuButton.innerHTML += emojiTag;
};
const clearEmojiButton = document.getElementById('js-clear-user-status-button');
clearEmojiButton.addEventListener('click', () => {
statusEmojiField.value = '';
statusMessageField.value = '';
removeStatusEmoji();
2018-11-20 20:47:30 +05:30
toggleNoEmojiPlaceholder(true);
2018-11-18 11:00:15 +05:30
});
const emojiAutocomplete = new GfmAutoComplete();
emojiAutocomplete.setup($(statusMessageField), { emojis: true });
2019-03-02 22:35:43 +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
2019-07-31 22:56:46 +05:30
userNameInput.setCustomValidity(__('Invalid field'));
2019-03-02 22:35:43 +05:30
} else {
userNameInput.setCustomValidity('');
}
});
2018-11-18 11:00:15 +05:30
import(/* webpackChunkName: 'emoji' */ '~/emoji')
.then(Emoji => {
const emojiMenu = new EmojiMenu(
Emoji,
toggleEmojiMenuButtonSelector,
'js-status-emoji-menu',
selectEmojiCallback,
);
emojiMenu.bindEvents();
2018-11-20 20:47:30 +05:30
const defaultEmojiTag = Emoji.glEmojiTag(defaultStatusEmoji);
statusMessageField.addEventListener('input', () => {
const hasStatusMessage = statusMessageField.value.trim() !== '';
const statusEmoji = findStatusEmoji();
if (hasStatusMessage && statusEmoji) {
return;
}
if (hasStatusMessage) {
toggleNoEmojiPlaceholder(false);
toggleEmojiMenuButton.innerHTML += defaultEmojiTag;
} else if (statusEmoji.dataset.name === defaultStatusEmoji) {
toggleNoEmojiPlaceholder(true);
removeStatusEmoji();
}
});
2018-11-18 11:00:15 +05:30
})
2019-07-31 22:56:46 +05:30
.catch(() => createFlash(__('Failed to load emoji list.')));
2018-11-18 11:00:15 +05:30
});