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