]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wplink.js
Wordpress 4.6-scripts
[autoinstalls/wordpress.git] / wp-includes / js / wplink.js
1 var wpLink;
2
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,
7                 inputs = {},
8                 rivers = {},
9                 isTouch = ( 'ontouchend' in document );
10
11         function getLink() {
12                 return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
13         }
14
15         wpLink = {
16                 timeToTriggerRiver: 150,
17                 minRiverAJAXDuration: 200,
18                 riverBottomThreshold: 5,
19                 keySensitivity: 100,
20                 lastSearch: '',
21                 textarea: '',
22                 modalOpen: false,
23
24                 init: function() {
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' );
30
31                         // Input
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' );
37
38                         // Build Rivers
39                         rivers.search = new River( $( '#search-results' ) );
40                         rivers.recent = new River( $( '#most-recent-results' ) );
41                         rivers.elements = inputs.dialog.find( '.query-results' );
42
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' );
47
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();
53                                 wpLink.update();
54                         });
55
56                         inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel button' ).click( function( event ) {
57                                 event.preventDefault();
58                                 wpLink.close();
59                         });
60
61                         rivers.elements.on( 'river-select', wpLink.updateFields );
62
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();
70                         } );
71
72                         inputs.search.on( 'keyup input', function() {
73                                 window.clearTimeout( searchTimer );
74                                 searchTimer = window.setTimeout( function() {
75                                         wpLink.searchInternalLinks();
76                                 }, 500 );
77                         });
78
79                         inputs.url.on( 'paste', function() {
80                                 setTimeout( wpLink.correctURL, 0 );
81                         } );
82
83                         inputs.url.on( 'blur', wpLink.correctURL );
84                 },
85
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() );
89
90                         if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
91                                 inputs.url.val( 'http://' + url );
92                                 correctedURL = url;
93                         }
94                 },
95
96                 open: function( editorId, url, text, node ) {
97                         var ed,
98                                 $body = $( document.body );
99
100                         $body.addClass( 'modal-open' );
101                         wpLink.modalOpen = true;
102                         linkNode = node;
103
104                         wpLink.range = null;
105
106                         if ( editorId ) {
107                                 window.wpActiveEditor = editorId;
108                         }
109
110                         if ( ! window.wpActiveEditor ) {
111                                 return;
112                         }
113
114                         this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
115
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 );
120
121                                 ed = window.tinymce.get( window.wpActiveEditor );
122
123                                 if ( ed && ! ed.isHidden() ) {
124                                         editor = ed;
125                                 } else {
126                                         editor = null;
127                                 }
128
129                                 if ( editor && window.tinymce.isIE ) {
130                                         editor.windowManager.wplinkBookmark = editor.selection.getBookmark();
131                                 }
132                         }
133
134                         if ( ! wpLink.isMCE() && document.selection ) {
135                                 this.textarea.focus();
136                                 this.range = document.selection.createRange();
137                         }
138
139                         inputs.wrap.show();
140                         inputs.backdrop.show();
141
142                         wpLink.refresh( url, text );
143
144                         $( document ).trigger( 'wplink-open', inputs.wrap );
145                 },
146
147                 isMCE: function() {
148                         return editor && ! editor.isHidden();
149                 },
150
151                 refresh: function( url, text ) {
152                         var linkText = '';
153
154                         // Refresh rivers (clear links, check visibility)
155                         rivers.search.refresh();
156                         rivers.recent.refresh();
157
158                         if ( wpLink.isMCE() ) {
159                                 wpLink.mceRefresh( url, text );
160                         } else {
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' );
164                                 }
165
166                                 if ( document.selection ) {
167                                         // Old IE
168                                         linkText = document.selection.createRange().text || text || '';
169                                 } else if ( typeof this.textarea.selectionStart !== 'undefined' &&
170                                         ( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
171                                         // W3C
172                                         text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
173                                 }
174
175                                 inputs.text.val( text );
176                                 wpLink.setDefaultValues();
177                         }
178
179                         if ( isTouch ) {
180                                 // Close the onscreen keyboard
181                                 inputs.url.focus().blur();
182                         } else {
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();
188                                         inputs.url.focus();
189                                 } );
190                         }
191
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();
195                         }
196
197                         correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
198                 },
199
200                 hasSelectedText: function( linkNode ) {
201                         var node, nodes, i, html = editor.selection.getContent();
202
203                         // Partial html and not a fully selected anchor element
204                         if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
205                                 return false;
206                         }
207
208                         if ( linkNode ) {
209                                 nodes = linkNode.childNodes;
210
211                                 if ( nodes.length === 0 ) {
212                                         return false;
213                                 }
214
215                                 for ( i = nodes.length - 1; i >= 0; i-- ) {
216                                         node = nodes[i];
217
218                                         if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
219                                                 return false;
220                                         }
221                                 }
222                         }
223
224                         return true;
225                 },
226
227                 mceRefresh: function( searchStr, text ) {
228                         var linkText, href,
229                                 linkNode = getLink(),
230                                 onlyText = this.hasSelectedText( linkNode );
231
232                         if ( linkNode ) {
233                                 linkText = linkNode.textContent || linkNode.innerText;
234                                 href = editor.dom.getAttrib( linkNode, 'href' );
235
236                                 if ( ! $.trim( linkText ) ) {
237                                         linkText = text || '';
238                                 }
239
240                                 if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
241                                         href = searchStr;
242                                 }
243
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 );
248                                 } else {
249                                         this.setDefaultValues( linkText );
250                                 }
251
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 );
255                                 } else {
256                                         inputs.search.val( '' );
257                                 }
258
259                                 // Always reset the search
260                                 window.setTimeout( function() {
261                                         wpLink.searchInternalLinks();
262                                 } );
263                         } else {
264                                 linkText = editor.selection.getContent({ format: 'text' }) || text || '';
265                                 this.setDefaultValues( linkText );
266                         }
267
268                         if ( onlyText ) {
269                                 inputs.text.val( linkText );
270                                 inputs.wrap.addClass( 'has-text-field' );
271                         } else {
272                                 inputs.text.val( '' );
273                                 inputs.wrap.removeClass( 'has-text-field' );
274                         }
275                 },
276
277                 close: function( reset ) {
278                         $( document.body ).removeClass( 'modal-open' );
279                         wpLink.modalOpen = false;
280
281                         if ( reset !== 'noReset' ) {
282                                 if ( ! wpLink.isMCE() ) {
283                                         wpLink.textarea.focus();
284
285                                         if ( wpLink.range ) {
286                                                 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
287                                                 wpLink.range.select();
288                                         }
289                                 } else {
290                                         if ( editor.plugins.wplink ) {
291                                                 editor.plugins.wplink.close();
292                                         }
293
294                                         editor.focus();
295                                 }
296                         }
297
298                         inputs.backdrop.hide();
299                         inputs.wrap.hide();
300
301                         correctedURL = false;
302
303                         $( document ).trigger( 'wplink-close', inputs.wrap );
304                 },
305
306                 getAttrs: function() {
307                         wpLink.correctURL();
308
309                         return {
310                                 href: $.trim( inputs.url.val() ),
311                                 target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : ''
312                         };
313                 },
314
315                 buildHtml: function(attrs) {
316                         var html = '<a href="' + attrs.href + '"';
317
318                         if ( attrs.target ) {
319                                 html += ' target="' + attrs.target + '"';
320                         }
321
322                         return html + '>';
323                 },
324
325                 update: function() {
326                         if ( wpLink.isMCE() ) {
327                                 wpLink.mceUpdate();
328                         } else {
329                                 wpLink.htmlUpdate();
330                         }
331                 },
332
333                 htmlUpdate: function() {
334                         var attrs, text, html, begin, end, cursor, selection,
335                                 textarea = wpLink.textarea;
336
337                         if ( ! textarea ) {
338                                 return;
339                         }
340
341                         attrs = wpLink.getAttrs();
342                         text = inputs.text.val();
343
344                         // If there's no href, return.
345                         if ( ! attrs.href ) {
346                                 return;
347                         }
348
349                         html = wpLink.buildHtml(attrs);
350
351                         // Insert HTML
352                         if ( document.selection && wpLink.range ) {
353                                 // IE
354                                 // Note: If no text is selected, IE will not place the cursor
355                                 //       inside the closing tag.
356                                 textarea.focus();
357                                 wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
358                                 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
359                                 wpLink.range.select();
360
361                                 wpLink.range = null;
362                         } else if ( typeof textarea.selectionStart !== 'undefined' ) {
363                                 // W3C
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;
369
370                                 // If no text is selected, place the cursor inside the closing tag.
371                                 if ( begin === end && ! selection ) {
372                                         cursor -= 4;
373                                 }
374
375                                 textarea.value = (
376                                         textarea.value.substring( 0, begin ) +
377                                         html +
378                                         textarea.value.substring( end, textarea.value.length )
379                                 );
380
381                                 // Update cursor position
382                                 textarea.selectionStart = textarea.selectionEnd = cursor;
383                         }
384
385                         wpLink.close();
386                         textarea.focus();
387
388                         // Audible confirmation message when a link has been inserted in the Editor.
389                         wp.a11y.speak( wpLinkL10n.linkInserted );
390                 },
391
392                 mceUpdate: function() {
393                         var attrs = wpLink.getAttrs(),
394                                 link, text;
395
396                         if ( window.tinymce.isIE && editor.windowManager.wplinkBookmark ) {
397                                 editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
398                                 editor.windowManager.wplinkBookmark = null;
399                         }
400
401                         if ( ! attrs.href ) {
402                                 editor.execCommand( 'unlink' );
403                                 wpLink.close();
404                                 return;
405                         }
406
407                         link = getLink();
408
409                         if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
410                                 text = inputs.text.val() || attrs.href;
411                         }
412
413                         if ( link ) {
414                                 if ( text ) {
415                                         if ( 'innerText' in link ) {
416                                                 link.innerText = text;
417                                         } else {
418                                                 link.textContent = text;
419                                         }
420                                 }
421
422                                 // Not editing any more
423                                 attrs['data-wplink-edit'] = null;
424                                 editor.dom.setAttribs( link, attrs );
425                         } else {
426                                 if ( text ) {
427                                         editor.selection.setNode( editor.dom.create( 'a', attrs, editor.dom.encode( text ) ) );
428                                 } else {
429                                         editor.execCommand( 'mceInsertLink', false, attrs );
430                                 }
431                         }
432
433                         wpLink.close( 'noReset' );
434                         editor.focus();
435                         editor.nodeChanged();
436
437                         if ( link && editor.plugins.wplink ) {
438                                 editor.plugins.wplink.checkLink( link );
439                         }
440
441                         // Audible confirmation message when a link has been inserted in the Editor.
442                         wp.a11y.speak( wpLinkL10n.linkInserted );
443                 },
444
445                 updateFields: function( e, li ) {
446                         inputs.url.val( li.children( '.item-permalink' ).val() );
447                 },
448
449                 getUrlFromSelection: function( selection ) {
450                         if ( ! 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 );
457                                 }
458                         }
459
460                         selection = $.trim( selection );
461
462                         if ( selection && emailRegexp.test( selection ) ) {
463                                 // Selection is email address
464                                 return 'mailto:' + selection;
465                         } else if ( selection && urlRegexp.test( selection ) ) {
466                                 // Selection is URL
467                                 return selection.replace( /&amp;|&#0?38;/gi, '&' );
468                         }
469
470                         return '';
471                 },
472
473                 setDefaultValues: function( selection ) {
474                         inputs.url.val( this.getUrlFromSelection( selection ) );
475
476                         // Empty the search field and swap the "rivers".
477                         inputs.search.val('');
478                         wpLink.searchInternalLinks();
479
480                         // Update save prompt.
481                         inputs.submit.val( wpLinkL10n.save );
482                 },
483
484                 searchInternalLinks: function() {
485                         var waiting,
486                                 search = inputs.search.val() || '';
487
488                         if ( search.length > 2 ) {
489                                 rivers.recent.hide();
490                                 rivers.search.show();
491
492                                 // Don't search if the keypress didn't change the title.
493                                 if ( wpLink.lastSearch == search )
494                                         return;
495
496                                 wpLink.lastSearch = search;
497                                 waiting = inputs.search.parent().find( '.spinner' ).addClass( 'is-active' );
498
499                                 rivers.search.change( search );
500                                 rivers.search.ajax( function() {
501                                         waiting.removeClass( 'is-active' );
502                                 });
503                         } else {
504                                 rivers.search.hide();
505                                 rivers.recent.show();
506                         }
507                 },
508
509                 next: function() {
510                         rivers.search.next();
511                         rivers.recent.next();
512                 },
513
514                 prev: function() {
515                         rivers.search.prev();
516                         rivers.recent.prev();
517                 },
518
519                 keydown: function( event ) {
520                         var fn, id;
521
522                         // Escape key.
523                         if ( 27 === event.keyCode ) {
524                                 wpLink.close();
525                                 event.stopImmediatePropagation();
526                         // Tab key.
527                         } else if ( 9 === event.keyCode ) {
528                                 id = event.target.id;
529
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();
538                                 }
539                         }
540
541                         // Up Arrow and Down Arrow keys.
542                         if ( 38 !== event.keyCode && 40 !== event.keyCode ) {
543                                 return;
544                         }
545
546                         if ( document.activeElement &&
547                                 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
548                                 return;
549                         }
550
551                         // Up Arrow key.
552                         fn = 38 === event.keyCode ? 'prev' : 'next';
553                         clearInterval( wpLink.keyInterval );
554                         wpLink[ fn ]();
555                         wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
556                         event.preventDefault();
557                 },
558
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();
564                         }
565                 },
566
567                 delayedCallback: function( func, delay ) {
568                         var timeoutTriggered, funcTriggered, funcArgs, funcContext;
569
570                         if ( ! delay )
571                                 return func;
572
573                         setTimeout( function() {
574                                 if ( funcTriggered )
575                                         return func.apply( funcContext, funcArgs );
576                                 // Otherwise, wait.
577                                 timeoutTriggered = true;
578                         }, delay );
579
580                         return function() {
581                                 if ( timeoutTriggered )
582                                         return func.apply( this, arguments );
583                                 // Otherwise, wait.
584                                 funcArgs = arguments;
585                                 funcContext = this;
586                                 funcTriggered = true;
587                         };
588                 }
589         };
590
591         River = function( element, search ) {
592                 var self = this;
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');
597
598                 this.change( search );
599                 this.refresh();
600
601                 $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
602                         self.maybeLoad();
603                 });
604                 element.on( 'click', 'li', function( event ) {
605                         self.select( $( this ), event );
606                 });
607         };
608
609         $.extend( River.prototype, {
610                 refresh: function() {
611                         this.deselect();
612                         this.visible = this.element.is( ':visible' );
613                 },
614                 show: function() {
615                         if ( ! this.visible ) {
616                                 this.deselect();
617                                 this.element.show();
618                                 this.visible = true;
619                         }
620                 },
621                 hide: function() {
622                         this.element.hide();
623                         this.visible = false;
624                 },
625                 // Selects a list item and triggers the river-select event.
626                 select: function( li, event ) {
627                         var liHeight, elHeight, liTop, elTop;
628
629                         if ( li.hasClass( 'unselectable' ) || li == this.selected )
630                                 return;
631
632                         this.deselect();
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();
639
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 );
644
645                         // Trigger the river-select event
646                         this.element.trigger( 'river-select', [ li, event, this ] );
647                 },
648                 deselect: function() {
649                         if ( this.selected )
650                                 this.selected.removeClass( 'selected' );
651                         this.selected = false;
652                 },
653                 prev: function() {
654                         if ( ! this.visible )
655                                 return;
656
657                         var to;
658                         if ( this.selected ) {
659                                 to = this.selected.prev( 'li' );
660                                 if ( to.length )
661                                         this.select( to );
662                         }
663                 },
664                 next: function() {
665                         if ( ! this.visible )
666                                 return;
667
668                         var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
669                         if ( to.length )
670                                 this.select( to );
671                 },
672                 ajax: function( callback ) {
673                         var self = this,
674                                 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
675                                 response = wpLink.delayedCallback( function( results, params ) {
676                                         self.process( results, params );
677                                         if ( callback )
678                                                 callback( results, params );
679                                 }, delay );
680
681                         this.query.ajax( response );
682                 },
683                 change: function( search ) {
684                         if ( this.query && this._search == search )
685                                 return;
686
687                         this._search = search;
688                         this.query = new Query( search );
689                         this.element.scrollTop( 0 );
690                 },
691                 process: function( results, params ) {
692                         var list = '', alt = true, classes = '',
693                                 firstPage = params.page == 1;
694
695                         if ( ! results ) {
696                                 if ( firstPage ) {
697                                         list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
698                                                 wpLinkL10n.noMatchesFound + '</em></span></li>';
699                                 }
700                         } else {
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>';
709                                         alt = ! alt;
710                                 });
711                         }
712
713                         this.ul[ firstPage ? 'html' : 'append' ]( list );
714                 },
715                 maybeLoad: function() {
716                         var self = this,
717                                 el = this.element,
718                                 bottom = el.scrollTop() + el.height();
719
720                         if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
721                                 return;
722
723                         setTimeout(function() {
724                                 var newTop = el.scrollTop(),
725                                         newBottom = newTop + el.height();
726
727                                 if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
728                                         return;
729
730                                 self.waiting.addClass( 'is-active' );
731                                 el.scrollTop( newTop + self.waiting.outerHeight() );
732
733                                 self.ajax( function() {
734                                         self.waiting.removeClass( 'is-active' );
735                                 });
736                         }, wpLink.timeToTriggerRiver );
737                 }
738         });
739
740         Query = function( search ) {
741                 this.page = 1;
742                 this.allLoaded = false;
743                 this.querying = false;
744                 this.search = search;
745         };
746
747         $.extend( Query.prototype, {
748                 ready: function() {
749                         return ! ( this.querying || this.allLoaded );
750                 },
751                 ajax: function( callback ) {
752                         var self = this,
753                                 query = {
754                                         action : 'wp-link-ajax',
755                                         page : this.page,
756                                         '_ajax_linking_nonce' : inputs.nonce.val()
757                                 };
758
759                         if ( this.search )
760                                 query.search = this.search;
761
762                         this.querying = true;
763
764                         $.post( window.ajaxurl, query, function( r ) {
765                                 self.page++;
766                                 self.querying = false;
767                                 self.allLoaded = ! r;
768                                 callback( r, query );
769                         }, 'json' );
770                 }
771         });
772
773         $( document ).ready( wpLink.init );
774 })( jQuery, window.wpLinkL10n, window.wp );