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() ) {
130 if ( ! wpLink.isMCE() && document.selection ) {
131 this.textarea.focus();
132 this.range = document.selection.createRange();
136 inputs.backdrop.show();
138 wpLink.refresh( url, text );
140 $( document ).trigger( 'wplink-open', inputs.wrap );
144 return editor && ! editor.isHidden();
147 refresh: function( url, text ) {
150 // Refresh rivers (clear links, check visibility)
151 rivers.search.refresh();
152 rivers.recent.refresh();
154 if ( wpLink.isMCE() ) {
155 wpLink.mceRefresh( url, text );
157 // For the Text editor the "Link text" field is always shown
158 if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
159 inputs.wrap.addClass( 'has-text-field' );
162 if ( document.selection ) {
164 linkText = document.selection.createRange().text || text || '';
165 } else if ( typeof this.textarea.selectionStart !== 'undefined' &&
166 ( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
168 text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
171 inputs.text.val( text );
172 wpLink.setDefaultValues();
176 // Close the onscreen keyboard
177 inputs.url.focus().blur();
179 // Focus the URL field and highlight its contents.
180 // If this is moved above the selection changes,
181 // IE will show a flashing cursor over the dialog.
182 window.setTimeout( function() {
183 inputs.url[0].select();
188 // Load the most recent results if this is the first time opening the panel.
189 if ( ! rivers.recent.ul.children().length ) {
190 rivers.recent.ajax();
193 correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
196 hasSelectedText: function( linkNode ) {
197 var node, nodes, i, html = editor.selection.getContent();
199 // Partial html and not a fully selected anchor element
200 if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
205 nodes = linkNode.childNodes;
207 if ( nodes.length === 0 ) {
211 for ( i = nodes.length - 1; i >= 0; i-- ) {
214 if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
223 mceRefresh: function( searchStr, text ) {
225 linkNode = getLink(),
226 onlyText = this.hasSelectedText( linkNode );
229 linkText = linkNode.textContent || linkNode.innerText;
230 href = editor.dom.getAttrib( linkNode, 'href' );
232 if ( ! $.trim( linkText ) ) {
233 linkText = text || '';
236 if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
240 if ( href !== '_wp_link_placeholder' ) {
241 inputs.url.val( href );
242 inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
243 inputs.submit.val( wpLinkL10n.update );
245 this.setDefaultValues( linkText );
248 if ( searchStr && searchStr !== href ) {
249 // The user has typed something in the inline dialog. Trigger a search with it.
250 inputs.search.val( searchStr );
252 inputs.search.val( '' );
255 // Always reset the search
256 window.setTimeout( function() {
257 wpLink.searchInternalLinks();
260 linkText = editor.selection.getContent({ format: 'text' }) || text || '';
261 this.setDefaultValues( linkText );
265 inputs.text.val( linkText );
266 inputs.wrap.addClass( 'has-text-field' );
268 inputs.text.val( '' );
269 inputs.wrap.removeClass( 'has-text-field' );
273 close: function( reset ) {
274 $( document.body ).removeClass( 'modal-open' );
275 wpLink.modalOpen = false;
277 if ( reset !== 'noReset' ) {
278 if ( ! wpLink.isMCE() ) {
279 wpLink.textarea.focus();
281 if ( wpLink.range ) {
282 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
283 wpLink.range.select();
286 if ( editor.plugins.wplink ) {
287 editor.plugins.wplink.close();
294 inputs.backdrop.hide();
297 correctedURL = false;
299 $( document ).trigger( 'wplink-close', inputs.wrap );
302 getAttrs: function() {
306 href: $.trim( inputs.url.val() )
309 if ( inputs.openInNewTab.prop( 'checked' ) ) {
310 attrs.target = '_blank';
316 buildHtml: function(attrs) {
317 var html = '<a href="' + attrs.href + '"';
319 if ( attrs.target ) {
320 html += ' target="' + attrs.target + '"';
327 if ( wpLink.isMCE() ) {
334 htmlUpdate: function() {
335 var attrs, text, html, begin, end, cursor, selection,
336 textarea = wpLink.textarea;
342 attrs = wpLink.getAttrs();
343 text = inputs.text.val();
345 // If there's no href, return.
346 if ( ! attrs.href ) {
350 html = wpLink.buildHtml(attrs);
353 if ( document.selection && wpLink.range ) {
355 // Note: If no text is selected, IE will not place the cursor
356 // inside the closing tag.
358 wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
359 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
360 wpLink.range.select();
363 } else if ( typeof textarea.selectionStart !== 'undefined' ) {
365 begin = textarea.selectionStart;
366 end = textarea.selectionEnd;
367 selection = text || textarea.value.substring( begin, end );
368 html = html + selection + '</a>';
369 cursor = begin + html.length;
371 // If no text is selected, place the cursor inside the closing tag.
372 if ( begin === end && ! selection ) {
377 textarea.value.substring( 0, begin ) +
379 textarea.value.substring( end, textarea.value.length )
382 // Update cursor position
383 textarea.selectionStart = textarea.selectionEnd = cursor;
389 // Audible confirmation message when a link has been inserted in the Editor.
390 wp.a11y.speak( wpLinkL10n.linkInserted );
393 mceUpdate: function() {
394 var attrs = wpLink.getAttrs(),
395 $link, text, hasText, $mceCaret;
397 if ( ! attrs.href ) {
398 editor.execCommand( 'unlink' );
403 $link = editor.$( getLink() );
405 editor.undoManager.transact( function() {
406 if ( ! $link.length ) {
407 editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder', 'data-wp-temp-link': 1 } );
408 $link = editor.$( 'a[data-wp-temp-link="1"]' ).removeAttr( 'data-wp-temp-link' );
409 hasText = $.trim( $link.text() );
412 if ( ! $link.length ) {
413 editor.execCommand( 'unlink' );
415 if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
416 text = inputs.text.val();
420 } else if ( ! hasText ) {
421 $link.text( attrs.href );
425 attrs['data-wplink-edit'] = null;
426 attrs['data-mce-href'] = null; // attrs.href
431 wpLink.close( 'noReset' );
434 if ( $link.length ) {
435 $mceCaret = $link.parent( '#_mce_caret' );
437 if ( $mceCaret.length ) {
438 $mceCaret.before( $link.removeAttr( 'data-mce-bogus' ) );
441 editor.selection.select( $link[0] );
442 editor.selection.collapse();
444 if ( editor.plugins.wplink ) {
445 editor.plugins.wplink.checkLink( $link[0] );
449 editor.nodeChanged();
451 // Audible confirmation message when a link has been inserted in the Editor.
452 wp.a11y.speak( wpLinkL10n.linkInserted );
455 updateFields: function( e, li ) {
456 inputs.url.val( li.children( '.item-permalink' ).val() );
459 getUrlFromSelection: function( selection ) {
461 if ( this.isMCE() ) {
462 selection = editor.selection.getContent({ format: 'text' });
463 } else if ( document.selection && wpLink.range ) {
464 selection = wpLink.range.text;
465 } else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
466 selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
470 selection = $.trim( selection );
472 if ( selection && emailRegexp.test( selection ) ) {
473 // Selection is email address
474 return 'mailto:' + selection;
475 } else if ( selection && urlRegexp.test( selection ) ) {
477 return selection.replace( /&|�?38;/gi, '&' );
483 setDefaultValues: function( selection ) {
484 inputs.url.val( this.getUrlFromSelection( selection ) );
486 // Empty the search field and swap the "rivers".
487 inputs.search.val('');
488 wpLink.searchInternalLinks();
490 // Update save prompt.
491 inputs.submit.val( wpLinkL10n.save );
494 searchInternalLinks: function() {
496 search = inputs.search.val() || '';
498 if ( search.length > 2 ) {
499 rivers.recent.hide();
500 rivers.search.show();
502 // Don't search if the keypress didn't change the title.
503 if ( wpLink.lastSearch == search )
506 wpLink.lastSearch = search;
507 waiting = inputs.search.parent().find( '.spinner' ).addClass( 'is-active' );
509 rivers.search.change( search );
510 rivers.search.ajax( function() {
511 waiting.removeClass( 'is-active' );
514 rivers.search.hide();
515 rivers.recent.show();
520 rivers.search.next();
521 rivers.recent.next();
525 rivers.search.prev();
526 rivers.recent.prev();
529 keydown: function( event ) {
533 if ( 27 === event.keyCode ) {
535 event.stopImmediatePropagation();
537 } else if ( 9 === event.keyCode ) {
538 id = event.target.id;
540 // wp-link-submit must always be the last focusable element in the dialog.
541 // following focusable elements will be skipped on keyboard navigation.
542 if ( id === 'wp-link-submit' && ! event.shiftKey ) {
543 inputs.close.focus();
544 event.preventDefault();
545 } else if ( id === 'wp-link-close' && event.shiftKey ) {
546 inputs.submit.focus();
547 event.preventDefault();
551 // Up Arrow and Down Arrow keys.
552 if ( 38 !== event.keyCode && 40 !== event.keyCode ) {
556 if ( document.activeElement &&
557 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
562 fn = 38 === event.keyCode ? 'prev' : 'next';
563 clearInterval( wpLink.keyInterval );
565 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
566 event.preventDefault();
569 keyup: function( event ) {
570 // Up Arrow and Down Arrow keys.
571 if ( 38 === event.keyCode || 40 === event.keyCode ) {
572 clearInterval( wpLink.keyInterval );
573 event.preventDefault();
577 delayedCallback: function( func, delay ) {
578 var timeoutTriggered, funcTriggered, funcArgs, funcContext;
583 setTimeout( function() {
585 return func.apply( funcContext, funcArgs );
587 timeoutTriggered = true;
591 if ( timeoutTriggered )
592 return func.apply( this, arguments );
594 funcArgs = arguments;
596 funcTriggered = true;
601 River = function( element, search ) {
603 this.element = element;
604 this.ul = element.children( 'ul' );
605 this.contentHeight = element.children( '#link-selector-height' );
606 this.waiting = element.find('.river-waiting');
608 this.change( search );
611 $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
614 element.on( 'click', 'li', function( event ) {
615 self.select( $( this ), event );
619 $.extend( River.prototype, {
620 refresh: function() {
622 this.visible = this.element.is( ':visible' );
625 if ( ! this.visible ) {
633 this.visible = false;
635 // Selects a list item and triggers the river-select event.
636 select: function( li, event ) {
637 var liHeight, elHeight, liTop, elTop;
639 if ( li.hasClass( 'unselectable' ) || li == this.selected )
643 this.selected = li.addClass( 'selected' );
644 // Make sure the element is visible
645 liHeight = li.outerHeight();
646 elHeight = this.element.height();
647 liTop = li.position().top;
648 elTop = this.element.scrollTop();
650 if ( liTop < 0 ) // Make first visible element
651 this.element.scrollTop( elTop + liTop );
652 else if ( liTop + liHeight > elHeight ) // Make last visible element
653 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
655 // Trigger the river-select event
656 this.element.trigger( 'river-select', [ li, event, this ] );
658 deselect: function() {
660 this.selected.removeClass( 'selected' );
661 this.selected = false;
664 if ( ! this.visible )
668 if ( this.selected ) {
669 to = this.selected.prev( 'li' );
675 if ( ! this.visible )
678 var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
682 ajax: function( callback ) {
684 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
685 response = wpLink.delayedCallback( function( results, params ) {
686 self.process( results, params );
688 callback( results, params );
691 this.query.ajax( response );
693 change: function( search ) {
694 if ( this.query && this._search == search )
697 this._search = search;
698 this.query = new Query( search );
699 this.element.scrollTop( 0 );
701 process: function( results, params ) {
702 var list = '', alt = true, classes = '',
703 firstPage = params.page == 1;
707 list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
708 wpLinkL10n.noMatchesFound + '</em></span></li>';
711 $.each( results, function() {
712 classes = alt ? 'alternate' : '';
713 classes += this.title ? '' : ' no-title';
714 list += classes ? '<li class="' + classes + '">' : '<li>';
715 list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
716 list += '<span class="item-title">';
717 list += this.title ? this.title : wpLinkL10n.noTitle;
718 list += '</span><span class="item-info">' + this.info + '</span></li>';
723 this.ul[ firstPage ? 'html' : 'append' ]( list );
725 maybeLoad: function() {
728 bottom = el.scrollTop() + el.height();
730 if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
733 setTimeout(function() {
734 var newTop = el.scrollTop(),
735 newBottom = newTop + el.height();
737 if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
740 self.waiting.addClass( 'is-active' );
741 el.scrollTop( newTop + self.waiting.outerHeight() );
743 self.ajax( function() {
744 self.waiting.removeClass( 'is-active' );
746 }, wpLink.timeToTriggerRiver );
750 Query = function( search ) {
752 this.allLoaded = false;
753 this.querying = false;
754 this.search = search;
757 $.extend( Query.prototype, {
759 return ! ( this.querying || this.allLoaded );
761 ajax: function( callback ) {
764 action : 'wp-link-ajax',
766 '_ajax_linking_nonce' : inputs.nonce.val()
770 query.search = this.search;
772 this.querying = true;
774 $.post( window.ajaxurl, query, function( r ) {
776 self.querying = false;
777 self.allLoaded = ! r;
778 callback( r, query );
783 $( document ).ready( wpLink.init );
784 })( jQuery, window.wpLinkL10n, window.wp );