X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/e08b42e8ad054ec67522d7ac1aaae5dc68cb3d01..refs/tags/wordpress-4.1-scripts:/wp-includes/js/wplink.js diff --git a/wp-includes/js/wplink.js b/wp-includes/js/wplink.js index 7756c0fd..acb8ccc3 100644 --- a/wp-includes/js/wplink.js +++ b/wp-includes/js/wplink.js @@ -1,8 +1,11 @@ -/* global ajaxurl, tinymce, wpLinkL10n, tinyMCEPopup, setUserSetting, wpActiveEditor */ +/* global ajaxurl, tinymce, wpLinkL10n, setUserSetting, wpActiveEditor */ var wpLink; -(function($){ - var inputs = {}, rivers = {}, ed, River, Query; +( function( $ ) { + var editor, searchTimer, River, Query, + inputs = {}, + rivers = {}, + isTouch = ( 'ontouchend' in document ); wpLink = { timeToTriggerRiver: 150, @@ -12,109 +15,149 @@ var wpLink; lastSearch: '', textarea: '', - init : function() { - inputs.dialog = $('#wp-link'); - inputs.submit = $('#wp-link-submit'); + init: function() { + inputs.wrap = $('#wp-link-wrap'); + inputs.dialog = $( '#wp-link' ); + inputs.backdrop = $( '#wp-link-backdrop' ); + inputs.submit = $( '#wp-link-submit' ); + inputs.close = $( '#wp-link-close' ); // URL - inputs.url = $('#url-field'); - inputs.nonce = $('#_ajax_linking_nonce'); + inputs.url = $( '#url-field' ); + inputs.nonce = $( '#_ajax_linking_nonce' ); // Secondary options - inputs.title = $('#link-title-field'); + inputs.title = $( '#link-title-field' ); // Advanced Options - inputs.openInNewTab = $('#link-target-checkbox'); - inputs.search = $('#search-field'); + inputs.openInNewTab = $( '#link-target-checkbox' ); + inputs.search = $( '#search-field' ); // Build Rivers - rivers.search = new River( $('#search-results') ); - rivers.recent = new River( $('#most-recent-results') ); - rivers.elements = $('.query-results', inputs.dialog); + rivers.search = new River( $( '#search-results' ) ); + rivers.recent = new River( $( '#most-recent-results' ) ); + rivers.elements = inputs.dialog.find( '.query-results' ); + + // Get search notice text + inputs.queryNotice = $( '#query-notice-message' ); + inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' ); + inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' ); // Bind event handlers inputs.dialog.keydown( wpLink.keydown ); inputs.dialog.keyup( wpLink.keyup ); - inputs.submit.click( function(e){ - e.preventDefault(); + inputs.submit.click( function( event ) { + event.preventDefault(); wpLink.update(); }); - $('#wp-link-cancel').click( function(e){ - e.preventDefault(); + inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel a' ).click( function( event ) { + event.preventDefault(); wpLink.close(); }); - $('#internal-toggle').click( wpLink.toggleInternalLinking ); - rivers.elements.bind('river-select', wpLink.updateFields ); + $( '#wp-link-search-toggle' ).on( 'click', wpLink.toggleInternalLinking ); + + rivers.elements.on( 'river-select', wpLink.updateFields ); + + // Display 'hint' message when search field or 'query-results' box are focused + inputs.search.on( 'focus.wplink', function() { + inputs.queryNoticeTextDefault.hide(); + inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show(); + } ).on( 'blur.wplink', function() { + inputs.queryNoticeTextDefault.show(); + inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide(); + } ); - inputs.search.keyup( wpLink.searchInternalLinks ); + inputs.search.keyup( function() { + var self = this; - inputs.dialog.bind('wpdialogrefresh', wpLink.refresh); - inputs.dialog.bind('wpdialogbeforeopen', wpLink.beforeOpen); - inputs.dialog.bind('wpdialogclose', wpLink.onClose); + window.clearTimeout( searchTimer ); + searchTimer = window.setTimeout( function() { + wpLink.searchInternalLinks.call( self ); + }, 500 ); + }); }, - beforeOpen : function() { + open: function( editorId ) { + var ed; + + $( document.body ).addClass( 'modal-open' ); + wpLink.range = null; - if ( ! wpLink.isMCE() && document.selection ) { - wpLink.textarea.focus(); - wpLink.range = document.selection.createRange(); + if ( editorId ) { + window.wpActiveEditor = editorId; } - }, - open : function() { - if ( !wpActiveEditor ) + if ( ! window.wpActiveEditor ) { return; + } - this.textarea = $('#'+wpActiveEditor).get(0); + this.textarea = $( '#' + window.wpActiveEditor ).get( 0 ); - // Initialize the dialog if necessary (html mode). - if ( ! inputs.dialog.data('wpdialog') ) { - inputs.dialog.wpdialog({ - title: wpLinkL10n.title, - width: 480, - height: 'auto', - modal: true, - dialogClass: 'wp-dialog' - }); + if ( typeof tinymce !== 'undefined' ) { + ed = tinymce.get( wpActiveEditor ); + + if ( ed && ! ed.isHidden() ) { + editor = ed; + } else { + editor = null; + } + + if ( editor && tinymce.isIE ) { + editor.windowManager.bookmark = editor.selection.getBookmark(); + } + } + + if ( ! wpLink.isMCE() && document.selection ) { + this.textarea.focus(); + this.range = document.selection.createRange(); } - inputs.dialog.wpdialog('open'); + inputs.wrap.show(); + inputs.backdrop.show(); + + wpLink.refresh(); + $( document ).trigger( 'wplink-open', inputs.wrap ); }, - isMCE : function() { - return tinyMCEPopup && ( ed = tinyMCEPopup.editor ) && ! ed.isHidden(); + isMCE: function() { + return editor && ! editor.isHidden(); }, - refresh : function() { + refresh: function() { // Refresh rivers (clear links, check visibility) rivers.search.refresh(); rivers.recent.refresh(); - if ( wpLink.isMCE() ) + if ( wpLink.isMCE() ) { wpLink.mceRefresh(); - else + } else { wpLink.setDefaultValues(); + } + + if ( isTouch ) { + // Close the onscreen keyboard + inputs.url.focus().blur(); + } else { + // Focus the URL field and highlight its contents. + // If this is moved above the selection changes, + // IE will show a flashing cursor over the dialog. + inputs.url.focus()[0].select(); + } - // Focus the URL field and highlight its contents. - // If this is moved above the selection changes, - // IE will show a flashing cursor over the dialog. - inputs.url.focus()[0].select(); // Load the most recent results if this is the first time opening the panel. - if ( ! rivers.recent.ul.children().length ) + if ( ! rivers.recent.ul.children().length ) { rivers.recent.ajax(); + } }, - mceRefresh : function() { + mceRefresh: function() { var e; - ed = tinyMCEPopup.editor; - - tinyMCEPopup.restoreSelection(); // If link exists, select proper values. - if ( e = ed.dom.getParent(ed.selection.getNode(), 'A') ) { + if ( e = editor.dom.getParent( editor.selection.getNode(), 'A' ) ) { // Set URL and description. - inputs.url.val( ed.dom.getAttrib(e, 'href') ); - inputs.title.val( ed.dom.getAttrib(e, 'title') ); + inputs.url.val( editor.dom.getAttrib( e, 'href' ) ); + inputs.title.val( editor.dom.getAttrib( e, 'title' ) ); // Set open in new tab. - inputs.openInNewTab.prop('checked', ( '_blank' == ed.dom.getAttrib( e, 'target' ) ) ); + inputs.openInNewTab.prop( 'checked', ( '_blank' === editor.dom.getAttrib( e, 'target' ) ) ); // Update save prompt. inputs.submit.val( wpLinkL10n.update ); @@ -124,40 +167,42 @@ var wpLink; } }, - close : function() { - if ( wpLink.isMCE() ) - tinyMCEPopup.close(); - else - inputs.dialog.wpdialog('close'); - }, + close: function() { + $( document.body ).removeClass( 'modal-open' ); - onClose: function() { if ( ! wpLink.isMCE() ) { wpLink.textarea.focus(); + if ( wpLink.range ) { wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); wpLink.range.select(); } + } else { + editor.focus(); } + + inputs.backdrop.hide(); + inputs.wrap.hide(); + $( document ).trigger( 'wplink-close', inputs.wrap ); }, - getAttrs : function() { + getAttrs: function() { return { - href : inputs.url.val(), - title : inputs.title.val(), - target : inputs.openInNewTab.prop('checked') ? '_blank' : '' + href: inputs.url.val(), + title: inputs.title.val(), + target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : '' }; }, - update : function() { + update: function() { if ( wpLink.isMCE() ) wpLink.mceUpdate(); else wpLink.htmlUpdate(); }, - htmlUpdate : function() { - var attrs, html, begin, end, cursor, selection, + htmlUpdate: function() { + var attrs, html, begin, end, cursor, title, selection, textarea = wpLink.textarea; if ( ! textarea ) @@ -172,10 +217,14 @@ var wpLink; // Build HTML html = '/g, '>' ).replace( /"/g, '"' ); + html += ' title="' + title + '"'; + } + + if ( attrs.target ) { html += ' target="' + attrs.target + '"'; + } html += '>'; @@ -213,78 +262,65 @@ var wpLink; textarea.focus(); }, - mceUpdate : function() { - var ed = tinyMCEPopup.editor, - attrs = wpLink.getAttrs(), - e, b; + mceUpdate: function() { + var link, + attrs = wpLink.getAttrs(); + + wpLink.close(); + editor.focus(); + + if ( tinymce.isIE ) { + editor.selection.moveToBookmark( editor.windowManager.bookmark ); + } - tinyMCEPopup.restoreSelection(); - e = ed.dom.getParent(ed.selection.getNode(), 'A'); + link = editor.dom.getParent( editor.selection.getNode(), 'a[href]' ); // If the values are empty, unlink and return if ( ! attrs.href || attrs.href == 'http://' ) { - if ( e ) { - b = ed.selection.getBookmark(); - ed.dom.remove(e, 1); - ed.selection.moveToBookmark(b); - tinyMCEPopup.execCommand('mceEndUndoLevel'); - wpLink.close(); - } + editor.execCommand( 'unlink' ); return; } - if (e == null) { - ed.getDoc().execCommand('unlink', false, null); - tinyMCEPopup.execCommand('mceInsertLink', false, '#mce_temp_url#', {skip_undo : 1}); - - tinymce.each(ed.dom.select('a'), function(n) { - if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') { - e = n; - ed.dom.setAttribs(e, attrs); - } - }); - - // Sometimes WebKit lets a user create a link where - // they shouldn't be able to. In this case, CreateLink - // injects "#mce_temp_url#" into their content. Fix it. - if ( tinymce.isWebKit && $(e).text() == '#mce_temp_url#' ) { - ed.dom.remove(e); - e = null; - } + if ( link ) { + editor.dom.setAttribs( link, attrs ); } else { - ed.dom.setAttribs(e, attrs); - } - - // Move the caret if selection was not an image. - if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) { - ed.selection.select(e); - ed.selection.collapse(0); - tinyMCEPopup.storeSelection(); + editor.execCommand( 'mceInsertLink', false, attrs ); } - ed.execCommand('mceEndUndoLevel'); - wpLink.close(); - ed.focus(); + // Move the cursor to the end of the selection + editor.selection.collapse(); }, - updateFields : function( e, li, originalEvent ) { - inputs.url.val( li.children('.item-permalink').val() ); - inputs.title.val( li.hasClass('no-title') ? '' : li.children('.item-title').text() ); - if ( originalEvent && originalEvent.type == 'click' ) - inputs.url.focus(); + updateFields: function( e, li ) { + inputs.url.val( li.children( '.item-permalink' ).val() ); + inputs.title.val( li.hasClass( 'no-title' ) ? '' : li.children( '.item-title' ).text() ); }, - setDefaultValues : function() { - // Set URL and description to defaults. - // Leave the new tab setting as-is. - inputs.url.val('http://'); - inputs.title.val(''); + + setDefaultValues: function() { + var selection = editor && editor.selection.getContent(), + emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, + urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i; + + if ( selection && emailRegexp.test( selection ) ) { + // Selection is email address + inputs.url.val( 'mailto:' + selection ); + } else if ( selection && urlRegexp.test( selection ) ) { + // Selection is URL + inputs.url.val( selection.replace( /&|�?38;/gi, '&' ) ); + } else { + // Set URL to default. + inputs.url.val( 'http://' ); + } + + // Set description to default. + inputs.title.val( '' ); // Update save prompt. inputs.submit.val( wpLinkL10n.save ); }, - searchInternalLinks : function() { - var t = $(this), waiting, + searchInternalLinks: function() { + var t = $( this ), waiting, search = t.val(); if ( search.length > 2 ) { @@ -299,30 +335,56 @@ var wpLink; waiting = t.parent().find('.spinner').show(); rivers.search.change( search ); - rivers.search.ajax( function(){ waiting.hide(); }); + rivers.search.ajax( function() { + waiting.hide(); + }); } else { rivers.search.hide(); rivers.recent.show(); } }, - next : function() { + next: function() { rivers.search.next(); rivers.recent.next(); }, - prev : function() { + + prev: function() { rivers.search.prev(); rivers.recent.prev(); }, - keydown : function( event ) { - var fn, key = $.ui.keyCode; + keydown: function( event ) { + var fn, id, + key = $.ui.keyCode; + + if ( key.ESCAPE === event.keyCode ) { + wpLink.close(); + event.stopImmediatePropagation(); + } else if ( key.TAB === event.keyCode ) { + id = event.target.id; + + // wp-link-submit must always be the last focusable element in the dialog. + // following focusable elements will be skipped on keyboard navigation. + if ( id === 'wp-link-submit' && ! event.shiftKey ) { + inputs.close.focus(); + event.preventDefault(); + } else if ( id === 'wp-link-close' && event.shiftKey ) { + inputs.submit.focus(); + event.preventDefault(); + } + } + + if ( event.keyCode !== key.UP && event.keyCode !== key.DOWN ) { + return; + } - if ( event.which !== key.UP && event.which !== key.DOWN ) { + if ( document.activeElement && + ( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) { return; } - fn = event.which === key.UP ? 'prev' : 'next'; + fn = event.keyCode === key.UP ? 'prev' : 'next'; clearInterval( wpLink.keyInterval ); wpLink[ fn ](); wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity ); @@ -332,20 +394,13 @@ var wpLink; keyup: function( event ) { var key = $.ui.keyCode; - if ( event.which === key.ESCAPE ) { - event.stopImmediatePropagation(); - if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wplink', cb: wpLink.close }] ) ) - wpLink.close(); - return false; - } - if ( event.which === key.UP || event.which === key.DOWN ) { clearInterval( wpLink.keyInterval ); event.preventDefault(); } }, - delayedCallback : function( func, delay ) { + delayedCallback: function( func, delay ) { var timeoutTriggered, funcTriggered, funcArgs, funcContext; if ( ! delay ) @@ -356,7 +411,7 @@ var wpLink; return func.apply( funcContext, funcArgs ); // Otherwise, wait. timeoutTriggered = true; - }, delay); + }, delay ); return function() { if ( timeoutTriggered ) @@ -368,32 +423,12 @@ var wpLink; }; }, - toggleInternalLinking : function( event ) { - var panel = $('#search-panel'), - widget = inputs.dialog.wpdialog('widget'), - // We're about to toggle visibility; it's currently the opposite - visible = !panel.is(':visible'), - win = $(window); - - $(this).toggleClass('toggle-arrow-active', visible); - - inputs.dialog.height('auto'); - panel.slideToggle( 300, function() { - setUserSetting('wplink', visible ? '1' : '0'); - inputs[ visible ? 'search' : 'url' ].focus(); - - // Move the box if the box is now expanded, was opened in a collapsed state, - // and if it needs to be moved. (Judged by bottom not being positive or - // bottom being smaller than top.) - var scroll = win.scrollTop(), - top = widget.offset().top, - bottom = top + widget.outerHeight(), - diff = bottom - win.height(); - - if ( diff > scroll ) { - widget.animate({'top': diff < top ? top - diff : scroll }, 200); - } - }); + toggleInternalLinking: function( event ) { + var visible = inputs.wrap.hasClass( 'search-panel-visible' ); + + inputs.wrap.toggleClass( 'search-panel-visible', ! visible ); + setUserSetting( 'wplink', visible ? '0' : '1' ); + inputs[ ! visible ? 'search' : 'url' ].focus(); event.preventDefault(); } }; @@ -401,20 +436,25 @@ var wpLink; River = function( element, search ) { var self = this; this.element = element; - this.ul = element.children('ul'); + this.ul = element.children( 'ul' ); + this.contentHeight = element.children( '#link-selector-height' ); this.waiting = element.find('.river-waiting'); this.change( search ); this.refresh(); - element.scroll( function(){ self.maybeLoad(); }); - element.delegate('li', 'click', function(e){ self.select( $(this), e ); }); + $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() { + self.maybeLoad(); + }); + element.on( 'click', 'li', function( event ) { + self.select( $( this ), event ); + }); }; $.extend( River.prototype, { refresh: function() { this.deselect(); - this.visible = this.element.is(':visible'); + this.visible = this.element.is( ':visible' ); }, show: function() { if ( ! this.visible ) { @@ -431,11 +471,11 @@ var wpLink; select: function( li, event ) { var liHeight, elHeight, liTop, elTop; - if ( li.hasClass('unselectable') || li == this.selected ) + if ( li.hasClass( 'unselectable' ) || li == this.selected ) return; this.deselect(); - this.selected = li.addClass('selected'); + this.selected = li.addClass( 'selected' ); // Make sure the element is visible liHeight = li.outerHeight(); elHeight = this.element.height(); @@ -448,11 +488,11 @@ var wpLink; this.element.scrollTop( elTop + liTop - elHeight + liHeight ); // Trigger the river-select event - this.element.trigger('river-select', [ li, event, this ]); + this.element.trigger( 'river-select', [ li, event, this ] ); }, deselect: function() { if ( this.selected ) - this.selected.removeClass('selected'); + this.selected.removeClass( 'selected' ); this.selected = false; }, prev: function() { @@ -461,7 +501,7 @@ var wpLink; var to; if ( this.selected ) { - to = this.selected.prev('li'); + to = this.selected.prev( 'li' ); if ( to.length ) this.select( to ); } @@ -470,7 +510,7 @@ var wpLink; if ( ! this.visible ) return; - var to = this.selected ? this.selected.next('li') : $('li:not(.unselectable):first', this.element); + var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element ); if ( to.length ) this.select( to ); }, @@ -491,15 +531,15 @@ var wpLink; this._search = search; this.query = new Query( search ); - this.element.scrollTop(0); + this.element.scrollTop( 0 ); }, process: function( results, params ) { var list = '', alt = true, classes = '', firstPage = params.page == 1; - if ( !results ) { + if ( ! results ) { if ( firstPage ) { - list += '
  • ' + + list += '
  • ' + wpLinkL10n.noMatchesFound + '
  • '; } } else { @@ -522,20 +562,22 @@ var wpLink; el = this.element, bottom = el.scrollTop() + el.height(); - if ( ! this.query.ready() || bottom < this.ul.height() - wpLink.riverBottomThreshold ) + if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold ) return; setTimeout(function() { var newTop = el.scrollTop(), newBottom = newTop + el.height(); - if ( ! self.query.ready() || newBottom < self.ul.height() - wpLink.riverBottomThreshold ) + if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold ) return; self.waiting.show(); el.scrollTop( newTop + self.waiting.outerHeight() ); - self.ajax( function() { self.waiting.hide(); }); + self.ajax( function() { + self.waiting.hide(); + }); }, wpLink.timeToTriggerRiver ); } }); @@ -549,7 +591,7 @@ var wpLink; $.extend( Query.prototype, { ready: function() { - return !( this.querying || this.allLoaded ); + return ! ( this.querying || this.allLoaded ); }, ajax: function( callback ) { var self = this, @@ -564,14 +606,14 @@ var wpLink; this.querying = true; - $.post( ajaxurl, query, function(r) { + $.post( ajaxurl, query, function( r ) { self.page++; self.querying = false; - self.allLoaded = !r; + self.allLoaded = ! r; callback( r, query ); }, 'json' ); } }); - $(document).ready( wpLink.init ); -})(jQuery); + $( document ).ready( wpLink.init ); +})( jQuery );