emoji.js 2.68 KB
Newer Older
Madhur's avatar
Madhur committed
1 2
(function()
{
Madhur's avatar
Madhur committed
3

Madhur's avatar
Madhur committed
4 5 6 7 8
    function emoji()
        {}
        // settings
    emoji.inits = {};
    emoji.map = {};
Madhur's avatar
Madhur committed
9

Madhur's avatar
Madhur committed
10 11 12 13 14 15 16 17
    emoji.replace_unified = function(str)
    {
        emoji.init_unified();
        return str.replace(emoji.rx_unified, function(m)
        {
            // var val = emoji.map.unified[m];
            var val = Config.reversemap[m];
            //console.log(val);
Madhur's avatar
Madhur committed
18

Madhur's avatar
Madhur committed
19 20 21
            if (val)
            {
                val = ":" + val + ":";
Madhur's avatar
Madhur committed
22

Madhur's avatar
Madhur committed
23 24 25 26 27 28 29 30
                var $img = $.emojiarea.createIcon($.emojiarea.icons[val]);
                return $img;
            }
            else
                return "";
            // return val ? emoji.replacement(val) : m;
        });
    };
Madhur's avatar
Madhur committed
31

Madhur's avatar
Madhur committed
32 33 34 35 36 37
    emoji.init_emoticons = function()
    {
        if (emoji.inits.emoticons)
            return;
        emoji.init_colons(); // we require this for the emoticons map
        emoji.inits.emoticons = 1;
Madhur's avatar
Madhur committed
38

Madhur's avatar
Madhur committed
39 40 41 42 43 44 45 46
        var a = [];
        emoji.map.emoticons = {};
        for (var i in emoji.emoticons_data)
        {
            // because we never see some characters in our text except as
            // entities, we must do some replacing
            var emoticon = i.replace(/\&/g, '&amp;').replace(/\</g, '&lt;')
                .replace(/\>/g, '&gt;');
Madhur's avatar
Madhur committed
47

Madhur's avatar
Madhur committed
48 49
            if (!emoji.map.colons[emoji.emoticons_data[i]])
                continue;
Madhur's avatar
Madhur committed
50

Madhur's avatar
Madhur committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            emoji.map.emoticons[emoticon] = emoji.map.colons[emoji.emoticons_data[i]];
            a.push(emoji.escape_rx(emoticon));
        }
        emoji.rx_emoticons = new RegExp(
            ('(^|\\s)(' + a.join('|') + ')(?=$|[\\s|\\?\\.,!])'), 'g');
    };
    emoji.init_colons = function()
    {
        if (emoji.inits.colons)
            return;
        emoji.inits.colons = 1;
        emoji.rx_colons = new RegExp('\:[^\\s:]+\:', 'g');
        emoji.map.colons = {};
        for (var i in emoji.data)
        {
            for (var j = 0; j < emoji.data[i][3].length; j++)
            {
                emoji.map.colons[emoji.data[i][3][j]] = i;
            }
        }
    };
    emoji.init_unified = function()
    {
        if (emoji.inits.unified)
            return;
        emoji.inits.unified = 1;
Madhur's avatar
Madhur committed
77

Madhur's avatar
Madhur committed
78 79
        var a = [];
        emoji.map.unified = {};
Madhur's avatar
Madhur committed
80

Madhur's avatar
Madhur committed
81 82 83 84 85 86 87 88
        for (var i in emoji.data)
        {
            for (var j = 0; j < emoji.data[i][0].length; j++)
            {
                a.push(emoji.data[i][0][j]);
                emoji.map.unified[emoji.data[i][0][j]] = i;
            }
        }
Madhur's avatar
Madhur committed
89

Madhur's avatar
Madhur committed
90 91 92 93 94 95
        emoji.rx_unified = new RegExp('(' + a.join('|') + ')', "g");
    };
    emoji.escape_rx = function(text)
    {
        return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
    };
Madhur's avatar
Madhur committed
96

Madhur's avatar
Madhur committed
97 98 99
}).call(function()
{
    return this || (typeof window !== 'undefined' ? window : global);
Madhur's avatar
Madhur committed
100
}());