WordPress/wp-includes/js/wplink.min.js

1 line
8.6 KiB
JavaScript

var wpLink;!function(s){var t,e,i,o={},n={};wpLink={timeToTriggerRiver:150,minRiverAJAXDuration:200,riverBottomThreshold:5,keySensitivity:100,lastSearch:"",textarea:"",init:function(){o.dialog=s("#wp-link"),o.submit=s("#wp-link-submit"),o.url=s("#url-field"),o.nonce=s("#_ajax_linking_nonce"),o.title=s("#link-title-field"),o.openInNewTab=s("#link-target-checkbox"),o.search=s("#search-field"),n.search=new e(s("#search-results")),n.recent=new e(s("#most-recent-results")),n.elements=s(".query-results",o.dialog),o.dialog.keydown(wpLink.keydown),o.dialog.keyup(wpLink.keyup),o.submit.click(function(e){e.preventDefault(),wpLink.update()}),s("#wp-link-cancel").click(function(e){e.preventDefault(),wpLink.close()}),s("#internal-toggle").click(wpLink.toggleInternalLinking),n.elements.bind("river-select",wpLink.updateFields),o.search.keyup(wpLink.searchInternalLinks),o.dialog.bind("wpdialogrefresh",wpLink.refresh),o.dialog.bind("wpdialogbeforeopen",wpLink.beforeOpen),o.dialog.bind("wpdialogclose",wpLink.onClose)},beforeOpen:function(){wpLink.range=null,!wpLink.isMCE()&&document.selection&&(wpLink.textarea.focus(),wpLink.range=document.selection.createRange())},open:function(){wpActiveEditor&&(this.textarea=s("#"+wpActiveEditor).get(0),o.dialog.data("wpdialog")||o.dialog.wpdialog({title:wpLinkL10n.title,width:480,height:"auto",modal:!0,dialogClass:"wp-dialog"}),o.dialog.wpdialog("open"))},isMCE:function(){return tinyMCEPopup&&(t=tinyMCEPopup.editor)&&!t.isHidden()},refresh:function(){n.search.refresh(),n.recent.refresh(),wpLink.isMCE()?wpLink.mceRefresh():wpLink.setDefaultValues(),o.url.focus()[0].select(),n.recent.ul.children().length||n.recent.ajax()},mceRefresh:function(){var e;t=tinyMCEPopup.editor,tinyMCEPopup.restoreSelection(),(e=t.dom.getParent(t.selection.getNode(),"A"))?(o.url.val(t.dom.getAttrib(e,"href")),o.title.val(t.dom.getAttrib(e,"title")),o.openInNewTab.prop("checked","_blank"==t.dom.getAttrib(e,"target")),o.submit.val(wpLinkL10n.update)):wpLink.setDefaultValues()},close:function(){wpLink.isMCE()?tinyMCEPopup.close():o.dialog.wpdialog("close")},onClose:function(){wpLink.isMCE()||(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select()))},getAttrs:function(){return{href:o.url.val(),title:o.title.val(),target:o.openInNewTab.prop("checked")?"_blank":""}},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var e,t,i,n,l=wpLink.textarea;l&&(i=wpLink.getAttrs(),(n=document.createElement("a")).href=i.href,"javascript:"!==n.protocol&&"data:"!==n.protocol||(i.href=""),i.href&&"http://"!=i.href&&(e='<a href="'+i.href+'"',i.title&&(e+=' title="'+i.title+'"'),i.target&&(e+=' rel="noopener" target="'+i.target+'"'),e+=">",document.selection&&wpLink.range?(l.focus(),wpLink.range.text=e+wpLink.range.text+"</a>",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):void 0!==l.selectionStart&&(t=l.selectionStart,n=l.selectionEnd,selection=l.value.substring(t,n),i=t+(e=e+selection+"</a>").length,t==n&&(i-="</a>".length),l.value=l.value.substring(0,t)+e+l.value.substring(n,l.value.length),l.selectionStart=l.selectionEnd=i),wpLink.close(),l.focus()))},mceUpdate:function(){var t,i=tinyMCEPopup.editor,n=wpLink.getAttrs();tinyMCEPopup.restoreSelection(),t=i.dom.getParent(i.selection.getNode(),"A");var e=document.createElement("a");e.href=n.href,"javascript:"!==e.protocol&&"data:"!==e.protocol||(n.href=""),n.href&&"http://"!=n.href?(null==t?(i.getDoc().execCommand("unlink",!1,null),tinyMCEPopup.execCommand("mceInsertLink",!1,"#mce_temp_url#",{skip_undo:1}),tinymce.each(i.dom.select("a"),function(e){"#mce_temp_url#"==i.dom.getAttrib(e,"href")&&(t=e,i.dom.setAttribs(t,n))}),tinymce.isWebKit&&"#mce_temp_url#"==s(t).text()&&(i.dom.remove(t),t=null)):i.dom.setAttribs(t,n),!t||1==t.childNodes.length&&"IMG"==t.firstChild.nodeName||(i.selection.select(t),i.selection.collapse(0),tinyMCEPopup.storeSelection()),i.execCommand("mceEndUndoLevel"),wpLink.close(),i.focus()):t&&(e=i.selection.getBookmark(),i.dom.remove(t,1),i.selection.moveToBookmark(e),tinyMCEPopup.execCommand("mceEndUndoLevel"),wpLink.close())},updateFields:function(e,t,i){o.url.val(t.children(".item-permalink").val()),o.title.val(t.hasClass("no-title")?"":t.children(".item-title").text()),i&&"click"==i.type&&o.url.focus()},setDefaultValues:function(){o.url.val("http://"),o.title.val(""),o.submit.val(wpLinkL10n.save)},searchInternalLinks:function(){var e,t=s(this),i=t.val();2<i.length?(n.recent.hide(),n.search.show(),wpLink.lastSearch!=i&&(wpLink.lastSearch=i,e=t.parent().find(".spinner").show(),n.search.change(i),n.search.ajax(function(){e.hide()}))):(n.search.hide(),n.recent.show())},next:function(){n.search.next(),n.recent.next()},prev:function(){n.search.prev(),n.recent.prev()},keydown:function(e){var t,i=s.ui.keyCode;switch(e.which){case i.UP:t="prev";case i.DOWN:t=t||"next",clearInterval(wpLink.keyInterval),wpLink[t](),wpLink.keyInterval=setInterval(wpLink[t],wpLink.keySensitivity);break;default:return}e.preventDefault()},keyup:function(e){var t=s.ui.keyCode;switch(e.which){case t.ESCAPE:return e.stopImmediatePropagation(),s(document).triggerHandler("wp_CloseOnEscape",[{event:e,what:"wplink",cb:wpLink.close}])||wpLink.close(),!1;case t.UP:case t.DOWN:clearInterval(wpLink.keyInterval);break;default:return}e.preventDefault()},delayedCallback:function(e,t){var i,n,l,a;return t?(setTimeout(function(){return n?e.apply(a,l):void(i=!0)},t),function(){if(i)return e.apply(this,arguments);l=arguments,a=this,n=!0}):e},toggleInternalLinking:function(e){var t=s("#search-panel"),n=o.dialog.wpdialog("widget"),l=!t.is(":visible"),a=s(window);s(this).toggleClass("toggle-arrow-active",l),o.dialog.height("auto"),t.slideToggle(300,function(){setUserSetting("wplink",l?"1":"0"),o[l?"search":"url"].focus();var e=a.scrollTop(),t=n.offset().top,i=t+n.outerHeight()-a.height();e<i&&n.animate({top:i<t?t-i:e},200)}),e.preventDefault()}},e=function(e,t){var i=this;this.element=e,this.ul=e.children("ul"),this.waiting=e.find(".river-waiting"),this.change(t),this.refresh(),e.scroll(function(){i.maybeLoad()}),e.delegate("li","click",function(e){i.select(s(this),e)})},s.extend(e.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 i,n,l,a;e.hasClass("unselectable")||e==this.selected||(this.deselect(),this.selected=e.addClass("selected"),i=e.outerHeight(),n=this.element.height(),l=e.position().top,a=this.element.scrollTop(),l<0?this.element.scrollTop(a+l):n<l+i&&this.element.scrollTop(a+l-n+i),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(i){var n=this,e=1==this.query.page?0:wpLink.minRiverAJAXDuration,e=wpLink.delayedCallback(function(e,t){n.process(e,t),i&&i(e,t)},e);this.query.ajax(e)},change:function(e){this.query&&this._search==e||(this._search=e,this.query=new i(e),this.element.scrollTop(0))},process:function(e,t){var i,n="",l=!0,t=1==t.page;e?s.each(e,function(){i=l?"alternate":"",i+=this.title?"":" no-title",n+=i?'<li class="'+i+'">':"<li>",n+='<input type="hidden" class="item-permalink" value="'+this.permalink+'" />',n+='<span class="item-title">',n+=this.title||wpLinkL10n.noTitle,n+='</span><span class="item-info">'+this.info+"</span></li>",l=!l}):t&&(n+='<li class="unselectable"><span class="item-title"><em>'+wpLinkL10n.noMatchesFound+"</em></span></li>"),this.ul[t?"html":"append"](n)},maybeLoad:function(){var i=this,n=this.element,e=n.scrollTop()+n.height();!this.query.ready()||e<this.ul.height()-wpLink.riverBottomThreshold||setTimeout(function(){var e=n.scrollTop(),t=e+n.height();!i.query.ready()||t<i.ul.height()-wpLink.riverBottomThreshold||(i.waiting.show(),n.scrollTop(e+i.waiting.outerHeight()),i.ajax(function(){i.waiting.hide()}))},wpLink.timeToTriggerRiver)}}),i=function(e){this.page=1,this.allLoaded=!1,this.querying=!1,this.search=e},s.extend(i.prototype,{ready:function(){return!(this.querying||this.allLoaded)},ajax:function(t){var i=this,n={action:"wp-link-ajax",page:this.page,_ajax_linking_nonce:o.nonce.val()};this.search&&(n.search=this.search),this.querying=!0,s.post(ajaxurl,n,function(e){i.page++,i.querying=!1,i.allLoaded=!e,t(e,n)},"json")}}),s(document).ready(wpLink.init)}(jQuery);