CKEDITOR.plugins.add("ipsautolink",{init:function(c){c.widgets.add("ipsembedded",{inline:!1,upcast:function(b){if("div"==b.name&&b.hasClass("ipsEmbeddedVideo")||"iframe"==b.name&&!_.isUndefined(b.attributes["data-embedcontent"]))return!0}});new CKEDITOR.plugins.ipsautolink(c)}});
CKEDITOR.plugins.ipsautolink=function(c){this.urlRegex=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:localhost)|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;this.handleKey=
function(b){13==b.data.keyCode?CKEDITOR.tools.setTimeout(function(){this._handleKey(b)},0,this):32==b.data.keyCode&&this._handleKey(b)};this._handleKey=function(b){var a=c.getSelection();if(null!==a)for(var a=a.getRanges(!0),d=0;d<a.length;d++)if(a[d].collapsed){var f=32==b.data.keyCode?a[d].getCommonAncestor(!0,!0):a[d].getCommonAncestor(!0,!0).getPrevious();f&&f instanceof CKEDITOR.dom.element&&this.replaceInElement(f)}};c.on("key",this.handleKey,this);this.handlePaste=function(b){if(b.data.dataTransfer.getTransferType(c)!=
CKEDITOR.DATA_TRANSFER_INTERNAL){var a=b.data.dataValue;if(-1<a.indexOf("\x3c")){if(a=a.match(/<a href="(.+?)">(\1)<\/a>/))b.data.dataValue="\x3ca href\x3d'"+a[1]+"' ipsNoEmbed\x3d'false'\x3e"+decodeURI(a[1])+"\x3c/a\x3e"}else{for(var d=c.getSelection().getRanges(!0),f=0;f<d.length;f++)if(d[f].startOffset){var e=d[f].getPreviousEditableNode();if(e&&(e=e.getText(),"[url\x3d"==e.substr(-5)||"[img]"==e.substr(-5)||"[img\x3d"==e.substr(-5)||"[code]"==e.substr(-6)))return}if(a=this.replaceTextWithLink(a))b.data.dataValue=
a.getOuterHtml()}}};c.on("paste",this.handlePaste,this,{},11);this.handleAfterPaste=function(b){b=c.editable().find('a[ipsNoEmbed\x3d"false"]');for(var a=0;a<b.count();a++)this.replaceLinkWithEmbed(b.getItem(a))};c.on("afterPaste",this.handleAfterPaste,this);c.on("contentDom",function(){var b=this,a=c.editable();$("."+c.id).closest("form").on("submit",function(){for(var d=a.getChildren(),f=0;f<d.count();f++)d.getItem(f)&&d.getItem(f)instanceof CKEDITOR.dom.element&&b.replaceInElement(d.getItem(f));
c.updateElement()})},this);this.replaceInElement=function(b){if("a"!=b.getName()&&"pre"!=b.getName()&&!b.getAttribute("ipsnoautolink")){b=b.getChildren();for(var a=0;a<b.count();a++){var c=b.getItem(a);if(c instanceof CKEDITOR.dom.text){var f=[],e="",k=!1,g=c.getText().split(" ");for(word in g){if("[url\x3d"==g[word]||"[img]"==g[word]||"[code]"==g[word])return;var h=this.replaceWord(g[word].trim());h?("a"==h.getName()&&this.replaceLinkWithEmbed(h),e.length&&(f.push(new CKEDITOR.dom.text(e)),e=""),
f.push(h),k=!0):e+=g[word];word<g.length-1&&(e+=" ")}e.length&&f.push(new CKEDITOR.dom.text(e));if(1<f.length||k){for(e=0;e<f.length;e++)f[e].insertBefore(c);c.remove()}}else c&&c instanceof CKEDITOR.dom.element&&this.replaceInElement(c)}}};this.replaceWord=function(b){var a=this.replaceTextWithLink(b);return a?a:(b=this.replaceTextWithEmoticon(b))?b:null};this.replaceTextWithLink=function(b){if(XRegExp.exec(b,this.urlRegex)){var a=new CKEDITOR.dom.element("a");a.setAttribute("ipsNoEmbed","false");
a.setAttribute("href",$("\x3ctextarea /\x3e").html(b).text());a.appendHtml(decodeURI(b));return a}return null};this.replaceTextWithEmoticon=function(b){for(key in c.config.ipsEmoticons){var a=new RegExp("^"+key.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")+"$","i");if(b.match(a)){if(75>$("\x3cdiv\x3e"+c.getData()+"\x3c/div\x3e").find("img[data-emoticon]").length)return b=new CKEDITOR.dom.element("img"),b.setAttribute("src",c.config.ipsEmoticons[key].image),c.config.ipsEmoticons[key].image_2x&&b.setAttribute("srcset",
c.config.ipsEmoticons[key].image_2x),c.config.ipsEmoticons[key].width&&c.config.ipsEmoticons[key].height&&(b.setAttribute("width",c.config.ipsEmoticons[key].width),b.setAttribute("height",c.config.ipsEmoticons[key].height)),b.setAttribute("data-emoticon","true"),b.setAttribute("alt",key),b.setAttribute("title",key),b;var d=$("."+c.id).closest("[data-ipsEditor]").find('[data-role\x3d"emoticonMessage"]');d.slideDown();setTimeout(function(){c.once("key",function(){d.slideUp()});c.once("setData",function(){d.slideUp()})},
2500)}}return null};this.replaceLinkWithEmbed=function(b){c.config.ipsAutoEmbed&&"true"!=b.getAttribute("ipsNoEmbed")&&ips.getAjax()(c.config.controller+"\x26do\x3dvalidateLink",{data:{url:b.getAttribute("href").replace(/&amp;/g,"\x26")},type:"post"}).done(function(a){if(a.embed){var d,f,e,k=CKEDITOR.dom.element.createFromHtml(a.preview);if("img"==k.getName())k.replace(b);else{a=b.getParents();for(i in a.reverse())if("p"==a[i].getName()){b.breakParent(a[i]);(a=b.getPrevious())&&0==a.getChildCount()?
a.remove():f=a;if((a=b.getNext())&&(a.getChildCount(0)||a.getChildCount(1)&&a.getChild(0).is("br"))){var g=a.getNext();g&&(a.getChildCount(0)||a.getChildCount(1)&&a.getChild(0).is("br"))&&(a.remove(),a=g)}e=a;g=c.createRange();g.moveToElementEditEnd(a);c.getSelection().selectRanges([g]);break}k.replace(b);d=c.widgets.initOn(k,"ipsembedded");$(document).trigger("contentChange",[$("."+c.id).closest("[data-ipsEditor]")])}var h=$("."+c.id).closest("[data-ipsEditor]").find('[data-role\x3d"embedMessage"]');
h.slideDown();var l=function(){h.slideUp();h.find('[data-action\x3d"keepEmbeddedMedia"]').off("click.ipsEmbed");h.find('[data-action\x3d"removeEmbeddedMedia"]').off("click.ipsEmbed")};setTimeout(function(){c.once("key",function(){l()});c.once("setData",function(){l()})},2500);h.find('[data-action\x3d"keepEmbeddedMedia"]').on("click.ipsEmbed",function(){c.focus();l()});h.find('[data-action\x3d"removeEmbeddedMedia"]').on("click.ipsEmbed",function(){d&&d.destroy();b.replace(k);b.setAttribute("ipsNoEmbed",
"true");if(e&&f)b.move(e,!0),e.moveChildren(f);else if(e){var a=new CKEDITOR.dom.element("p");a.insertBefore(b);b.move(a)}c.focus();l()})}else if(b.setAttribute("ipsNoEmbed","true"),a.errorMessage){var m=$("."+c.id).closest("[data-ipsEditor]").find('[data-role\x3d"embedFailMessage"]');m.find("p").html(a.errorMessage);m.slideDown();setTimeout(function(){c.once("key",function(){m.slideUp()});c.once("setData",function(){m.slideUp()})},2500)}}).fail(function(a){b.setAttribute("ipsNoEmbed","true");var d=
$("."+c.id).closest("[data-ipsEditor]").find('[data-role\x3d"embedFailMessage"]');d.find("p").html(ips.getString("embed_error_message_admin",{error:a.statusText+": "+a.responseText}));d.slideDown();setTimeout(function(){c.once("key",function(){d.slideUp()});c.once("setData",function(){d.slideUp()})},2500)})}};