WordPress/wp-includes/js/wplink.min.js
Andrew Ozz 1750c89afb wpLink:
- Prepend 'http://' to pasted URLs.
- Do not prepend it when typing an URL.
- Do not prepend it when pasting the same URL for the second time (trying to correct wrong guess).
Props iseulde. Fixes #18149.
Built from https://develop.svn.wordpress.org/trunk@31602


git-svn-id: http://core.svn.wordpress.org/trunk@31583 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-03-03 00:01:28 +00:00

1 line
9.0 KiB
JavaScript

var wpLink;!function(a){var b,c,d,e,f,g={},h={},i="ontouchend"in document;wpLink={timeToTriggerRiver:150,minRiverAJAXDuration:200,riverBottomThreshold:5,keySensitivity:100,lastSearch:"",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.url=a("#url-field"),g.nonce=a("#_ajax_linking_nonce"),g.title=a("#link-title-field"),g.openInNewTab=a("#link-target-checkbox"),g.search=a("#search-field"),h.search=new d(a("#search-results")),h.recent=new d(a("#most-recent-results")),h.elements=g.dialog.find(".query-results"),g.queryNotice=a("#query-notice-message"),g.queryNoticeTextDefault=g.queryNotice.find(".query-notice-default"),g.queryNoticeTextHint=g.queryNotice.find(".query-notice-hint"),g.dialog.keydown(wpLink.keydown),g.dialog.keyup(wpLink.keyup),g.submit.click(function(a){a.preventDefault(),wpLink.update()}),g.close.add(g.backdrop).add("#wp-link-cancel a").click(function(a){a.preventDefault(),wpLink.close()}),a("#wp-link-search-toggle").on("click",wpLink.toggleInternalLinking),h.elements.on("river-select",wpLink.updateFields),g.search.on("focus.wplink",function(){g.queryNoticeTextDefault.hide(),g.queryNoticeTextHint.removeClass("screen-reader-text").show()}).on("blur.wplink",function(){g.queryNoticeTextDefault.show(),g.queryNoticeTextHint.addClass("screen-reader-text").hide()}),g.search.keyup(function(){var a=this;window.clearTimeout(c),c=window.setTimeout(function(){wpLink.searchInternalLinks.call(a)},500)}),g.url.on("paste blur",function(){setTimeout(function(){var b=a.trim(g.url.val());b&&f!==b&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(b)&&(g.url.val("http://"+b),f=b)},0)})},open:function(c){var d;a(document.body).addClass("modal-open"),wpLink.range=null,c&&(window.wpActiveEditor=c),window.wpActiveEditor&&(this.textarea=a("#"+window.wpActiveEditor).get(0),"undefined"!=typeof tinymce&&(d=tinymce.get(wpActiveEditor),b=d&&!d.isHidden()?d:null,b&&tinymce.isIE&&(b.windowManager.bookmark=b.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),g.wrap.show(),g.backdrop.show(),wpLink.refresh(),a(document).trigger("wplink-open",g.wrap))},isMCE:function(){return b&&!b.isHidden()},refresh:function(){h.search.refresh(),h.recent.refresh(),wpLink.isMCE()?wpLink.mceRefresh():wpLink.setDefaultValues(),i?g.url.focus().blur():g.url.focus()[0].select(),h.recent.ul.children().length||h.recent.ajax()},mceRefresh:function(){var a;(a=b.dom.getParent(b.selection.getNode(),"A"))?(g.url.val(b.dom.getAttrib(a,"href")),g.title.val(b.dom.getAttrib(a,"title")),g.openInNewTab.prop("checked","_blank"===b.dom.getAttrib(a,"target")),g.submit.val(wpLinkL10n.update)):wpLink.setDefaultValues()},close:function(){a(document.body).removeClass("modal-open"),wpLink.isMCE()?b.focus():(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select())),g.backdrop.hide(),g.wrap.hide(),f=!1,a(document).trigger("wplink-close",g.wrap)},getAttrs:function(){return{href:g.url.val(),title:g.title.val(),target:g.openInNewTab.prop("checked")?"_blank":""}},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var a,b,c,d,e,f,g,h=wpLink.textarea;h&&(a=wpLink.getAttrs(),a.href&&(b='<a href="'+a.href+'"',a.title&&(f=a.title.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;"),b+=' title="'+f+'"'),a.target&&(b+=' target="'+a.target+'"'),b+=">",document.selection&&wpLink.range?(h.focus(),wpLink.range.text=b+wpLink.range.text+"</a>",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):"undefined"!=typeof h.selectionStart&&(c=h.selectionStart,d=h.selectionEnd,g=h.value.substring(c,d),b=b+g+"</a>",e=c+b.length,c==d&&(e-="</a>".length),h.value=h.value.substring(0,c)+b+h.value.substring(d,h.value.length),h.selectionStart=h.selectionEnd=e),wpLink.close(),h.focus()))},mceUpdate:function(){var a,c=wpLink.getAttrs();return wpLink.close(),b.focus(),tinymce.isIE&&b.selection.moveToBookmark(b.windowManager.bookmark),a=b.dom.getParent(b.selection.getNode(),"a[href]"),c.href&&"http://"!=c.href?(a?b.dom.setAttribs(a,c):b.execCommand("mceInsertLink",!1,c),void b.selection.collapse()):void b.execCommand("unlink")},updateFields:function(a,b){g.url.val(b.children(".item-permalink").val()),g.title.val(b.hasClass("no-title")?"":b.children(".item-title").text())},setDefaultValues:function(){var a=b&&b.selection.getContent(),c=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,d=/^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;g.url.val(a&&c.test(a)?"mailto:"+a:a&&d.test(a)?a.replace(/&amp;|&#0?38;/gi,"&"):""),g.title.val(""),g.submit.val(wpLinkL10n.save)},searchInternalLinks:function(){var b,c=a(this),d=c.val();if(d.length>2){if(h.recent.hide(),h.search.show(),wpLink.lastSearch==d)return;wpLink.lastSearch=d,b=c.parent().find(".spinner").show(),h.search.change(d),h.search.ajax(function(){b.hide()})}else h.search.hide(),h.recent.show()},next:function(){h.search.next(),h.recent.next()},prev:function(){h.search.prev(),h.recent.prev()},keydown:function(b){var c,d,e=a.ui.keyCode;e.ESCAPE===b.keyCode?(wpLink.close(),b.stopImmediatePropagation()):e.TAB===b.keyCode&&(d=b.target.id,"wp-link-submit"!==d||b.shiftKey?"wp-link-close"===d&&b.shiftKey&&(g.submit.focus(),b.preventDefault()):(g.close.focus(),b.preventDefault())),(b.keyCode===e.UP||b.keyCode===e.DOWN)&&(!document.activeElement||"link-title-field"!==document.activeElement.id&&"url-field"!==document.activeElement.id)&&(c=b.keyCode===e.UP?"prev":"next",clearInterval(wpLink.keyInterval),wpLink[c](),wpLink.keyInterval=setInterval(wpLink[c],wpLink.keySensitivity),b.preventDefault())},keyup:function(b){var c=a.ui.keyCode;(b.which===c.UP||b.which===c.DOWN)&&(clearInterval(wpLink.keyInterval),b.preventDefault())},delayedCallback:function(a,b){var c,d,e,f;return b?(setTimeout(function(){return d?a.apply(f,e):void(c=!0)},b),function(){return c?a.apply(this,arguments):(e=arguments,f=this,void(d=!0))}):a},toggleInternalLinking:function(a){var b=g.wrap.hasClass("search-panel-visible");g.wrap.toggleClass("search-panel-visible",!b),setUserSetting("wplink",b?"0":"1"),g[b?"url":"search"].focus(),a.preventDefault()}},d=function(b,c){var d=this;this.element=b,this.ul=b.children("ul"),this.contentHeight=b.children("#link-selector-height"),this.waiting=b.find(".river-waiting"),this.change(c),this.refresh(),a("#wp-link .query-results, #wp-link #link-selector").scroll(function(){d.maybeLoad()}),b.on("click","li",function(b){d.select(a(this),b)})},a.extend(d.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(a,b){var c,d,e,f;a.hasClass("unselectable")||a==this.selected||(this.deselect(),this.selected=a.addClass("selected"),c=a.outerHeight(),d=this.element.height(),e=a.position().top,f=this.element.scrollTop(),0>e?this.element.scrollTop(f+e):e+c>d&&this.element.scrollTop(f+e-d+c),this.element.trigger("river-select",[a,b,this]))},deselect:function(){this.selected&&this.selected.removeClass("selected"),this.selected=!1},prev:function(){if(this.visible){var a;this.selected&&(a=this.selected.prev("li"),a.length&&this.select(a))}},next:function(){if(this.visible){var b=this.selected?this.selected.next("li"):a("li:not(.unselectable):first",this.element);b.length&&this.select(b)}},ajax:function(a){var b=this,c=1==this.query.page?0:wpLink.minRiverAJAXDuration,d=wpLink.delayedCallback(function(c,d){b.process(c,d),a&&a(c,d)},c);this.query.ajax(d)},change:function(a){this.query&&this._search==a||(this._search=a,this.query=new e(a),this.element.scrollTop(0))},process:function(b,c){var d="",e=!0,f="",g=1==c.page;b?a.each(b,function(){f=e?"alternate":"",f+=this.title?"":" no-title",d+=f?'<li class="'+f+'">':"<li>",d+='<input type="hidden" class="item-permalink" value="'+this.permalink+'" />',d+='<span class="item-title">',d+=this.title?this.title:wpLinkL10n.noTitle,d+='</span><span class="item-info">'+this.info+"</span></li>",e=!e}):g&&(d+='<li class="unselectable no-matches-found"><span class="item-title"><em>'+wpLinkL10n.noMatchesFound+"</em></span></li>"),this.ul[g?"html":"append"](d)},maybeLoad:function(){var a=this,b=this.element,c=b.scrollTop()+b.height();!this.query.ready()||c<this.contentHeight.height()-wpLink.riverBottomThreshold||setTimeout(function(){var c=b.scrollTop(),d=c+b.height();!a.query.ready()||d<a.contentHeight.height()-wpLink.riverBottomThreshold||(a.waiting.show(),b.scrollTop(c+a.waiting.outerHeight()),a.ajax(function(){a.waiting.hide()}))},wpLink.timeToTriggerRiver)}}),e=function(a){this.page=1,this.allLoaded=!1,this.querying=!1,this.search=a},a.extend(e.prototype,{ready:function(){return!(this.querying||this.allLoaded)},ajax:function(b){var c=this,d={action:"wp-link-ajax",page:this.page,_ajax_linking_nonce:g.nonce.val()};this.search&&(d.search=this.search),this.querying=!0,a.post(ajaxurl,d,function(a){c.page++,c.querying=!1,c.allLoaded=!a,b(a,d)},"json")}}),a(document).ready(wpLink.init)}(jQuery);