1 line
7.3 KiB
JavaScript
1 line
7.3 KiB
JavaScript
(function(e,t,n){var r=1;var i=3;var s=["p","div","pre","form"];var o=27;var u=9;e.emojiarea={path:"",icons:{},defaults:{button:null,buttonLabel:"Emojis",buttonPosition:"after"}};e.fn.emojiarea=function(t){t=e.extend({},e.emojiarea.defaults,t);return this.each(function(){var r=e(this);if("contentEditable"in n.body&&t.wysiwyg!==false){new c(r,t)}else{new l(r,t)}})};var a={};a.restoreSelection=function(){if(t.getSelection){return function(e){var n=t.getSelection();n.removeAllRanges();for(var r=0,i=e.length;r<i;++r){n.addRange(e[r])}}}else if(n.selection&&n.selection.createRange){return function(e){if(e){e.select()}}}}();a.saveSelection=function(){if(t.getSelection){return function(){var e=t.getSelection(),n=[];if(e.rangeCount){for(var r=0,i=e.rangeCount;r<i;++r){n.push(e.getRangeAt(r))}}return n}}else if(n.selection&&n.selection.createRange){return function(){var e=n.selection;return e.type.toLowerCase()!=="none"?e.createRange():null}}}();a.replaceSelection=function(){if(t.getSelection){return function(e){var r,i=t.getSelection();var s=typeof e==="string"?n.createTextNode(e):e;if(i.getRangeAt&&i.rangeCount){r=i.getRangeAt(0);r.deleteContents();r.insertNode(n.createTextNode(" "));r.insertNode(s);r.setStart(s,0);t.setTimeout(function(){r=n.createRange();r.setStartAfter(s);r.collapse(true);i.removeAllRanges();i.addRange(r)},0)}}}else if(n.selection&&n.selection.createRange){return function(e){var t=n.selection.createRange();if(typeof e==="string"){t.text=e}else{t.pasteHTML(e.outerHTML)}}}}();a.insertAtCursor=function(e,t){e=" "+e;var r=t.value,i,s,o;if(typeof t.selectionStart!="undefined"&&typeof t.selectionEnd!="undefined"){s=t.selectionStart;i=t.selectionEnd;t.value=r.substring(0,s)+e+r.substring(t.selectionEnd);t.selectionStart=t.selectionEnd=s+e.length}else if(typeof n.selection!="undefined"&&typeof n.selection.createRange!="undefined"){t.focus();o=n.selection.createRange();o.text=e;o.select()}};a.extend=function(e,t){if(typeof e==="undefined"||!e){e={}}if(typeof t==="object"){for(var n in t){if(t.hasOwnProperty(n)){e[n]=t[n]}}}return e};a.escapeRegex=function(e){return(e+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")};a.htmlEntities=function(e){return String(e).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")};var f=function(){};f.prototype.setup=function(){var e=this;this.$editor.on("focus",function(){e.hasFocus=true});this.$editor.on("blur",function(){e.hasFocus=false});this.setupButton()};f.prototype.setupButton=function(){var t=this;var n;if(this.options.button){n=e(this.options.button)}else if(this.options.button!==false){n=e('<a href="javascript:void(0)">');n.html(this.options.buttonLabel);n.addClass("emoji-button");n.attr({title:this.options.buttonLabel});this.$editor[this.options.buttonPosition](n)}else{n=e("")}n.on("click",function(e){h.show(t);e.stopPropagation()});this.$button=n};f.createIcon=function(t,n){var r=e.emojiarea.icons[t]["icons"][n];var i=e.emojiarea.path||"";if(i.length&&i.charAt(i.length-1)!=="/"){i+="/"}return'<img src="'+i+r+'" alt="'+a.htmlEntities(n)+'">'};var l=function(e,t){this.options=t;this.$textarea=e;this.$editor=e;this.setup()};l.prototype.insert=function(t,n){if(!e.emojiarea.icons[t]["icons"].hasOwnProperty(n))return;a.insertAtCursor(n,this.$textarea[0]);this.$textarea.trigger("change")};l.prototype.val=function(){return this.$textarea.val()};a.extend(l.prototype,f.prototype);var c=function(t,r){var i=this;this.options=r;this.$textarea=t;this.$editor=e("<div>").addClass("emoji-wysiwyg-editor");this.$editor.text(t.val());this.$editor.attr({contenteditable:"true"});this.$editor.on("blur keyup paste",function(){return i.onChange.apply(i,arguments)});this.$editor.on("mousedown focus",function(){n.execCommand("enableObjectResizing",false,false)});this.$editor.on("blur",function(){n.execCommand("enableObjectResizing",true,true)});var s=this.$editor.text();var o=e.emojiarea.icons;for(var u in o){for(var l in o[u]["icons"]){if(o[u]["icons"].hasOwnProperty(l)){s=s.replace(new RegExp(a.escapeRegex(l),"g"),f.createIcon(u,l))}}}this.$editor.html(s);t.hide().after(this.$editor);this.setup();this.$button.on("mousedown",function(){if(i.hasFocus){i.selection=a.saveSelection()}})};c.prototype.onChange=function(){this.$textarea.val(this.val()).trigger("change")};c.prototype.insert=function(t,n){var r;var i=e(f.createIcon(t,n));if(i[0].attachEvent){i[0].attachEvent("onresizestart",function(e){e.returnValue=false},false)}this.$editor.trigger("focus");if(this.selection){a.restoreSelection(this.selection)}try{a.replaceSelection(i[0])}catch(s){}this.onChange()};c.prototype.val=function(){var e=[];var t=[];var n=function(){e.push(t.join(""));t=[]};var o=function(e){if(e.nodeType===i){t.push(e.nodeValue)}else if(e.nodeType===r){var u=e.tagName.toLowerCase();var a=s.indexOf(u)!==-1;if(a&&t.length)n();if(u==="img"){var f=e.getAttribute("alt")||"";if(f)t.push(f);return}else if(u==="br"){n()}var l=e.childNodes;for(var c=0;c<l.length;c++){o(l[c])}if(a&&t.length)n()}};var u=this.$editor[0].childNodes;for(var a=0;a<u.length;a++){o(u[a])}if(t.length)n();return e.join("\n")};a.extend(c.prototype,f.prototype);var h=function(){var r=this;var i=e(n.body);var s=e(t);this.visible=false;this.emojiarea=null;this.$menu=e("<div>");this.$menu.addClass("emoji-menu");this.$menu.hide();this.$items=e("<div>").appendTo(this.$menu);i.append(this.$menu);i.on("keydown",function(e){if(e.keyCode===o||e.keyCode===u){r.hide()}});i.on("mouseup",function(){r.hide()});s.on("resize",function(){if(r.visible)r.reposition()});this.$menu.on("mouseup","a",function(e){e.stopPropagation();return false});this.$menu.on("click","a",function(n){var i=e(".label",e(this)).text();var s=e(".label",e(this)).parent().parent().attr("group");if(s&&i!==""){t.setTimeout(function(){r.onItemSelected.apply(r,[s,i])},0);n.stopPropagation();return false}});this.load()};h.prototype.onItemSelected=function(e,t){this.emojiarea.insert(e,t);this.hide()};h.prototype.load=function(){var t=[];var n=[];var r=e.emojiarea.icons;var i=e.emojiarea.path;if(i.length&&i.charAt(i.length-1)!=="/"){i+="/"}n.push('<ul class="group-selector">');for(var s in r){n.push('<a href="#group_'+s+'" class="tab_switch"><li>'+r[s]["name"]+"</li></a>");t.push('<div class="select_group" group="'+s+'" id="group_'+s+'">');for(var o in r[s]["icons"]){if(r[s]["icons"].hasOwnProperty(o)){var u=r[o];t.push('<a href="javascript:void(0)" title="'+a.htmlEntities(o)+'">'+f.createIcon(s,o)+'<span class="label">'+a.htmlEntities(o)+"</span></a>")}}t.push("</div>")}n.push("</ul>");this.$items.html(t.join(""));this.$menu.prepend(n.join(""));this.$menu.find(".tab_switch").each(function(t){if(t!=0){var n=e(this).attr("href");e(n).hide()}else{e(this).addClass("active")}e(this).click(function(){e(this).addClass("active");e(this).siblings().removeClass("active");e(".select_group").hide();var t=e(this).attr("href");e(t).show()})})};h.prototype.reposition=function(){var e=this.emojiarea.$button;var t=e.offset();t.top+=e.outerHeight();t.left+=Math.round(e.outerWidth()/2);this.$menu.css({top:t.top,left:t.left})};h.prototype.hide=function(e){if(this.emojiarea){this.emojiarea.menu=null;this.emojiarea.$button.removeClass("on");this.emojiarea=null}this.visible=false;this.$menu.hide()};h.prototype.show=function(e){if(this.emojiarea&&this.emojiarea===e)return;this.emojiarea=e;this.emojiarea.menu=this;this.reposition();this.$menu.show();this.visible=true};h.show=function(){var e=null;return function(t){e=e||new h;e.show(t)}}()})(jQuery,window,document); |