2018-06-27 22:30:15 -04:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/wplink.js
|
|
|
|
*/
|
|
|
|
|
2018-08-19 09:33:24 -04:00
|
|
|
/* global wpLink */
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-03-13 20:53:26 -04:00
|
|
|
( function( $, wpLinkL10n, wp ) {
|
2019-01-24 06:21:50 -05:00
|
|
|
var editor, searchTimer, River, Query, correctedURL,
|
2016-07-20 23:42:34 -04:00
|
|
|
emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i,
|
|
|
|
urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i,
|
2014-07-31 19:28:19 -04:00
|
|
|
inputs = {},
|
2016-04-01 17:13:29 -04:00
|
|
|
rivers = {},
|
2014-07-31 19:28:19 -04:00
|
|
|
isTouch = ( 'ontouchend' in document );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
function getLink() {
|
2019-01-24 06:21:50 -05:00
|
|
|
if ( editor ) {
|
|
|
|
return editor.$( 'a[data-wplink-edit="true"]' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-19 09:33:24 -04:00
|
|
|
window.wpLink = {
|
2016-04-01 17:13:29 -04:00
|
|
|
timeToTriggerRiver: 150,
|
|
|
|
minRiverAJAXDuration: 200,
|
|
|
|
riverBottomThreshold: 5,
|
|
|
|
keySensitivity: 100,
|
|
|
|
lastSearch: '',
|
2012-08-22 20:04:18 -04:00
|
|
|
textarea: '',
|
2016-06-02 19:58:28 -04:00
|
|
|
modalOpen: false,
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
init: function() {
|
|
|
|
inputs.wrap = $('#wp-link-wrap');
|
|
|
|
inputs.dialog = $( '#wp-link' );
|
|
|
|
inputs.backdrop = $( '#wp-link-backdrop' );
|
|
|
|
inputs.submit = $( '#wp-link-submit' );
|
|
|
|
inputs.close = $( '#wp-link-close' );
|
2015-03-10 22:40:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Input.
|
2015-03-10 22:40:27 -04:00
|
|
|
inputs.text = $( '#wp-link-text' );
|
|
|
|
inputs.url = $( '#wp-link-url' );
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.nonce = $( '#_ajax_linking_nonce' );
|
2015-03-10 22:40:27 -04:00
|
|
|
inputs.openInNewTab = $( '#wp-link-target' );
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.search = $( '#wp-link-search' );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Build rivers.
|
2016-04-01 17:13:29 -04:00
|
|
|
rivers.search = new River( $( '#search-results' ) );
|
|
|
|
rivers.recent = new River( $( '#most-recent-results' ) );
|
|
|
|
rivers.elements = inputs.dialog.find( '.query-results' );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Get search notice text.
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.queryNotice = $( '#query-notice-message' );
|
|
|
|
inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
|
|
|
|
inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Bind event handlers.
|
2021-01-22 07:32:03 -05:00
|
|
|
inputs.dialog.on( 'keydown', wpLink.keydown );
|
|
|
|
inputs.dialog.on( 'keyup', wpLink.keyup );
|
|
|
|
inputs.submit.on( 'click', function( event ) {
|
2014-03-10 20:04:14 -04:00
|
|
|
event.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.update();
|
|
|
|
});
|
2016-02-24 01:20:26 -05:00
|
|
|
|
2021-01-22 07:32:03 -05:00
|
|
|
inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel button' ).on( 'click', function( event ) {
|
2014-03-10 20:04:14 -04:00
|
|
|
event.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.close();
|
|
|
|
});
|
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
rivers.elements.on( 'river-select', wpLink.updateFields );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Display 'hint' message when search field or 'query-results' box are focused.
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.search.on( 'focus.wplink', function() {
|
|
|
|
inputs.queryNoticeTextDefault.hide();
|
|
|
|
inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
|
|
|
|
} ).on( 'blur.wplink', function() {
|
|
|
|
inputs.queryNoticeTextDefault.show();
|
|
|
|
inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide();
|
|
|
|
} );
|
2016-02-24 01:20:26 -05:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.search.on( 'keyup input', function() {
|
|
|
|
window.clearTimeout( searchTimer );
|
|
|
|
searchTimer = window.setTimeout( function() {
|
|
|
|
wpLink.searchInternalLinks();
|
|
|
|
}, 500 );
|
|
|
|
});
|
2016-03-13 20:53:26 -04:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.url.on( 'paste', function() {
|
|
|
|
setTimeout( wpLink.correctURL, 0 );
|
|
|
|
} );
|
2016-02-24 01:20:26 -05:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.url.on( 'blur', wpLink.correctURL );
|
2015-06-28 19:07:25 -04:00
|
|
|
},
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// If URL wasn't corrected last time and doesn't start with http:, https:, ? # or /, prepend http://.
|
2015-06-28 19:07:25 -04:00
|
|
|
correctURL: function () {
|
2021-03-18 15:01:03 -04:00
|
|
|
var url = inputs.url.val().trim();
|
2015-06-28 19:07:25 -04:00
|
|
|
|
|
|
|
if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
|
|
|
|
inputs.url.val( 'http://' + url );
|
|
|
|
correctedURL = url;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2019-01-24 06:21:50 -05:00
|
|
|
open: function( editorId, url, text ) {
|
2015-06-26 16:29:24 -04:00
|
|
|
var ed,
|
|
|
|
$body = $( document.body );
|
2014-06-09 11:35:14 -04:00
|
|
|
|
Editor: A11y: Set ARIA attributes for Classic Editor link inserter.
Set `aria-modal` and `aria-hidden` attributes when the Classic Editor link inserter modal is active, so that content behind the modal will be properly treated as inert when interacting with the modal.
Props lyonmuller, jwgoedert, sabernhardt, alexstine, afercia, rajinsharwar, rcreators, audrasjb, hmbashar, joedolson.
Fixes #60799.
Built from https://develop.svn.wordpress.org/trunk@58450
git-svn-id: http://core.svn.wordpress.org/trunk@57899 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-20 16:27:11 -04:00
|
|
|
$( '#wpwrap' ).attr( 'aria-hidden', 'true' );
|
2015-06-26 16:29:24 -04:00
|
|
|
$body.addClass( 'modal-open' );
|
2016-06-02 19:58:28 -04:00
|
|
|
wpLink.modalOpen = true;
|
2014-12-02 03:09:25 -05:00
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.range = null;
|
|
|
|
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
if ( editorId ) {
|
|
|
|
window.wpActiveEditor = editorId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! window.wpActiveEditor ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
}
|
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-02-29 14:38:27 -05:00
|
|
|
if ( typeof window.tinymce !== 'undefined' ) {
|
2015-08-05 22:17:25 -04:00
|
|
|
// Make sure the link wrapper is the last element in the body,
|
|
|
|
// or the inline editor toolbar may show above the backdrop.
|
|
|
|
$body.append( inputs.backdrop, inputs.wrap );
|
2015-06-26 16:29:24 -04:00
|
|
|
|
2016-02-29 14:38:27 -05:00
|
|
|
ed = window.tinymce.get( window.wpActiveEditor );
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
if ( ed && ! ed.isHidden() ) {
|
|
|
|
editor = ed;
|
|
|
|
} else {
|
|
|
|
editor = null;
|
|
|
|
}
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
if ( ! wpLink.isMCE() && document.selection ) {
|
|
|
|
this.textarea.focus();
|
|
|
|
this.range = document.selection.createRange();
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
inputs.wrap.show();
|
|
|
|
inputs.backdrop.show();
|
|
|
|
|
2016-02-24 01:20:26 -05:00
|
|
|
wpLink.refresh( url, text );
|
2015-03-10 22:40:27 -04:00
|
|
|
|
2014-07-31 22:04:17 -04:00
|
|
|
$( document ).trigger( 'wplink-open', inputs.wrap );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-10 20:04:14 -04:00
|
|
|
isMCE: function() {
|
|
|
|
return editor && ! editor.isHidden();
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2016-02-24 01:20:26 -05:00
|
|
|
refresh: function( url, text ) {
|
|
|
|
var linkText = '';
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Refresh rivers (clear links, check visibility).
|
2016-04-01 17:13:29 -04:00
|
|
|
rivers.search.refresh();
|
|
|
|
rivers.recent.refresh();
|
|
|
|
|
2014-07-31 19:28:19 -04:00
|
|
|
if ( wpLink.isMCE() ) {
|
2016-02-24 01:20:26 -05:00
|
|
|
wpLink.mceRefresh( url, text );
|
2014-07-31 19:28:19 -04:00
|
|
|
} else {
|
2020-01-28 19:45:18 -05:00
|
|
|
// For the Text editor the "Link text" field is always shown.
|
2015-04-21 09:59:30 -04:00
|
|
|
if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
|
|
|
|
inputs.wrap.addClass( 'has-text-field' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( document.selection ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Old IE.
|
2016-02-24 01:20:26 -05:00
|
|
|
linkText = document.selection.createRange().text || text || '';
|
2015-04-21 09:59:30 -04:00
|
|
|
} else if ( typeof this.textarea.selectionStart !== 'undefined' &&
|
|
|
|
( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// W3C.
|
2016-02-24 01:20:26 -05:00
|
|
|
text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
|
2015-04-21 09:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inputs.text.val( text );
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.setDefaultValues();
|
2014-07-31 19:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( isTouch ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Close the onscreen keyboard.
|
2021-02-23 14:45:04 -05:00
|
|
|
inputs.url.trigger( 'focus' ).trigger( 'blur' );
|
2014-07-31 19:28:19 -04:00
|
|
|
} else {
|
2020-01-28 19:45:18 -05:00
|
|
|
/*
|
|
|
|
* Focus the URL field and highlight its contents.
|
|
|
|
* If this is moved above the selection changes,
|
|
|
|
* IE will show a flashing cursor over the dialog.
|
|
|
|
*/
|
2016-02-24 22:48:26 -05:00
|
|
|
window.setTimeout( function() {
|
2016-04-01 17:13:29 -04:00
|
|
|
inputs.url[0].select();
|
2021-02-23 14:45:04 -05:00
|
|
|
inputs.url.trigger( 'focus' );
|
2016-02-26 19:52:26 -05:00
|
|
|
} );
|
2014-07-31 19:28:19 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
// Load the most recent results if this is the first time opening the panel.
|
|
|
|
if ( ! rivers.recent.ul.children().length ) {
|
|
|
|
rivers.recent.ajax();
|
|
|
|
}
|
|
|
|
|
2015-03-03 16:21:25 -05:00
|
|
|
correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
hasSelectedText: function( linkNode ) {
|
2016-02-24 01:20:26 -05:00
|
|
|
var node, nodes, i, html = editor.selection.getContent();
|
2015-03-10 22:40:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Partial html and not a fully selected anchor element.
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-24 06:21:50 -05:00
|
|
|
if ( linkNode.length ) {
|
|
|
|
nodes = linkNode[0].childNodes;
|
2015-03-10 22:40:27 -04:00
|
|
|
|
2019-01-24 06:21:50 -05:00
|
|
|
if ( ! nodes || ! nodes.length ) {
|
2015-03-10 22:40:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = nodes.length - 1; i >= 0; i-- ) {
|
2016-02-24 01:20:26 -05:00
|
|
|
node = nodes[i];
|
|
|
|
|
2016-02-29 14:38:27 -05:00
|
|
|
if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
|
2015-03-10 22:40:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
mceRefresh: function( searchStr, text ) {
|
|
|
|
var linkText, href,
|
2016-02-25 19:21:26 -05:00
|
|
|
linkNode = getLink(),
|
2015-03-10 22:40:27 -04:00
|
|
|
onlyText = this.hasSelectedText( linkNode );
|
|
|
|
|
2019-01-24 06:21:50 -05:00
|
|
|
if ( linkNode.length ) {
|
|
|
|
linkText = linkNode.text();
|
|
|
|
href = linkNode.attr( 'href' );
|
2016-02-25 19:21:26 -05:00
|
|
|
|
2021-03-18 15:01:03 -04:00
|
|
|
if ( ! linkText.trim() ) {
|
2016-02-25 19:21:26 -05:00
|
|
|
linkText = text || '';
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
|
|
|
|
href = searchStr;
|
|
|
|
}
|
2016-02-20 16:36:25 -05:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
if ( href !== '_wp_link_placeholder' ) {
|
|
|
|
inputs.url.val( href );
|
2019-01-24 06:21:50 -05:00
|
|
|
inputs.openInNewTab.prop( 'checked', '_blank' === linkNode.attr( 'target' ) );
|
2016-02-27 16:33:26 -05:00
|
|
|
inputs.submit.val( wpLinkL10n.update );
|
|
|
|
} else {
|
|
|
|
this.setDefaultValues( linkText );
|
2016-02-20 16:36:25 -05:00
|
|
|
}
|
2016-04-01 17:13:29 -04:00
|
|
|
|
|
|
|
if ( searchStr && searchStr !== href ) {
|
|
|
|
// The user has typed something in the inline dialog. Trigger a search with it.
|
|
|
|
inputs.search.val( searchStr );
|
|
|
|
} else {
|
|
|
|
inputs.search.val( '' );
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Always reset the search.
|
2016-04-01 17:13:29 -04:00
|
|
|
window.setTimeout( function() {
|
|
|
|
wpLink.searchInternalLinks();
|
|
|
|
} );
|
2015-03-10 22:40:27 -04:00
|
|
|
} else {
|
2016-02-27 16:33:26 -05:00
|
|
|
linkText = editor.selection.getContent({ format: 'text' }) || text || '';
|
|
|
|
this.setDefaultValues( linkText );
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( onlyText ) {
|
2016-02-27 16:33:26 -05:00
|
|
|
inputs.text.val( linkText );
|
2015-03-10 22:40:27 -04:00
|
|
|
inputs.wrap.addClass( 'has-text-field' );
|
2012-08-22 20:04:18 -04:00
|
|
|
} else {
|
2015-03-10 22:40:27 -04:00
|
|
|
inputs.text.val( '' );
|
|
|
|
inputs.wrap.removeClass( 'has-text-field' );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
close: function( reset ) {
|
2014-12-02 03:09:25 -05:00
|
|
|
$( document.body ).removeClass( 'modal-open' );
|
Editor: A11y: Set ARIA attributes for Classic Editor link inserter.
Set `aria-modal` and `aria-hidden` attributes when the Classic Editor link inserter modal is active, so that content behind the modal will be properly treated as inert when interacting with the modal.
Props lyonmuller, jwgoedert, sabernhardt, alexstine, afercia, rajinsharwar, rcreators, audrasjb, hmbashar, joedolson.
Fixes #60799.
Built from https://develop.svn.wordpress.org/trunk@58450
git-svn-id: http://core.svn.wordpress.org/trunk@57899 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-20 16:27:11 -04:00
|
|
|
$( '#wpwrap' ).removeAttr( 'aria-hidden' );
|
2016-06-02 19:58:28 -04:00
|
|
|
wpLink.modalOpen = false;
|
2014-12-02 03:09:25 -05:00
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
if ( reset !== 'noReset' ) {
|
|
|
|
if ( ! wpLink.isMCE() ) {
|
|
|
|
wpLink.textarea.focus();
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
if ( wpLink.range ) {
|
|
|
|
wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
|
|
|
|
wpLink.range.select();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( editor.plugins.wplink ) {
|
|
|
|
editor.plugins.wplink.close();
|
|
|
|
}
|
2016-02-25 19:21:26 -05:00
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
editor.focus();
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2014-03-10 20:04:14 -04:00
|
|
|
|
|
|
|
inputs.backdrop.hide();
|
|
|
|
inputs.wrap.hide();
|
2015-03-02 19:01:28 -05:00
|
|
|
|
|
|
|
correctedURL = false;
|
|
|
|
|
2014-07-31 22:04:17 -04:00
|
|
|
$( document ).trigger( 'wplink-close', inputs.wrap );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
getAttrs: function() {
|
2015-06-28 19:07:25 -04:00
|
|
|
wpLink.correctURL();
|
|
|
|
|
2016-12-14 15:30:44 -05:00
|
|
|
return {
|
2021-03-18 15:01:03 -04:00
|
|
|
href: inputs.url.val().trim(),
|
2016-12-14 15:30:44 -05:00
|
|
|
target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : null
|
2012-08-22 20:04:18 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-06-16 16:20:26 -04:00
|
|
|
buildHtml: function(attrs) {
|
|
|
|
var html = '<a href="' + attrs.href + '"';
|
|
|
|
|
|
|
|
if ( attrs.target ) {
|
General: Remove `noopener` from links opening in a new tab.
Removes the automatic addition of `rel="noopener noreferrer"` from links targeting a new tab or window, `target='_blank'`. Since this was introduced, supported browsers have changed their security policies and no longer allow the opened link to have JavaScript access to the previous tab.
Deprecates:
* `wp_targeted_link_rel()`
* `wp_targeted_link_rel_callback()`
* `wp_init_targeted_link_rel_filters()`: converted to a noop function
* `wp_remove_targeted_link_rel_filters()`: converted to a noop function
The deprecated functions are retained in `formatting.php` as in `SHORTINIT` mode the file is included while `deprecated.php` is not.
This also removes the `noopener` from links hard coded within the WordPress dashboard linking to documentation and other resources.
Props audrasjb, azaozz, dhruval04, dorzki, neo2k23, presskopp, sabernhardt, swissspidy, tobiasbg.
Fixes #53843.
Built from https://develop.svn.wordpress.org/trunk@59120
git-svn-id: http://core.svn.wordpress.org/trunk@58516 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-30 01:19:16 -04:00
|
|
|
html += ' target="' + attrs.target + '"';
|
2015-06-16 16:20:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return html + '>';
|
|
|
|
},
|
|
|
|
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
update: function() {
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( wpLink.isMCE() ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.mceUpdate();
|
2015-03-10 22:40:27 -04:00
|
|
|
} else {
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.htmlUpdate();
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
htmlUpdate: function() {
|
2015-03-10 22:40:27 -04:00
|
|
|
var attrs, text, html, begin, end, cursor, selection,
|
2012-08-22 20:04:18 -04:00
|
|
|
textarea = wpLink.textarea;
|
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( ! textarea ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
attrs = wpLink.getAttrs();
|
2015-03-10 22:40:27 -04:00
|
|
|
text = inputs.text.val();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-09-19 04:01:45 -04:00
|
|
|
var parser = document.createElement( 'a' );
|
|
|
|
parser.href = attrs.href;
|
|
|
|
|
|
|
|
if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line
|
|
|
|
attrs.href = '';
|
|
|
|
}
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
// If there's no href, return.
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( ! attrs.href ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-06-16 16:20:26 -04:00
|
|
|
html = wpLink.buildHtml(attrs);
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Insert HTML.
|
2012-08-22 20:04:18 -04:00
|
|
|
if ( document.selection && wpLink.range ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// IE.
|
2012-08-22 20:04:18 -04:00
|
|
|
// Note: If no text is selected, IE will not place the cursor
|
2020-01-28 19:45:18 -05:00
|
|
|
// inside the closing tag.
|
2012-08-22 20:04:18 -04:00
|
|
|
textarea.focus();
|
2015-03-10 22:40:27 -04:00
|
|
|
wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
|
2012-08-22 20:04:18 -04:00
|
|
|
wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
|
|
|
|
wpLink.range.select();
|
|
|
|
|
|
|
|
wpLink.range = null;
|
|
|
|
} else if ( typeof textarea.selectionStart !== 'undefined' ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// W3C.
|
2015-03-10 22:40:27 -04:00
|
|
|
begin = textarea.selectionStart;
|
|
|
|
end = textarea.selectionEnd;
|
|
|
|
selection = text || textarea.value.substring( begin, end );
|
|
|
|
html = html + selection + '</a>';
|
|
|
|
cursor = begin + html.length;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-26 16:45:09 -05:00
|
|
|
// If no text is selected, place the cursor inside the closing tag.
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( begin === end && ! selection ) {
|
|
|
|
cursor -= 4;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
textarea.value = (
|
|
|
|
textarea.value.substring( 0, begin ) +
|
|
|
|
html +
|
|
|
|
textarea.value.substring( end, textarea.value.length )
|
|
|
|
);
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Update cursor position.
|
2012-08-22 20:04:18 -04:00
|
|
|
textarea.selectionStart = textarea.selectionEnd = cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpLink.close();
|
|
|
|
textarea.focus();
|
2017-05-10 20:29:41 -04:00
|
|
|
$( textarea ).trigger( 'change' );
|
2016-03-13 20:53:26 -04:00
|
|
|
|
|
|
|
// Audible confirmation message when a link has been inserted in the Editor.
|
|
|
|
wp.a11y.speak( wpLinkL10n.linkInserted );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
mceUpdate: function() {
|
2015-03-10 22:40:27 -04:00
|
|
|
var attrs = wpLink.getAttrs(),
|
2019-01-26 07:59:49 -05:00
|
|
|
$link, text, hasText;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-09-19 04:01:45 -04:00
|
|
|
var parser = document.createElement( 'a' );
|
|
|
|
parser.href = attrs.href;
|
|
|
|
|
|
|
|
if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line
|
|
|
|
attrs.href = '';
|
|
|
|
}
|
|
|
|
|
2015-03-10 22:40:27 -04:00
|
|
|
if ( ! attrs.href ) {
|
2014-03-10 20:04:14 -04:00
|
|
|
editor.execCommand( 'unlink' );
|
2016-02-27 16:33:26 -05:00
|
|
|
wpLink.close();
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-24 06:21:50 -05:00
|
|
|
$link = getLink();
|
2015-03-10 22:40:27 -04:00
|
|
|
|
2016-10-17 16:01:31 -04:00
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
if ( ! $link.length ) {
|
|
|
|
editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder', 'data-wp-temp-link': 1 } );
|
|
|
|
$link = editor.$( 'a[data-wp-temp-link="1"]' ).removeAttr( 'data-wp-temp-link' );
|
2021-03-18 15:01:03 -04:00
|
|
|
hasText = $link.text().trim();
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 16:01:31 -04:00
|
|
|
if ( ! $link.length ) {
|
|
|
|
editor.execCommand( 'unlink' );
|
2015-03-10 22:40:27 -04:00
|
|
|
} else {
|
2016-10-17 16:01:31 -04:00
|
|
|
if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
|
|
|
|
text = inputs.text.val();
|
|
|
|
|
|
|
|
if ( text ) {
|
|
|
|
$link.text( text );
|
|
|
|
} else if ( ! hasText ) {
|
|
|
|
$link.text( attrs.href );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs['data-wplink-edit'] = null;
|
2019-01-24 06:21:50 -05:00
|
|
|
attrs['data-mce-href'] = attrs.href;
|
2016-10-17 16:01:31 -04:00
|
|
|
$link.attr( attrs );
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2016-10-17 16:01:31 -04:00
|
|
|
} );
|
2015-06-24 19:23:26 -04:00
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
wpLink.close( 'noReset' );
|
|
|
|
editor.focus();
|
2016-03-13 20:53:26 -04:00
|
|
|
|
2016-10-17 16:01:31 -04:00
|
|
|
if ( $link.length ) {
|
|
|
|
editor.selection.select( $link[0] );
|
|
|
|
|
|
|
|
if ( editor.plugins.wplink ) {
|
|
|
|
editor.plugins.wplink.checkLink( $link[0] );
|
|
|
|
}
|
2016-06-17 16:41:28 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 16:01:31 -04:00
|
|
|
editor.nodeChanged();
|
|
|
|
|
2016-03-13 20:53:26 -04:00
|
|
|
// Audible confirmation message when a link has been inserted in the Editor.
|
|
|
|
wp.a11y.speak( wpLinkL10n.linkInserted );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
updateFields: function( e, li ) {
|
|
|
|
inputs.url.val( li.children( '.item-permalink' ).val() );
|
2019-01-24 06:21:50 -05:00
|
|
|
|
|
|
|
if ( inputs.wrap.hasClass( 'has-text-field' ) && ! inputs.text.val() ) {
|
|
|
|
inputs.text.val( li.children( '.item-title' ).text() );
|
|
|
|
}
|
2016-02-26 19:52:26 -05:00
|
|
|
},
|
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
getUrlFromSelection: function( selection ) {
|
|
|
|
if ( ! selection ) {
|
|
|
|
if ( this.isMCE() ) {
|
|
|
|
selection = editor.selection.getContent({ format: 'text' });
|
|
|
|
} else if ( document.selection && wpLink.range ) {
|
|
|
|
selection = wpLink.range.text;
|
|
|
|
} else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
|
|
|
|
selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
|
|
|
|
}
|
2015-03-10 22:40:27 -04:00
|
|
|
}
|
2014-06-09 11:35:14 -04:00
|
|
|
|
2021-03-18 15:01:03 -04:00
|
|
|
selection = selection || '';
|
|
|
|
selection = selection.trim();
|
2016-02-27 16:33:26 -05:00
|
|
|
|
2014-06-13 17:14:16 -04:00
|
|
|
if ( selection && emailRegexp.test( selection ) ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Selection is email address.
|
2016-02-27 16:33:26 -05:00
|
|
|
return 'mailto:' + selection;
|
2014-06-13 17:14:16 -04:00
|
|
|
} else if ( selection && urlRegexp.test( selection ) ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Selection is URL.
|
2016-02-27 16:33:26 -05:00
|
|
|
return selection.replace( /&|�?38;/gi, '&' );
|
2014-06-09 11:35:14 -04:00
|
|
|
}
|
|
|
|
|
2016-02-27 16:33:26 -05:00
|
|
|
return '';
|
|
|
|
},
|
|
|
|
|
|
|
|
setDefaultValues: function( selection ) {
|
|
|
|
inputs.url.val( this.getUrlFromSelection( selection ) );
|
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
// Empty the search field and swap the "rivers".
|
|
|
|
inputs.search.val('');
|
|
|
|
wpLink.searchInternalLinks();
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
// Update save prompt.
|
|
|
|
inputs.submit.val( wpLinkL10n.save );
|
2016-04-01 17:13:29 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
searchInternalLinks: function() {
|
|
|
|
var waiting,
|
2019-12-17 20:15:03 -05:00
|
|
|
search = inputs.search.val() || '',
|
|
|
|
minInputLength = parseInt( wpLinkL10n.minInputLength, 10 ) || 3;
|
2016-04-01 17:13:29 -04:00
|
|
|
|
2019-12-17 20:15:03 -05:00
|
|
|
if ( search.length >= minInputLength ) {
|
2016-04-01 17:13:29 -04:00
|
|
|
rivers.recent.hide();
|
|
|
|
rivers.search.show();
|
|
|
|
|
|
|
|
// Don't search if the keypress didn't change the title.
|
|
|
|
if ( wpLink.lastSearch == search )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wpLink.lastSearch = search;
|
|
|
|
waiting = inputs.search.parent().find( '.spinner' ).addClass( 'is-active' );
|
|
|
|
|
|
|
|
rivers.search.change( search );
|
|
|
|
rivers.search.ajax( function() {
|
|
|
|
waiting.removeClass( 'is-active' );
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
rivers.search.hide();
|
|
|
|
rivers.recent.show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
|
|
|
rivers.search.next();
|
|
|
|
rivers.recent.next();
|
|
|
|
},
|
|
|
|
|
|
|
|
prev: function() {
|
|
|
|
rivers.search.prev();
|
|
|
|
rivers.recent.prev();
|
|
|
|
},
|
|
|
|
|
|
|
|
keydown: function( event ) {
|
|
|
|
var fn, id;
|
|
|
|
|
|
|
|
// Escape key.
|
|
|
|
if ( 27 === event.keyCode ) {
|
|
|
|
wpLink.close();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
// Tab key.
|
|
|
|
} else if ( 9 === event.keyCode ) {
|
|
|
|
id = event.target.id;
|
|
|
|
|
|
|
|
// wp-link-submit must always be the last focusable element in the dialog.
|
2020-01-28 19:45:18 -05:00
|
|
|
// Following focusable elements will be skipped on keyboard navigation.
|
2016-04-01 17:13:29 -04:00
|
|
|
if ( id === 'wp-link-submit' && ! event.shiftKey ) {
|
2021-02-23 14:45:04 -05:00
|
|
|
inputs.close.trigger( 'focus' );
|
2016-04-01 17:13:29 -04:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( id === 'wp-link-close' && event.shiftKey ) {
|
2021-02-23 14:45:04 -05:00
|
|
|
inputs.submit.trigger( 'focus' );
|
2016-04-01 17:13:29 -04:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up Arrow and Down Arrow keys.
|
2018-03-08 15:58:30 -05:00
|
|
|
if ( event.shiftKey || ( 38 !== event.keyCode && 40 !== event.keyCode ) ) {
|
2016-04-01 17:13:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( document.activeElement &&
|
|
|
|
( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up Arrow key.
|
|
|
|
fn = 38 === event.keyCode ? 'prev' : 'next';
|
|
|
|
clearInterval( wpLink.keyInterval );
|
|
|
|
wpLink[ fn ]();
|
|
|
|
wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
|
|
|
|
keyup: function( event ) {
|
|
|
|
// Up Arrow and Down Arrow keys.
|
|
|
|
if ( 38 === event.keyCode || 40 === event.keyCode ) {
|
|
|
|
clearInterval( wpLink.keyInterval );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
delayedCallback: function( func, delay ) {
|
|
|
|
var timeoutTriggered, funcTriggered, funcArgs, funcContext;
|
|
|
|
|
|
|
|
if ( ! delay )
|
|
|
|
return func;
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
if ( funcTriggered )
|
|
|
|
return func.apply( funcContext, funcArgs );
|
|
|
|
// Otherwise, wait.
|
|
|
|
timeoutTriggered = true;
|
|
|
|
}, delay );
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
if ( timeoutTriggered )
|
|
|
|
return func.apply( this, arguments );
|
|
|
|
// Otherwise, wait.
|
|
|
|
funcArgs = arguments;
|
|
|
|
funcContext = this;
|
|
|
|
funcTriggered = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
River = function( element, search ) {
|
|
|
|
var self = this;
|
|
|
|
this.element = element;
|
|
|
|
this.ul = element.children( 'ul' );
|
|
|
|
this.contentHeight = element.children( '#link-selector-height' );
|
|
|
|
this.waiting = element.find('.river-waiting');
|
|
|
|
|
|
|
|
this.change( search );
|
|
|
|
this.refresh();
|
|
|
|
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#wp-link .query-results, #wp-link #link-selector' ).on( 'scroll', function() {
|
2016-04-01 17:13:29 -04:00
|
|
|
self.maybeLoad();
|
|
|
|
});
|
|
|
|
element.on( 'click', 'li', function( event ) {
|
|
|
|
self.select( $( this ), event );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( River.prototype, {
|
|
|
|
refresh: function() {
|
|
|
|
this.deselect();
|
|
|
|
this.visible = this.element.is( ':visible' );
|
|
|
|
},
|
|
|
|
show: function() {
|
|
|
|
if ( ! this.visible ) {
|
|
|
|
this.deselect();
|
|
|
|
this.element.show();
|
|
|
|
this.visible = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hide: function() {
|
|
|
|
this.element.hide();
|
|
|
|
this.visible = false;
|
|
|
|
},
|
|
|
|
// Selects a list item and triggers the river-select event.
|
|
|
|
select: function( li, event ) {
|
|
|
|
var liHeight, elHeight, liTop, elTop;
|
|
|
|
|
|
|
|
if ( li.hasClass( 'unselectable' ) || li == this.selected )
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.deselect();
|
|
|
|
this.selected = li.addClass( 'selected' );
|
2020-01-28 19:45:18 -05:00
|
|
|
// Make sure the element is visible.
|
2016-04-01 17:13:29 -04:00
|
|
|
liHeight = li.outerHeight();
|
|
|
|
elHeight = this.element.height();
|
|
|
|
liTop = li.position().top;
|
|
|
|
elTop = this.element.scrollTop();
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
if ( liTop < 0 ) // Make first visible element.
|
2016-04-01 17:13:29 -04:00
|
|
|
this.element.scrollTop( elTop + liTop );
|
2020-01-28 19:45:18 -05:00
|
|
|
else if ( liTop + liHeight > elHeight ) // Make last visible element.
|
2016-04-01 17:13:29 -04:00
|
|
|
this.element.scrollTop( elTop + liTop - elHeight + liHeight );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Trigger the river-select event.
|
2016-04-01 17:13:29 -04:00
|
|
|
this.element.trigger( 'river-select', [ li, event, this ] );
|
|
|
|
},
|
|
|
|
deselect: function() {
|
|
|
|
if ( this.selected )
|
|
|
|
this.selected.removeClass( 'selected' );
|
|
|
|
this.selected = false;
|
|
|
|
},
|
|
|
|
prev: function() {
|
|
|
|
if ( ! this.visible )
|
|
|
|
return;
|
|
|
|
|
|
|
|
var to;
|
|
|
|
if ( this.selected ) {
|
|
|
|
to = this.selected.prev( 'li' );
|
|
|
|
if ( to.length )
|
|
|
|
this.select( to );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
next: function() {
|
|
|
|
if ( ! this.visible )
|
|
|
|
return;
|
|
|
|
|
|
|
|
var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
|
|
|
|
if ( to.length )
|
|
|
|
this.select( to );
|
|
|
|
},
|
|
|
|
ajax: function( callback ) {
|
|
|
|
var self = this,
|
|
|
|
delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
|
|
|
|
response = wpLink.delayedCallback( function( results, params ) {
|
|
|
|
self.process( results, params );
|
|
|
|
if ( callback )
|
|
|
|
callback( results, params );
|
|
|
|
}, delay );
|
|
|
|
|
|
|
|
this.query.ajax( response );
|
|
|
|
},
|
|
|
|
change: function( search ) {
|
|
|
|
if ( this.query && this._search == search )
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._search = search;
|
|
|
|
this.query = new Query( search );
|
|
|
|
this.element.scrollTop( 0 );
|
|
|
|
},
|
|
|
|
process: function( results, params ) {
|
|
|
|
var list = '', alt = true, classes = '',
|
|
|
|
firstPage = params.page == 1;
|
|
|
|
|
|
|
|
if ( ! results ) {
|
|
|
|
if ( firstPage ) {
|
|
|
|
list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
|
|
|
|
wpLinkL10n.noMatchesFound + '</em></span></li>';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$.each( results, function() {
|
|
|
|
classes = alt ? 'alternate' : '';
|
|
|
|
classes += this.title ? '' : ' no-title';
|
|
|
|
list += classes ? '<li class="' + classes + '">' : '<li>';
|
|
|
|
list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
|
|
|
|
list += '<span class="item-title">';
|
|
|
|
list += this.title ? this.title : wpLinkL10n.noTitle;
|
|
|
|
list += '</span><span class="item-info">' + this.info + '</span></li>';
|
|
|
|
alt = ! alt;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ul[ firstPage ? 'html' : 'append' ]( list );
|
|
|
|
},
|
|
|
|
maybeLoad: function() {
|
|
|
|
var self = this,
|
|
|
|
el = this.element,
|
|
|
|
bottom = el.scrollTop() + el.height();
|
|
|
|
|
|
|
|
if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
|
|
|
|
return;
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
var newTop = el.scrollTop(),
|
|
|
|
newBottom = newTop + el.height();
|
|
|
|
|
|
|
|
if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
|
|
|
|
return;
|
|
|
|
|
|
|
|
self.waiting.addClass( 'is-active' );
|
|
|
|
el.scrollTop( newTop + self.waiting.outerHeight() );
|
|
|
|
|
|
|
|
self.ajax( function() {
|
|
|
|
self.waiting.removeClass( 'is-active' );
|
|
|
|
});
|
|
|
|
}, wpLink.timeToTriggerRiver );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2016-04-01 17:13:29 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Query = function( search ) {
|
|
|
|
this.page = 1;
|
|
|
|
this.allLoaded = false;
|
|
|
|
this.querying = false;
|
|
|
|
this.search = search;
|
2013-11-07 05:42:09 -05:00
|
|
|
};
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-04-01 17:13:29 -04:00
|
|
|
$.extend( Query.prototype, {
|
|
|
|
ready: function() {
|
|
|
|
return ! ( this.querying || this.allLoaded );
|
|
|
|
},
|
|
|
|
ajax: function( callback ) {
|
|
|
|
var self = this,
|
|
|
|
query = {
|
|
|
|
action : 'wp-link-ajax',
|
|
|
|
page : this.page,
|
|
|
|
'_ajax_linking_nonce' : inputs.nonce.val()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( this.search )
|
|
|
|
query.search = this.search;
|
|
|
|
|
|
|
|
this.querying = true;
|
|
|
|
|
|
|
|
$.post( window.ajaxurl, query, function( r ) {
|
|
|
|
self.page++;
|
|
|
|
self.querying = false;
|
|
|
|
self.allLoaded = ! r;
|
|
|
|
callback( r, query );
|
|
|
|
}, 'json' );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-18 15:01:03 -04:00
|
|
|
$( wpLink.init );
|
2016-03-13 20:53:26 -04:00
|
|
|
})( jQuery, window.wpLinkL10n, window.wp );
|