CKEDITOR.plugins.add("ipsmentions",{init:function(c){new CKEDITOR.plugins.ipsmentions(c)}});
CKEDITOR.plugins.ipsmentions=function(c){this.currentMention=this.listenWithinMention=this.listenForAtSymbolEvent=null;this.callsWithNoResults=0;this.ajaxObj=this.results=null;this.listenForAtSymbol=function(){this.listenForAtSymbolEvent=c.on("change",function(){CKEDITOR.tools.setTimeout(function(){var a=c.getSelection();if(a.getType()==CKEDITOR.SELECTION_TEXT)for(var a=a.getRanges(!0),b=0;b<a.length;b++)a[b].collapsed&&a[b].startOffset&&(a[b].setStart(a[b].startContainer,0),"@"==a[b].cloneContents().$.textContent.substr(-1)&&
this.respondToAtSymbol(a[b]))},0,this)},this)};this.respondToAtSymbol=function(a){var b=a.cloneContents().$.textContent;if(!(1<b.length)||b.substr(-2,1).match(/\s/))this.listenForAtSymbolEvent.removeListener(),this.currentMention=new CKEDITOR.dom.element("span"),this.currentMention.setText("@"),b=a.endContainer.split(a.endOffset-1),b.split(1),this.currentMention.replace(b),b=c.createRange(),b.moveToPosition(this.currentMention,CKEDITOR.POSITION_BEFORE_END),c.getSelection().selectRanges([b]),this.callsWithNoResults=
0,this.results=$('<ul class="ipsMenu ipsMenu_auto ipsMenu_bottomLeft" data-mentionMenu></ul>').hide(),this.results.append('<li class="ipsLoading ipsLoading_small" style="height: 40px">&nbsp;</li>'),$("body").append(this.results),b=$(this.currentMention.$).offset(),this.results.css({position:"absolute",top:b.top+a.getCommonAncestor(!0,!0).getSize("height"),left:b.left}),this.listenWithinMention=c.on("key",this.listenWithinMentionEvent,this)};this.listenWithinMentionEvent=function(a){if(27==a.data.keyCode)this.cancelMention(),
this.closeResults();else if(40==a.data.keyCode||38==a.data.keyCode){var b=this.results.children("[data-selected]");b.length?(b.removeAttr("data-selected"),40==a.data.keyCode?b.next().attr("data-selected",!0):b.prev().attr("data-selected",!0)):40==a.data.keyCode?this.results.children(":first-child").attr("data-selected",!0):this.results.children(":last-child").attr("data-selected",!0);a.cancel()}else 13==a.data.keyCode||9==a.data.keyCode?(b=this.results.children("[data-selected]"),b.length&&b.click(),
a.cancel()):(8==a.data.keyCode&&(this.callsWithNoResults=0),CKEDITOR.tools.setTimeout(function(){if(this.currentMention.getAddress().length){for(var a=c.getSelection().getRanges(),b=0;b<a.length;b++)if(!a[b].getCommonAncestor(true,true).equals(this.currentMention)){this.cancelMention();this.closeResults();return}var d=this.currentMention.getText().substr(1).trim();this.results.show();if(this.ajaxObj&&_.isFunction(this.ajaxObj.abort))try{this.ajaxObj.abort()}catch(e){}this.ajaxObj=ips.getAjax()(ips.getSetting("baseURL")+
"?app=core&module=system&controller=editor&do=mention&input="+encodeURIComponent(d),{context:this}).done(function(a){if(a){this.results.removeClass("ipsLoading");this.results.html(a);this.results.children().click($.proxy(this.selectMentionResult,this))}else if(d.length){this.callsWithNoResults++;if(this.callsWithNoResults>=3){this.cancelMention();this.closeResults()}}})}else this.closeResults()},0,this))};this.selectMentionResult=function(a){a=$(a.currentTarget);this.currentMention.renameNode("a");
this.currentMention.setAttribute("href",a.attr("data-mentionhref"));this.currentMention.setAttribute("contenteditable","false");this.currentMention.setAttribute("data-ipsHover","");this.currentMention.setAttribute("data-ipsHover-target",a.attr("data-mentionhover"));this.currentMention.setAttribute("data-mentionid",a.attr("data-mentionid"));this.currentMention.setHtml("@"+a.find('[data-role="mentionname"]').html());c.focus();a=c.createRange();a.moveToElementEditEnd(this.currentMention);c.getSelection().selectRanges([a]);
this.closeResults()};this.cancelMention=function(){this.currentMention.remove(!0)};this.closeResults=function(){this.currentMention=null;this.results.remove();this.listenWithinMention.removeListener();this.listenForAtSymbol()};this.listenForAtSymbol()};