WordPress/wp-includes/js/wplink.min.js
Andrew Ozz 40b3bb7988 TinyMCE, inline link:
- Add `uiAutocompleteL10n` with translatable strings for use in UI Autocomplete live region.
- Use the above strings in both the editor plugin and wplink.js.

See #33301.
Built from https://develop.svn.wordpress.org/trunk@36806


git-svn-id: http://core.svn.wordpress.org/trunk@36773 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-03-02 20:06:26 +00:00

1 line
6.9 KiB
JavaScript

var wpLink;!function(a,b){function c(){return f||d.dom.getParent(d.selection.getNode(),"a[href]")}var d,e,f,g={},h="ontouchend"in document;wpLink={textarea:"",init:function(){g.wrap=a("#wp-link-wrap"),g.dialog=a("#wp-link"),g.backdrop=a("#wp-link-backdrop"),g.submit=a("#wp-link-submit"),g.close=a("#wp-link-close"),g.text=a("#wp-link-text"),g.url=a("#wp-link-url"),g.openInNewTab=a("#wp-link-target"),a.ui&&a.ui.autocomplete&&wpLink.setAutocomplete(),g.dialog.on("keydown",wpLink.keydown),g.submit.on("click",function(a){a.preventDefault(),wpLink.update()}),g.close.add(g.backdrop).add("#wp-link-cancel a").click(function(a){a.preventDefault(),wpLink.close()}),g.url.on("paste",function(){setTimeout(wpLink.correctURL,0)})},setAutocomplete:function(){var b,c,d=g.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(b,c){return d.val(c.item.permalink),g.wrap.hasClass("has-text-field")&&""===a.trim(g.text.val())&&g.text.val(c.item.title),!1},open:function(){d.attr("aria-expanded","true")},close:function(){d.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2"},messages:{noResults:"undefined"!=typeof window.uiAutocompleteL10n?window.uiAutocompleteL10n.noResults:"",results:function(a){return"undefined"!=typeof window.uiAutocompleteL10n?a>1?window.uiAutocompleteL10n.manyResults.replace("%d",a):window.uiAutocompleteL10n.oneResult:void 0}}}).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>&nbsp;<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(g.url.val());b&&e!==b&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(b)&&(g.url.val("http://"+b),e=b)},open:function(b,c,e,h){var i,j=a(document.body);j.addClass("modal-open"),f=h,wpLink.range=null,b&&(window.wpActiveEditor=b),window.wpActiveEditor&&(this.textarea=a("#"+window.wpActiveEditor).get(0),"undefined"!=typeof window.tinymce&&(j.append(g.backdrop,g.wrap),i=window.tinymce.get(window.wpActiveEditor),d=i&&!i.isHidden()?i:null,d&&window.tinymce.isIE&&!d.windowManager.wplinkBookmark&&(d.windowManager.wplinkBookmark=d.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),g.wrap.show(),g.backdrop.show(),wpLink.refresh(c,e),a(document).trigger("wplink-open",g.wrap))},isMCE:function(){return d&&!d.isHidden()},refresh:function(a,b){var c="";wpLink.isMCE()?wpLink.mceRefresh(a,b):(g.wrap.hasClass("has-text-field")||g.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||""),g.text.val(b),wpLink.setDefaultValues()),h?g.url.focus().blur():window.setTimeout(function(){g.url.focus()[0].select()}),e=g.url.val().replace(/^http:\/\//,"")},hasSelectedText:function(a){var b,c,e,f=d.selection.getContent();if(/</.test(f)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(f)||-1===f.indexOf("href=")))return!1;if(a){if(c=a.childNodes,0===c.length)return!1;for(e=c.length-1;e>=0;e--)if(b=c[e],3!=b.nodeType&&!window.tinymce.dom.BookmarkManager.isBookmarkNode(b))return!1}return!0},mceRefresh:function(e,f){var h,i=c(),j=this.hasSelectedText(i);i?(h=i.innerText||i.textContent,a.trim(h)||(h=f||""),e=e||d.dom.getAttrib(i,"href"),"_wp_link_placeholder"!==e?(g.url.val(e),g.openInNewTab.prop("checked","_blank"===d.dom.getAttrib(i,"target")),g.submit.val(b.update)):this.setDefaultValues(h)):(h=d.selection.getContent({format:"text"})||f||"",this.setDefaultValues(h)),j?(g.text.val(h),g.wrap.addClass("has-text-field")):(g.text.val(""),g.wrap.removeClass("has-text-field"))},close:function(b){a(document.body).removeClass("modal-open"),"noReset"!==b&&(wpLink.isMCE()?(d.plugins.wplink&&d.plugins.wplink.close(),d.focus()):(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select()))),g.backdrop.hide(),g.wrap.hide(),e=!1,a(document).trigger("wplink-close",g.wrap)},getAttrs:function(){return wpLink.correctURL(),{href:a.trim(g.url.val()),target:g.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,f,h,i=wpLink.textarea;i&&(a=wpLink.getAttrs(),b=g.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>",f=d+c.length,d!==e||h||(f-=4),i.value=i.value.substring(0,d)+c+i.value.substring(e,i.value.length),i.selectionStart=i.selectionEnd=f),wpLink.close(),i.focus()))},mceUpdate:function(){var a,b,e=wpLink.getAttrs();return d.focus(),window.tinymce.isIE&&d.windowManager.wplinkBookmark&&(d.selection.moveToBookmark(d.windowManager.wplinkBookmark),d.windowManager.wplinkBookmark=null),e.href?(a=c(),g.wrap.hasClass("has-text-field")&&(b=g.text.val()||e.href),a?(b&&("innerText"in a?a.innerText=b:a.textContent=b),d.dom.setAttribs(a,e)):b?d.selection.setNode(d.dom.create("a",e,d.dom.encode(b))):d.execCommand("mceInsertLink",!1,e),wpLink.close("noReset"),d.focus(),void d.nodeChanged()):(d.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&&(g.submit.focus(),a.preventDefault()):(g.close.focus(),a.preventDefault()))},getUrlFromSelection:function(b){var c=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,e=/^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;return b||(this.isMCE()?b=d.selection.getContent({format:"text"}):document.selection&&wpLink.range?b=wpLink.range.text:"undefined"!=typeof this.textarea.selectionStart&&(b=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd))),b=a.trim(b),b&&c.test(b)?"mailto:"+b:b&&e.test(b)?b.replace(/&amp;|&#0?38;/gi,"&"):""},setDefaultValues:function(a){g.url.val(this.getUrlFromSelection(a)),g.submit.val(b.save)}},a(document).ready(wpLink.init)}(jQuery,window.wpLinkL10n);