1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
var wpLink;!function(a){function b(){return e||c.dom.getParent(c.selection.getNode(),"a[href]")}var c,d,e,f={},g="ontouchend"in document;wpLink={textarea:"",init:function(){f.wrap=a("#wp-link-wrap"),f.dialog=a("#wp-link"),f.backdrop=a("#wp-link-backdrop"),f.submit=a("#wp-link-submit"),f.close=a("#wp-link-close"),f.text=a("#wp-link-text"),f.url=a("#wp-link-url"),f.openInNewTab=a("#wp-link-target"),a.ui&&a.ui.autocomplete&&wpLink.setAutocomplete(),f.dialog.on("keydown",wpLink.keydown),f.submit.on("click",function(a){a.preventDefault(),wpLink.update()}),f.close.add(f.backdrop).add("#wp-link-cancel a").click(function(a){a.preventDefault(),wpLink.close()}),f.url.on("paste",function(){setTimeout(wpLink.correctURL,0)})},setAutocomplete:function(){var b,c,d=f.url;d.on("keydown",function(){d.removeAttr("aria-activedescendant")}).autocomplete({source:function(d,e){return c===d.term?void e(b):/^https?:/.test(d.term)||-1!==d.term.indexOf(".")?e():(a.post(window.ajaxurl,{action:"wp-link-ajax",page:1,search:d.term,_ajax_linking_nonce:a("#_ajax_linking_nonce").val()},function(a){b=a,e(a)},"json"),void(c=d.term))},focus:function(a,b){d.attr("aria-activedescendant","mce-wp-autocomplete-"+b.item.ID)},select:function(a,b){return d.val(b.item.permalink),f.wrap.hasClass("has-text-field")&&""===tinymce.trim(f.text.val())&&f.text.val(b.item.title),!1},open:function(){d.attr("aria-expanded","true")},close:function(){d.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2"}}).autocomplete("instance")._renderItem=function(b,c){return a('<li role="option" id="mce-wp-autocomplete-'+c.ID+'">').append('<span class="item-title">'+c.title+'</span> <span class="item-date alignright">'+c.info+"</span>").appendTo(b)},d.attr({"aria-owns":d.autocomplete("widget").attr("id")}).on("focus",function(){d.autocomplete("search")}).autocomplete("widget").addClass("wplink-autocomplete").attr("role","listbox")},correctURL:function(){var b=a.trim(f.url.val());b&&d!==b&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(b)&&(f.url.val("http://"+b),d=b)},open:function(b,d,g,h){var i,j=a(document.body);j.addClass("modal-open"),e=h,wpLink.range=null,b&&(window.wpActiveEditor=b),window.wpActiveEditor&&(this.textarea=a("#"+window.wpActiveEditor).get(0),"undefined"!=typeof tinymce&&(j.append(f.backdrop,f.wrap),i=tinymce.get(wpActiveEditor),c=i&&!i.isHidden()?i:null,c&&tinymce.isIE&&!c.windowManager.wplinkBookmark&&(c.windowManager.wplinkBookmark=c.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),f.wrap.show(),f.backdrop.show(),wpLink.refresh(d,g),a(document).trigger("wplink-open",f.wrap))},isMCE:function(){return c&&!c.isHidden()},refresh:function(a,b){var c="";wpLink.isMCE()?wpLink.mceRefresh(a,b):(f.wrap.hasClass("has-text-field")||f.wrap.addClass("has-text-field"),document.selection?c=document.selection.createRange().text||b||"":"undefined"!=typeof this.textarea.selectionStart&&this.textarea.selectionStart!==this.textarea.selectionEnd&&(b=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd)||b||""),f.text.val(b),wpLink.setDefaultValues()),g?f.url.focus().blur():window.setTimeout(function(){f.url.focus()[0].select()}),d=f.url.val().replace(/^http:\/\//,"")},hasSelectedText:function(a){var b,d,e,f=c.selection.getContent();if(/</.test(f)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(f)||-1===f.indexOf("href=")))return!1;if(a){if(d=a.childNodes,0===d.length)return!1;for(e=d.length-1;e>=0;e--)if(b=d[e],3!=b.nodeType&&!tinymce.dom.BookmarkManager.isBookmarkNode(b))return!1}return!0},mceRefresh:function(a,d){var e,g=b(),h=this.hasSelectedText(g);g?(e=g.innerText||g.textContent,tinymce.trim(e)||(e=d||""),a=a||c.dom.getAttrib(g,"href"),"_wp_link_placeholder"!==a?(f.url.val(a),f.openInNewTab.prop("checked","_blank"===c.dom.getAttrib(g,"target")),f.submit.val(wpLinkL10n.update)):this.setDefaultValues(e)):(e=c.selection.getContent({format:"text"})||d||"",this.setDefaultValues(e)),h?(f.text.val(e),f.wrap.addClass("has-text-field")):(f.text.val(""),f.wrap.removeClass("has-text-field"))},close:function(b){a(document.body).removeClass("modal-open"),"noReset"!==b&&(wpLink.isMCE()?(c.plugins.wplink&&c.plugins.wplink.close(),c.focus()):(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select()))),f.backdrop.hide(),f.wrap.hide(),d=!1,a(document).trigger("wplink-close",f.wrap)},getAttrs:function(){return wpLink.correctURL(),{href:a.trim(f.url.val()),target:f.openInNewTab.prop("checked")?"_blank":""}},buildHtml:function(a){var b='<a href="'+a.href+'"';return a.target&&(b+=' target="'+a.target+'"'),b+">"},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var a,b,c,d,e,g,h,i=wpLink.textarea;i&&(a=wpLink.getAttrs(),b=f.text.val(),a.href&&(c=wpLink.buildHtml(a),document.selection&&wpLink.range?(i.focus(),wpLink.range.text=c+(b||wpLink.range.text)+"</a>",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):"undefined"!=typeof i.selectionStart&&(d=i.selectionStart,e=i.selectionEnd,h=b||i.value.substring(d,e),c=c+h+"</a>",g=d+c.length,d!==e||h||(g-=4),i.value=i.value.substring(0,d)+c+i.value.substring(e,i.value.length),i.selectionStart=i.selectionEnd=g),wpLink.close(),i.focus()))},mceUpdate:function(){var a,d,e=wpLink.getAttrs();return c.focus(),tinymce.isIE&&c.windowManager.wplinkBookmark&&(c.selection.moveToBookmark(c.windowManager.wplinkBookmark),c.windowManager.wplinkBookmark=null),e.href?(a=b(),f.wrap.hasClass("has-text-field")&&(d=f.text.val()||e.href),a?(d&&("innerText"in a?a.innerText=d:a.textContent=d),c.dom.setAttribs(a,e)):d?c.selection.setNode(c.dom.create("a",e,c.dom.encode(d))):c.execCommand("mceInsertLink",!1,e),wpLink.close("noReset"),c.focus(),void c.nodeChanged()):(c.execCommand("unlink"),void wpLink.close())},keydown:function(a){var b;27===a.keyCode?(wpLink.close(),a.stopImmediatePropagation()):9===a.keyCode&&(b=a.target.id,"wp-link-submit"!==b||a.shiftKey?"wp-link-close"===b&&a.shiftKey&&(f.submit.focus(),a.preventDefault()):(f.close.focus(),a.preventDefault()))},getUrlFromSelection:function(a){var b=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,d=/^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;return a||(this.isMCE()?a=c.selection.getContent({format:"text"}):document.selection&&wpLink.range?a=wpLink.range.text:"undefined"!=typeof this.textarea.selectionStart&&(a=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd))),a=tinymce.trim(a),a&&b.test(a)?"mailto:"+a:a&&d.test(a)?a.replace(/&|�?38;/gi,"&"):""},setDefaultValues:function(a){f.url.val(this.getUrlFromSelection(a)),f.submit.val(wpLinkL10n.save)}},a(document).ready(wpLink.init)}(jQuery); |