1/**
2 * Autocomplete script for Emoji plugin
3 *
4 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author      Patrick Brown <ptbrown@whoopdedo.org>
6 */
7/* DOKUWIKI:include_once script/jquery.textcomplete.js */
8
9+function(){
10'use strict';
11
12var byLengthCompare = function(a,b) { return a.length>b.length; };
13var sortByLength = function(a) {
14    return a.sort(byLengthCompare);
15};
16
17jQuery(function(){
18
19    var $editForm = jQuery('#wiki__text');
20    if($editForm.length) {
21        jQuery.getJSON(DOKU_BASE + 'lib/plugins/emoji/assets/emoji_strategy.json',
22              function(emojiStrategy) {
23                    var assetUri = 'https://cdn.jsdelivr.net/emojione/assets/png/';
24                    var cacheBustParam = '?v=2.2.7';
25                    var langFooter = '<a href="https://www.emojicopy.com" target="_blank">&nbsp;' +
26                                      LANG.plugins.emoji.browseall +
27                                      '<span class="arrow">&#10697;</span></a>';
28                    if(emoji_assetsrc) {
29                        assetUri = emoji_assetsrc + 'assets/png/';
30                    }
31                    $editForm.textcomplete([{
32                        match: /(^|\s):([\-+]?[\-+\w]+)$/,
33                        index: 2,
34                        /* TODO disable where emoji is not allowed (code blocks, headings, etc)
35                        context: function(text) {
36                        },
37                        */
38                        search: function(term, addTerm) {
39                            var names = [], aliases = [], keywords = [];
40                            jQuery.each(emojiStrategy, function(shortname, data) {
41                                if(shortname.indexOf(term) > -1) {
42                                    names.push(shortname);
43                                } else if((data.aliases !== null) && (data.aliases.indexOf(term) > -1)) {
44                                    aliases.push(shortname);
45                                } else if((data.keywords !== null) && (data.keywords.indexOf(term) > -1)) {
46                                    keywords.push(shortname);
47                                }
48                            });
49
50                            if(term.length >= 3) {
51                                sortByLength(names);
52                                sortByLength(aliases);
53                                keywords.sort();
54                            }
55                            addTerm(names.concat(aliases).concat(keywords));
56                        },
57                        template: function(shortname) {
58                            var fileName = assetUri + emojiStrategy[shortname].unicode + '.png' + cacheBustParam;
59                            return '<img class="emojione" src="' + fileName + '"/> :' + shortname + ':';
60                        },
61                        replace: function(shortname) {
62                            return '$1:' + shortname + ': ';
63                        },
64                        cache: true
65                    }], { footer: langFooter });
66              });
67    }
68
69});
70}();
71