1 var wpLink; |
1 /** |
|
2 * @output wp-includes/js/wplink.js |
|
3 */ |
|
4 |
|
5 /* global wpLink */ |
2 |
6 |
3 ( function( $, wpLinkL10n, wp ) { |
7 ( function( $, wpLinkL10n, wp ) { |
4 var editor, searchTimer, River, Query, correctedURL, linkNode, |
8 var editor, searchTimer, River, Query, correctedURL, |
5 emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i, |
9 emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i, |
6 urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i, |
10 urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i, |
7 inputs = {}, |
11 inputs = {}, |
8 rivers = {}, |
12 rivers = {}, |
9 isTouch = ( 'ontouchend' in document ); |
13 isTouch = ( 'ontouchend' in document ); |
10 |
14 |
11 function getLink() { |
15 function getLink() { |
12 return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' ); |
16 if ( editor ) { |
|
17 return editor.$( 'a[data-wplink-edit="true"]' ); |
|
18 } |
|
19 |
|
20 return null; |
13 } |
21 } |
14 |
22 |
15 wpLink = { |
23 window.wpLink = { |
16 timeToTriggerRiver: 150, |
24 timeToTriggerRiver: 150, |
17 minRiverAJAXDuration: 200, |
25 minRiverAJAXDuration: 200, |
18 riverBottomThreshold: 5, |
26 riverBottomThreshold: 5, |
19 keySensitivity: 100, |
27 keySensitivity: 100, |
20 lastSearch: '', |
28 lastSearch: '', |
91 inputs.url.val( 'http://' + url ); |
99 inputs.url.val( 'http://' + url ); |
92 correctedURL = url; |
100 correctedURL = url; |
93 } |
101 } |
94 }, |
102 }, |
95 |
103 |
96 open: function( editorId, url, text, node ) { |
104 open: function( editorId, url, text ) { |
97 var ed, |
105 var ed, |
98 $body = $( document.body ); |
106 $body = $( document.body ); |
99 |
107 |
100 $body.addClass( 'modal-open' ); |
108 $body.addClass( 'modal-open' ); |
101 wpLink.modalOpen = true; |
109 wpLink.modalOpen = true; |
102 linkNode = node; |
|
103 |
110 |
104 wpLink.range = null; |
111 wpLink.range = null; |
105 |
112 |
106 if ( editorId ) { |
113 if ( editorId ) { |
107 window.wpActiveEditor = editorId; |
114 window.wpActiveEditor = editorId; |
199 // Partial html and not a fully selected anchor element |
206 // Partial html and not a fully selected anchor element |
200 if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) { |
207 if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) { |
201 return false; |
208 return false; |
202 } |
209 } |
203 |
210 |
204 if ( linkNode ) { |
211 if ( linkNode.length ) { |
205 nodes = linkNode.childNodes; |
212 nodes = linkNode[0].childNodes; |
206 |
213 |
207 if ( nodes.length === 0 ) { |
214 if ( ! nodes || ! nodes.length ) { |
208 return false; |
215 return false; |
209 } |
216 } |
210 |
217 |
211 for ( i = nodes.length - 1; i >= 0; i-- ) { |
218 for ( i = nodes.length - 1; i >= 0; i-- ) { |
212 node = nodes[i]; |
219 node = nodes[i]; |
223 mceRefresh: function( searchStr, text ) { |
230 mceRefresh: function( searchStr, text ) { |
224 var linkText, href, |
231 var linkText, href, |
225 linkNode = getLink(), |
232 linkNode = getLink(), |
226 onlyText = this.hasSelectedText( linkNode ); |
233 onlyText = this.hasSelectedText( linkNode ); |
227 |
234 |
228 if ( linkNode ) { |
235 if ( linkNode.length ) { |
229 linkText = linkNode.textContent || linkNode.innerText; |
236 linkText = linkNode.text(); |
230 href = editor.dom.getAttrib( linkNode, 'href' ); |
237 href = linkNode.attr( 'href' ); |
231 |
238 |
232 if ( ! $.trim( linkText ) ) { |
239 if ( ! $.trim( linkText ) ) { |
233 linkText = text || ''; |
240 linkText = text || ''; |
234 } |
241 } |
235 |
242 |
237 href = searchStr; |
244 href = searchStr; |
238 } |
245 } |
239 |
246 |
240 if ( href !== '_wp_link_placeholder' ) { |
247 if ( href !== '_wp_link_placeholder' ) { |
241 inputs.url.val( href ); |
248 inputs.url.val( href ); |
242 inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) ); |
249 inputs.openInNewTab.prop( 'checked', '_blank' === linkNode.attr( 'target' ) ); |
243 inputs.submit.val( wpLinkL10n.update ); |
250 inputs.submit.val( wpLinkL10n.update ); |
244 } else { |
251 } else { |
245 this.setDefaultValues( linkText ); |
252 this.setDefaultValues( linkText ); |
246 } |
253 } |
247 |
254 |
393 wp.a11y.speak( wpLinkL10n.linkInserted ); |
400 wp.a11y.speak( wpLinkL10n.linkInserted ); |
394 }, |
401 }, |
395 |
402 |
396 mceUpdate: function() { |
403 mceUpdate: function() { |
397 var attrs = wpLink.getAttrs(), |
404 var attrs = wpLink.getAttrs(), |
398 $link, text, hasText, $mceCaret; |
405 $link, text, hasText; |
399 |
406 |
400 var parser = document.createElement( 'a' ); |
407 var parser = document.createElement( 'a' ); |
401 parser.href = attrs.href; |
408 parser.href = attrs.href; |
402 |
409 |
403 if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line |
410 if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line |
408 editor.execCommand( 'unlink' ); |
415 editor.execCommand( 'unlink' ); |
409 wpLink.close(); |
416 wpLink.close(); |
410 return; |
417 return; |
411 } |
418 } |
412 |
419 |
413 $link = editor.$( getLink() ); |
420 $link = getLink(); |
414 |
421 |
415 editor.undoManager.transact( function() { |
422 editor.undoManager.transact( function() { |
416 if ( ! $link.length ) { |
423 if ( ! $link.length ) { |
417 editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder', 'data-wp-temp-link': 1 } ); |
424 editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder', 'data-wp-temp-link': 1 } ); |
418 $link = editor.$( 'a[data-wp-temp-link="1"]' ).removeAttr( 'data-wp-temp-link' ); |
425 $link = editor.$( 'a[data-wp-temp-link="1"]' ).removeAttr( 'data-wp-temp-link' ); |
431 $link.text( attrs.href ); |
438 $link.text( attrs.href ); |
432 } |
439 } |
433 } |
440 } |
434 |
441 |
435 attrs['data-wplink-edit'] = null; |
442 attrs['data-wplink-edit'] = null; |
436 attrs['data-mce-href'] = null; // attrs.href |
443 attrs['data-mce-href'] = attrs.href; |
437 $link.attr( attrs ); |
444 $link.attr( attrs ); |
438 } |
445 } |
439 } ); |
446 } ); |
440 |
447 |
441 wpLink.close( 'noReset' ); |
448 wpLink.close( 'noReset' ); |
442 editor.focus(); |
449 editor.focus(); |
443 |
450 |
444 if ( $link.length ) { |
451 if ( $link.length ) { |
445 $mceCaret = $link.parent( '#_mce_caret' ); |
|
446 |
|
447 if ( $mceCaret.length ) { |
|
448 $mceCaret.before( $link.removeAttr( 'data-mce-bogus' ) ); |
|
449 } |
|
450 |
|
451 editor.selection.select( $link[0] ); |
452 editor.selection.select( $link[0] ); |
452 editor.selection.collapse(); |
|
453 |
453 |
454 if ( editor.plugins.wplink ) { |
454 if ( editor.plugins.wplink ) { |
455 editor.plugins.wplink.checkLink( $link[0] ); |
455 editor.plugins.wplink.checkLink( $link[0] ); |
456 } |
456 } |
457 } |
457 } |
462 wp.a11y.speak( wpLinkL10n.linkInserted ); |
462 wp.a11y.speak( wpLinkL10n.linkInserted ); |
463 }, |
463 }, |
464 |
464 |
465 updateFields: function( e, li ) { |
465 updateFields: function( e, li ) { |
466 inputs.url.val( li.children( '.item-permalink' ).val() ); |
466 inputs.url.val( li.children( '.item-permalink' ).val() ); |
|
467 |
|
468 if ( inputs.wrap.hasClass( 'has-text-field' ) && ! inputs.text.val() ) { |
|
469 inputs.text.val( li.children( '.item-title' ).text() ); |
|
470 } |
467 }, |
471 }, |
468 |
472 |
469 getUrlFromSelection: function( selection ) { |
473 getUrlFromSelection: function( selection ) { |
470 if ( ! selection ) { |
474 if ( ! selection ) { |
471 if ( this.isMCE() ) { |
475 if ( this.isMCE() ) { |
557 event.preventDefault(); |
561 event.preventDefault(); |
558 } |
562 } |
559 } |
563 } |
560 |
564 |
561 // Up Arrow and Down Arrow keys. |
565 // Up Arrow and Down Arrow keys. |
562 if ( 38 !== event.keyCode && 40 !== event.keyCode ) { |
566 if ( event.shiftKey || ( 38 !== event.keyCode && 40 !== event.keyCode ) ) { |
563 return; |
567 return; |
564 } |
568 } |
565 |
569 |
566 if ( document.activeElement && |
570 if ( document.activeElement && |
567 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) { |
571 ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) { |