1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
var wpLink;!function(s,o,l){var c,e,t,n,i,r,u=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,h=/^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i,d={},a={},p="ontouchend"in document;function w(){return r||c.dom.getParent(c.selection.getNode(),"a[href]")}wpLink={timeToTriggerRiver:150,minRiverAJAXDuration:200,riverBottomThreshold:5,keySensitivity:100,lastSearch:"",textarea:"",init:function(){d.wrap=s("#wp-link-wrap"),d.dialog=s("#wp-link"),d.backdrop=s("#wp-link-backdrop"),d.submit=s("#wp-link-submit"),d.close=s("#wp-link-close"),d.text=s("#wp-link-text"),d.url=s("#wp-link-url"),d.nonce=s("#_ajax_linking_nonce"),d.openInNewTab=s("#wp-link-target"),d.search=s("#wp-link-search"),a.search=new t(s("#search-results")),a.recent=new t(s("#most-recent-results")),a.elements=d.dialog.find(".query-results"),d.queryNotice=s("#query-notice-message"),d.queryNoticeTextDefault=d.queryNotice.find(".query-notice-default"),d.queryNoticeTextHint=d.queryNotice.find(".query-notice-hint"),d.dialog.keydown(wpLink.keydown),d.dialog.keyup(wpLink.keyup),d.submit.click(function(e){e.preventDefault(),wpLink.update()}),d.close.add(d.backdrop).add("#wp-link-cancel button").click(function(e){e.preventDefault(),wpLink.close()}),a.elements.on("river-select",wpLink.updateFields),d.search.on("focus.wplink",function(){d.queryNoticeTextDefault.hide(),d.queryNoticeTextHint.removeClass("screen-reader-text").show()}).on("blur.wplink",function(){d.queryNoticeTextDefault.show(),d.queryNoticeTextHint.addClass("screen-reader-text").hide()}),d.search.on("keyup input",function(){window.clearTimeout(e),e=window.setTimeout(function(){wpLink.searchInternalLinks()},500)}),d.url.on("paste",function(){setTimeout(wpLink.correctURL,0)}),d.url.on("blur",wpLink.correctURL)},correctURL:function(){var e=s.trim(d.url.val());e&&i!==e&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(e)&&(d.url.val("http://"+e),i=e)},open:function(e,t,n,i){var a=s(document.body);a.addClass("modal-open"),r=i,wpLink.range=null,e&&(window.wpActiveEditor=e),window.wpActiveEditor&&(this.textarea=s("#"+window.wpActiveEditor).get(0),void 0!==window.tinymce&&(a.append(d.backdrop,d.wrap),a=window.tinymce.get(window.wpActiveEditor),(c=a&&!a.isHidden()?a:null)&&window.tinymce.isIE&&(c.windowManager.wplinkBookmark=c.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),d.wrap.show(),d.backdrop.show(),wpLink.refresh(t,n),s(document).trigger("wplink-open",d.wrap))},isMCE:function(){return c&&!c.isHidden()},refresh:function(e,t){a.search.refresh(),a.recent.refresh(),wpLink.isMCE()?wpLink.mceRefresh(e,t):(d.wrap.hasClass("has-text-field")||d.wrap.addClass("has-text-field"),document.selection?document.selection.createRange().text:void 0!==this.textarea.selectionStart&&this.textarea.selectionStart!==this.textarea.selectionEnd&&(t=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd)||t||""),d.text.val(t),wpLink.setDefaultValues()),p?d.url.focus().blur():window.setTimeout(function(){d.url[0].select(),d.url.focus()}),a.recent.ul.children().length||a.recent.ajax(),i=d.url.val().replace(/^http:\/\//,"")},hasSelectedText:function(e){var t,n,i,a=c.selection.getContent();if(/</.test(a)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(a)||-1===a.indexOf("href=")))return!1;if(e){if(0===(n=e.childNodes).length)return!1;for(i=n.length-1;0<=i;i--)if(3!=(t=n[i]).nodeType&&!window.tinymce.dom.BookmarkManager.isBookmarkNode(t))return!1}return!0},mceRefresh:function(e,t){var n,i,a=w(),r=this.hasSelectedText(a);a?(n=a.innerText||a.textContent,i=c.dom.getAttrib(a,"href"),s.trim(n)||(n=t||""),"_wp_link_placeholder"!==(i=e&&(h.test(e)||u.test(e))?e:i)?(d.url.val(i),d.openInNewTab.prop("checked","_blank"===c.dom.getAttrib(a,"target")),d.submit.val(o.update)):this.setDefaultValues(n),e&&e!==i?d.search.val(e):d.search.val(""),window.setTimeout(function(){wpLink.searchInternalLinks()})):(n=c.selection.getContent({format:"text"})||t||"",this.setDefaultValues(n)),r?(d.text.val(n),d.wrap.addClass("has-text-field")):(d.text.val(""),d.wrap.removeClass("has-text-field"))},close:function(e){s(document.body).removeClass("modal-open"),"noReset"!==e&&(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()))),d.backdrop.hide(),d.wrap.hide(),i=!1,s(document).trigger("wplink-close",d.wrap)},getAttrs:function(){return wpLink.correctURL(),{href:s.trim(d.url.val()),target:d.openInNewTab.prop("checked")?"_blank":""}},buildHtml:function(e){var t='<a href="'+e.href+'"';return e.target&&(t+=' rel="noopener" target="'+e.target+'"'),t+">"},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var e,t,n,i,a,r=wpLink.textarea;r&&(i=wpLink.getAttrs(),n=d.text.val(),(a=document.createElement("a")).href=i.href,"javascript:"!==a.protocol&&"data:"!==a.protocol||(i.href=""),i.href&&(e=wpLink.buildHtml(i),document.selection&&wpLink.range?(r.focus(),wpLink.range.text=e+(n||wpLink.range.text)+"</a>",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):void 0!==r.selectionStart&&(t=r.selectionStart,a=r.selectionEnd,n=t+(e=e+(i=n||r.value.substring(t,a))+"</a>").length,t!==a||i||(n-=4),r.value=r.value.substring(0,t)+e+r.value.substring(a,r.value.length),r.selectionStart=r.selectionEnd=n),wpLink.close(),r.focus(),l.a11y.speak(o.linkInserted)))},mceUpdate:function(){var e,t=wpLink.getAttrs();window.tinymce.isIE&&c.windowManager.wplinkBookmark&&(c.selection.moveToBookmark(c.windowManager.wplinkBookmark),c.windowManager.wplinkBookmark=null);var n=document.createElement("a");if(n.href=t.href,"javascript:"!==n.protocol&&"data:"!==n.protocol||(t.href=""),!t.href)return c.execCommand("unlink"),void wpLink.close();n=w(),d.wrap.hasClass("has-text-field")&&(e=d.text.val()||t.href),n?(e&&("innerText"in n?n.innerText=e:n.textContent=e),t["data-wplink-edit"]=null,c.dom.setAttribs(n,t)):e?c.selection.setNode(c.dom.create("a",t,c.dom.encode(e))):c.execCommand("mceInsertLink",!1,t),wpLink.close("noReset"),c.focus(),c.nodeChanged(),l.a11y.speak(o.linkInserted)},updateFields:function(e,t){d.url.val(t.children(".item-permalink").val())},getUrlFromSelection:function(e){return e||(this.isMCE()?e=c.selection.getContent({format:"text"}):document.selection&&wpLink.range?e=wpLink.range.text:void 0!==this.textarea.selectionStart&&(e=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd))),(e=s.trim(e))&&u.test(e)?"mailto:"+e:e&&h.test(e)?e.replace(/&|�?38;/gi,"&"):""},setDefaultValues:function(e){d.url.val(this.getUrlFromSelection(e)),d.search.val(""),wpLink.searchInternalLinks(),d.submit.val(o.save)},searchInternalLinks:function(){var e,t=d.search.val()||"";2<t.length?(a.recent.hide(),a.search.show(),wpLink.lastSearch!=t&&(wpLink.lastSearch=t,e=d.search.parent().find(".spinner").addClass("is-active"),a.search.change(t),a.search.ajax(function(){e.removeClass("is-active")}))):(a.search.hide(),a.recent.show())},next:function(){a.search.next(),a.recent.next()},prev:function(){a.search.prev(),a.recent.prev()},keydown:function(e){var t;27===e.keyCode?(wpLink.close(),e.stopImmediatePropagation()):9===e.keyCode&&("wp-link-submit"!==(t=e.target.id)||e.shiftKey?"wp-link-close"===t&&e.shiftKey&&(d.submit.focus(),e.preventDefault()):(d.close.focus(),e.preventDefault())),38!==e.keyCode&&40!==e.keyCode||document.activeElement&&("link-title-field"===document.activeElement.id||"url-field"===document.activeElement.id)||(t=38===e.keyCode?"prev":"next",clearInterval(wpLink.keyInterval),wpLink[t](),wpLink.keyInterval=setInterval(wpLink[t],wpLink.keySensitivity),e.preventDefault())},keyup:function(e){38!==e.keyCode&&40!==e.keyCode||(clearInterval(wpLink.keyInterval),e.preventDefault())},delayedCallback:function(e,t){var n,i,a,r;return t?(setTimeout(function(){return i?e.apply(r,a):void(n=!0)},t),function(){if(n)return e.apply(this,arguments);a=arguments,r=this,i=!0}):e}},t=function(e,t){var n=this;this.element=e,this.ul=e.children("ul"),this.contentHeight=e.children("#link-selector-height"),this.waiting=e.find(".river-waiting"),this.change(t),this.refresh(),s("#wp-link .query-results, #wp-link #link-selector").scroll(function(){n.maybeLoad()}),e.on("click","li",function(e){n.select(s(this),e)})},s.extend(t.prototype,{refresh:function(){this.deselect(),this.visible=this.element.is(":visible")},show:function(){this.visible||(this.deselect(),this.element.show(),this.visible=!0)},hide:function(){this.element.hide(),this.visible=!1},select:function(e,t){var n,i,a,r;e.hasClass("unselectable")||e==this.selected||(this.deselect(),this.selected=e.addClass("selected"),n=e.outerHeight(),i=this.element.height(),a=e.position().top,r=this.element.scrollTop(),a<0?this.element.scrollTop(r+a):i<a+n&&this.element.scrollTop(r+a-i+n),this.element.trigger("river-select",[e,t,this]))},deselect:function(){this.selected&&this.selected.removeClass("selected"),this.selected=!1},prev:function(){var e;this.visible&&this.selected&&(e=this.selected.prev("li")).length&&this.select(e)},next:function(){var e;!this.visible||(e=this.selected?this.selected.next("li"):s("li:not(.unselectable):first",this.element)).length&&this.select(e)},ajax:function(n){var i=this,e=1==this.query.page?0:wpLink.minRiverAJAXDuration,e=wpLink.delayedCallback(function(e,t){i.process(e,t),n&&n(e,t)},e);this.query.ajax(e)},change:function(e){this.query&&this._search==e||(this._search=e,this.query=new n(e),this.element.scrollTop(0))},process:function(e,t){var n,i="",a=!0,t=1==t.page;e?s.each(e,function(){n=a?"alternate":"",n+=this.title?"":" no-title",i+=n?'<li class="'+n+'">':"<li>",i+='<input type="hidden" class="item-permalink" value="'+this.permalink+'" />',i+='<span class="item-title">',i+=this.title||o.noTitle,i+='</span><span class="item-info">'+this.info+"</span></li>",a=!a}):t&&(i+='<li class="unselectable no-matches-found"><span class="item-title"><em>'+o.noMatchesFound+"</em></span></li>"),this.ul[t?"html":"append"](i)},maybeLoad:function(){var n=this,i=this.element,e=i.scrollTop()+i.height();!this.query.ready()||e<this.contentHeight.height()-wpLink.riverBottomThreshold||setTimeout(function(){var e=i.scrollTop(),t=e+i.height();!n.query.ready()||t<n.contentHeight.height()-wpLink.riverBottomThreshold||(n.waiting.addClass("is-active"),i.scrollTop(e+n.waiting.outerHeight()),n.ajax(function(){n.waiting.removeClass("is-active")}))},wpLink.timeToTriggerRiver)}}),n=function(e){this.page=1,this.allLoaded=!1,this.querying=!1,this.search=e},s.extend(n.prototype,{ready:function(){return!(this.querying||this.allLoaded)},ajax:function(t){var n=this,i={action:"wp-link-ajax",page:this.page,_ajax_linking_nonce:d.nonce.val()};this.search&&(i.search=this.search),this.querying=!0,s.post(window.ajaxurl,i,function(e){n.page++,n.querying=!1,n.allLoaded=!e,t(e,i)},"json")}}),s(document).ready(wpLink.init)}(jQuery,window.wpLinkL10n,window.wp); |