2016-02-15 21:53:37 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
|
|
|
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.emoji_util = window.emoji_util || {};
|
|
|
|
|
2016-09-01 19:21:23 +00:00
|
|
|
// EmojiConverter overrides
|
2017-05-10 01:36:39 +00:00
|
|
|
EmojiConvertor.prototype.getCountOfAllMatches = function(str, regex) {
|
|
|
|
var match = regex.exec(str);
|
|
|
|
var count = 0;
|
|
|
|
|
2017-05-10 22:28:22 +00:00
|
|
|
if (!regex.global) {
|
|
|
|
return match ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2017-05-10 01:36:39 +00:00
|
|
|
while (match) {
|
|
|
|
count += 1;
|
|
|
|
match = regex.exec(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
|
|
|
EmojiConvertor.prototype.hasNormalCharacters = function(str) {
|
|
|
|
var self = this;
|
|
|
|
var noEmoji = str.replace(self.rx_unified, '').trim();
|
|
|
|
return noEmoji.length > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
EmojiConvertor.prototype.getSizeClass = function(str) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self.hasNormalCharacters(str)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
var emojiCount = self.getCountOfAllMatches(str, self.rx_unified);
|
|
|
|
if (emojiCount > 8) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
else if (emojiCount > 6) {
|
|
|
|
return 'small';
|
|
|
|
}
|
|
|
|
else if (emojiCount > 4) {
|
|
|
|
return 'medium';
|
|
|
|
}
|
|
|
|
else if (emojiCount > 2) {
|
|
|
|
return 'large';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'jumbo';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-15 20:22:11 +00:00
|
|
|
var imgClass = /(<img [^>]+ class="emoji)(")/g;
|
|
|
|
EmojiConvertor.prototype.addClass = function(text, sizeClass) {
|
|
|
|
if (!sizeClass) {
|
|
|
|
return text;
|
2017-05-10 01:36:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 20:22:11 +00:00
|
|
|
return text.replace(imgClass, function(match, before, after) {
|
|
|
|
return before + ' ' + sizeClass + after;
|
|
|
|
});
|
2017-05-10 01:36:39 +00:00
|
|
|
};
|
|
|
|
|
2017-09-15 20:22:11 +00:00
|
|
|
var imgTitle = /(<img [^>]+ class="emoji[^>]+ title=")([^:">]+)(")/g;
|
|
|
|
EmojiConvertor.prototype.ensureTitlesHaveColons = function(text) {
|
|
|
|
return text.replace(imgTitle, function(match, before, title, after) {
|
|
|
|
return before + ':' + title + ':' + after;
|
2016-09-01 19:21:23 +00:00
|
|
|
});
|
2016-09-01 06:32:17 +00:00
|
|
|
};
|
2017-09-15 20:22:11 +00:00
|
|
|
|
|
|
|
EmojiConvertor.prototype.signalReplace = function(str) {
|
|
|
|
var sizeClass = this.getSizeClass(str);
|
|
|
|
|
|
|
|
var text = this.replace_unified(str);
|
|
|
|
text = this.addClass(text, sizeClass);
|
|
|
|
|
|
|
|
return this.ensureTitlesHaveColons(text);
|
|
|
|
};
|
|
|
|
|
2016-09-01 06:32:17 +00:00
|
|
|
window.emoji = new EmojiConvertor();
|
|
|
|
emoji.init_colons();
|
2017-09-15 20:22:11 +00:00
|
|
|
emoji.img_sets.apple.path = 'node_modules/emoji-datasource-apple/img/apple/64/';
|
|
|
|
emoji.include_title = true;
|
|
|
|
emoji.replace_mode = 'img';
|
|
|
|
emoji.supports_css = false; // needed to avoid spans with background-image
|
2016-09-01 06:32:17 +00:00
|
|
|
|
|
|
|
window.emoji_util.parse = function($el) {
|
2016-09-01 21:01:51 +00:00
|
|
|
if (!$el || !$el.length) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 01:36:39 +00:00
|
|
|
|
2017-09-15 20:22:11 +00:00
|
|
|
$el.html(emoji.signalReplace($el.html()));
|
2016-09-01 06:32:17 +00:00
|
|
|
};
|
|
|
|
|
2016-02-15 21:53:37 +00:00
|
|
|
})();
|