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