X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..41578db67d72562346e4dbb2a14889b23d522813:/wp-includes/js/wplink.js diff --git a/wp-includes/js/wplink.js b/wp-includes/js/wplink.js index c52d2a19..7d1c6774 100644 --- a/wp-includes/js/wplink.js +++ b/wp-includes/js/wplink.js @@ -1,7 +1,8 @@ +/* global ajaxurl, tinymce, wpLinkL10n, setUserSetting, wpActiveEditor */ var wpLink; -(function($){ - var inputs = {}, rivers = {}, ed, River, Query; +( function( $ ) { + var inputs = {}, rivers = {}, editor, searchTimer, River, Query; wpLink = { timeToTriggerRiver: 150, @@ -11,79 +12,96 @@ 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' ); // 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' ).click( wpLink.toggleInternalLinking ); - inputs.search.keyup( wpLink.searchInternalLinks ); + rivers.elements.on( 'river-select', wpLink.updateFields ); - inputs.dialog.bind('wpdialogrefresh', wpLink.refresh); - inputs.dialog.bind('wpdialogbeforeopen', wpLink.beforeOpen); - inputs.dialog.bind('wpdialogclose', wpLink.onClose); + inputs.search.keyup( function() { + var self = this; + + window.clearTimeout( searchTimer ); + searchTimer = window.setTimeout( function() { + wpLink.searchInternalLinks.call( self ); + }, 500 ); + }); }, - beforeOpen : function() { + open: function( editorId ) { + var ed; + 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); - - // 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', - zIndex: 300000 - }); + this.textarea = $( '#' + window.wpActiveEditor ).get( 0 ); + + 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(); + } } - inputs.dialog.wpdialog('open'); + if ( ! wpLink.isMCE() && document.selection ) { + this.textarea.focus(); + this.range = document.selection.createRange(); + } + + inputs.wrap.show(); + inputs.backdrop.show(); + + wpLink.refresh(); }, - 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(); @@ -102,20 +120,16 @@ var wpLink; 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. - if ( "_blank" == ed.dom.getAttrib(e, 'target') ) - inputs.openInNewTab.prop('checked', true); + inputs.openInNewTab.prop( 'checked', ( '_blank' === editor.dom.getAttrib( e, 'target' ) ) ); // Update save prompt. inputs.submit.val( wpLinkL10n.update ); @@ -123,44 +137,41 @@ var wpLink; } else { wpLink.setDefaultValues(); } - - tinyMCEPopup.storeSelection(); - }, - - close : function() { - if ( wpLink.isMCE() ) - tinyMCEPopup.close(); - else - inputs.dialog.wpdialog('close'); }, - onClose: function() { + close: 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(); }, - 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, + htmlUpdate: function() { + var attrs, html, begin, end, cursor, title, selection, textarea = wpLink.textarea; if ( ! textarea ) @@ -175,10 +186,14 @@ var wpLink; // Build HTML html = '/g, '>' ).replace( /"/g, '"' ); + html += ' title="' + title + '"'; + } + + if ( attrs.target ) { html += ' target="' + attrs.target + '"'; + } html += '>'; @@ -201,13 +216,12 @@ var wpLink; html = html + selection + ''; cursor = begin + html.length; - // If no next is selected, place the cursor inside the closing tag. + // If no text is selected, place the cursor inside the closing tag. if ( begin == end ) cursor -= ''.length; - textarea.value = textarea.value.substring( 0, begin ) - + html - + textarea.value.substring( end, textarea.value.length ); + textarea.value = textarea.value.substring( 0, begin ) + html + + textarea.value.substring( end, textarea.value.length ); // Update cursor position textarea.selectionStart = textarea.selectionEnd = cursor; @@ -217,81 +231,54 @@ 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 ) { - tinyMCEPopup.execCommand("mceBeginUndoLevel"); - b = ed.selection.getBookmark(); - ed.dom.remove(e, 1); - ed.selection.moveToBookmark(b); - tinyMCEPopup.execCommand("mceEndUndoLevel"); - wpLink.close(); - } + editor.execCommand( 'unlink' ); return; } - tinyMCEPopup.execCommand("mceBeginUndoLevel"); - - 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 ( $(e).text() == '#mce_temp_url#' ) { - ed.dom.remove(e); - e = null; - } + if ( link ) { + editor.dom.setAttribs( link, attrs ); } else { - ed.dom.setAttribs(e, attrs); - } - - // Don't move caret if selection was image - if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) { - ed.focus(); - ed.selection.select(e); - ed.selection.collapse(0); - tinyMCEPopup.storeSelection(); + editor.execCommand( 'mceInsertLink', false, attrs ); } - tinyMCEPopup.execCommand("mceEndUndoLevel"); - wpLink.close(); + // 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" ) + 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(); }, - setDefaultValues : function() { + + setDefaultValues: function() { // Set URL and description to defaults. // Leave the new tab setting as-is. - inputs.url.val('http://'); - inputs.title.val(''); + inputs.url.val( 'http://' ); + 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 ) { @@ -306,61 +293,65 @@ 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; - - switch( event.which ) { - case key.UP: - fn = 'prev'; - case key.DOWN: - fn = fn || 'next'; - clearInterval( wpLink.keyInterval ); - wpLink[ fn ](); - wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity ); - break; - default: - return; + 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; + + 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; } + + fn = event.keyCode === key.UP ? 'prev' : 'next'; + clearInterval( wpLink.keyInterval ); + wpLink[ fn ](); + wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity ); event.preventDefault(); }, + keyup: function( event ) { var key = $.ui.keyCode; - switch( event.which ) { - case key.ESCAPE: - event.stopImmediatePropagation(); - if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wplink', cb: wpLink.close }] ) ) - wpLink.close(); - - return false; - break; - case key.UP: - case key.DOWN: - clearInterval( wpLink.keyInterval ); - break; - default: - return; + if ( event.which === key.UP || event.which === key.DOWN ) { + clearInterval( wpLink.keyInterval ); + event.preventDefault(); } - event.preventDefault(); }, - delayedCallback : function( func, delay ) { + delayedCallback: function( func, delay ) { var timeoutTriggered, funcTriggered, funcArgs, funcContext; if ( ! delay ) @@ -371,7 +362,7 @@ var wpLink; return func.apply( funcContext, funcArgs ); // Otherwise, wait. timeoutTriggered = true; - }, delay); + }, delay ); return function() { if ( timeoutTriggered ) @@ -383,53 +374,37 @@ 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); - } - }); - event.preventDefault(); + toggleInternalLinking: function() { + 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(); } - } + }; 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 ) { @@ -446,11 +421,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(); @@ -463,11 +438,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() { @@ -476,7 +451,7 @@ var wpLink; var to; if ( this.selected ) { - to = this.selected.prev('li'); + to = this.selected.prev( 'li' ); if ( to.length ) this.select( to ); } @@ -485,7 +460,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 ); }, @@ -506,27 +481,26 @@ 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 += '
  • ' - + wpLinkL10n.noMatchesFound - + '
  • '; + list += '
  • ' + + wpLinkL10n.noMatchesFound + '
  • '; } } else { $.each( results, function() { classes = alt ? 'alternate' : ''; - classes += this['title'] ? '' : ' no-title'; + classes += this.title ? '' : ' no-title'; list += classes ? '
  • ' : '
  • '; - list += ''; + list += ''; list += ''; - list += this['title'] ? this['title'] : wpLinkL10n.noTitle; - list += '' + this['info'] + '
  • '; + list += this.title ? this.title : wpLinkL10n.noTitle; + list += '' + this.info + ''; alt = ! alt; }); } @@ -538,20 +512,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 ); } }); @@ -565,7 +541,7 @@ var wpLink; $.extend( Query.prototype, { ready: function() { - return !( this.querying || this.allLoaded ); + return ! ( this.querying || this.allLoaded ); }, ajax: function( callback ) { var self = this, @@ -580,14 +556,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" ); + }, 'json' ); } }); - $(document).ready( wpLink.init ); -})(jQuery); + $( document ).ready( wpLink.init ); +})( jQuery );