-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.min.js
1 lines (1 loc) · 6.52 KB
/
plugin.min.js
1
(function(e,t){"use strict";var n=function(e,n){this.editor=e;this.options=t.extend({},{source:[],delay:500,queryBy:"name",items:10},n);this.matcher=this.options.matcher||this.matcher;this.renderDropdown=this.options.renderDropdown||this.renderDropdown;this.render=this.options.render||this.render;this.insert=this.options.insert||this.insert;this.highlighter=this.options.highlighter||this.highlighter;this.query="";this.hasFocus=true;this.renderInput();this.bindEvents()};n.prototype={constructor:n,renderInput:function(){var e='<span id="autocomplete">'+'<span id="autocomplete-delimiter">'+this.options.delimiter+"</span>"+'<span id="autocomplete-searchtext"><span class="dummy"></span></span>'+"</span>";this.editor.execCommand("mceInsertContent",false,e);this.editor.focus();this.editor.selection.select(this.editor.selection.dom.select("span#autocomplete-searchtext span")[0]);this.editor.selection.collapse(0)},bindEvents:function(){this.editor.on("keyup",this.editorKeyUpProxy=t.proxy(this.rteKeyUp,this));this.editor.on("keydown",this.editorKeyDownProxy=t.proxy(this.rteKeyDown,this));this.editor.on("click",this.editorClickProxy=t.proxy(this.rteClicked,this));var e=this.editor.__bindings.keydown.pop();this.editor.__bindings.keydown.unshift(e);t(this.editor.getWin()).on("scroll",this.rteScroll=t.proxy(function(){this.cleanUp(true)},this));t("body").on("click",this.bodyClickProxy=t.proxy(this.rteLostFocus,this))},unbindEvents:function(){this.editor.off("keyup",this.editorKeyUpProxy);this.editor.off("keydown",this.editorKeyDownProxy);this.editor.on("click",this.editorClickProxy);t(this.editor.getWin()).off("scroll",this.rteScroll);t("body").off("click",this.bodyClickProxy)},rteKeyUp:function(e){switch(e.which||e.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 8:if(this.query===""){this.cleanUp(true)}else{this.lookup()}break;case 9:case 13:var t=this.$dropdown!==undefined?this.$dropdown.find("li.active"):[];if(t.length){this.select(t.data());this.cleanUp(false)}else{this.cleanUp(true)}break;case 27:this.cleanUp(true);break;default:this.lookup()}},rteKeyDown:function(e){switch(e.which||e.keyCode){case 9:case 13:case 27:e.preventDefault();break;case 38:e.preventDefault();if(this.$dropdown!==undefined){this.highlightPreviousResult()}break;case 40:e.preventDefault();if(this.$dropdown!==undefined){this.highlightNextResult()}break}e.stopPropagation()},rteClicked:function(e){var n=t(e.target);if(this.hasFocus&&n.parent().attr("id")!=="autocomplete-searchtext"){this.cleanUp(true)}},rteLostFocus:function(e,t){if(this.hasFocus){this.cleanUp(true)}},lookup:function(){this.query=t.trim(t(this.editor.getBody()).find("#autocomplete-searchtext").text()).replace("","");if(this.$dropdown===undefined){this.show()}clearTimeout(this.searchTimeout);this.searchTimeout=setTimeout(t.proxy(function(){var e=t.isFunction(this.options.source)?this.options.source(this.query,t.proxy(this.process,this),this.options.delimiter):this.options.source;if(e){this.process(e)}},this),this.options.delay)},matcher:function(e){return~e[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(e){var t=[],n=[],r=[],i;while((i=e.shift())!==undefined){if(!i[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())){t.push(i)}else if(~i[this.options.queryBy].indexOf(this.query)){n.push(i)}else{r.push(i)}}return t.concat(n,r)},highlighter:function(e){return e.replace(new RegExp("("+this.query+")","ig"),function(e,t){return"<strong>"+t+"</strong>"})},show:function(){var e=t(this.editor.getContainer()).offset(),n=t(this.editor.getContentAreaContainer()).position(),r=t(this.editor.dom.select("span#autocomplete")).position(),i=e.top+n.top+r.top+t(this.editor.selection.getNode()).innerHeight()-t(this.editor.getDoc()).scrollTop()+5,s=e.left+n.left+r.left;this.$dropdown=t(this.renderDropdown()).css({top:i,left:s});t("body").append(this.$dropdown);this.$dropdown.on("click",t.proxy(this.autoCompleteClick,this))},process:function(e){if(!this.hasFocus){return}var n=this,r=[],i=t.grep(e,function(e){return n.matcher(e)});i=n.sorter(i);i=i.slice(0,this.options.items);t.each(i,function(e,s){var o=t(n.render(s));o.html(o.html().replace(o.text(),n.highlighter(o.text())));t.each(i[e],function(e,t){o.attr("data-"+e,t)});r.push(o[0].outerHTML)});if(r.length){this.$dropdown.html(r.join("")).show()}else{this.$dropdown.hide()}},renderDropdown:function(){return'<ul class="rte-autocomplete dropdown-menu"><li class="loading"></li></ul>'},render:function(e){return"<li>"+'<a href="javascript:;"><span>'+e.name+"</span></a>"+"</li>"},autoCompleteClick:function(e){var n=t(e.target).closest("li").data();if(!t.isEmptyObject(n)){this.select(n);this.cleanUp(false)}e.stopPropagation();e.preventDefault()},highlightPreviousResult:function(){var e=this.$dropdown.find("li.active").index(),t=e===0?this.$dropdown.find("li").length-1:--e;this.$dropdown.find("li").removeClass("active").eq(t).addClass("active")},highlightNextResult:function(){var e=this.$dropdown.find("li.active").index(),t=e===this.$dropdown.find("li").length-1?0:++e;this.$dropdown.find("li").removeClass("active").eq(t).addClass("active")},select:function(e){this.editor.focus();var t=this.editor.dom.select("span#autocomplete")[0];this.editor.dom.remove(t);this.editor.execCommand("mceInsertContent",false,this.insert(e)+" ")},insert:function(e){return"<span>"+e.name+"</span>"},cleanUp:function(e){this.unbindEvents();this.hasFocus=false;if(this.$dropdown!==undefined){this.$dropdown.remove();delete this.$dropdown}if(e){var n=this.query,r=t(this.editor.dom.select("span#autocomplete")),i=t("<p>"+this.options.delimiter+n+"</p>")[0].firstChild,s=t(this.editor.selection.getNode()).offset().top===r.offset().top+(r.outerHeight()-r.height())/2;this.editor.dom.replace(i,r[0]);if(s){this.editor.selection.select(i);this.editor.selection.collapse()}}}};e.create("tinymce.plugins.Mention",{init:function(e,r){function o(){var n=t(e.selection.getNode()).is("a"),r=e.selection.getRng().startOffset,i=e.selection.getRng().startContainer.textContent,s=i.substr(r-1,1);return n||!!t.trim(s).length?false:true}var i,s=e.getParam("mentions");s.delimiter=s.delimiter!==undefined?!t.isArray(s.delimiter)?[s.delimiter]:s.delimiter:["@"];e.on("keypress",function(r){var u=t.inArray(String.fromCharCode(r.which||r.keyCode),s.delimiter);if(u>-1&&o()){if(i===undefined||i.hasFocus!==undefined&&!i.hasFocus){r.preventDefault();i=new n(e,t.extend({},s,{delimiter:s.delimiter[u]}))}}})},getInfo:function(){return{longname:"mention",author:"Steven Devooght",version:e.majorVersion+"."+e.minorVersion}}});e.PluginManager.add("mention",e.plugins.Mention)})(tinymce,jQuery);