wp/wp-includes/js/wplink.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    34 			inputs.dialog = $( '#wp-link' );
    34 			inputs.dialog = $( '#wp-link' );
    35 			inputs.backdrop = $( '#wp-link-backdrop' );
    35 			inputs.backdrop = $( '#wp-link-backdrop' );
    36 			inputs.submit = $( '#wp-link-submit' );
    36 			inputs.submit = $( '#wp-link-submit' );
    37 			inputs.close = $( '#wp-link-close' );
    37 			inputs.close = $( '#wp-link-close' );
    38 
    38 
    39 			// Input
    39 			// Input.
    40 			inputs.text = $( '#wp-link-text' );
    40 			inputs.text = $( '#wp-link-text' );
    41 			inputs.url = $( '#wp-link-url' );
    41 			inputs.url = $( '#wp-link-url' );
    42 			inputs.nonce = $( '#_ajax_linking_nonce' );
    42 			inputs.nonce = $( '#_ajax_linking_nonce' );
    43 			inputs.openInNewTab = $( '#wp-link-target' );
    43 			inputs.openInNewTab = $( '#wp-link-target' );
    44 			inputs.search = $( '#wp-link-search' );
    44 			inputs.search = $( '#wp-link-search' );
    45 
    45 
    46 			// Build Rivers
    46 			// Build rivers.
    47 			rivers.search = new River( $( '#search-results' ) );
    47 			rivers.search = new River( $( '#search-results' ) );
    48 			rivers.recent = new River( $( '#most-recent-results' ) );
    48 			rivers.recent = new River( $( '#most-recent-results' ) );
    49 			rivers.elements = inputs.dialog.find( '.query-results' );
    49 			rivers.elements = inputs.dialog.find( '.query-results' );
    50 
    50 
    51 			// Get search notice text
    51 			// Get search notice text.
    52 			inputs.queryNotice = $( '#query-notice-message' );
    52 			inputs.queryNotice = $( '#query-notice-message' );
    53 			inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
    53 			inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
    54 			inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
    54 			inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
    55 
    55 
    56 			// Bind event handlers
    56 			// Bind event handlers.
    57 			inputs.dialog.keydown( wpLink.keydown );
    57 			inputs.dialog.keydown( wpLink.keydown );
    58 			inputs.dialog.keyup( wpLink.keyup );
    58 			inputs.dialog.keyup( wpLink.keyup );
    59 			inputs.submit.click( function( event ) {
    59 			inputs.submit.click( function( event ) {
    60 				event.preventDefault();
    60 				event.preventDefault();
    61 				wpLink.update();
    61 				wpLink.update();
    66 				wpLink.close();
    66 				wpLink.close();
    67 			});
    67 			});
    68 
    68 
    69 			rivers.elements.on( 'river-select', wpLink.updateFields );
    69 			rivers.elements.on( 'river-select', wpLink.updateFields );
    70 
    70 
    71 			// Display 'hint' message when search field or 'query-results' box are focused
    71 			// Display 'hint' message when search field or 'query-results' box are focused.
    72 			inputs.search.on( 'focus.wplink', function() {
    72 			inputs.search.on( 'focus.wplink', function() {
    73 				inputs.queryNoticeTextDefault.hide();
    73 				inputs.queryNoticeTextDefault.hide();
    74 				inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
    74 				inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
    75 			} ).on( 'blur.wplink', function() {
    75 			} ).on( 'blur.wplink', function() {
    76 				inputs.queryNoticeTextDefault.show();
    76 				inputs.queryNoticeTextDefault.show();
    89 			} );
    89 			} );
    90 
    90 
    91 			inputs.url.on( 'blur', wpLink.correctURL );
    91 			inputs.url.on( 'blur', wpLink.correctURL );
    92 		},
    92 		},
    93 
    93 
    94 		// If URL wasn't corrected last time and doesn't start with http:, https:, ? # or /, prepend http://
    94 		// If URL wasn't corrected last time and doesn't start with http:, https:, ? # or /, prepend http://.
    95 		correctURL: function () {
    95 		correctURL: function () {
    96 			var url = $.trim( inputs.url.val() );
    96 			var url = $.trim( inputs.url.val() );
    97 
    97 
    98 			if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
    98 			if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
    99 				inputs.url.val( 'http://' + url );
    99 				inputs.url.val( 'http://' + url );
   152 		},
   152 		},
   153 
   153 
   154 		refresh: function( url, text ) {
   154 		refresh: function( url, text ) {
   155 			var linkText = '';
   155 			var linkText = '';
   156 
   156 
   157 			// Refresh rivers (clear links, check visibility)
   157 			// Refresh rivers (clear links, check visibility).
   158 			rivers.search.refresh();
   158 			rivers.search.refresh();
   159 			rivers.recent.refresh();
   159 			rivers.recent.refresh();
   160 
   160 
   161 			if ( wpLink.isMCE() ) {
   161 			if ( wpLink.isMCE() ) {
   162 				wpLink.mceRefresh( url, text );
   162 				wpLink.mceRefresh( url, text );
   163 			} else {
   163 			} else {
   164 				// For the Text editor the "Link text" field is always shown
   164 				// For the Text editor the "Link text" field is always shown.
   165 				if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
   165 				if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
   166 					inputs.wrap.addClass( 'has-text-field' );
   166 					inputs.wrap.addClass( 'has-text-field' );
   167 				}
   167 				}
   168 
   168 
   169 				if ( document.selection ) {
   169 				if ( document.selection ) {
   170 					// Old IE
   170 					// Old IE.
   171 					linkText = document.selection.createRange().text || text || '';
   171 					linkText = document.selection.createRange().text || text || '';
   172 				} else if ( typeof this.textarea.selectionStart !== 'undefined' &&
   172 				} else if ( typeof this.textarea.selectionStart !== 'undefined' &&
   173 					( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
   173 					( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
   174 					// W3C
   174 					// W3C.
   175 					text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
   175 					text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
   176 				}
   176 				}
   177 
   177 
   178 				inputs.text.val( text );
   178 				inputs.text.val( text );
   179 				wpLink.setDefaultValues();
   179 				wpLink.setDefaultValues();
   180 			}
   180 			}
   181 
   181 
   182 			if ( isTouch ) {
   182 			if ( isTouch ) {
   183 				// Close the onscreen keyboard
   183 				// Close the onscreen keyboard.
   184 				inputs.url.focus().blur();
   184 				inputs.url.focus().blur();
   185 			} else {
   185 			} else {
   186 				// Focus the URL field and highlight its contents.
   186 				/*
   187 				// If this is moved above the selection changes,
   187 				 * Focus the URL field and highlight its contents.
   188 				// IE will show a flashing cursor over the dialog.
   188 				 * If this is moved above the selection changes,
       
   189 				 * IE will show a flashing cursor over the dialog.
       
   190 				 */
   189 				window.setTimeout( function() {
   191 				window.setTimeout( function() {
   190 					inputs.url[0].select();
   192 					inputs.url[0].select();
   191 					inputs.url.focus();
   193 					inputs.url.focus();
   192 				} );
   194 				} );
   193 			}
   195 			}
   201 		},
   203 		},
   202 
   204 
   203 		hasSelectedText: function( linkNode ) {
   205 		hasSelectedText: function( linkNode ) {
   204 			var node, nodes, i, html = editor.selection.getContent();
   206 			var node, nodes, i, html = editor.selection.getContent();
   205 
   207 
   206 			// Partial html and not a fully selected anchor element
   208 			// Partial html and not a fully selected anchor element.
   207 			if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
   209 			if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
   208 				return false;
   210 				return false;
   209 			}
   211 			}
   210 
   212 
   211 			if ( linkNode.length ) {
   213 			if ( linkNode.length ) {
   257 					inputs.search.val( searchStr );
   259 					inputs.search.val( searchStr );
   258 				} else {
   260 				} else {
   259 					inputs.search.val( '' );
   261 					inputs.search.val( '' );
   260 				}
   262 				}
   261 
   263 
   262 				// Always reset the search
   264 				// Always reset the search.
   263 				window.setTimeout( function() {
   265 				window.setTimeout( function() {
   264 					wpLink.searchInternalLinks();
   266 					wpLink.searchInternalLinks();
   265 				} );
   267 				} );
   266 			} else {
   268 			} else {
   267 				linkText = editor.selection.getContent({ format: 'text' }) || text || '';
   269 				linkText = editor.selection.getContent({ format: 'text' }) || text || '';
   356 				return;
   358 				return;
   357 			}
   359 			}
   358 
   360 
   359 			html = wpLink.buildHtml(attrs);
   361 			html = wpLink.buildHtml(attrs);
   360 
   362 
   361 			// Insert HTML
   363 			// Insert HTML.
   362 			if ( document.selection && wpLink.range ) {
   364 			if ( document.selection && wpLink.range ) {
   363 				// IE
   365 				// IE.
   364 				// Note: If no text is selected, IE will not place the cursor
   366 				// Note: If no text is selected, IE will not place the cursor
   365 				//       inside the closing tag.
   367 				// inside the closing tag.
   366 				textarea.focus();
   368 				textarea.focus();
   367 				wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
   369 				wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
   368 				wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
   370 				wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
   369 				wpLink.range.select();
   371 				wpLink.range.select();
   370 
   372 
   371 				wpLink.range = null;
   373 				wpLink.range = null;
   372 			} else if ( typeof textarea.selectionStart !== 'undefined' ) {
   374 			} else if ( typeof textarea.selectionStart !== 'undefined' ) {
   373 				// W3C
   375 				// W3C.
   374 				begin = textarea.selectionStart;
   376 				begin = textarea.selectionStart;
   375 				end = textarea.selectionEnd;
   377 				end = textarea.selectionEnd;
   376 				selection = text || textarea.value.substring( begin, end );
   378 				selection = text || textarea.value.substring( begin, end );
   377 				html = html + selection + '</a>';
   379 				html = html + selection + '</a>';
   378 				cursor = begin + html.length;
   380 				cursor = begin + html.length;
   386 					textarea.value.substring( 0, begin ) +
   388 					textarea.value.substring( 0, begin ) +
   387 					html +
   389 					html +
   388 					textarea.value.substring( end, textarea.value.length )
   390 					textarea.value.substring( end, textarea.value.length )
   389 				);
   391 				);
   390 
   392 
   391 				// Update cursor position
   393 				// Update cursor position.
   392 				textarea.selectionStart = textarea.selectionEnd = cursor;
   394 				textarea.selectionStart = textarea.selectionEnd = cursor;
   393 			}
   395 			}
   394 
   396 
   395 			wpLink.close();
   397 			wpLink.close();
   396 			textarea.focus();
   398 			textarea.focus();
   482 			}
   484 			}
   483 
   485 
   484 			selection = $.trim( selection );
   486 			selection = $.trim( selection );
   485 
   487 
   486 			if ( selection && emailRegexp.test( selection ) ) {
   488 			if ( selection && emailRegexp.test( selection ) ) {
   487 				// Selection is email address
   489 				// Selection is email address.
   488 				return 'mailto:' + selection;
   490 				return 'mailto:' + selection;
   489 			} else if ( selection && urlRegexp.test( selection ) ) {
   491 			} else if ( selection && urlRegexp.test( selection ) ) {
   490 				// Selection is URL
   492 				// Selection is URL.
   491 				return selection.replace( /&amp;|&#0?38;/gi, '&' );
   493 				return selection.replace( /&amp;|&#0?38;/gi, '&' );
   492 			}
   494 			}
   493 
   495 
   494 			return '';
   496 			return '';
   495 		},
   497 		},
   505 			inputs.submit.val( wpLinkL10n.save );
   507 			inputs.submit.val( wpLinkL10n.save );
   506 		},
   508 		},
   507 
   509 
   508 		searchInternalLinks: function() {
   510 		searchInternalLinks: function() {
   509 			var waiting,
   511 			var waiting,
   510 				search = inputs.search.val() || '';
   512 				search = inputs.search.val() || '',
   511 
   513 				minInputLength = parseInt( wpLinkL10n.minInputLength, 10 ) || 3;
   512 			if ( search.length > 2 ) {
   514 
       
   515 			if ( search.length >= minInputLength ) {
   513 				rivers.recent.hide();
   516 				rivers.recent.hide();
   514 				rivers.search.show();
   517 				rivers.search.show();
   515 
   518 
   516 				// Don't search if the keypress didn't change the title.
   519 				// Don't search if the keypress didn't change the title.
   517 				if ( wpLink.lastSearch == search )
   520 				if ( wpLink.lastSearch == search )
   550 			// Tab key.
   553 			// Tab key.
   551 			} else if ( 9 === event.keyCode ) {
   554 			} else if ( 9 === event.keyCode ) {
   552 				id = event.target.id;
   555 				id = event.target.id;
   553 
   556 
   554 				// wp-link-submit must always be the last focusable element in the dialog.
   557 				// wp-link-submit must always be the last focusable element in the dialog.
   555 				// following focusable elements will be skipped on keyboard navigation.
   558 				// Following focusable elements will be skipped on keyboard navigation.
   556 				if ( id === 'wp-link-submit' && ! event.shiftKey ) {
   559 				if ( id === 'wp-link-submit' && ! event.shiftKey ) {
   557 					inputs.close.focus();
   560 					inputs.close.focus();
   558 					event.preventDefault();
   561 					event.preventDefault();
   559 				} else if ( id === 'wp-link-close' && event.shiftKey ) {
   562 				} else if ( id === 'wp-link-close' && event.shiftKey ) {
   560 					inputs.submit.focus();
   563 					inputs.submit.focus();
   653 			if ( li.hasClass( 'unselectable' ) || li == this.selected )
   656 			if ( li.hasClass( 'unselectable' ) || li == this.selected )
   654 				return;
   657 				return;
   655 
   658 
   656 			this.deselect();
   659 			this.deselect();
   657 			this.selected = li.addClass( 'selected' );
   660 			this.selected = li.addClass( 'selected' );
   658 			// Make sure the element is visible
   661 			// Make sure the element is visible.
   659 			liHeight = li.outerHeight();
   662 			liHeight = li.outerHeight();
   660 			elHeight = this.element.height();
   663 			elHeight = this.element.height();
   661 			liTop = li.position().top;
   664 			liTop = li.position().top;
   662 			elTop = this.element.scrollTop();
   665 			elTop = this.element.scrollTop();
   663 
   666 
   664 			if ( liTop < 0 ) // Make first visible element
   667 			if ( liTop < 0 ) // Make first visible element.
   665 				this.element.scrollTop( elTop + liTop );
   668 				this.element.scrollTop( elTop + liTop );
   666 			else if ( liTop + liHeight > elHeight ) // Make last visible element
   669 			else if ( liTop + liHeight > elHeight ) // Make last visible element.
   667 				this.element.scrollTop( elTop + liTop - elHeight + liHeight );
   670 				this.element.scrollTop( elTop + liTop - elHeight + liHeight );
   668 
   671 
   669 			// Trigger the river-select event
   672 			// Trigger the river-select event.
   670 			this.element.trigger( 'river-select', [ li, event, this ] );
   673 			this.element.trigger( 'river-select', [ li, event, this ] );
   671 		},
   674 		},
   672 		deselect: function() {
   675 		deselect: function() {
   673 			if ( this.selected )
   676 			if ( this.selected )
   674 				this.selected.removeClass( 'selected' );
   677 				this.selected.removeClass( 'selected' );