1 /* global ajaxurl, tinymce, wpLinkL10n, setUserSetting, wpActiveEditor */
5 var inputs = {}, rivers = {}, editor, searchTimer, River, Query;
8 timeToTriggerRiver: 150,
9 minRiverAJAXDuration: 200,
10 riverBottomThreshold: 5,
16 inputs.wrap = $('#wp-link-wrap');
17 inputs.dialog = $( '#wp-link' );
18 inputs.backdrop = $( '#wp-link-backdrop' );
19 inputs.submit = $( '#wp-link-submit' );
20 inputs.close = $( '#wp-link-close' );
22 inputs.url = $( '#url-field' );
23 inputs.nonce = $( '#_ajax_linking_nonce' );
25 inputs.title = $( '#link-title-field' );
27 inputs.openInNewTab = $( '#link-target-checkbox' );
28 inputs.search = $( '#search-field' );
30 rivers.search = new River( $( '#search-results' ) );
31 rivers.recent = new River( $( '#most-recent-results' ) );
32 rivers.elements = inputs.dialog.find( '.query-results' );
34 // Bind event handlers
35 inputs.dialog.keydown( wpLink.keydown );
36 inputs.dialog.keyup( wpLink.keyup );
37 inputs.submit.click( function( event ) {
38 event.preventDefault();
41 inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel a' ).click( function( event ) {
42 event.preventDefault();
46 $( '#wp-link-search-toggle' ).click( wpLink.toggleInternalLinking );
48 rivers.elements.on( 'river-select', wpLink.updateFields );
50 inputs.search.keyup( function() {
53 window.clearTimeout( searchTimer );
54 searchTimer = window.setTimeout( function() {
55 wpLink.searchInternalLinks.call( self );
60 open: function( editorId ) {
66 window.wpActiveEditor = editorId;
69 if ( ! window.wpActiveEditor ) {
73 this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
75 if ( typeof tinymce !== 'undefined' ) {
76 ed = tinymce.get( wpActiveEditor );
78 if ( ed && ! ed.isHidden() ) {
84 if ( editor && tinymce.isIE ) {
85 editor.windowManager.bookmark = editor.selection.getBookmark();
89 if ( ! wpLink.isMCE() && document.selection ) {
90 this.textarea.focus();
91 this.range = document.selection.createRange();
95 inputs.backdrop.show();
101 return editor && ! editor.isHidden();
104 refresh: function() {
105 // Refresh rivers (clear links, check visibility)
106 rivers.search.refresh();
107 rivers.recent.refresh();
109 if ( wpLink.isMCE() )
112 wpLink.setDefaultValues();
114 // Focus the URL field and highlight its contents.
115 // If this is moved above the selection changes,
116 // IE will show a flashing cursor over the dialog.
117 inputs.url.focus()[0].select();
118 // Load the most recent results if this is the first time opening the panel.
119 if ( ! rivers.recent.ul.children().length )
120 rivers.recent.ajax();
123 mceRefresh: function() {
126 // If link exists, select proper values.
127 if ( e = editor.dom.getParent( editor.selection.getNode(), 'A' ) ) {
128 // Set URL and description.
129 inputs.url.val( editor.dom.getAttrib( e, 'href' ) );
130 inputs.title.val( editor.dom.getAttrib( e, 'title' ) );
131 // Set open in new tab.
132 inputs.openInNewTab.prop( 'checked', ( '_blank' === editor.dom.getAttrib( e, 'target' ) ) );
133 // Update save prompt.
134 inputs.submit.val( wpLinkL10n.update );
136 // If there's no link, set the default values.
138 wpLink.setDefaultValues();
143 if ( ! wpLink.isMCE() ) {
144 wpLink.textarea.focus();
146 if ( wpLink.range ) {
147 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
148 wpLink.range.select();
154 inputs.backdrop.hide();
158 getAttrs: function() {
160 href: inputs.url.val(),
161 title: inputs.title.val(),
162 target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : ''
167 if ( wpLink.isMCE() )
173 htmlUpdate: function() {
174 var attrs, html, begin, end, cursor, title, selection,
175 textarea = wpLink.textarea;
180 attrs = wpLink.getAttrs();
182 // If there's no href, return.
183 if ( ! attrs.href || attrs.href == 'http://' )
187 html = '<a href="' + attrs.href + '"';
190 title = attrs.title.replace( /</g, '<' ).replace( />/g, '>' ).replace( /"/g, '"' );
191 html += ' title="' + title + '"';
194 if ( attrs.target ) {
195 html += ' target="' + attrs.target + '"';
201 if ( document.selection && wpLink.range ) {
203 // Note: If no text is selected, IE will not place the cursor
204 // inside the closing tag.
206 wpLink.range.text = html + wpLink.range.text + '</a>';
207 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
208 wpLink.range.select();
211 } else if ( typeof textarea.selectionStart !== 'undefined' ) {
213 begin = textarea.selectionStart;
214 end = textarea.selectionEnd;
215 selection = textarea.value.substring( begin, end );
216 html = html + selection + '</a>';
217 cursor = begin + html.length;
219 // If no text is selected, place the cursor inside the closing tag.
221 cursor -= '</a>'.length;
223 textarea.value = textarea.value.substring( 0, begin ) + html +
224 textarea.value.substring( end, textarea.value.length );
226 // Update cursor position
227 textarea.selectionStart = textarea.selectionEnd = cursor;
234 mceUpdate: function() {
236 attrs = wpLink.getAttrs();
241 if ( tinymce.isIE ) {
242 editor.selection.moveToBookmark( editor.windowManager.bookmark );
245 link = editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
247 // If the values are empty, unlink and return
248 if ( ! attrs.href || attrs.href == 'http://' ) {
249 editor.execCommand( 'unlink' );
254 editor.dom.setAttribs( link, attrs );
256 editor.execCommand( 'mceInsertLink', false, attrs );
259 // Move the cursor to the end of the selection
260 editor.selection.collapse();
263 updateFields: function( e, li, originalEvent ) {
264 inputs.url.val( li.children( '.item-permalink' ).val() );
265 inputs.title.val( li.hasClass( 'no-title' ) ? '' : li.children( '.item-title' ).text() );
266 if ( originalEvent && originalEvent.type == 'click' )
270 setDefaultValues: function() {
271 // Set URL and description to defaults.
272 // Leave the new tab setting as-is.
273 inputs.url.val( 'http://' );
274 inputs.title.val( '' );
276 // Update save prompt.
277 inputs.submit.val( wpLinkL10n.save );
280 searchInternalLinks: function() {
281 var t = $( this ), waiting,
284 if ( search.length > 2 ) {
285 rivers.recent.hide();
286 rivers.search.show();
288 // Don't search if the keypress didn't change the title.
289 if ( wpLink.lastSearch == search )
292 wpLink.lastSearch = search;
293 waiting = t.parent().find('.spinner').show();
295 rivers.search.change( search );
296 rivers.search.ajax( function() {
300 rivers.search.hide();
301 rivers.recent.show();
306 rivers.search.next();
307 rivers.recent.next();
311 rivers.search.prev();
312 rivers.recent.prev();
315 keydown: function( event ) {
319 if ( key.ESCAPE === event.keyCode ) {
321 event.stopImmediatePropagation();
322 } else if ( key.TAB === event.keyCode ) {
323 id = event.target.id;
325 if ( id === 'wp-link-submit' && ! event.shiftKey ) {
326 inputs.close.focus();
327 event.preventDefault();
328 } else if ( id === 'wp-link-close' && event.shiftKey ) {
329 inputs.submit.focus();
330 event.preventDefault();
334 if ( event.keyCode !== key.UP && event.keyCode !== key.DOWN ) {
338 fn = event.keyCode === key.UP ? 'prev' : 'next';
339 clearInterval( wpLink.keyInterval );
341 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
342 event.preventDefault();
345 keyup: function( event ) {
346 var key = $.ui.keyCode;
348 if ( event.which === key.UP || event.which === key.DOWN ) {
349 clearInterval( wpLink.keyInterval );
350 event.preventDefault();
354 delayedCallback: function( func, delay ) {
355 var timeoutTriggered, funcTriggered, funcArgs, funcContext;
360 setTimeout( function() {
362 return func.apply( funcContext, funcArgs );
364 timeoutTriggered = true;
368 if ( timeoutTriggered )
369 return func.apply( this, arguments );
371 funcArgs = arguments;
373 funcTriggered = true;
377 toggleInternalLinking: function() {
378 var visible = inputs.wrap.hasClass( 'search-panel-visible' );
380 inputs.wrap.toggleClass( 'search-panel-visible', ! visible );
381 setUserSetting( 'wplink', visible ? '0' : '1' );
382 inputs[ ! visible ? 'search' : 'url' ].focus();
386 River = function( element, search ) {
388 this.element = element;
389 this.ul = element.children( 'ul' );
390 this.contentHeight = element.children( '#link-selector-height' );
391 this.waiting = element.find('.river-waiting');
393 this.change( search );
396 $( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
399 element.on( 'click', 'li', function( event ) {
400 self.select( $( this ), event );
404 $.extend( River.prototype, {
405 refresh: function() {
407 this.visible = this.element.is( ':visible' );
410 if ( ! this.visible ) {
418 this.visible = false;
420 // Selects a list item and triggers the river-select event.
421 select: function( li, event ) {
422 var liHeight, elHeight, liTop, elTop;
424 if ( li.hasClass( 'unselectable' ) || li == this.selected )
428 this.selected = li.addClass( 'selected' );
429 // Make sure the element is visible
430 liHeight = li.outerHeight();
431 elHeight = this.element.height();
432 liTop = li.position().top;
433 elTop = this.element.scrollTop();
435 if ( liTop < 0 ) // Make first visible element
436 this.element.scrollTop( elTop + liTop );
437 else if ( liTop + liHeight > elHeight ) // Make last visible element
438 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
440 // Trigger the river-select event
441 this.element.trigger( 'river-select', [ li, event, this ] );
443 deselect: function() {
445 this.selected.removeClass( 'selected' );
446 this.selected = false;
449 if ( ! this.visible )
453 if ( this.selected ) {
454 to = this.selected.prev( 'li' );
460 if ( ! this.visible )
463 var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
467 ajax: function( callback ) {
469 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
470 response = wpLink.delayedCallback( function( results, params ) {
471 self.process( results, params );
473 callback( results, params );
476 this.query.ajax( response );
478 change: function( search ) {
479 if ( this.query && this._search == search )
482 this._search = search;
483 this.query = new Query( search );
484 this.element.scrollTop( 0 );
486 process: function( results, params ) {
487 var list = '', alt = true, classes = '',
488 firstPage = params.page == 1;
492 list += '<li class="unselectable"><span class="item-title"><em>' +
493 wpLinkL10n.noMatchesFound + '</em></span></li>';
496 $.each( results, function() {
497 classes = alt ? 'alternate' : '';
498 classes += this.title ? '' : ' no-title';
499 list += classes ? '<li class="' + classes + '">' : '<li>';
500 list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
501 list += '<span class="item-title">';
502 list += this.title ? this.title : wpLinkL10n.noTitle;
503 list += '</span><span class="item-info">' + this.info + '</span></li>';
508 this.ul[ firstPage ? 'html' : 'append' ]( list );
510 maybeLoad: function() {
513 bottom = el.scrollTop() + el.height();
515 if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
518 setTimeout(function() {
519 var newTop = el.scrollTop(),
520 newBottom = newTop + el.height();
522 if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
526 el.scrollTop( newTop + self.waiting.outerHeight() );
528 self.ajax( function() {
531 }, wpLink.timeToTriggerRiver );
535 Query = function( search ) {
537 this.allLoaded = false;
538 this.querying = false;
539 this.search = search;
542 $.extend( Query.prototype, {
544 return ! ( this.querying || this.allLoaded );
546 ajax: function( callback ) {
549 action : 'wp-link-ajax',
551 '_ajax_linking_nonce' : inputs.nonce.val()
555 query.search = this.search;
557 this.querying = true;
559 $.post( ajaxurl, query, function( r ) {
561 self.querying = false;
562 self.allLoaded = ! r;
563 callback( r, query );
568 $( document ).ready( wpLink.init );