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
2016-09-01 06:32:17 +00:00
EmojiConvertor . prototype . init _env = function ( ) {
if ( this . inits . env ) {
return ;
}
2016-09-01 19:21:23 +00:00
this . inits . env = 1 ;
2016-09-01 06:32:17 +00:00
this . include _title = true ;
2017-03-08 00:54:46 +00:00
this . img _sets . apple . path = 'images/emoji/apple/' ;
2016-09-01 06:32:17 +00:00
this . replace _mode = 'img' ;
2016-09-01 19:21:23 +00:00
} ;
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' ;
}
} ;
// A stripped-down version of the original: https://github.com/WhisperSystems/Signal-Desktop/blob/aed573562018462fbacd8f2f715e9daeddcde0dd/components/emojijs/lib/emoji.js#L323-L396
// One primary change - we inject the second parameter as an additional class
EmojiConvertor . prototype . replacement = function ( idx , sizeClass , actual , wrapper , variation ) {
var self = this ;
var img _set = self . img _set ;
var extra = '' ;
var variation _idx = 0 ;
if ( typeof variation === 'object' ) {
extra = self . replacement ( variation . idx , null , variation . actual , variation . wrapper ) ;
variation _idx = idx + '-' + variation . idx ;
}
var img = self . data [ idx ] [ 7 ] || self . img _sets [ img _set ] . path + idx + '.png' + self . img _suffix ;
var title = self . include _title ? ' title="' + ( actual || self . data [ idx ] [ 3 ] [ 0 ] ) + '"' : '' ;
if ( variation _idx && self . variations _data [ variation _idx ] && self . variations _data [ variation _idx ] [ 2 ] && ! self . data [ idx ] [ 7 ] ) {
if ( self . variations _data [ variation _idx ] [ 2 ] & self . img _sets [ self . img _set ] . mask ) {
img = self . img _sets [ self . img _set ] . path + variation _idx + '.png' ;
extra = '' ;
}
}
return '<img src="' + img + '" class="emoji' + ( sizeClass ? ' ' + sizeClass : '' ) + '"' + title + '/>' ;
} ;
// Modeled after the original: https://github.com/WhisperSystems/Signal-Desktop/blob/aed573562018462fbacd8f2f715e9daeddcde0dd/components/emojijs/lib/emoji.js#L265-L286
2016-09-01 19:21:23 +00:00
EmojiConvertor . prototype . replace _unified = function ( str ) {
var self = this ;
self . init _unified ( ) ;
2017-05-10 01:36:39 +00:00
var sizeClass = self . getSizeClass ( str ) ;
2016-09-01 19:21:23 +00:00
return str . replace ( self . rx _unified , function ( m , p1 , p2 ) {
var val = self . map . unified [ p1 ] ;
if ( ! val ) { return m ; }
var idx = null ;
if ( p2 == '\uD83C\uDFFB' ) { idx = '1f3fb' ; }
if ( p2 == '\uD83C\uDFFC' ) { idx = '1f3fc' ; }
if ( p2 == '\uD83C\uDFFD' ) { idx = '1f3fd' ; }
if ( p2 == '\uD83C\uDFFE' ) { idx = '1f3fe' ; }
if ( p2 == '\uD83C\uDFFF' ) { idx = '1f3ff' ; }
if ( idx ) {
2017-05-10 01:36:39 +00:00
return self . replacement ( val , sizeClass , null , null , {
2016-09-01 19:21:23 +00:00
idx : idx ,
actual : p2 ,
wrapper : ':'
} ) ;
}
// wrap names in :'s
2017-05-10 01:36:39 +00:00
return self . replacement ( val , sizeClass , ':' + self . data [ val ] [ 3 ] [ 0 ] + ':' ) ;
2016-09-01 19:21:23 +00:00
} ) ;
2016-09-01 06:32:17 +00:00
} ;
window . emoji = new EmojiConvertor ( ) ;
emoji . init _colons ( ) ;
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
2016-09-01 20:31:36 +00:00
$el . html ( emoji . replace _unified ( $el . html ( ) ) ) ;
2016-09-01 06:32:17 +00:00
} ;
2016-02-15 21:53:37 +00:00
} ) ( ) ;