1+
2+ var stickerpipe = null ;
3+
4+ function setupStickerPipe ( ) {
5+
6+ var stickerpipeConfig = config . stickerpipe ,
7+
8+ $stickersBtn = $ ( '#' + stickerpipeConfig . elId + ' i' ) ,
9+ $messageText = $ ( '#message_text' ) ;
10+
11+ stickerpipeConfig . userId = currentUser . id ;
12+
13+ stickerpipe = new Stickers ( stickerpipeConfig ) ;
14+
15+ stickerpipe . render ( function ( ) {
16+
17+ this . stickerpipe . onClickSticker ( ( function ( stickerCode ) {
18+ sendMessage ( stickerCode , null ) ;
19+ } ) . bind ( this ) ) ;
20+
21+ this . stickerpipe . onClickEmoji ( function ( emoji ) {
22+ console . log ( 'click on emoji' , emoji ) ;
23+ $messageText . focus ( ) ;
24+ pasteHtmlAtCaret ( stickerpipe . parseEmojiFromText ( emoji ) ) ;
25+ } ) ;
26+
27+ } ) ;
28+
29+ stickerpipe . onPurchase ( ( function ( packName , packTitle , pricePoint ) {
30+
31+ try {
32+ // do purchase transaction ...
33+ if ( confirm ( 'Do you want buy pack "' + packName + '"?' ) ) {
34+ stickerpipe . purchaseSuccess ( packName , pricePoint ) ;
35+ } else {
36+ stickerpipe . purchaseFail ( ) ;
37+ }
38+ } catch ( e ) {
39+ stickerpipe . purchaseFail ( ) ;
40+ }
41+ } ) . bind ( this ) ) ;
42+
43+ window . addEventListener ( 'sp:popover:shown' , function ( ) {
44+ $stickersBtn . addClass ( 'active' ) ;
45+ } ) ;
46+
47+ window . addEventListener ( 'sp:popover:hidden' , function ( ) {
48+ $stickersBtn . removeClass ( 'active' ) ;
49+ } ) ;
50+
51+ }
52+
53+ function pasteHtmlAtCaret ( html ) {
54+ var sel , range ;
55+ if ( window . getSelection ) {
56+ // IE9 and non-IE
57+ sel = window . getSelection ( ) ;
58+ if ( sel . getRangeAt && sel . rangeCount ) {
59+ range = sel . getRangeAt ( 0 ) ;
60+ range . deleteContents ( ) ;
61+
62+ // Range.createContextualFragment() would be useful here but is
63+ // only relatively recently standardized and is not supported in
64+ // some browsers (IE9, for one)
65+ var el = document . createElement ( "div" ) ;
66+ el . innerHTML = html ;
67+ var frag = document . createDocumentFragment ( ) , node , lastNode ;
68+ while ( ( node = el . firstChild ) ) {
69+ lastNode = frag . appendChild ( node ) ;
70+ }
71+ var firstNode = frag . firstChild ;
72+ range . insertNode ( frag ) ;
73+
74+ // Preserve the selection
75+ if ( lastNode ) {
76+ range = range . cloneRange ( ) ;
77+ range . setStartAfter ( lastNode ) ;
78+ range . collapse ( true ) ;
79+ sel . removeAllRanges ( ) ;
80+ sel . addRange ( range ) ;
81+ }
82+ }
83+ } else if ( ( sel = document . selection ) && sel . type != "Control" ) {
84+ // IE < 9
85+ var originalRange = sel . createRange ( ) ;
86+ originalRange . collapse ( true ) ;
87+ sel . createRange ( ) . pasteHTML ( html ) ;
88+ }
89+ }
0 commit comments