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