3 ( function( $, wpLinkL10n, wp ) {
4 var editor, searchTimer, River, Query, correctedURL, linkNode,
5 emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i,
6 urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i,
9 isTouch = ( 'ontouchend' in document );
12 return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
16 timeToTriggerRiver: 150,
17 minRiverAJAXDuration: 200,
18 riverBottomThreshold: 5,
25 inputs.wrap = $('#wp-link-wrap');
26 inputs.dialog = $( '#wp-link' );
27 inputs.backdrop = $( '#wp-link-backdrop' );
28 inputs.submit = $( '#wp-link-submit' );
29 inputs.close = $( '#wp-link-close' );
32 inputs.text = $( '#wp-link-text' );
33 inputs.url = $( '#wp-link-url' );
34 inputs.nonce = $( '#_ajax_linking_nonce' );
35 inputs.openInNewTab = $( '#wp-link-target' );
36 inputs.search = $( '#wp-link-search' );
39 rivers.search = new River( $( '#search-results' ) );
40 rivers.recent = new River( $( '#most-recent-results' ) );
41 rivers.elements = inputs.dialog.find( '.query-results' );
43 // Get search notice text
44 inputs.queryNotice = $( '#query-notice-message' );
45 inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
46 inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
48 // Bind event handlers
49 inputs.dialog.keydown( wpLink.keydown );
50 inputs.dialog.keyup( wpLink.keyup );
51 inputs.submit.click( function( event ) {
52 event.preventDefault();
56 inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel button' ).click( function( event ) {
57 event.preventDefault();
61 rivers.elements.on( 'river-select', wpLink.updateFields );
63 // Display 'hint' message when search field or 'query-results' box are focused
64 inputs.search.on( 'focus.wplink', function() {
65 inputs.queryNoticeTextDefault.hide();
66 inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
67 } ).on( 'blur.wplink', function() {
68 inputs.queryNoticeTextDefault.show();
69 inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide();
72 inputs.search.on( 'keyup input', function() {
73 window.clearTimeout( searchTimer );
74 searchTimer = window.setTimeout( function() {
75 wpLink.searchInternalLinks();
79 inputs.url.on( 'paste', function() {
80 setTimeout( wpLink.correctURL, 0 );
83 inputs.url.on( 'blur', wpLink.correctURL );
86 // If URL wasn't corrected last time and doesn't start with http:, https:, ? # or /, prepend http://
87 correctURL: function () {
88 var url = $.trim( inputs.url.val() );
90 if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
91 inputs.url.val( 'http://' + url );
96 open: function( editorId, url, text, node ) {
98 $body = $( document.body );
100 $body.addClass( 'modal-open' );
101 wpLink.modalOpen = true;
107 window.wpActiveEditor = editorId;
110 if ( ! window.wpActiveEditor ) {
114 this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
116 if ( typeof window.tinymce !== 'undefined' ) {
117 // Make sure the link wrapper is the last element in the body,
118 // or the inline editor toolbar may show above the backdrop.
119 $body.append( inputs.backdrop, inputs.wrap );
121 ed = window.tinymce.get( window.wpActiveEditor );
123 if ( ed && ! ed.isHidden() ) {
129 if ( editor && window.tinymce.isIE ) {
130 editor.windowManager.wplinkBookmark = editor.selection.getBookmark();
134 if ( ! wpLink.isMCE() && document.selection ) {
135 this.textarea.focus();
136 this.range = document.selection.createRange();
140 inputs.backdrop.show();
142 wpLink.refresh( url, text );
144 $( document ).trigger( 'wplink-open', inputs.wrap );
148 return editor && ! editor.isHidden();
151 refresh: function( url, text ) {
154 // Refresh rivers (clear links, check visibility)
155 rivers.search.refresh();
156 rivers.recent.refresh();
158 if ( wpLink.isMCE() ) {
159 wpLink.mceRefresh( url, text );
161 // For the Text editor the "Link text" field is always shown
162 if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
163 inputs.wrap.addClass( 'has-text-field' );
166 if ( document.selection ) {
168 linkText = document.selection.createRange().text || text || '';
169 } else if ( typeof this.textarea.selectionStart !== 'undefined' &&
170 ( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
172 text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
175 inputs.text.val( text );
176 wpLink.setDefaultValues();
180 // Close the onscreen keyboard
181 inputs.url.focus().blur();
183 // Focus the URL field and highlight its contents.
184 // If this is moved above the selection changes,
185 // IE will show a flashing cursor over the dialog.
186 window.setTimeout( function() {
187 inputs.url[0].select();
192 // Load the most recent results if this is the first time opening the panel.
193 if ( ! rivers.recent.ul.children().length ) {
194 rivers.recent.ajax();
197 correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
200 hasSelectedText: function( linkNode ) {
201 var node, nodes, i, html = editor.selection.getContent();
203 // Partial html and not a fully selected anchor element
204 if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
209 nodes = linkNode.childNodes;
211 if ( nodes.length === 0 ) {
215 for ( i = nodes.length - 1; i >= 0; i-- ) {
218 if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
227 mceRefresh: function( searchStr, text ) {
229 linkNode = getLink(),
230 onlyText = this.hasSelectedText( linkNode );
233 linkText = linkNode.textContent || linkNode.innerText;
234 href = editor.dom.getAttrib( linkNode, 'href' );
236 if ( ! $.trim( linkText ) ) {
237 linkText = text || '';
240 if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
244 if ( href !== '_wp_link_placeholder' ) {
245 inputs.url.val( href );
246 inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
247 inputs.submit.val( wpLinkL10n.update );
249 this.setDefaultValues( linkText );
252 if ( searchStr && searchStr !== href ) {
253 // The user has typed something in the inline dialog. Trigger a search with it.
254 inputs.search.val( searchStr );
256 inputs.search.val( '' );
259 // Always reset the search
260 window.setTimeout( function() {
261 wpLink.searchInternalLinks();
264 linkText = editor.selection.getContent({ format: 'text' }) || text || '';
265 this.setDefaultValues( linkText );
269 inputs.text.val( linkText );
270 inputs.wrap.addClass( 'has-text-field' );
272 inputs.text.val( '' );
273 inputs.wrap.removeClass( 'has-text-field' );
277 close: function( reset ) {
278 $( document.body ).removeClass( 'modal-open' );
279 wpLink.modalOpen = false;
281 if ( reset !== 'noReset' ) {
282 if ( ! wpLink.isMCE() ) {
283 wpLink.textarea.focus();
285 if ( wpLink.range ) {
286 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
287 wpLink.range.select();
290 if ( editor.plugins.wplink ) {
291 editor.plugins.wplink.close();
298 inputs.backdrop.hide();
301 correctedURL = false;
303 $( document ).trigger( 'wplink-close', inputs.wrap );
306 getAttrs: function() {
310 href: $.trim( inputs.url.val() ),
311 target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : ''
315 buildHtml: function(attrs) {
316 var html = '<a href="' + attrs.href + '"';
318 if ( attrs.target ) {
319 html += ' target="' + attrs.target + '"';
326 if ( wpLink.isMCE() ) {
333 htmlUpdate: function() {
334 var attrs, text, html, begin, end, cursor, selection,
335 textarea = wpLink.textarea;
341 attrs = wpLink.getAttrs();
342 text = inputs.text.val();
344 // If there's no href, return.
345 if ( ! attrs.href ) {
349 html = wpLink.buildHtml(attrs);
352 if ( document.selection && wpLink.range ) {
354 // Note: If no text is selected, IE will not place the cursor
355 // inside the closing tag.
357 wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
358 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
359 wpLink.range.select();
362 } else if ( typeof textarea.selectionStart !== 'undefined' ) {
364 begin = textarea.selectionStart;
365 end = textarea.selectionEnd;
366 selection = text || textarea.value.substring( begin, end );
367 html = html + selection + '</a>';
368 cursor = begin + html.length;
370 // If no text is selected, place the cursor inside the closing tag.
371 if ( begin === end && ! selection ) {
376 textarea.value.substring( 0, begin ) +
378 textarea.value.substring( end, textarea.value.length )
381 // Update cursor position
382 textarea.selectionStart = textarea.selectionEnd = cursor;
388 // Audible confirmation message when a link has been inserted in the Editor.
389 wp.a11y.speak( wpLinkL10n.linkInserted );
392 mceUpdate: function() {
393 var attrs = wpLink.getAttrs(),
396 if ( window.tinymce.isIE && editor.windowManager.wplinkBookmark ) {
397 editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
398 editor.windowManager.wplinkBookmark = null;
401 if ( ! attrs.href ) {
402 editor.execCommand( 'unlink' );
409 if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
410 text = inputs.text.val() || attrs.href;
415 if ( 'innerText' in link ) {
416 link.innerText = text;
418 link.textContent = text;
422 // Not editing any more
423 attrs['data-wplink-edit'] = null;
424 editor.dom.setAttribs( link, attrs );
427 editor.selection.setNode( editor.dom.create( 'a', attrs, editor.dom.encode( text ) ) );
429 editor.execCommand( 'mceInsertLink', false, attrs );
433 wpLink.close( 'noReset' );
435 editor.nodeChanged();
437 if ( link && editor.plugins.wplink ) {
438 editor.plugins.wplink.checkLink( link );
441 // Audible confirmation message when a link has been inserted in the Editor.
442 wp.a11y.speak( wpLinkL10n.linkInserted );
445 updateFields: function( e, li ) {
446 inputs.url.val( li.children( '.item-permalink' ).val() );
449 getUrlFromSelection: function( selection ) {
451 if ( this.isMCE() ) {
452 selection = editor.selection.getContent({ format: 'text' });
453 } else if ( document.selection && wpLink.range ) {
454 selection = wpLink.range.text;
455 } else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
456 selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
460 selection = $.trim( selection );
462 if ( selection && emailRegexp.test( selection ) ) {
463 // Selection is email address
464 return 'mailto:' + selection;
465 } else if ( selection && urlRegexp.test( selection ) ) {
467 return selection.replace( /&|�?38;/gi, '&' );
473 setDefaultValues: function( selection ) {
474 inputs.url.val( this.getUrlFromSelection( selection ) );
476 // Empty the search field and swap the "rivers".
477 inputs.search.val('');
478 wpLink.searchInternalLinks();
480 // Update save prompt.
481 inputs.submit.val( wpLinkL10n.save );
484 searchInternalLinks: function() {
486 search = inputs.search.val() || '';
488 if ( search.length > 2 ) {
489 rivers.recent.hide();
490 rivers.search.show();
492 // Don't search if the keypress didn't change the title.
493 if ( wpLink.lastSearch == search )
496 wpLink.lastSearch = search;
497 waiting = inputs.search.parent().find( '.spinner' ).addClass( 'is-active' );
499 rivers.search.change( search );
500 rivers.search.ajax( function() {
501 waiting.removeClass( 'is-active' );
504 rivers.search.hide();
505 rivers.recent.show();
510 rivers.search.next();
511 rivers.recent.next();
515 rivers.search.prev();
516 rivers.recent.prev();
519 keydown: function( event ) {
523 if ( 27 === event.keyCode ) {
525 event.stopImmediatePropagation();
527 } else if ( 9 === event.keyCode ) {
528 id = event.target.id;
530 // wp-link-submit must always be the last focusable element in the dialog.
531 // following focusable elements will be skipped on keyboard navigation.
532 if ( id === 'wp-link-submit' && ! event.shiftKey ) {
533 inputs.close.focus();
534 event.preventDefault();
535 } else if ( id === 'wp-link-close' && event.shiftKey ) {
536 inputs.submit.focus();
537 event.preventDefault();
541 // Up Arrow and Down Arrow keys.
542 if ( 38 !== event.keyCode && 40 !== event.keyCode ) {
546 if ( document.activeElement &&
547 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
552 fn = 38 === event.keyCode ? 'prev' : 'next';
553 clearInterval( wpLink.keyInterval );
555 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
556 event.preventDefault();
559 keyup: function( event ) {
560 // Up Arrow and Down Arrow keys.
561 if ( 38 === event.keyCode || 40 === event.keyCode ) {
562 clearInterval( wpLink.keyInterval );
563 event.preventDefault();
567 delayedCallback: function( func, delay ) {
568 var timeoutTriggered, funcTriggered, funcArgs, funcContext;
573 setTimeout( function() {
575 return func.apply( funcContext, funcArgs );
577 timeoutTriggered = true;
581 if ( timeoutTriggered )
582 return func.apply( this, arguments );
584 funcArgs = arguments;
586 funcTriggered = true;
591 River = function( element, search ) {
593 this.element = element;
594 this.ul = element.children( 'ul' );
595 this.contentHeight = element.children( '#link-selector-height' );
596 this.waiting = element.find('.river-waiting');
598 this.change( search );
601 $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
604 element.on( 'click', 'li', function( event ) {
605 self.select( $( this ), event );
609 $.extend( River.prototype, {
610 refresh: function() {
612 this.visible = this.element.is( ':visible' );
615 if ( ! this.visible ) {
623 this.visible = false;
625 // Selects a list item and triggers the river-select event.
626 select: function( li, event ) {
627 var liHeight, elHeight, liTop, elTop;
629 if ( li.hasClass( 'unselectable' ) || li == this.selected )
633 this.selected = li.addClass( 'selected' );
634 // Make sure the element is visible
635 liHeight = li.outerHeight();
636 elHeight = this.element.height();
637 liTop = li.position().top;
638 elTop = this.element.scrollTop();
640 if ( liTop < 0 ) // Make first visible element
641 this.element.scrollTop( elTop + liTop );
642 else if ( liTop + liHeight > elHeight ) // Make last visible element
643 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
645 // Trigger the river-select event
646 this.element.trigger( 'river-select', [ li, event, this ] );
648 deselect: function() {
650 this.selected.removeClass( 'selected' );
651 this.selected = false;
654 if ( ! this.visible )
658 if ( this.selected ) {
659 to = this.selected.prev( 'li' );
665 if ( ! this.visible )
668 var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
672 ajax: function( callback ) {
674 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
675 response = wpLink.delayedCallback( function( results, params ) {
676 self.process( results, params );
678 callback( results, params );
681 this.query.ajax( response );
683 change: function( search ) {
684 if ( this.query && this._search == search )
687 this._search = search;
688 this.query = new Query( search );
689 this.element.scrollTop( 0 );
691 process: function( results, params ) {
692 var list = '', alt = true, classes = '',
693 firstPage = params.page == 1;
697 list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
698 wpLinkL10n.noMatchesFound + '</em></span></li>';
701 $.each( results, function() {
702 classes = alt ? 'alternate' : '';
703 classes += this.title ? '' : ' no-title';
704 list += classes ? '<li class="' + classes + '">' : '<li>';
705 list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
706 list += '<span class="item-title">';
707 list += this.title ? this.title : wpLinkL10n.noTitle;
708 list += '</span><span class="item-info">' + this.info + '</span></li>';
713 this.ul[ firstPage ? 'html' : 'append' ]( list );
715 maybeLoad: function() {
718 bottom = el.scrollTop() + el.height();
720 if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
723 setTimeout(function() {
724 var newTop = el.scrollTop(),
725 newBottom = newTop + el.height();
727 if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
730 self.waiting.addClass( 'is-active' );
731 el.scrollTop( newTop + self.waiting.outerHeight() );
733 self.ajax( function() {
734 self.waiting.removeClass( 'is-active' );
736 }, wpLink.timeToTriggerRiver );
740 Query = function( search ) {
742 this.allLoaded = false;
743 this.querying = false;
744 this.search = search;
747 $.extend( Query.prototype, {
749 return ! ( this.querying || this.allLoaded );
751 ajax: function( callback ) {
754 action : 'wp-link-ajax',
756 '_ajax_linking_nonce' : inputs.nonce.val()
760 query.search = this.search;
762 this.querying = true;
764 $.post( window.ajaxurl, query, function( r ) {
766 self.querying = false;
767 self.allLoaded = ! r;
768 callback( r, query );
773 $( document ).ready( wpLink.init );
774 })( jQuery, window.wpLinkL10n, window.wp );