-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathjquery.emojiarea.min.js
14 lines (14 loc) · 6.28 KB
/
jquery.emojiarea.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*! jquery.emojiarea.js | https://github.com/diy/jquery-emojiarea | Apache License (v2) */
(function(d,i,e){var p=["p","div","pre","form"];d.emojiarea={path:"",icons:{},defaults:{button:null,buttonLabel:"Emojis",buttonPosition:"after"}};d.fn.emojiarea=function(a){a=d.extend({},d.emojiarea.defaults,a);return this.each(function(){var b=d(this);"contentEditable"in e.body&&!1!==a.wysiwyg?new l(b,a):new m(b,a)})};var g={},k;k=i.getSelection?function(a){var b=i.getSelection();b.removeAllRanges();for(var c=0,f=a.length;c<f;++c)b.addRange(a[c])}:e.selection&&e.selection.createRange?function(a){a&&
a.select()}:void 0;g.restoreSelection=k;k=i.getSelection?function(){var a=i.getSelection(),b=[];if(a.rangeCount)for(var c=0,f=a.rangeCount;c<f;++c)b.push(a.getRangeAt(c));return b}:e.selection&&e.selection.createRange?function(){var a=e.selection;return"none"!==a.type.toLowerCase()?a.createRange():null}:void 0;g.saveSelection=k;k=i.getSelection?function(a){var b,c=i.getSelection(),f="string"===typeof a?e.createTextNode(a):a;c.getRangeAt&&c.rangeCount&&(b=c.getRangeAt(0),b.deleteContents(),b.insertNode(e.createTextNode(" ")),
b.insertNode(f),b.setStart(f,0),i.setTimeout(function(){b=e.createRange();b.setStartAfter(f);b.collapse(!0);c.removeAllRanges();c.addRange(b)},0))}:e.selection&&e.selection.createRange?function(a){var b=e.selection.createRange();"string"===typeof a?b.text=a:b.pasteHTML(a.outerHTML)}:void 0;g.replaceSelection=k;g.insertAtCursor=function(a,b){var a=" "+a,c=b.value,f;"undefined"!=typeof b.selectionStart&&"undefined"!=typeof b.selectionEnd?(f=b.selectionStart,b.value=c.substring(0,f)+a+c.substring(b.selectionEnd),
b.selectionStart=b.selectionEnd=f+a.length):"undefined"!=typeof e.selection&&"undefined"!=typeof e.selection.createRange&&(b.focus(),c=e.selection.createRange(),c.text=a,c.select())};g.extend=function(a,b){if("undefined"===typeof a||!a)a={};if("object"===typeof b)for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a};g.escapeRegex=function(a){return(a+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")};g.htmlEntities=function(a){return String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,
">").replace(/"/g,""")};var j=function(){};j.prototype.setup=function(){var a=this;this.$editor.on("focus",function(){a.hasFocus=!0});this.$editor.on("blur",function(){a.hasFocus=!1});this.setupButton()};j.prototype.setupButton=function(){var a=this,b;this.options.button?b=d(this.options.button):!1!==this.options.button?(b=d('<a href="javascript:void(0)">'),b.html(this.options.buttonLabel),b.addClass("emoji-button"),b.attr({title:this.options.buttonLabel}),this.$editor[this.options.buttonPosition](b)):
b=d("");b.on("click",function(b){h.show(a);b.stopPropagation()});this.$button=b};j.createIcon=function(a){var b=d.emojiarea.icons[a],c=d.emojiarea.path||"";c.length&&"/"!==c.charAt(c.length-1)&&(c+="/");return'<img src="'+c+b+'" alt="'+g.htmlEntities(a)+'">'};var m=function(a,b){this.options=b;this.$editor=this.$textarea=a;this.setup()};m.prototype.insert=function(a){d.emojiarea.icons.hasOwnProperty(a)&&(g.insertAtCursor(a,this.$textarea[0]),this.$textarea.trigger("change"))};m.prototype.val=function(){return this.$textarea.val()};
g.extend(m.prototype,j.prototype);var l=function(a,b){var c=this;this.options=b;this.$textarea=a;this.$editor=d("<div>").addClass("emoji-wysiwyg-editor");this.$editor.text(a.val());this.$editor.attr({contenteditable:"true"});this.$editor.on("blur keyup paste",function(){return c.onChange.apply(c,arguments)});this.$editor.on("mousedown focus",function(){e.execCommand("enableObjectResizing",!1,!1)});this.$editor.on("blur",function(){e.execCommand("enableObjectResizing",!0,!0)});var f=this.$editor.text(),
i=d.emojiarea.icons,h;for(h in i)i.hasOwnProperty(h)&&(f=f.replace(RegExp(g.escapeRegex(h),"g"),j.createIcon(h)));this.$editor.html(f);a.hide().after(this.$editor);this.setup();this.$button.on("mousedown",function(){c.hasFocus&&(c.selection=g.saveSelection())})};l.prototype.onChange=function(){this.$textarea.val(this.val()).trigger("change")};l.prototype.insert=function(a){a=d(j.createIcon(a));a[0].attachEvent&&a[0].attachEvent("onresizestart",function(a){a.returnValue=!1},!1);this.$editor.trigger("focus");
this.selection&&g.restoreSelection(this.selection);try{g.replaceSelection(a[0])}catch(b){}this.onChange()};l.prototype.val=function(){for(var a=[],b=[],c=function(){a.push(b.join(""));b=[]},f=function(a){if(3===a.nodeType)b.push(a.nodeValue);else if(1===a.nodeType){var d=a.tagName.toLowerCase(),e=-1!==p.indexOf(d);e&&b.length&&c();if("img"===d)(e=a.getAttribute("alt")||"")&&b.push(e);else{"br"===d&&c();a=a.childNodes;for(d=0;d<a.length;d++)f(a[d]);e&&b.length&&c()}}},d=this.$editor[0].childNodes,
e=0;e<d.length;e++)f(d[e]);b.length&&c();return a.join("\n")};g.extend(l.prototype,j.prototype);var h=function(){var a=this,b=d(e.body),c=d(i);this.visible=!1;this.emojiarea=null;this.$menu=d("<div>");this.$menu.addClass("emoji-menu");this.$menu.hide();this.$items=d("<div>").appendTo(this.$menu);b.append(this.$menu);b.on("keydown",function(b){(27===b.keyCode||9===b.keyCode)&&a.hide()});b.on("mouseup",function(){a.hide()});c.on("resize",function(){a.visible&&a.reposition()});this.$menu.on("mouseup",
"a",function(a){a.stopPropagation();return!1});this.$menu.on("click","a",function(b){var c=d(".label",d(this)).text();i.setTimeout(function(){a.onItemSelected.apply(a,[c])},0);b.stopPropagation();return!1});this.load()};h.prototype.onItemSelected=function(a){this.emojiarea.insert(a);this.hide()};h.prototype.load=function(){var a=[],b=d.emojiarea.icons,c=d.emojiarea.path;c.length&&c.charAt(c.length-1);for(var e in b)b.hasOwnProperty(e)&&a.push('<a href="javascript:void(0)" title="'+g.htmlEntities(e)+
'">'+j.createIcon(e)+'<span class="label">'+g.htmlEntities(e)+"</span></a>");this.$items.html(a.join(""))};h.prototype.reposition=function(){var a=this.emojiarea.$button,b=a.offset();b.top+=a.outerHeight();b.left+=Math.round(a.outerWidth()/2);this.$menu.css({top:b.top,left:b.left})};h.prototype.hide=function(){this.emojiarea&&(this.emojiarea.menu=null,this.emojiarea.$button.removeClass("on"),this.emojiarea=null);this.visible=!1;this.$menu.hide()};h.prototype.show=function(a){this.emojiarea&&this.emojiarea===
a||(this.emojiarea=a,this.emojiarea.menu=this,this.reposition(),this.$menu.show(),this.visible=!0)};var n=null;h.show=function(a){n=n||new h;n.show(a)}})(jQuery,window,document);