]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/wplink.js
WordPress 3.9
[autoinstalls/wordpress.git] / wp-includes / js / wplink.js
index 7756c0fdb706c0c9e15e1c2284baa98edaee75b7..7d1c6774e8897e2466829175abde458269bc65b0 100644 (file)
@@ -1,8 +1,8 @@
-/* global ajaxurl, tinymce, wpLinkL10n, tinyMCEPopup, setUserSetting, wpActiveEditor */
+/* 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,
@@ -12,78 +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);
+                       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();
                },
 
-               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,19 +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.
-                               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 +139,39 @@ var wpLink;
                        }
                },
 
-               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, selection,
+               htmlUpdate: function() {
+                       var attrs, html, begin, end, cursor, title, selection,
                                textarea = wpLink.textarea;
 
                        if ( ! textarea )
@@ -172,10 +186,14 @@ var wpLink;
                        // Build HTML
                        html = '<a href="' + attrs.href + '"';
 
-                       if ( attrs.title )
-                               html += ' title="' + attrs.title + '"';
-                       if ( attrs.target )
+                       if ( attrs.title ) {
+                               title = attrs.title.replace( /</g, '&lt;' ).replace( />/g, '&gt;' ).replace( /"/g, '&quot;' );
+                               html += ' title="' + title + '"';
+                       }
+
+                       if ( attrs.target ) {
                                html += ' target="' + attrs.target + '"';
+                       }
 
                        html += '>';
 
@@ -213,78 +231,54 @@ var wpLink;
                        textarea.focus();
                },
 
-               mceUpdate : function() {
-                       var ed = tinyMCEPopup.editor,
-                               attrs = wpLink.getAttrs(),
-                               e, b;
+               mceUpdate: function() {
+                       var link,
+                               attrs = wpLink.getAttrs();
 
-                       tinyMCEPopup.restoreSelection();
-                       e = ed.dom.getParent(ed.selection.getNode(), 'A');
+                       wpLink.close();
+                       editor.focus();
+
+                       if ( tinymce.isIE ) {
+                               editor.selection.moveToBookmark( editor.windowManager.bookmark );
+                       }
+
+                       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);
+                               editor.execCommand( 'mceInsertLink', false, 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();
-                       }
-
-                       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() );
+               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 ) {
@@ -299,30 +293,49 @@ 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 ( event.which !== key.UP && event.which !== key.DOWN ) {
+                       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.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 +345,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 +362,7 @@ var wpLink;
                                        return func.apply( funcContext, funcArgs );
                                // Otherwise, wait.
                                timeoutTriggered = true;
-                       }, delay);
+                       }, delay );
 
                        return function() {
                                if ( timeoutTriggered )
@@ -368,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 ) {
@@ -431,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();
@@ -448,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() {
@@ -461,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 );
                        }
@@ -470,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 );
                },
@@ -491,13 +481,13 @@ 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 += '<li class="unselectable"><span class="item-title"><em>' +
                                                wpLinkL10n.noMatchesFound + '</em></span></li>';
@@ -522,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 );
                }
        });
@@ -549,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,
@@ -564,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' );
                }
        });
 
-       $(document).ready( wpLink.init );
-})(jQuery);
+       $( document ).ready( wpLink.init );
+})( jQuery );