WordPress/wp-includes/js/wplink.min.js
Andrew Ozz 771cfe22ba TinyMCE, inline link dialog:
- Remove the bottom half of the (old) modal and add autocomplete on the URL field.
- Disable the inline edit dialog in old IE (7, 8 and 9). Use only the modal there.
- Fix in IE10 and 11.
- Fix (most?) remaining edge cases.
- Fix focusing the inline dialog, the modal and the editor.

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


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

1 line
6.2 KiB
JavaScript

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