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