-
Notifications
You must be signed in to change notification settings - Fork 2
/
giphytinymce.js
66 lines (55 loc) · 2.57 KB
/
giphytinymce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var giphytinymce = {
searchInputStringSelector: '#gifsearch',
ouputListStringSelector: '#giflisting',
keywordTranslation: 'Mots clés',
searchTranslation: 'Chercher un gif',
modalStringSelector: '.bs-modal-lg',
apiBase: 'https://api.giphy.com/v1',
apiKey: 'dc6zaTOxFJmzC',
giphySearch: function(searchTerm) {
uri = '${apiBase}/gifs/search?q=${encodedSearch}&api_key=${apiKey}';
uri = uri.replace("${apiBase}", giphytinymce.apiBase);
uri = uri.replace("${apiKey}", giphytinymce.apiKey);
uri = uri.replace("${encodedSearch}", encodeURIComponent(searchTerm));
$.get(uri, giphytinymce.outputResult);
},
outputResult: function(results){
$.each(results.data, function(index, element) {
$(giphytinymce.ouputListStringSelector).append('<li><img src="' + element.images.original.url + '" /></li>')
});
$(giphytinymce.ouputListStringSelector).find('li img').off().on('click', giphytinymce.bindGif);
$('#infoGif').hide();
},
bindGif: function(){
imgpath = $(this).attr('src');
tinyMCE.execCommand('mceInsertContent', false, '[img]' + imgpath + '[/img]');
$(giphytinymce.modalStringSelector).modal('hide');
},
bindSearchInput: function(){
$(giphytinymce.searchInputStringSelector).keyup(function() {
if (typeof timeoutID !== 'undefined') {
clearTimeout(timeoutID);
}
var keyword = $(this).val();
timeoutID = setTimeout(function() { giphytinymce.giphySearch(keyword); }, 1000);
});
},
init: function(){
_this = this;
modalGif = $(_this.modalStringSelector);
gifsearch = _this.searchInputStringSelector.replace('#', '');
giflisting = _this.ouputListStringSelector.replace('#', '');
// modal not used yet
if (modalGif.find('.modal-content').find(_this.searchInputStringSelector)) {
modalGif.find('.modal-content').empty();
modalGif.find('.modal-content').append('<input type="text" class="form-control input-block" id="' + gifsearch + '" placeholder="' + _this.keywordTranslation + '" />');
modalGif.find('.modal-content').append('<ul id="' + giflisting + '"></ul>');
modalGif.find('.modal-content').append('<div class="row-fluid" id="infoGif"><div class="alert alert-warning" role="alert">' + _this.searchTranslation + '</div></div>');
modalGif.modal('show');
_this.bindSearchInput();
}
}
}
function tinymce_button_giphy(){
giphytinymce.init();
}