|
1 /* global ajaxurl, tinymce, wpLinkL10n, setUserSetting, wpActiveEditor */ |
1 var wpLink; |
2 var wpLink; |
2 |
3 |
3 (function($){ |
4 ( function( $ ) { |
4 var inputs = {}, rivers = {}, ed, River, Query; |
5 var editor, searchTimer, River, Query, correctedURL, |
|
6 inputs = {}, |
|
7 rivers = {}, |
|
8 isTouch = ( 'ontouchend' in document ); |
|
9 |
|
10 function getLink() { |
|
11 return editor.dom.getParent( editor.selection.getNode(), 'a' ); |
|
12 } |
5 |
13 |
6 wpLink = { |
14 wpLink = { |
7 timeToTriggerRiver: 150, |
15 timeToTriggerRiver: 150, |
8 minRiverAJAXDuration: 200, |
16 minRiverAJAXDuration: 200, |
9 riverBottomThreshold: 5, |
17 riverBottomThreshold: 5, |
10 keySensitivity: 100, |
18 keySensitivity: 100, |
11 lastSearch: '', |
19 lastSearch: '', |
12 textarea: '', |
20 textarea: '', |
13 |
21 |
14 init : function() { |
22 init: function() { |
15 inputs.dialog = $('#wp-link'); |
23 inputs.wrap = $('#wp-link-wrap'); |
16 inputs.submit = $('#wp-link-submit'); |
24 inputs.dialog = $( '#wp-link' ); |
17 // URL |
25 inputs.backdrop = $( '#wp-link-backdrop' ); |
18 inputs.url = $('#url-field'); |
26 inputs.submit = $( '#wp-link-submit' ); |
19 inputs.nonce = $('#_ajax_linking_nonce'); |
27 inputs.close = $( '#wp-link-close' ); |
20 // Secondary options |
28 |
21 inputs.title = $('#link-title-field'); |
29 // Input |
22 // Advanced Options |
30 inputs.text = $( '#wp-link-text' ); |
23 inputs.openInNewTab = $('#link-target-checkbox'); |
31 inputs.url = $( '#wp-link-url' ); |
24 inputs.search = $('#search-field'); |
32 inputs.nonce = $( '#_ajax_linking_nonce' ); |
|
33 inputs.openInNewTab = $( '#wp-link-target' ); |
|
34 inputs.search = $( '#wp-link-search' ); |
|
35 |
25 // Build Rivers |
36 // Build Rivers |
26 rivers.search = new River( $('#search-results') ); |
37 rivers.search = new River( $( '#search-results' ) ); |
27 rivers.recent = new River( $('#most-recent-results') ); |
38 rivers.recent = new River( $( '#most-recent-results' ) ); |
28 rivers.elements = $('.query-results', inputs.dialog); |
39 rivers.elements = inputs.dialog.find( '.query-results' ); |
|
40 |
|
41 // Get search notice text |
|
42 inputs.queryNotice = $( '#query-notice-message' ); |
|
43 inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' ); |
|
44 inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' ); |
29 |
45 |
30 // Bind event handlers |
46 // Bind event handlers |
31 inputs.dialog.keydown( wpLink.keydown ); |
47 inputs.dialog.keydown( wpLink.keydown ); |
32 inputs.dialog.keyup( wpLink.keyup ); |
48 inputs.dialog.keyup( wpLink.keyup ); |
33 inputs.submit.click( function(e){ |
49 inputs.submit.click( function( event ) { |
34 e.preventDefault(); |
50 event.preventDefault(); |
35 wpLink.update(); |
51 wpLink.update(); |
36 }); |
52 }); |
37 $('#wp-link-cancel').click( function(e){ |
53 inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel a' ).click( function( event ) { |
38 e.preventDefault(); |
54 event.preventDefault(); |
39 wpLink.close(); |
55 wpLink.close(); |
40 }); |
56 }); |
41 $('#internal-toggle').click( wpLink.toggleInternalLinking ); |
57 |
42 |
58 $( '#wp-link-search-toggle' ).on( 'click', wpLink.toggleInternalLinking ); |
43 rivers.elements.bind('river-select', wpLink.updateFields ); |
59 |
44 |
60 rivers.elements.on( 'river-select', wpLink.updateFields ); |
45 inputs.search.keyup( wpLink.searchInternalLinks ); |
61 |
46 |
62 // Display 'hint' message when search field or 'query-results' box are focused |
47 inputs.dialog.bind('wpdialogrefresh', wpLink.refresh); |
63 inputs.search.on( 'focus.wplink', function() { |
48 inputs.dialog.bind('wpdialogbeforeopen', wpLink.beforeOpen); |
64 inputs.queryNoticeTextDefault.hide(); |
49 inputs.dialog.bind('wpdialogclose', wpLink.onClose); |
65 inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show(); |
50 }, |
66 } ).on( 'blur.wplink', function() { |
51 |
67 inputs.queryNoticeTextDefault.show(); |
52 beforeOpen : function() { |
68 inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide(); |
|
69 } ); |
|
70 |
|
71 inputs.search.keyup( function() { |
|
72 var self = this; |
|
73 |
|
74 window.clearTimeout( searchTimer ); |
|
75 searchTimer = window.setTimeout( function() { |
|
76 wpLink.searchInternalLinks.call( self ); |
|
77 }, 500 ); |
|
78 }); |
|
79 |
|
80 function correctURL() { |
|
81 var url = $.trim( inputs.url.val() ); |
|
82 |
|
83 if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) { |
|
84 inputs.url.val( 'http://' + url ); |
|
85 correctedURL = url; |
|
86 } |
|
87 } |
|
88 |
|
89 inputs.url.on( 'paste', function() { |
|
90 setTimeout( correctURL, 0 ); |
|
91 } ); |
|
92 |
|
93 inputs.url.on( 'blur', correctURL ); |
|
94 }, |
|
95 |
|
96 open: function( editorId ) { |
|
97 var ed; |
|
98 |
|
99 $( document.body ).addClass( 'modal-open' ); |
|
100 |
53 wpLink.range = null; |
101 wpLink.range = null; |
54 |
102 |
|
103 if ( editorId ) { |
|
104 window.wpActiveEditor = editorId; |
|
105 } |
|
106 |
|
107 if ( ! window.wpActiveEditor ) { |
|
108 return; |
|
109 } |
|
110 |
|
111 this.textarea = $( '#' + window.wpActiveEditor ).get( 0 ); |
|
112 |
|
113 if ( typeof tinymce !== 'undefined' ) { |
|
114 ed = tinymce.get( wpActiveEditor ); |
|
115 |
|
116 if ( ed && ! ed.isHidden() ) { |
|
117 editor = ed; |
|
118 } else { |
|
119 editor = null; |
|
120 } |
|
121 |
|
122 if ( editor && tinymce.isIE ) { |
|
123 editor.windowManager.bookmark = editor.selection.getBookmark(); |
|
124 } |
|
125 } |
|
126 |
55 if ( ! wpLink.isMCE() && document.selection ) { |
127 if ( ! wpLink.isMCE() && document.selection ) { |
56 wpLink.textarea.focus(); |
128 this.textarea.focus(); |
57 wpLink.range = document.selection.createRange(); |
129 this.range = document.selection.createRange(); |
58 } |
130 } |
59 }, |
131 |
60 |
132 inputs.wrap.show(); |
61 open : function() { |
133 inputs.backdrop.show(); |
62 if ( !wpActiveEditor ) |
134 |
63 return; |
135 wpLink.refresh(); |
64 |
136 |
65 this.textarea = $('#'+wpActiveEditor).get(0); |
137 $( document ).trigger( 'wplink-open', inputs.wrap ); |
66 |
138 }, |
67 // Initialize the dialog if necessary (html mode). |
139 |
68 if ( ! inputs.dialog.data('wpdialog') ) { |
140 isMCE: function() { |
69 inputs.dialog.wpdialog({ |
141 return editor && ! editor.isHidden(); |
70 title: wpLinkL10n.title, |
142 }, |
71 width: 480, |
143 |
72 height: 'auto', |
144 refresh: function() { |
73 modal: true, |
145 var text = ''; |
74 dialogClass: 'wp-dialog' |
146 |
75 }); |
|
76 } |
|
77 |
|
78 inputs.dialog.wpdialog('open'); |
|
79 }, |
|
80 |
|
81 isMCE : function() { |
|
82 return tinyMCEPopup && ( ed = tinyMCEPopup.editor ) && ! ed.isHidden(); |
|
83 }, |
|
84 |
|
85 refresh : function() { |
|
86 // Refresh rivers (clear links, check visibility) |
147 // Refresh rivers (clear links, check visibility) |
87 rivers.search.refresh(); |
148 rivers.search.refresh(); |
88 rivers.recent.refresh(); |
149 rivers.recent.refresh(); |
89 |
150 |
90 if ( wpLink.isMCE() ) |
151 if ( wpLink.isMCE() ) { |
91 wpLink.mceRefresh(); |
152 wpLink.mceRefresh(); |
92 else |
153 } else { |
|
154 // For the Text editor the "Link text" field is always shown |
|
155 if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) { |
|
156 inputs.wrap.addClass( 'has-text-field' ); |
|
157 } |
|
158 |
|
159 if ( document.selection ) { |
|
160 // Old IE |
|
161 text = document.selection.createRange().text || ''; |
|
162 } else if ( typeof this.textarea.selectionStart !== 'undefined' && |
|
163 ( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) { |
|
164 // W3C |
|
165 text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || ''; |
|
166 } |
|
167 |
|
168 inputs.text.val( text ); |
93 wpLink.setDefaultValues(); |
169 wpLink.setDefaultValues(); |
94 |
170 } |
95 // Focus the URL field and highlight its contents. |
171 |
96 // If this is moved above the selection changes, |
172 if ( isTouch ) { |
97 // IE will show a flashing cursor over the dialog. |
173 // Close the onscreen keyboard |
98 inputs.url.focus()[0].select(); |
174 inputs.url.focus().blur(); |
|
175 } else { |
|
176 // Focus the URL field and highlight its contents. |
|
177 // If this is moved above the selection changes, |
|
178 // IE will show a flashing cursor over the dialog. |
|
179 inputs.url.focus()[0].select(); |
|
180 } |
|
181 |
99 // Load the most recent results if this is the first time opening the panel. |
182 // Load the most recent results if this is the first time opening the panel. |
100 if ( ! rivers.recent.ul.children().length ) |
183 if ( ! rivers.recent.ul.children().length ) { |
101 rivers.recent.ajax(); |
184 rivers.recent.ajax(); |
102 }, |
185 } |
103 |
186 |
104 mceRefresh : function() { |
187 correctedURL = inputs.url.val().replace( /^http:\/\//, '' ); |
105 var e; |
188 }, |
106 ed = tinyMCEPopup.editor; |
189 |
107 |
190 hasSelectedText: function( linkNode ) { |
108 tinyMCEPopup.restoreSelection(); |
191 var html = editor.selection.getContent(); |
109 |
192 |
110 // If link exists, select proper values. |
193 // Partial html and not a fully selected anchor element |
111 if ( e = ed.dom.getParent(ed.selection.getNode(), 'A') ) { |
194 if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) { |
112 // Set URL and description. |
195 return false; |
113 inputs.url.val( ed.dom.getAttrib(e, 'href') ); |
196 } |
114 inputs.title.val( ed.dom.getAttrib(e, 'title') ); |
197 |
115 // Set open in new tab. |
198 if ( linkNode ) { |
116 inputs.openInNewTab.prop('checked', ( "_blank" == ed.dom.getAttrib( e, 'target' ) ) ); |
199 var nodes = linkNode.childNodes, i; |
117 // Update save prompt. |
200 |
|
201 if ( nodes.length === 0 ) { |
|
202 return false; |
|
203 } |
|
204 |
|
205 for ( i = nodes.length - 1; i >= 0; i-- ) { |
|
206 if ( nodes[i].nodeType != 3 ) { |
|
207 return false; |
|
208 } |
|
209 } |
|
210 } |
|
211 |
|
212 return true; |
|
213 }, |
|
214 |
|
215 mceRefresh: function() { |
|
216 var text, |
|
217 selectedNode = editor.selection.getNode(), |
|
218 linkNode = editor.dom.getParent( selectedNode, 'a[href]' ), |
|
219 onlyText = this.hasSelectedText( linkNode ); |
|
220 |
|
221 if ( linkNode ) { |
|
222 text = linkNode.innerText || linkNode.textContent; |
|
223 inputs.url.val( editor.dom.getAttrib( linkNode, 'href' ) ); |
|
224 inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) ); |
118 inputs.submit.val( wpLinkL10n.update ); |
225 inputs.submit.val( wpLinkL10n.update ); |
119 |
226 } else { |
120 // If there's no link, set the default values. |
227 text = editor.selection.getContent({ format: 'text' }); |
121 } else { |
228 this.setDefaultValues(); |
122 wpLink.setDefaultValues(); |
229 } |
123 } |
230 |
124 }, |
231 if ( onlyText ) { |
125 |
232 inputs.text.val( text || '' ); |
126 close : function() { |
233 inputs.wrap.addClass( 'has-text-field' ); |
127 if ( wpLink.isMCE() ) |
234 } else { |
128 tinyMCEPopup.close(); |
235 inputs.text.val( '' ); |
129 else |
236 inputs.wrap.removeClass( 'has-text-field' ); |
130 inputs.dialog.wpdialog('close'); |
237 } |
131 }, |
238 }, |
132 |
239 |
133 onClose: function() { |
240 close: function() { |
|
241 $( document.body ).removeClass( 'modal-open' ); |
|
242 |
134 if ( ! wpLink.isMCE() ) { |
243 if ( ! wpLink.isMCE() ) { |
135 wpLink.textarea.focus(); |
244 wpLink.textarea.focus(); |
|
245 |
136 if ( wpLink.range ) { |
246 if ( wpLink.range ) { |
137 wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); |
247 wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); |
138 wpLink.range.select(); |
248 wpLink.range.select(); |
139 } |
249 } |
140 } |
250 } else { |
141 }, |
251 editor.focus(); |
142 |
252 } |
143 getAttrs : function() { |
253 |
|
254 inputs.backdrop.hide(); |
|
255 inputs.wrap.hide(); |
|
256 |
|
257 correctedURL = false; |
|
258 |
|
259 $( document ).trigger( 'wplink-close', inputs.wrap ); |
|
260 }, |
|
261 |
|
262 getAttrs: function() { |
144 return { |
263 return { |
145 href : inputs.url.val(), |
264 href: $.trim( inputs.url.val() ), |
146 title : inputs.title.val(), |
265 target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : '' |
147 target : inputs.openInNewTab.prop('checked') ? '_blank' : '' |
|
148 }; |
266 }; |
149 }, |
267 }, |
150 |
268 |
151 update : function() { |
269 update: function() { |
152 if ( wpLink.isMCE() ) |
270 if ( wpLink.isMCE() ) { |
153 wpLink.mceUpdate(); |
271 wpLink.mceUpdate(); |
154 else |
272 } else { |
155 wpLink.htmlUpdate(); |
273 wpLink.htmlUpdate(); |
156 }, |
274 } |
157 |
275 }, |
158 htmlUpdate : function() { |
276 |
159 var attrs, html, begin, end, cursor, |
277 htmlUpdate: function() { |
|
278 var attrs, text, html, begin, end, cursor, selection, |
160 textarea = wpLink.textarea; |
279 textarea = wpLink.textarea; |
161 |
280 |
162 if ( ! textarea ) |
281 if ( ! textarea ) { |
163 return; |
282 return; |
|
283 } |
164 |
284 |
165 attrs = wpLink.getAttrs(); |
285 attrs = wpLink.getAttrs(); |
|
286 text = inputs.text.val(); |
166 |
287 |
167 // If there's no href, return. |
288 // If there's no href, return. |
168 if ( ! attrs.href || attrs.href == 'http://' ) |
289 if ( ! attrs.href ) { |
169 return; |
290 return; |
|
291 } |
170 |
292 |
171 // Build HTML |
293 // Build HTML |
172 html = '<a href="' + attrs.href + '"'; |
294 html = '<a href="' + attrs.href + '"'; |
173 |
295 |
174 if ( attrs.title ) |
296 if ( attrs.target ) { |
175 html += ' title="' + attrs.title + '"'; |
|
176 if ( attrs.target ) |
|
177 html += ' target="' + attrs.target + '"'; |
297 html += ' target="' + attrs.target + '"'; |
|
298 } |
178 |
299 |
179 html += '>'; |
300 html += '>'; |
180 |
301 |
181 // Insert HTML |
302 // Insert HTML |
182 if ( document.selection && wpLink.range ) { |
303 if ( document.selection && wpLink.range ) { |
183 // IE |
304 // IE |
184 // Note: If no text is selected, IE will not place the cursor |
305 // Note: If no text is selected, IE will not place the cursor |
185 // inside the closing tag. |
306 // inside the closing tag. |
186 textarea.focus(); |
307 textarea.focus(); |
187 wpLink.range.text = html + wpLink.range.text + '</a>'; |
308 wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>'; |
188 wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); |
309 wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); |
189 wpLink.range.select(); |
310 wpLink.range.select(); |
190 |
311 |
191 wpLink.range = null; |
312 wpLink.range = null; |
192 } else if ( typeof textarea.selectionStart !== 'undefined' ) { |
313 } else if ( typeof textarea.selectionStart !== 'undefined' ) { |
193 // W3C |
314 // W3C |
194 begin = textarea.selectionStart; |
315 begin = textarea.selectionStart; |
195 end = textarea.selectionEnd; |
316 end = textarea.selectionEnd; |
196 selection = textarea.value.substring( begin, end ); |
317 selection = text || textarea.value.substring( begin, end ); |
197 html = html + selection + '</a>'; |
318 html = html + selection + '</a>'; |
198 cursor = begin + html.length; |
319 cursor = begin + html.length; |
199 |
320 |
200 // If no next is selected, place the cursor inside the closing tag. |
321 // If no text is selected, place the cursor inside the closing tag. |
201 if ( begin == end ) |
322 if ( begin === end && ! selection ) { |
202 cursor -= '</a>'.length; |
323 cursor -= 4; |
203 |
324 } |
204 textarea.value = textarea.value.substring( 0, begin ) |
325 |
205 + html |
326 textarea.value = ( |
206 + textarea.value.substring( end, textarea.value.length ); |
327 textarea.value.substring( 0, begin ) + |
|
328 html + |
|
329 textarea.value.substring( end, textarea.value.length ) |
|
330 ); |
207 |
331 |
208 // Update cursor position |
332 // Update cursor position |
209 textarea.selectionStart = textarea.selectionEnd = cursor; |
333 textarea.selectionStart = textarea.selectionEnd = cursor; |
210 } |
334 } |
211 |
335 |
212 wpLink.close(); |
336 wpLink.close(); |
213 textarea.focus(); |
337 textarea.focus(); |
214 }, |
338 }, |
215 |
339 |
216 mceUpdate : function() { |
340 mceUpdate: function() { |
217 var ed = tinyMCEPopup.editor, |
341 var attrs = wpLink.getAttrs(), |
218 attrs = wpLink.getAttrs(), |
342 link, text; |
219 e, b; |
343 |
220 |
344 wpLink.close(); |
221 tinyMCEPopup.restoreSelection(); |
345 editor.focus(); |
222 e = ed.dom.getParent(ed.selection.getNode(), 'A'); |
346 |
223 |
347 if ( tinymce.isIE ) { |
224 // If the values are empty, unlink and return |
348 editor.selection.moveToBookmark( editor.windowManager.bookmark ); |
225 if ( ! attrs.href || attrs.href == 'http://' ) { |
349 } |
226 if ( e ) { |
350 |
227 b = ed.selection.getBookmark(); |
351 if ( ! attrs.href ) { |
228 ed.dom.remove(e, 1); |
352 editor.execCommand( 'unlink' ); |
229 ed.selection.moveToBookmark(b); |
353 return; |
230 tinyMCEPopup.execCommand("mceEndUndoLevel"); |
354 } |
231 wpLink.close(); |
355 |
232 } |
356 link = getLink(); |
233 return; |
357 text = inputs.text.val(); |
234 } |
358 |
235 |
359 if ( link ) { |
236 if (e == null) { |
360 if ( text ) { |
237 ed.getDoc().execCommand("unlink", false, null); |
361 if ( 'innerText' in link ) { |
238 tinyMCEPopup.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo : 1}); |
362 link.innerText = text; |
239 |
363 } else { |
240 tinymce.each(ed.dom.select("a"), function(n) { |
364 link.textContent = text; |
241 if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') { |
|
242 e = n; |
|
243 ed.dom.setAttribs(e, attrs); |
|
244 } |
365 } |
245 }); |
366 } |
246 |
367 |
247 // Sometimes WebKit lets a user create a link where |
368 editor.dom.setAttribs( link, attrs ); |
248 // they shouldn't be able to. In this case, CreateLink |
369 } else { |
249 // injects "#mce_temp_url#" into their content. Fix it. |
370 if ( text ) { |
250 if ( tinymce.isWebKit && $(e).text() == '#mce_temp_url#' ) { |
371 editor.selection.setNode( editor.dom.create( 'a', attrs, text ) ); |
251 ed.dom.remove(e); |
372 } else { |
252 e = null; |
373 editor.execCommand( 'mceInsertLink', false, attrs ); |
253 } |
374 } |
254 } else { |
375 } |
255 ed.dom.setAttribs(e, attrs); |
376 }, |
256 } |
377 |
257 |
378 updateFields: function( e, li ) { |
258 // Move the caret if selection was not an image. |
379 inputs.url.val( li.children( '.item-permalink' ).val() ); |
259 if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) { |
380 }, |
260 ed.selection.select(e); |
381 |
261 ed.selection.collapse(0); |
382 setDefaultValues: function() { |
262 tinyMCEPopup.storeSelection(); |
383 var selection, |
263 } |
384 emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, |
264 |
385 urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i; |
265 ed.execCommand("mceEndUndoLevel"); |
386 |
266 wpLink.close(); |
387 if ( this.isMCE() ) { |
267 ed.focus(); |
388 selection = editor.selection.getContent(); |
268 }, |
389 } else if ( document.selection && wpLink.range ) { |
269 |
390 selection = wpLink.range.text; |
270 updateFields : function( e, li, originalEvent ) { |
391 } else if ( typeof this.textarea.selectionStart !== 'undefined' ) { |
271 inputs.url.val( li.children('.item-permalink').val() ); |
392 selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ); |
272 inputs.title.val( li.hasClass('no-title') ? '' : li.children('.item-title').text() ); |
393 } |
273 if ( originalEvent && originalEvent.type == "click" ) |
394 |
274 inputs.url.focus(); |
395 if ( selection && emailRegexp.test( selection ) ) { |
275 }, |
396 // Selection is email address |
276 setDefaultValues : function() { |
397 inputs.url.val( 'mailto:' + selection ); |
277 // Set URL and description to defaults. |
398 } else if ( selection && urlRegexp.test( selection ) ) { |
278 // Leave the new tab setting as-is. |
399 // Selection is URL |
279 inputs.url.val('http://'); |
400 inputs.url.val( selection.replace( /&|�?38;/gi, '&' ) ); |
280 inputs.title.val(''); |
401 } else { |
|
402 // Set URL to default. |
|
403 inputs.url.val( '' ); |
|
404 } |
281 |
405 |
282 // Update save prompt. |
406 // Update save prompt. |
283 inputs.submit.val( wpLinkL10n.save ); |
407 inputs.submit.val( wpLinkL10n.save ); |
284 }, |
408 }, |
285 |
409 |
286 searchInternalLinks : function() { |
410 searchInternalLinks: function() { |
287 var t = $(this), waiting, |
411 var t = $( this ), waiting, |
288 search = t.val(); |
412 search = t.val(); |
289 |
413 |
290 if ( search.length > 2 ) { |
414 if ( search.length > 2 ) { |
291 rivers.recent.hide(); |
415 rivers.recent.hide(); |
292 rivers.search.show(); |
416 rivers.search.show(); |
294 // Don't search if the keypress didn't change the title. |
418 // Don't search if the keypress didn't change the title. |
295 if ( wpLink.lastSearch == search ) |
419 if ( wpLink.lastSearch == search ) |
296 return; |
420 return; |
297 |
421 |
298 wpLink.lastSearch = search; |
422 wpLink.lastSearch = search; |
299 waiting = t.parent().find('.spinner').show(); |
423 waiting = t.parent().find( '.spinner' ).addClass( 'is-active' ); |
300 |
424 |
301 rivers.search.change( search ); |
425 rivers.search.change( search ); |
302 rivers.search.ajax( function(){ waiting.hide(); }); |
426 rivers.search.ajax( function() { |
|
427 waiting.removeClass( 'is-active' ); |
|
428 }); |
303 } else { |
429 } else { |
304 rivers.search.hide(); |
430 rivers.search.hide(); |
305 rivers.recent.show(); |
431 rivers.recent.show(); |
306 } |
432 } |
307 }, |
433 }, |
308 |
434 |
309 next : function() { |
435 next: function() { |
310 rivers.search.next(); |
436 rivers.search.next(); |
311 rivers.recent.next(); |
437 rivers.recent.next(); |
312 }, |
438 }, |
313 prev : function() { |
439 |
|
440 prev: function() { |
314 rivers.search.prev(); |
441 rivers.search.prev(); |
315 rivers.recent.prev(); |
442 rivers.recent.prev(); |
316 }, |
443 }, |
317 |
444 |
318 keydown : function( event ) { |
445 keydown: function( event ) { |
319 var fn, key = $.ui.keyCode; |
446 var fn, id, |
320 |
447 key = $.ui.keyCode; |
321 switch( event.which ) { |
448 |
322 case key.UP: |
449 if ( key.ESCAPE === event.keyCode ) { |
323 fn = 'prev'; |
450 wpLink.close(); |
324 case key.DOWN: |
451 event.stopImmediatePropagation(); |
325 fn = fn || 'next'; |
452 } else if ( key.TAB === event.keyCode ) { |
326 clearInterval( wpLink.keyInterval ); |
453 id = event.target.id; |
327 wpLink[ fn ](); |
454 |
328 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity ); |
455 // wp-link-submit must always be the last focusable element in the dialog. |
329 break; |
456 // following focusable elements will be skipped on keyboard navigation. |
330 default: |
457 if ( id === 'wp-link-submit' && ! event.shiftKey ) { |
331 return; |
458 inputs.close.focus(); |
332 } |
459 event.preventDefault(); |
|
460 } else if ( id === 'wp-link-close' && event.shiftKey ) { |
|
461 inputs.submit.focus(); |
|
462 event.preventDefault(); |
|
463 } |
|
464 } |
|
465 |
|
466 if ( event.keyCode !== key.UP && event.keyCode !== key.DOWN ) { |
|
467 return; |
|
468 } |
|
469 |
|
470 if ( document.activeElement && |
|
471 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) { |
|
472 return; |
|
473 } |
|
474 |
|
475 fn = event.keyCode === key.UP ? 'prev' : 'next'; |
|
476 clearInterval( wpLink.keyInterval ); |
|
477 wpLink[ fn ](); |
|
478 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity ); |
333 event.preventDefault(); |
479 event.preventDefault(); |
334 }, |
480 }, |
|
481 |
335 keyup: function( event ) { |
482 keyup: function( event ) { |
336 var key = $.ui.keyCode; |
483 var key = $.ui.keyCode; |
337 |
484 |
338 switch( event.which ) { |
485 if ( event.which === key.UP || event.which === key.DOWN ) { |
339 case key.ESCAPE: |
486 clearInterval( wpLink.keyInterval ); |
340 event.stopImmediatePropagation(); |
487 event.preventDefault(); |
341 if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wplink', cb: wpLink.close }] ) ) |
488 } |
342 wpLink.close(); |
489 }, |
343 |
490 |
344 return false; |
491 delayedCallback: function( func, delay ) { |
345 break; |
|
346 case key.UP: |
|
347 case key.DOWN: |
|
348 clearInterval( wpLink.keyInterval ); |
|
349 break; |
|
350 default: |
|
351 return; |
|
352 } |
|
353 event.preventDefault(); |
|
354 }, |
|
355 |
|
356 delayedCallback : function( func, delay ) { |
|
357 var timeoutTriggered, funcTriggered, funcArgs, funcContext; |
492 var timeoutTriggered, funcTriggered, funcArgs, funcContext; |
358 |
493 |
359 if ( ! delay ) |
494 if ( ! delay ) |
360 return func; |
495 return func; |
361 |
496 |
362 setTimeout( function() { |
497 setTimeout( function() { |
363 if ( funcTriggered ) |
498 if ( funcTriggered ) |
364 return func.apply( funcContext, funcArgs ); |
499 return func.apply( funcContext, funcArgs ); |
365 // Otherwise, wait. |
500 // Otherwise, wait. |
366 timeoutTriggered = true; |
501 timeoutTriggered = true; |
367 }, delay); |
502 }, delay ); |
368 |
503 |
369 return function() { |
504 return function() { |
370 if ( timeoutTriggered ) |
505 if ( timeoutTriggered ) |
371 return func.apply( this, arguments ); |
506 return func.apply( this, arguments ); |
372 // Otherwise, wait. |
507 // Otherwise, wait. |