emojipicker.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. if(typeof jQuery !== 'undefined'){
  2. (function ($, win) {
  3. 'use strict';
  4. var emoji = lsxEmojiData;
  5. var settings = {};
  6. $.fn.lsxEmojiPicker = function(options) {
  7. if(options == "destroy") {
  8. $(this).off("click");
  9. $(this).children().remove();
  10. }
  11. else {
  12. settings = $.extend({
  13. width: 265,
  14. height: 200,
  15. twemoji: false,
  16. closeOnSelect: true,
  17. onSelect: function(em){}
  18. }, options);
  19. var appender = $('<div></div>')
  20. .addClass('lsx-emojipicker-appender');
  21. var container = $('<div></div>')
  22. .addClass('lsx-emojipicker-container')
  23. var wrapper = $('<div></div>')
  24. .addClass('lsx-emojipicker-wrapper');
  25. var spinnerContainer = $('<div></div>')
  26. .addClass('spinner-container');
  27. var spinner = $('<div></div>')
  28. .addClass('loader');
  29. spinnerContainer.append(spinner);
  30. var customemojis = JSON.parse(localStorage.current_custom_emojis);
  31. var addhide = "";
  32. if(customemojis.length > 0) {
  33. addhide = " hidden";
  34. var emojiCustomContainer = $('<div></div>')
  35. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-custom')
  36. .css({'width': settings.width, 'height': settings.height});
  37. }
  38. var emojiPeopleContainer = $('<div></div>')
  39. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-people'+addhide)
  40. .css({'width': settings.width, 'height': settings.height});
  41. var emojiNatureContainer = $('<div></div>')
  42. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-nature hidden')
  43. .css({'width': settings.width, 'height': settings.height});
  44. var emojiFoodsContainer = $('<div></div>')
  45. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-foods hidden')
  46. .css({'width': settings.width, 'height': settings.height});
  47. var emojiActivityContainer = $('<div></div>')
  48. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-activity hidden')
  49. .css({'width': settings.width, 'height': settings.height});
  50. var emojiPlacesContainer = $('<div></div>')
  51. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-places hidden')
  52. .css({'width': settings.width, 'height': settings.height});
  53. var emojiObjectsContainer = $('<div></div>')
  54. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-objects hidden')
  55. .css({'width': settings.width, 'height': settings.height});
  56. var emojiSymbolsContainer = $('<div></div>')
  57. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-symbols hidden')
  58. .css({'width': settings.width, 'height': settings.height});
  59. var emojiFlagsContainer = $('<div></div>')
  60. .addClass('lsx-emojipicker-emoji lsx-emoji-tab lsx-emoji-flags hidden')
  61. .css({'width': settings.width, 'height': settings.height});
  62. var tabs = $('<ul></ul>')
  63. .addClass('lsx-emojipicker-tabs');
  64. if(customemojis.length > 0) {
  65. var customEmoji = $('<li></li>')
  66. .addClass('selected')
  67. .html("&#x2A;&#x20E3")
  68. .click(function(e){
  69. e.preventDefault();
  70. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  71. $(this).addClass('selected');
  72. $('.lsx-emoji-tab').addClass('hidden');
  73. emojiCustomContainer.removeClass('hidden');
  74. });
  75. }
  76. var peopleEmoji = $('<li></li>')
  77. .html("😀")
  78. .click(function(e){
  79. e.preventDefault();
  80. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  81. $(this).addClass('selected');
  82. $('.lsx-emoji-tab').addClass('hidden');
  83. emojiPeopleContainer.removeClass('hidden');
  84. });
  85. if(customemojis.length == 0) {
  86. peopleEmoji.addClass("selected");
  87. }
  88. var natureEmoji = $('<li></li>')
  89. .html("🐶")
  90. .click(function(e){
  91. e.preventDefault();
  92. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  93. $(this).addClass('selected');
  94. $('.lsx-emoji-tab').addClass('hidden');
  95. emojiNatureContainer.removeClass('hidden');
  96. });
  97. var foodsEmoji = $('<li></li>')
  98. .html("🍏")
  99. .click(function(e){
  100. e.preventDefault();
  101. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  102. $(this).addClass('selected');
  103. $('.lsx-emoji-tab').addClass('hidden');
  104. emojiFoodsContainer.removeClass('hidden');
  105. });
  106. var activityEmoji = $('<li></li>')
  107. .html("⚽")
  108. .click(function(e){
  109. e.preventDefault();
  110. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  111. $(this).addClass('selected');
  112. $('.lsx-emoji-tab').addClass('hidden');
  113. emojiActivityContainer.removeClass('hidden');
  114. });
  115. var placesEmoji = $('<li></li>')
  116. .html("🚗")
  117. .click(function(e){
  118. e.preventDefault();
  119. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  120. $(this).addClass('selected');
  121. $('.lsx-emoji-tab').addClass('hidden');
  122. emojiPlacesContainer.removeClass('hidden');
  123. });
  124. var objectsEmoji = $('<li></li>')
  125. .html("⌚")
  126. .click(function(e){
  127. e.preventDefault();
  128. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  129. $(this).addClass('selected');
  130. $('.lsx-emoji-tab').addClass('hidden');
  131. emojiObjectsContainer.removeClass('hidden');
  132. });
  133. var symbolsEmoji = $('<li></li>')
  134. .html("💯")
  135. .click(function(e){
  136. e.preventDefault();
  137. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  138. $(this).addClass('selected');
  139. $('.lsx-emoji-tab').addClass('hidden');
  140. emojiSymbolsContainer.removeClass('hidden');
  141. });
  142. var flagsEmoji = $('<li></li>')
  143. .html("🇦🇫")
  144. .click(function(e){
  145. e.preventDefault();
  146. $('ul.lsx-emojipicker-tabs li').removeClass('selected');
  147. $(this).addClass('selected');
  148. $('.lsx-emoji-tab').addClass('hidden');
  149. emojiFlagsContainer.removeClass('hidden');
  150. });
  151. if(customemojis.length > 0) {
  152. tabs.append(customEmoji);
  153. }
  154. tabs.append(peopleEmoji)
  155. .append(natureEmoji)
  156. .append(placesEmoji)
  157. .append(foodsEmoji)
  158. .append(activityEmoji)
  159. .append(objectsEmoji)
  160. .append(symbolsEmoji)
  161. .append(flagsEmoji);
  162. if(customemojis.length > 0) {
  163. createCustomEmojiTab(emojiCustomContainer,container);
  164. }
  165. createEmojiTab('people',emojiPeopleContainer,container);
  166. createEmojiTab('nature',emojiNatureContainer,container);
  167. createEmojiTab('foods',emojiFoodsContainer,container);
  168. createEmojiTab('activity',emojiActivityContainer,container);
  169. createEmojiTab('places',emojiPlacesContainer,container);
  170. createEmojiTab('objects',emojiObjectsContainer,container);
  171. createEmojiTab('symbols',emojiSymbolsContainer,container);
  172. createEmojiTab('flags',emojiFlagsContainer,container);
  173. //wrapper.append(spinnerContainer);
  174. wrapper.append(tabs);
  175. if(customemojis.length > 0) {
  176. wrapper.append(emojiCustomContainer);
  177. }
  178. wrapper.append(emojiPeopleContainer)
  179. .append(emojiNatureContainer)
  180. .append(emojiFoodsContainer)
  181. .append(emojiActivityContainer)
  182. .append(emojiPlacesContainer)
  183. .append(emojiObjectsContainer)
  184. .append(emojiSymbolsContainer)
  185. .append(emojiFlagsContainer);
  186. container.append(wrapper);
  187. appender.append(container);
  188. this.append(appender);
  189. if(settings.twemoji){
  190. twemoji.parse(emojiPeopleContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  191. twemoji.parse(emojiNatureContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  192. twemoji.parse(emojiFoodsContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  193. twemoji.parse(emojiActivityContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  194. twemoji.parse(emojiPlacesContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  195. twemoji.parse(emojiObjectsContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  196. twemoji.parse(emojiSymbolsContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  197. twemoji.parse(emojiFlagsContainer[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  198. twemoji.parse(tabs[0],{base:"https://"+current_instance,ext:".svg",folder:"/emoji"});
  199. }
  200. this.click(function(e){
  201. e.preventDefault();
  202. if(!$(e.target).parent().hasClass('lsx-emojipicker-tabs')
  203. && !$(e.target).parent().parent().hasClass('lsx-emojipicker-tabs')
  204. && !$(e.target).parent().hasClass('lsx-emoji-tab')
  205. && !$(e.target).parent().parent().hasClass('lsx-emoji-tab')){
  206. if(container.is(':visible')){
  207. container.hide();
  208. } else {
  209. container.show();
  210. }
  211. }
  212. });
  213. return this;
  214. }
  215. }
  216. function createEmojiTab(type,container,wrapper) {
  217. for(var i = 0;i < emoji[type].length;i++){
  218. var selectedEmoji = emoji[type][i];
  219. var emoticon = $('<span></span>')
  220. .data('value',selectedEmoji.value)
  221. .attr('title',selectedEmoji.name)
  222. .html(selectedEmoji.value);
  223. emoticon.click(function(e){
  224. e.preventDefault();
  225. settings.onSelect({
  226. 'name':$(this).attr('title'),
  227. 'value':$(this).data('value')
  228. });
  229. if(settings.closeOnSelect){
  230. wrapper.hide();
  231. }
  232. });
  233. container.append(emoticon);
  234. }
  235. }
  236. function createCustomEmojiTab(container,wrapper) {
  237. var customemojis = JSON.parse(localStorage.current_custom_emojis);
  238. for(var i = 0;i < customemojis.length;i++){
  239. var selectedEmoji = customemojis[i];
  240. var emoticon = $('<span></span>')
  241. .data('value',selectedEmoji.url)
  242. .attr('title',selectedEmoji.code)
  243. .html($("<img>").addClass("emoji").attr("src",selectedEmoji.url));
  244. emoticon.click(function(e){
  245. e.preventDefault();
  246. settings.onSelect({
  247. 'name':$(this).attr('title'),
  248. 'value':$(this).data('value')
  249. });
  250. if(settings.closeOnSelect){
  251. wrapper.hide();
  252. }
  253. });
  254. container.append(emoticon);
  255. }
  256. }
  257. }(jQuery, window));
  258. }