[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> wplink.js (source)

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


Generated: Wed Sep 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1