4 var inputs = {}, rivers = {}, ed, River, Query;
7 timeToTriggerRiver: 150,
8 minRiverAJAXDuration: 200,
9 riverBottomThreshold: 5,
15 inputs.dialog = $('#wp-link');
16 inputs.submit = $('#wp-link-submit');
18 inputs.url = $('#url-field');
19 inputs.nonce = $('#_ajax_linking_nonce');
21 inputs.title = $('#link-title-field');
23 inputs.openInNewTab = $('#link-target-checkbox');
24 inputs.search = $('#search-field');
26 rivers.search = new River( $('#search-results') );
27 rivers.recent = new River( $('#most-recent-results') );
28 rivers.elements = $('.query-results', inputs.dialog);
30 // Bind event handlers
31 inputs.dialog.keydown( wpLink.keydown );
32 inputs.dialog.keyup( wpLink.keyup );
33 inputs.submit.click( function(e){
37 $('#wp-link-cancel').click( function(e){
41 $('#internal-toggle').click( wpLink.toggleInternalLinking );
43 rivers.elements.bind('river-select', wpLink.updateFields );
45 inputs.search.keyup( wpLink.searchInternalLinks );
47 inputs.dialog.bind('wpdialogrefresh', wpLink.refresh);
48 inputs.dialog.bind('wpdialogbeforeopen', wpLink.beforeOpen);
49 inputs.dialog.bind('wpdialogclose', wpLink.onClose);
52 beforeOpen : function() {
55 if ( ! wpLink.isMCE() && document.selection ) {
56 wpLink.textarea.focus();
57 wpLink.range = document.selection.createRange();
62 if ( !wpActiveEditor )
65 this.textarea = $('#'+wpActiveEditor).get(0);
67 // Initialize the dialog if necessary (html mode).
68 if ( ! inputs.dialog.data('wpdialog') ) {
69 inputs.dialog.wpdialog({
70 title: wpLinkL10n.title,
74 dialogClass: 'wp-dialog'
78 inputs.dialog.wpdialog('open');
82 return tinyMCEPopup && ( ed = tinyMCEPopup.editor ) && ! ed.isHidden();
85 refresh : function() {
86 // Refresh rivers (clear links, check visibility)
87 rivers.search.refresh();
88 rivers.recent.refresh();
93 wpLink.setDefaultValues();
95 // Focus the URL field and highlight its contents.
96 // If this is moved above the selection changes,
97 // IE will show a flashing cursor over the dialog.
98 inputs.url.focus()[0].select();
99 // Load the most recent results if this is the first time opening the panel.
100 if ( ! rivers.recent.ul.children().length )
101 rivers.recent.ajax();
104 mceRefresh : function() {
106 ed = tinyMCEPopup.editor;
108 tinyMCEPopup.restoreSelection();
110 // If link exists, select proper values.
111 if ( e = ed.dom.getParent(ed.selection.getNode(), 'A') ) {
112 // Set URL and description.
113 inputs.url.val( ed.dom.getAttrib(e, 'href') );
114 inputs.title.val( ed.dom.getAttrib(e, 'title') );
115 // Set open in new tab.
116 inputs.openInNewTab.prop('checked', ( "_blank" == ed.dom.getAttrib( e, 'target' ) ) );
117 // Update save prompt.
118 inputs.submit.val( wpLinkL10n.update );
120 // If there's no link, set the default values.
122 wpLink.setDefaultValues();
127 if ( wpLink.isMCE() )
128 tinyMCEPopup.close();
130 inputs.dialog.wpdialog('close');
133 onClose: function() {
134 if ( ! wpLink.isMCE() ) {
135 wpLink.textarea.focus();
136 if ( wpLink.range ) {
137 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
138 wpLink.range.select();
143 getAttrs : function() {
145 href : inputs.url.val(),
146 title : inputs.title.val(),
147 target : inputs.openInNewTab.prop('checked') ? '_blank' : ''
151 update : function() {
152 if ( wpLink.isMCE() )
158 htmlUpdate : function() {
159 var attrs, html, begin, end, cursor,
160 textarea = wpLink.textarea;
165 attrs = wpLink.getAttrs();
167 // If there's no href, return.
168 if ( ! attrs.href || attrs.href == 'http://' )
172 html = '<a href="' + attrs.href + '"';
175 html += ' title="' + attrs.title + '"';
177 html += ' target="' + attrs.target + '"';
182 if ( document.selection && wpLink.range ) {
184 // Note: If no text is selected, IE will not place the cursor
185 // inside the closing tag.
187 wpLink.range.text = html + wpLink.range.text + '</a>';
188 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
189 wpLink.range.select();
192 } else if ( typeof textarea.selectionStart !== 'undefined' ) {
194 begin = textarea.selectionStart;
195 end = textarea.selectionEnd;
196 selection = textarea.value.substring( begin, end );
197 html = html + selection + '</a>';
198 cursor = begin + html.length;
200 // If no next is selected, place the cursor inside the closing tag.
202 cursor -= '</a>'.length;
204 textarea.value = textarea.value.substring( 0, begin )
206 + textarea.value.substring( end, textarea.value.length );
208 // Update cursor position
209 textarea.selectionStart = textarea.selectionEnd = cursor;
216 mceUpdate : function() {
217 var ed = tinyMCEPopup.editor,
218 attrs = wpLink.getAttrs(),
221 tinyMCEPopup.restoreSelection();
222 e = ed.dom.getParent(ed.selection.getNode(), 'A');
224 // If the values are empty, unlink and return
225 if ( ! attrs.href || attrs.href == 'http://' ) {
227 b = ed.selection.getBookmark();
229 ed.selection.moveToBookmark(b);
230 tinyMCEPopup.execCommand("mceEndUndoLevel");
237 ed.getDoc().execCommand("unlink", false, null);
238 tinyMCEPopup.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo : 1});
240 tinymce.each(ed.dom.select("a"), function(n) {
241 if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
243 ed.dom.setAttribs(e, attrs);
247 // Sometimes WebKit lets a user create a link where
248 // they shouldn't be able to. In this case, CreateLink
249 // injects "#mce_temp_url#" into their content. Fix it.
250 if ( tinymce.isWebKit && $(e).text() == '#mce_temp_url#' ) {
255 ed.dom.setAttribs(e, attrs);
258 // Move the caret if selection was not an image.
259 if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) {
260 ed.selection.select(e);
261 ed.selection.collapse(0);
262 tinyMCEPopup.storeSelection();
265 ed.execCommand("mceEndUndoLevel");
270 updateFields : function( e, li, originalEvent ) {
271 inputs.url.val( li.children('.item-permalink').val() );
272 inputs.title.val( li.hasClass('no-title') ? '' : li.children('.item-title').text() );
273 if ( originalEvent && originalEvent.type == "click" )
276 setDefaultValues : function() {
277 // Set URL and description to defaults.
278 // Leave the new tab setting as-is.
279 inputs.url.val('http://');
280 inputs.title.val('');
282 // Update save prompt.
283 inputs.submit.val( wpLinkL10n.save );
286 searchInternalLinks : function() {
287 var t = $(this), waiting,
290 if ( search.length > 2 ) {
291 rivers.recent.hide();
292 rivers.search.show();
294 // Don't search if the keypress didn't change the title.
295 if ( wpLink.lastSearch == search )
298 wpLink.lastSearch = search;
299 waiting = t.parent().find('.spinner').show();
301 rivers.search.change( search );
302 rivers.search.ajax( function(){ waiting.hide(); });
304 rivers.search.hide();
305 rivers.recent.show();
310 rivers.search.next();
311 rivers.recent.next();
314 rivers.search.prev();
315 rivers.recent.prev();
318 keydown : function( event ) {
319 var fn, key = $.ui.keyCode;
321 switch( event.which ) {
326 clearInterval( wpLink.keyInterval );
328 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
333 event.preventDefault();
335 keyup: function( event ) {
336 var key = $.ui.keyCode;
338 switch( event.which ) {
340 event.stopImmediatePropagation();
341 if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wplink', cb: wpLink.close }] ) )
348 clearInterval( wpLink.keyInterval );
353 event.preventDefault();
356 delayedCallback : function( func, delay ) {
357 var timeoutTriggered, funcTriggered, funcArgs, funcContext;
362 setTimeout( function() {
364 return func.apply( funcContext, funcArgs );
366 timeoutTriggered = true;
370 if ( timeoutTriggered )
371 return func.apply( this, arguments );
373 funcArgs = arguments;
375 funcTriggered = true;
379 toggleInternalLinking : function( event ) {
380 var panel = $('#search-panel'),
381 widget = inputs.dialog.wpdialog('widget'),
382 // We're about to toggle visibility; it's currently the opposite
383 visible = !panel.is(':visible'),
386 $(this).toggleClass('toggle-arrow-active', visible);
388 inputs.dialog.height('auto');
389 panel.slideToggle( 300, function() {
390 setUserSetting('wplink', visible ? '1' : '0');
391 inputs[ visible ? 'search' : 'url' ].focus();
393 // Move the box if the box is now expanded, was opened in a collapsed state,
394 // and if it needs to be moved. (Judged by bottom not being positive or
395 // bottom being smaller than top.)
396 var scroll = win.scrollTop(),
397 top = widget.offset().top,
398 bottom = top + widget.outerHeight(),
399 diff = bottom - win.height();
401 if ( diff > scroll ) {
402 widget.animate({'top': diff < top ? top - diff : scroll }, 200);
405 event.preventDefault();
409 River = function( element, search ) {
411 this.element = element;
412 this.ul = element.children('ul');
413 this.waiting = element.find('.river-waiting');
415 this.change( search );
418 element.scroll( function(){ self.maybeLoad(); });
419 element.delegate('li', 'click', function(e){ self.select( $(this), e ); });
422 $.extend( River.prototype, {
423 refresh: function() {
425 this.visible = this.element.is(':visible');
428 if ( ! this.visible ) {
436 this.visible = false;
438 // Selects a list item and triggers the river-select event.
439 select: function( li, event ) {
440 var liHeight, elHeight, liTop, elTop;
442 if ( li.hasClass('unselectable') || li == this.selected )
446 this.selected = li.addClass('selected');
447 // Make sure the element is visible
448 liHeight = li.outerHeight();
449 elHeight = this.element.height();
450 liTop = li.position().top;
451 elTop = this.element.scrollTop();
453 if ( liTop < 0 ) // Make first visible element
454 this.element.scrollTop( elTop + liTop );
455 else if ( liTop + liHeight > elHeight ) // Make last visible element
456 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
458 // Trigger the river-select event
459 this.element.trigger('river-select', [ li, event, this ]);
461 deselect: function() {
463 this.selected.removeClass('selected');
464 this.selected = false;
467 if ( ! this.visible )
471 if ( this.selected ) {
472 to = this.selected.prev('li');
478 if ( ! this.visible )
481 var to = this.selected ? this.selected.next('li') : $('li:not(.unselectable):first', this.element);
485 ajax: function( callback ) {
487 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
488 response = wpLink.delayedCallback( function( results, params ) {
489 self.process( results, params );
491 callback( results, params );
494 this.query.ajax( response );
496 change: function( search ) {
497 if ( this.query && this._search == search )
500 this._search = search;
501 this.query = new Query( search );
502 this.element.scrollTop(0);
504 process: function( results, params ) {
505 var list = '', alt = true, classes = '',
506 firstPage = params.page == 1;
510 list += '<li class="unselectable"><span class="item-title"><em>'
511 + wpLinkL10n.noMatchesFound
512 + '</em></span></li>';
515 $.each( results, function() {
516 classes = alt ? 'alternate' : '';
517 classes += this['title'] ? '' : ' no-title';
518 list += classes ? '<li class="' + classes + '">' : '<li>';
519 list += '<input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
520 list += '<span class="item-title">';
521 list += this['title'] ? this['title'] : wpLinkL10n.noTitle;
522 list += '</span><span class="item-info">' + this['info'] + '</span></li>';
527 this.ul[ firstPage ? 'html' : 'append' ]( list );
529 maybeLoad: function() {
532 bottom = el.scrollTop() + el.height();
534 if ( ! this.query.ready() || bottom < this.ul.height() - wpLink.riverBottomThreshold )
537 setTimeout(function() {
538 var newTop = el.scrollTop(),
539 newBottom = newTop + el.height();
541 if ( ! self.query.ready() || newBottom < self.ul.height() - wpLink.riverBottomThreshold )
545 el.scrollTop( newTop + self.waiting.outerHeight() );
547 self.ajax( function() { self.waiting.hide(); });
548 }, wpLink.timeToTriggerRiver );
552 Query = function( search ) {
554 this.allLoaded = false;
555 this.querying = false;
556 this.search = search;
559 $.extend( Query.prototype, {
561 return !( this.querying || this.allLoaded );
563 ajax: function( callback ) {
566 action : 'wp-link-ajax',
568 '_ajax_linking_nonce' : inputs.nonce.val()
572 query.search = this.search;
574 this.querying = true;
576 $.post( ajaxurl, query, function(r) {
578 self.querying = false;
580 callback( r, query );
585 $(document).ready( wpLink.init );