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();
125 tinyMCEPopup.storeSelection();
129 if ( wpLink.isMCE() )
130 tinyMCEPopup.close();
132 inputs.dialog.wpdialog('close');
135 onClose: function() {
136 if ( ! wpLink.isMCE() ) {
137 wpLink.textarea.focus();
138 if ( wpLink.range ) {
139 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
140 wpLink.range.select();
145 getAttrs : function() {
147 href : inputs.url.val(),
148 title : inputs.title.val(),
149 target : inputs.openInNewTab.prop('checked') ? '_blank' : ''
153 update : function() {
154 if ( wpLink.isMCE() )
160 htmlUpdate : function() {
161 var attrs, html, begin, end, cursor,
162 textarea = wpLink.textarea;
167 attrs = wpLink.getAttrs();
169 // If there's no href, return.
170 if ( ! attrs.href || attrs.href == 'http://' )
174 html = '<a href="' + attrs.href + '"';
177 html += ' title="' + attrs.title + '"';
179 html += ' target="' + attrs.target + '"';
184 if ( document.selection && wpLink.range ) {
186 // Note: If no text is selected, IE will not place the cursor
187 // inside the closing tag.
189 wpLink.range.text = html + wpLink.range.text + '</a>';
190 wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
191 wpLink.range.select();
194 } else if ( typeof textarea.selectionStart !== 'undefined' ) {
196 begin = textarea.selectionStart;
197 end = textarea.selectionEnd;
198 selection = textarea.value.substring( begin, end );
199 html = html + selection + '</a>';
200 cursor = begin + html.length;
202 // If no next is selected, place the cursor inside the closing tag.
204 cursor -= '</a>'.length;
206 textarea.value = textarea.value.substring( 0, begin )
208 + textarea.value.substring( end, textarea.value.length );
210 // Update cursor position
211 textarea.selectionStart = textarea.selectionEnd = cursor;
218 mceUpdate : function() {
219 var ed = tinyMCEPopup.editor,
220 attrs = wpLink.getAttrs(),
223 tinyMCEPopup.restoreSelection();
224 e = ed.dom.getParent(ed.selection.getNode(), 'A');
226 // If the values are empty, unlink and return
227 if ( ! attrs.href || attrs.href == 'http://' ) {
229 tinyMCEPopup.execCommand("mceBeginUndoLevel");
230 b = ed.selection.getBookmark();
232 ed.selection.moveToBookmark(b);
233 tinyMCEPopup.execCommand("mceEndUndoLevel");
239 tinyMCEPopup.execCommand("mceBeginUndoLevel");
242 ed.getDoc().execCommand("unlink", false, null);
243 tinyMCEPopup.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo : 1});
245 tinymce.each(ed.dom.select("a"), function(n) {
246 if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
248 ed.dom.setAttribs(e, attrs);
252 // Sometimes WebKit lets a user create a link where
253 // they shouldn't be able to. In this case, CreateLink
254 // injects "#mce_temp_url#" into their content. Fix it.
255 if ( $(e).text() == '#mce_temp_url#' ) {
260 ed.dom.setAttribs(e, attrs);
263 // Don't move caret if selection was image
264 if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) {
266 ed.selection.select(e);
267 ed.selection.collapse(0);
268 tinyMCEPopup.storeSelection();
271 tinyMCEPopup.execCommand("mceEndUndoLevel");
275 updateFields : function( e, li, originalEvent ) {
276 inputs.url.val( li.children('.item-permalink').val() );
277 inputs.title.val( li.hasClass('no-title') ? '' : li.children('.item-title').text() );
278 if ( originalEvent && originalEvent.type == "click" )
281 setDefaultValues : function() {
282 // Set URL and description to defaults.
283 // Leave the new tab setting as-is.
284 inputs.url.val('http://');
285 inputs.title.val('');
287 // Update save prompt.
288 inputs.submit.val( wpLinkL10n.save );
291 searchInternalLinks : function() {
292 var t = $(this), waiting,
295 if ( search.length > 2 ) {
296 rivers.recent.hide();
297 rivers.search.show();
299 // Don't search if the keypress didn't change the title.
300 if ( wpLink.lastSearch == search )
303 wpLink.lastSearch = search;
304 waiting = t.parent().find('.spinner').show();
306 rivers.search.change( search );
307 rivers.search.ajax( function(){ waiting.hide(); });
309 rivers.search.hide();
310 rivers.recent.show();
315 rivers.search.next();
316 rivers.recent.next();
319 rivers.search.prev();
320 rivers.recent.prev();
323 keydown : function( event ) {
324 var fn, key = $.ui.keyCode;
326 switch( event.which ) {
331 clearInterval( wpLink.keyInterval );
333 wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
338 event.preventDefault();
340 keyup: function( event ) {
341 var key = $.ui.keyCode;
343 switch( event.which ) {
345 event.stopImmediatePropagation();
346 if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wplink', cb: wpLink.close }] ) )
353 clearInterval( wpLink.keyInterval );
358 event.preventDefault();
361 delayedCallback : function( func, delay ) {
362 var timeoutTriggered, funcTriggered, funcArgs, funcContext;
367 setTimeout( function() {
369 return func.apply( funcContext, funcArgs );
371 timeoutTriggered = true;
375 if ( timeoutTriggered )
376 return func.apply( this, arguments );
378 funcArgs = arguments;
380 funcTriggered = true;
384 toggleInternalLinking : function( event ) {
385 var panel = $('#search-panel'),
386 widget = inputs.dialog.wpdialog('widget'),
387 // We're about to toggle visibility; it's currently the opposite
388 visible = !panel.is(':visible'),
391 $(this).toggleClass('toggle-arrow-active', visible);
393 inputs.dialog.height('auto');
394 panel.slideToggle( 300, function() {
395 setUserSetting('wplink', visible ? '1' : '0');
396 inputs[ visible ? 'search' : 'url' ].focus();
398 // Move the box if the box is now expanded, was opened in a collapsed state,
399 // and if it needs to be moved. (Judged by bottom not being positive or
400 // bottom being smaller than top.)
401 var scroll = win.scrollTop(),
402 top = widget.offset().top,
403 bottom = top + widget.outerHeight(),
404 diff = bottom - win.height();
406 if ( diff > scroll ) {
407 widget.animate({'top': diff < top ? top - diff : scroll }, 200);
410 event.preventDefault();
414 River = function( element, search ) {
416 this.element = element;
417 this.ul = element.children('ul');
418 this.waiting = element.find('.river-waiting');
420 this.change( search );
423 element.scroll( function(){ self.maybeLoad(); });
424 element.delegate('li', 'click', function(e){ self.select( $(this), e ); });
427 $.extend( River.prototype, {
428 refresh: function() {
430 this.visible = this.element.is(':visible');
433 if ( ! this.visible ) {
441 this.visible = false;
443 // Selects a list item and triggers the river-select event.
444 select: function( li, event ) {
445 var liHeight, elHeight, liTop, elTop;
447 if ( li.hasClass('unselectable') || li == this.selected )
451 this.selected = li.addClass('selected');
452 // Make sure the element is visible
453 liHeight = li.outerHeight();
454 elHeight = this.element.height();
455 liTop = li.position().top;
456 elTop = this.element.scrollTop();
458 if ( liTop < 0 ) // Make first visible element
459 this.element.scrollTop( elTop + liTop );
460 else if ( liTop + liHeight > elHeight ) // Make last visible element
461 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
463 // Trigger the river-select event
464 this.element.trigger('river-select', [ li, event, this ]);
466 deselect: function() {
468 this.selected.removeClass('selected');
469 this.selected = false;
472 if ( ! this.visible )
476 if ( this.selected ) {
477 to = this.selected.prev('li');
483 if ( ! this.visible )
486 var to = this.selected ? this.selected.next('li') : $('li:not(.unselectable):first', this.element);
490 ajax: function( callback ) {
492 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
493 response = wpLink.delayedCallback( function( results, params ) {
494 self.process( results, params );
496 callback( results, params );
499 this.query.ajax( response );
501 change: function( search ) {
502 if ( this.query && this._search == search )
505 this._search = search;
506 this.query = new Query( search );
507 this.element.scrollTop(0);
509 process: function( results, params ) {
510 var list = '', alt = true, classes = '',
511 firstPage = params.page == 1;
515 list += '<li class="unselectable"><span class="item-title"><em>'
516 + wpLinkL10n.noMatchesFound
517 + '</em></span></li>';
520 $.each( results, function() {
521 classes = alt ? 'alternate' : '';
522 classes += this['title'] ? '' : ' no-title';
523 list += classes ? '<li class="' + classes + '">' : '<li>';
524 list += '<input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
525 list += '<span class="item-title">';
526 list += this['title'] ? this['title'] : wpLinkL10n.noTitle;
527 list += '</span><span class="item-info">' + this['info'] + '</span></li>';
532 this.ul[ firstPage ? 'html' : 'append' ]( list );
534 maybeLoad: function() {
537 bottom = el.scrollTop() + el.height();
539 if ( ! this.query.ready() || bottom < this.ul.height() - wpLink.riverBottomThreshold )
542 setTimeout(function() {
543 var newTop = el.scrollTop(),
544 newBottom = newTop + el.height();
546 if ( ! self.query.ready() || newBottom < self.ul.height() - wpLink.riverBottomThreshold )
550 el.scrollTop( newTop + self.waiting.outerHeight() );
552 self.ajax( function() { self.waiting.hide(); });
553 }, wpLink.timeToTriggerRiver );
557 Query = function( search ) {
559 this.allLoaded = false;
560 this.querying = false;
561 this.search = search;
564 $.extend( Query.prototype, {
566 return !( this.querying || this.allLoaded );
568 ajax: function( callback ) {
571 action : 'wp-link-ajax',
573 '_ajax_linking_nonce' : inputs.nonce.val()
577 query.search = this.search;
579 this.querying = true;
581 $.post( ajaxurl, query, function(r) {
583 self.querying = false;
585 callback( r, query );
590 $(document).ready( wpLink.init );