trumbowyg.giphy.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. (function ($) {
  2. 'use strict';
  3. $.extend(true, $.trumbowyg, {
  4. langs: {
  5. // jshint camelcase:false
  6. en: {
  7. giphy: 'Insert GIF',
  8. },
  9. fr: {
  10. giphy: 'Insérer un GIF',
  11. },
  12. hu: {
  13. giphy: 'GIF beszúrás',
  14. },
  15. // jshint camelcase:true
  16. }
  17. });
  18. var giphyLogo = '<svg viewBox="0 0 231 53" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2"><path d="M48.32 22.386c0-1.388-.252-1.892-1.767-1.85-3.448.126-6.855.042-10.303.042H25.443c-.927 0-1.346.211-1.305 1.22.085 2.86.085 5.72.043 8.58 0 .883.252 1.169 1.169 1.135 2.018-.084 3.995-.042 6.014 0 1.64 0 4.164-.546 4.752.252.841 1.169.421 3.364.337 5.089-.043.547-.547 1.304-1.094 1.598-2.692 1.556-5.678 2.018-8.747 1.892-5.342-.21-9.336-2.439-11.481-7.527-1.388-3.364-1.725-6.855-1.01-10.43 1.01-4.963 3.407-8.747 8.58-10.051 5.215-1.305 10.136-.547 14.467 2.817 1.219.967 1.798.715 2.691-.294 1.514-1.724 3.154-3.322 4.753-4.963 1.892-1.933 1.892-1.892-.169-3.7C38.429.813 31.238-.617 23.5.224 12.818 1.393 5.248 6.658 1.59 17.045-.177 22.008-.428 27.097.623 32.227c1.682 7.914 5.551 14.12 13.289 17.368 6.898 2.901 14.046 3.448 21.321 1.598 4.331-1.093 8.411-2.608 11.354-6.223 1.136-1.388 1.725-2.902 1.682-4.71l.043-17.873.008-.001zm125.153 3.784l.042-23.046c0-1.136-.168-1.598-1.472-1.556a238.02 238.02 0 0 1-11.017 0c-1.136-.042-1.439.337-1.439 1.439v15.645c0 1.345-.421 1.556-1.641 1.556a422.563 422.563 0 0 0-14.593 0c-1.262.042-1.472-.421-1.439-1.556l.043-15.813c0-.926-.169-1.304-1.17-1.262h-11.513c-.927 0-1.304.169-1.304 1.22v46.764c0 .967.252 1.262 1.219 1.262h11.512c1.169.042 1.262-.462 1.262-1.388l-.042-15.644c0-1.053.251-1.346 1.304-1.346h15.14c1.22 0 1.388.421 1.388 1.472l-.042 15.477c0 1.093.21 1.472 1.388 1.439 3.615-.085 7.233-.085 10.807 0 1.304.042 1.598-.337 1.598-1.598l-.042-23.047.011-.018zM106.565 1.654c-8.369-.211-16.728-.126-25.065-.211-1.346 0-1.767.337-1.767 1.724l.043 23.004v23.215c0 1.009.168 1.439 1.304 1.387a271.22 271.22 0 0 1 11.691 0c1.094 0 1.346-.336 1.346-1.345l-.042-10.64c0-1.052.294-1.345 1.345-1.345 3.322.042 6.645.085 9.967-.085 4.407-.21 8.621-1.219 12.111-4.12 5.551-4.584 7.613-12.701 5.131-20.061-2.313-6.561-8.747-11.354-16.064-11.522v-.001zm-3.028 24.013c-2.818.042-5.594-.043-8.411.042-1.169.042-1.439-.378-1.345-1.439.084-1.556 0-3.069 0-4.626v-5.131c-.043-.841.251-1.094 1.052-1.052 2.986.042 5.929-.085 8.915.042 3.616.126 5.887 2.692 5.846 6.266-.126 3.658-2.313 5.846-6.055 5.887l-.002.011zM229.699 1.569c-4.458 0-8.915-.042-13.415.043-.629 0-1.472.503-1.85 1.052a505.695 505.695 0 0 0-8.957 14.214c-.884 1.472-1.22 1.169-1.977-.084l-8.496-14.089c-.503-.841-1.052-1.136-2.018-1.136l-13.078.043c-.462 0-.967.125-1.439.21.21.378.378.799.629 1.169l17.412 27.167c.462.715.715 1.682.757 2.524v16.653c0 1.052.168 1.514 1.388 1.472 3.784-.084 7.57-.084 11.354 0 1.136.043 1.304-.377 1.304-1.387l-.042-8.58c0-2.734-.084-5.51.042-8.243.043-.926.337-1.933.841-2.649l18.167-27.041c.252-.337.337-.758.547-1.17a3.636 3.636 0 0 0-1.169-.168zM70.104 2.661c0-1.009-.294-1.219-1.262-1.219H57.69c-1.262-.043-1.472.377-1.472 1.513l.042 23.004v23.34c0 1.053.126 1.514 1.346 1.473 3.7-.085 7.444-.043 11.152 0 .966 0 1.387-.085 1.387-1.262l-.042-46.857.001.008z" fill="currentColor" fill-rule="nonzero"/></svg>'; // jshint ignore:line
  19. var CANCEL_EVENT = 'tbwcancel';
  20. // Throttle helper
  21. function trumbowygThrottle(callback, delay) {
  22. var last;
  23. var timer;
  24. return function () {
  25. var context = this;
  26. var now = +new Date();
  27. var args = arguments;
  28. if (last && now < last + delay) {
  29. clearTimeout(timer);
  30. timer = setTimeout(function () {
  31. last = now;
  32. callback.apply(context, args);
  33. }, delay);
  34. } else {
  35. last = now;
  36. callback.apply(context, args);
  37. }
  38. };
  39. }
  40. // Fills modal with response gifs
  41. function renderGifs(response, $giphyModal, trumbowyg, mustEmpty) {
  42. var width = ($giphyModal.width() - 20) / 3;
  43. var html = response.data
  44. .filter(function (gifData) {
  45. return gifData.images.downsized.url !== '';
  46. })
  47. .map(function (gifData) {
  48. var image = gifData.images.downsized,
  49. imageRatio = image.height / image.width;
  50. return '<div class="img-container"><img src=' + image.url + ' width="' + width + '" height="' + imageRatio * width + '" loading="lazy" onload="this.classList.add(\'tbw-loaded\')"/></div>';
  51. })
  52. .join('')
  53. ;
  54. if (mustEmpty === true) {
  55. if (html.length === 0) {
  56. if ($('.' + trumbowyg.o.prefix + 'giphy-no-result', $giphyModal).length > 0) {
  57. return;
  58. }
  59. html = '<img class="' + trumbowyg.o.prefix + 'giphy-no-result" src="' + trumbowyg.o.plugins.giphy.noResultGifUrl + '"/>';
  60. }
  61. $giphyModal.empty();
  62. }
  63. $giphyModal.append(html);
  64. $('img', $giphyModal).on('click', function () {
  65. trumbowyg.restoreRange();
  66. trumbowyg.execCmd('insertImage', $(this).attr('src'), false, true);
  67. $('img', $giphyModal).off();
  68. trumbowyg.closeModal();
  69. });
  70. }
  71. var defaultOptions = {
  72. rating: 'g',
  73. apiKey: null,
  74. throttleDelay: 300,
  75. noResultGifUrl: 'https://media.giphy.com/media/2Faz9FbRzmwxY0pZS/giphy.gif'
  76. };
  77. // Add dropdown with font sizes
  78. $.extend(true, $.trumbowyg, {
  79. plugins: {
  80. giphy: {
  81. init: function (trumbowyg) {
  82. trumbowyg.o.plugins.giphy = $.extend({},
  83. defaultOptions,
  84. trumbowyg.o.plugins.giphy || {}
  85. );
  86. trumbowyg.addBtnDef('giphy', {
  87. fn: function() {
  88. if (trumbowyg.o.plugins.giphy.apiKey === null) {
  89. throw new Error('You must set a Giphy API Key');
  90. }
  91. var BASE_URL = 'https://api.giphy.com/v1/gifs/search?api_key=' + trumbowyg.o.plugins.giphy.apiKey + '&rating=' + trumbowyg.o.plugins.giphy.rating,
  92. DEFAULT_URL = BASE_URL.replace('/search', '/trending');
  93. var previousAjaxCall = {abort: function () {}};
  94. var prefix = trumbowyg.o.prefix;
  95. // Create and open the modal
  96. var searchInput = '<input name="" class="' + prefix + 'giphy-search" placeholder="Search a GIF" autofocus="autofocus">',
  97. closeButton = '<button class="' + prefix + 'giphy-close" title="' + trumbowyg.lang.close + '"><svg><use xlink:href="' + trumbowyg.svgPath + '#' + prefix + 'close"/></svg></button>',
  98. poweredByGiphy = '<div class="' + prefix + 'powered-by-giphy"><span>Powered by</span>' + giphyLogo + '</div>',
  99. giphyModalHtml = searchInput + closeButton + poweredByGiphy + '<div class="' + prefix + 'giphy-modal-scroll"><div class="' + prefix + 'giphy-modal"></div></div>';
  100. trumbowyg
  101. .openModal(null, giphyModalHtml, false)
  102. .one(CANCEL_EVENT, function () {
  103. try {
  104. previousAjaxCall.abort();
  105. } catch (e) {}
  106. trumbowyg.closeModal();
  107. });
  108. var $giphyInput = $('.' + prefix + 'giphy-search'),
  109. $giphyClose = $('.' + prefix + 'giphy-close'),
  110. $giphyModal = $('.' + prefix + 'giphy-modal');
  111. var ajaxError = function () {
  112. if (!navigator.onLine && !$('.' + prefix + 'giphy-offline', $giphyModal).length) {
  113. $giphyModal.empty();
  114. $giphyModal.append('<p class="' + prefix + 'giphy-offline">You are offline</p>');
  115. }
  116. };
  117. // Load trending gifs as default
  118. $.ajax({
  119. url: DEFAULT_URL,
  120. dataType: 'json',
  121. success: function(response) {
  122. renderGifs(response, $giphyModal, trumbowyg, true);
  123. },
  124. error: ajaxError
  125. });
  126. var searchGifsOnInput = function () {
  127. var query = $giphyInput.val();
  128. if (query.length === 0) {
  129. return;
  130. }
  131. try {
  132. previousAjaxCall.abort();
  133. } catch (e) {}
  134. previousAjaxCall = $.ajax({
  135. url: BASE_URL + '&q=' + encodeURIComponent(query),
  136. dataType: 'json',
  137. success: function(response) {
  138. renderGifs(response, $giphyModal, trumbowyg, true);
  139. },
  140. error: ajaxError
  141. });
  142. };
  143. var throttledInputRequest = trumbowygThrottle(searchGifsOnInput, trumbowyg.o.plugins.giphy.throttleDelay);
  144. $giphyInput.on('input', throttledInputRequest);
  145. $giphyInput.focus();
  146. $giphyClose.one('click', function() {
  147. $giphyModal.trigger(CANCEL_EVENT);
  148. });
  149. },
  150. });
  151. }
  152. }
  153. }
  154. });
  155. })(jQuery);