]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wplink.js
WordPress 4.7-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
130                         if ( ! wpLink.isMCE() && document.selection ) {
131                                 this.textarea.focus();
132                                 this.range = document.selection.createRange();
133                         }
134
135                         inputs.wrap.show();
136                         inputs.backdrop.show();
137
138                         wpLink.refresh( url, text );
139
140                         $( document ).trigger( 'wplink-open', inputs.wrap );
141                 },
142
143                 isMCE: function() {
144                         return editor && ! editor.isHidden();
145                 },
146
147                 refresh: function( url, text ) {
148                         var linkText = '';
149
150                         // Refresh rivers (clear links, check visibility)
151                         rivers.search.refresh();
152                         rivers.recent.refresh();
153
154                         if ( wpLink.isMCE() ) {
155                                 wpLink.mceRefresh( url, text );
156                         } else {
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' );
160                                 }
161
162                                 if ( document.selection ) {
163                                         // Old IE
164                                         linkText = document.selection.createRange().text || text || '';
165                                 } else if ( typeof this.textarea.selectionStart !== 'undefined' &&
166                                         ( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
167                                         // W3C
168                                         text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
169                                 }
170
171                                 inputs.text.val( text );
172                                 wpLink.setDefaultValues();
173                         }
174
175                         if ( isTouch ) {
176                                 // Close the onscreen keyboard
177                                 inputs.url.focus().blur();
178                         } else {
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();
184                                         inputs.url.focus();
185                                 } );
186                         }
187
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();
191                         }
192
193                         correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
194                 },
195
196                 hasSelectedText: function( linkNode ) {
197                         var node, nodes, i, html = editor.selection.getContent();
198
199                         // Partial html and not a fully selected anchor element
200                         if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
201                                 return false;
202                         }
203
204                         if ( linkNode ) {
205                                 nodes = linkNode.childNodes;
206
207                                 if ( nodes.length === 0 ) {
208                                         return false;
209                                 }
210
211                                 for ( i = nodes.length - 1; i >= 0; i-- ) {
212                                         node = nodes[i];
213
214                                         if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
215                                                 return false;
216                                         }
217                                 }
218                         }
219
220                         return true;
221                 },
222
223                 mceRefresh: function( searchStr, text ) {
224                         var linkText, href,
225                                 linkNode = getLink(),
226                                 onlyText = this.hasSelectedText( linkNode );
227
228                         if ( linkNode ) {
229                                 linkText = linkNode.textContent || linkNode.innerText;
230                                 href = editor.dom.getAttrib( linkNode, 'href' );
231
232                                 if ( ! $.trim( linkText ) ) {
233                                         linkText = text || '';
234                                 }
235
236                                 if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
237                                         href = searchStr;
238                                 }
239
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 );
244                                 } else {
245                                         this.setDefaultValues( linkText );
246                                 }
247
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 );
251                                 } else {
252                                         inputs.search.val( '' );
253                                 }
254
255                                 // Always reset the search
256                                 window.setTimeout( function() {
257                                         wpLink.searchInternalLinks();
258                                 } );
259                         } else {
260                                 linkText = editor.selection.getContent({ format: 'text' }) || text || '';
261                                 this.setDefaultValues( linkText );
262                         }
263
264                         if ( onlyText ) {
265                                 inputs.text.val( linkText );
266                                 inputs.wrap.addClass( 'has-text-field' );
267                         } else {
268                                 inputs.text.val( '' );
269                                 inputs.wrap.removeClass( 'has-text-field' );
270                         }
271                 },
272
273                 close: function( reset ) {
274                         $( document.body ).removeClass( 'modal-open' );
275                         wpLink.modalOpen = false;
276
277                         if ( reset !== 'noReset' ) {
278                                 if ( ! wpLink.isMCE() ) {
279                                         wpLink.textarea.focus();
280
281                                         if ( wpLink.range ) {
282                                                 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
283                                                 wpLink.range.select();
284                                         }
285                                 } else {
286                                         if ( editor.plugins.wplink ) {
287                                                 editor.plugins.wplink.close();
288                                         }
289
290                                         editor.focus();
291                                 }
292                         }
293
294                         inputs.backdrop.hide();
295                         inputs.wrap.hide();
296
297                         correctedURL = false;
298
299                         $( document ).trigger( 'wplink-close', inputs.wrap );
300                 },
301
302                 getAttrs: function() {
303                         wpLink.correctURL();
304
305                         var attrs = {
306                                 href: $.trim( inputs.url.val() )
307                         };
308
309                         if ( inputs.openInNewTab.prop( 'checked' ) ) {
310                                 attrs.target = '_blank';
311                         }
312
313                         return attrs;
314                 },
315
316                 buildHtml: function(attrs) {
317                         var html = '<a href="' + attrs.href + '"';
318
319                         if ( attrs.target ) {
320                                 html += ' target="' + attrs.target + '"';
321                         }
322
323                         return html + '>';
324                 },
325
326                 update: function() {
327                         if ( wpLink.isMCE() ) {
328                                 wpLink.mceUpdate();
329                         } else {
330                                 wpLink.htmlUpdate();
331                         }
332                 },
333
334                 htmlUpdate: function() {
335                         var attrs, text, html, begin, end, cursor, selection,
336                                 textarea = wpLink.textarea;
337
338                         if ( ! textarea ) {
339                                 return;
340                         }
341
342                         attrs = wpLink.getAttrs();
343                         text = inputs.text.val();
344
345                         // If there's no href, return.
346                         if ( ! attrs.href ) {
347                                 return;
348                         }
349
350                         html = wpLink.buildHtml(attrs);
351
352                         // Insert HTML
353                         if ( document.selection && wpLink.range ) {
354                                 // IE
355                                 // Note: If no text is selected, IE will not place the cursor
356                                 //       inside the closing tag.
357                                 textarea.focus();
358                                 wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
359                                 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
360                                 wpLink.range.select();
361
362                                 wpLink.range = null;
363                         } else if ( typeof textarea.selectionStart !== 'undefined' ) {
364                                 // W3C
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;
370
371                                 // If no text is selected, place the cursor inside the closing tag.
372                                 if ( begin === end && ! selection ) {
373                                         cursor -= 4;
374                                 }
375
376                                 textarea.value = (
377                                         textarea.value.substring( 0, begin ) +
378                                         html +
379                                         textarea.value.substring( end, textarea.value.length )
380                                 );
381
382                                 // Update cursor position
383                                 textarea.selectionStart = textarea.selectionEnd = cursor;
384                         }
385
386                         wpLink.close();
387                         textarea.focus();
388
389                         // Audible confirmation message when a link has been inserted in the Editor.
390                         wp.a11y.speak( wpLinkL10n.linkInserted );
391                 },
392
393                 mceUpdate: function() {
394                         var attrs = wpLink.getAttrs(),
395                                 $link, text, hasText, $mceCaret;
396
397                         if ( ! attrs.href ) {
398                                 editor.execCommand( 'unlink' );
399                                 wpLink.close();
400                                 return;
401                         }
402
403                         $link = editor.$( getLink() );
404
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() );
410                                 }
411
412                                 if ( ! $link.length ) {
413                                         editor.execCommand( 'unlink' );
414                                 } else {
415                                         if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
416                                                 text = inputs.text.val();
417
418                                                 if ( text ) {
419                                                         $link.text( text );
420                                                 } else if ( ! hasText ) {
421                                                         $link.text( attrs.href );
422                                                 }
423                                         }
424
425                                         attrs['data-wplink-edit'] = null;
426                                         attrs['data-mce-href'] = null; // attrs.href
427                                         $link.attr( attrs );
428                                 }
429                         } );
430
431                         wpLink.close( 'noReset' );
432                         editor.focus();
433
434                         if ( $link.length ) {
435                                 $mceCaret = $link.parent( '#_mce_caret' );
436
437                                 if ( $mceCaret.length ) {
438                                         $mceCaret.before( $link.removeAttr( 'data-mce-bogus' ) );
439                                 }
440
441                                 editor.selection.select( $link[0] );
442                                 editor.selection.collapse();
443
444                                 if ( editor.plugins.wplink ) {
445                                         editor.plugins.wplink.checkLink( $link[0] );
446                                 }
447                         }
448
449                         editor.nodeChanged();
450
451                         // Audible confirmation message when a link has been inserted in the Editor.
452                         wp.a11y.speak( wpLinkL10n.linkInserted );
453                 },
454
455                 updateFields: function( e, li ) {
456                         inputs.url.val( li.children( '.item-permalink' ).val() );
457                 },
458
459                 getUrlFromSelection: function( selection ) {
460                         if ( ! 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 );
467                                 }
468                         }
469
470                         selection = $.trim( selection );
471
472                         if ( selection && emailRegexp.test( selection ) ) {
473                                 // Selection is email address
474                                 return 'mailto:' + selection;
475                         } else if ( selection && urlRegexp.test( selection ) ) {
476                                 // Selection is URL
477                                 return selection.replace( /&amp;|&#0?38;/gi, '&' );
478                         }
479
480                         return '';
481                 },
482
483                 setDefaultValues: function( selection ) {
484                         inputs.url.val( this.getUrlFromSelection( selection ) );
485
486                         // Empty the search field and swap the "rivers".
487                         inputs.search.val('');
488                         wpLink.searchInternalLinks();
489
490                         // Update save prompt.
491                         inputs.submit.val( wpLinkL10n.save );
492                 },
493
494                 searchInternalLinks: function() {
495                         var waiting,
496                                 search = inputs.search.val() || '';
497
498                         if ( search.length > 2 ) {
499                                 rivers.recent.hide();
500                                 rivers.search.show();
501
502                                 // Don't search if the keypress didn't change the title.
503                                 if ( wpLink.lastSearch == search )
504                                         return;
505
506                                 wpLink.lastSearch = search;
507                                 waiting = inputs.search.parent().find( '.spinner' ).addClass( 'is-active' );
508
509                                 rivers.search.change( search );
510                                 rivers.search.ajax( function() {
511                                         waiting.removeClass( 'is-active' );
512                                 });
513                         } else {
514                                 rivers.search.hide();
515                                 rivers.recent.show();
516                         }
517                 },
518
519                 next: function() {
520                         rivers.search.next();
521                         rivers.recent.next();
522                 },
523
524                 prev: function() {
525                         rivers.search.prev();
526                         rivers.recent.prev();
527                 },
528
529                 keydown: function( event ) {
530                         var fn, id;
531
532                         // Escape key.
533                         if ( 27 === event.keyCode ) {
534                                 wpLink.close();
535                                 event.stopImmediatePropagation();
536                         // Tab key.
537                         } else if ( 9 === event.keyCode ) {
538                                 id = event.target.id;
539
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();
548                                 }
549                         }
550
551                         // Up Arrow and Down Arrow keys.
552                         if ( 38 !== event.keyCode && 40 !== event.keyCode ) {
553                                 return;
554                         }
555
556                         if ( document.activeElement &&
557                                 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
558                                 return;
559                         }
560
561                         // Up Arrow key.
562                         fn = 38 === event.keyCode ? 'prev' : 'next';
563                         clearInterval( wpLink.keyInterval );
564                         wpLink[ fn ]();
565                         wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
566                         event.preventDefault();
567                 },
568
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();
574                         }
575                 },
576
577                 delayedCallback: function( func, delay ) {
578                         var timeoutTriggered, funcTriggered, funcArgs, funcContext;
579
580                         if ( ! delay )
581                                 return func;
582
583                         setTimeout( function() {
584                                 if ( funcTriggered )
585                                         return func.apply( funcContext, funcArgs );
586                                 // Otherwise, wait.
587                                 timeoutTriggered = true;
588                         }, delay );
589
590                         return function() {
591                                 if ( timeoutTriggered )
592                                         return func.apply( this, arguments );
593                                 // Otherwise, wait.
594                                 funcArgs = arguments;
595                                 funcContext = this;
596                                 funcTriggered = true;
597                         };
598                 }
599         };
600
601         River = function( element, search ) {
602                 var self = this;
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');
607
608                 this.change( search );
609                 this.refresh();
610
611                 $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
612                         self.maybeLoad();
613                 });
614                 element.on( 'click', 'li', function( event ) {
615                         self.select( $( this ), event );
616                 });
617         };
618
619         $.extend( River.prototype, {
620                 refresh: function() {
621                         this.deselect();
622                         this.visible = this.element.is( ':visible' );
623                 },
624                 show: function() {
625                         if ( ! this.visible ) {
626                                 this.deselect();
627                                 this.element.show();
628                                 this.visible = true;
629                         }
630                 },
631                 hide: function() {
632                         this.element.hide();
633                         this.visible = false;
634                 },
635                 // Selects a list item and triggers the river-select event.
636                 select: function( li, event ) {
637                         var liHeight, elHeight, liTop, elTop;
638
639                         if ( li.hasClass( 'unselectable' ) || li == this.selected )
640                                 return;
641
642                         this.deselect();
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();
649
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 );
654
655                         // Trigger the river-select event
656                         this.element.trigger( 'river-select', [ li, event, this ] );
657                 },
658                 deselect: function() {
659                         if ( this.selected )
660                                 this.selected.removeClass( 'selected' );
661                         this.selected = false;
662                 },
663                 prev: function() {
664                         if ( ! this.visible )
665                                 return;
666
667                         var to;
668                         if ( this.selected ) {
669                                 to = this.selected.prev( 'li' );
670                                 if ( to.length )
671                                         this.select( to );
672                         }
673                 },
674                 next: function() {
675                         if ( ! this.visible )
676                                 return;
677
678                         var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
679                         if ( to.length )
680                                 this.select( to );
681                 },
682                 ajax: function( callback ) {
683                         var self = this,
684                                 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
685                                 response = wpLink.delayedCallback( function( results, params ) {
686                                         self.process( results, params );
687                                         if ( callback )
688                                                 callback( results, params );
689                                 }, delay );
690
691                         this.query.ajax( response );
692                 },
693                 change: function( search ) {
694                         if ( this.query && this._search == search )
695                                 return;
696
697                         this._search = search;
698                         this.query = new Query( search );
699                         this.element.scrollTop( 0 );
700                 },
701                 process: function( results, params ) {
702                         var list = '', alt = true, classes = '',
703                                 firstPage = params.page == 1;
704
705                         if ( ! results ) {
706                                 if ( firstPage ) {
707                                         list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
708                                                 wpLinkL10n.noMatchesFound + '</em></span></li>';
709                                 }
710                         } else {
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>';
719                                         alt = ! alt;
720                                 });
721                         }
722
723                         this.ul[ firstPage ? 'html' : 'append' ]( list );
724                 },
725                 maybeLoad: function() {
726                         var self = this,
727                                 el = this.element,
728                                 bottom = el.scrollTop() + el.height();
729
730                         if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
731                                 return;
732
733                         setTimeout(function() {
734                                 var newTop = el.scrollTop(),
735                                         newBottom = newTop + el.height();
736
737                                 if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
738                                         return;
739
740                                 self.waiting.addClass( 'is-active' );
741                                 el.scrollTop( newTop + self.waiting.outerHeight() );
742
743                                 self.ajax( function() {
744                                         self.waiting.removeClass( 'is-active' );
745                                 });
746                         }, wpLink.timeToTriggerRiver );
747                 }
748         });
749
750         Query = function( search ) {
751                 this.page = 1;
752                 this.allLoaded = false;
753                 this.querying = false;
754                 this.search = search;
755         };
756
757         $.extend( Query.prototype, {
758                 ready: function() {
759                         return ! ( this.querying || this.allLoaded );
760                 },
761                 ajax: function( callback ) {
762                         var self = this,
763                                 query = {
764                                         action : 'wp-link-ajax',
765                                         page : this.page,
766                                         '_ajax_linking_nonce' : inputs.nonce.val()
767                                 };
768
769                         if ( this.search )
770                                 query.search = this.search;
771
772                         this.querying = true;
773
774                         $.post( window.ajaxurl, query, function( r ) {
775                                 self.page++;
776                                 self.querying = false;
777                                 self.allLoaded = ! r;
778                                 callback( r, query );
779                         }, 'json' );
780                 }
781         });
782
783         $( document ).ready( wpLink.init );
784 })( jQuery, window.wpLinkL10n, window.wp );