2019-02-18 03:40:07 -05:00
|
|
|
import emojiRegex from 'emoji-regex';
|
2019-12-09 07:07:58 -05:00
|
|
|
import { __ } from '~/locale';
|
2019-06-06 03:39:59 -04:00
|
|
|
import InputValidator from '../validators/input_validator';
|
2019-02-18 03:40:07 -05:00
|
|
|
|
2019-06-06 03:39:59 -04:00
|
|
|
export default class NoEmojiValidator extends InputValidator {
|
2019-02-18 03:40:07 -05:00
|
|
|
constructor(opts = {}) {
|
2019-06-06 03:39:59 -04:00
|
|
|
super();
|
|
|
|
|
2019-02-18 03:40:07 -05:00
|
|
|
const container = opts.container || '';
|
|
|
|
this.noEmojiEmelents = document.querySelectorAll(`${container} .js-block-emoji`);
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
this.noEmojiEmelents.forEach((element) =>
|
2019-02-18 03:40:07 -05:00
|
|
|
element.addEventListener('input', this.eventHandler.bind(this)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventHandler(event) {
|
|
|
|
this.inputDomElement = event.target;
|
|
|
|
this.inputErrorMessage = this.inputDomElement.nextSibling;
|
|
|
|
|
|
|
|
const { value } = this.inputDomElement;
|
|
|
|
|
2019-06-06 03:39:59 -04:00
|
|
|
this.errorMessage = __('Invalid input, please avoid emojis');
|
|
|
|
|
2019-02-18 03:40:07 -05:00
|
|
|
this.validatePattern(value);
|
|
|
|
this.setValidationStateAndMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
validatePattern(value) {
|
|
|
|
const pattern = emojiRegex();
|
2019-06-06 03:39:59 -04:00
|
|
|
this.invalidInput = new RegExp(pattern).test(value);
|
2019-02-18 03:40:07 -05:00
|
|
|
}
|
|
|
|
}
|