2010-11-15 18:42:49 +00:00
|
|
|
(function($){
|
2010-11-15 23:48:24 +00:00
|
|
|
var inputs = {}, results = {}, ed,
|
2010-11-15 18:42:49 +00:00
|
|
|
wpLink = {
|
2010-11-16 18:30:34 +00:00
|
|
|
timeToTriggerRiver: 150,
|
|
|
|
minRiverAJAXDuration: 200,
|
2010-11-16 05:11:41 +00:00
|
|
|
lastSearch: '',
|
|
|
|
riverDefaults: function() {
|
|
|
|
return {
|
|
|
|
page : 2,
|
|
|
|
allLoaded: false,
|
|
|
|
active: false
|
|
|
|
};
|
|
|
|
},
|
2010-11-15 18:42:49 +00:00
|
|
|
init : function() {
|
2010-11-16 05:11:41 +00:00
|
|
|
var e;
|
2010-11-15 18:42:49 +00:00
|
|
|
// Init shared vars
|
|
|
|
ed = tinyMCEPopup.editor;
|
2010-11-15 23:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
// URL
|
|
|
|
inputs.url = $('#url-field');
|
2010-11-15 18:42:49 +00:00
|
|
|
// Secondary options
|
|
|
|
inputs.title = $('#link-title-field');
|
|
|
|
// Advanced Options
|
|
|
|
inputs.openInNewTab = $('#link-target-checkbox');
|
2010-11-15 23:48:24 +00:00
|
|
|
inputs.search = $('#search-field');
|
|
|
|
// Result lists
|
|
|
|
results.search = $('#search-results');
|
|
|
|
results.recent = $('#most-recent-results');
|
2010-11-16 05:11:41 +00:00
|
|
|
results.search.data('river', wpLink.riverDefaults() );
|
|
|
|
results.recent.data('river', wpLink.riverDefaults() );
|
2010-11-15 18:42:49 +00:00
|
|
|
|
|
|
|
// Bind event handlers
|
|
|
|
$('#wp-update').click( wpLink.update );
|
|
|
|
$('#wp-cancel').click( function() { tinyMCEPopup.close(); } );
|
2010-11-15 23:48:24 +00:00
|
|
|
$('.query-results').delegate('li', 'click', wpLink.selectInternalLink );
|
2010-11-16 05:11:41 +00:00
|
|
|
$('.query-results').scroll( wpLink.maybeLoadRiver );
|
2010-11-15 23:48:24 +00:00
|
|
|
inputs.search.keyup( wpLink.searchInternalLinks );
|
2010-11-15 18:42:49 +00:00
|
|
|
|
|
|
|
// If link exists, select proper values.
|
2010-11-15 23:48:24 +00:00
|
|
|
if ( e = ed.dom.getParent(ed.selection.getNode(), 'A') ) {
|
|
|
|
// Set URL and description.
|
|
|
|
inputs.url.val( e.href );
|
|
|
|
inputs.title.val( ed.dom.getAttrib(e, 'title') );
|
|
|
|
// Set open in new tab.
|
|
|
|
if ( "_blank" == ed.dom.getAttrib(e, 'target') )
|
|
|
|
inputs.openInNewTab.attr('checked','checked');
|
|
|
|
}
|
2010-11-16 05:11:41 +00:00
|
|
|
|
|
|
|
// Focus the URL field
|
|
|
|
inputs.url.focus();
|
2010-11-15 18:42:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
update : function() {
|
|
|
|
var el,
|
|
|
|
ed = tinyMCEPopup.editor,
|
|
|
|
attrs = {
|
2010-11-15 23:48:24 +00:00
|
|
|
href : inputs.url.val(),
|
2010-11-15 18:42:49 +00:00
|
|
|
title : inputs.title.val(),
|
|
|
|
target : inputs.openInNewTab.attr('checked') ? '_blank' : ''
|
2010-11-15 23:48:24 +00:00
|
|
|
}, e, b,
|
|
|
|
defaultContent = attrs.title ? attrs.title : attrs.href;
|
2010-11-15 18:42:49 +00:00
|
|
|
|
|
|
|
tinyMCEPopup.restoreSelection();
|
|
|
|
e = ed.dom.getParent(ed.selection.getNode(), 'A');
|
|
|
|
|
|
|
|
// If the values are empty...
|
|
|
|
if ( ! attrs.href ) {
|
|
|
|
// ...and nothing is selected, we should return
|
|
|
|
if ( ed.selection.isCollapsed() ) {
|
|
|
|
tinyMCEPopup.close();
|
|
|
|
return;
|
|
|
|
// ...and a link exists, we should unlink and return
|
|
|
|
} else if ( e ) {
|
|
|
|
tinyMCEPopup.execCommand("mceBeginUndoLevel");
|
|
|
|
b = ed.selection.getBookmark();
|
|
|
|
ed.dom.remove(e, 1);
|
|
|
|
ed.selection.moveToBookmark(b);
|
|
|
|
tinyMCEPopup.execCommand("mceEndUndoLevel");
|
|
|
|
tinyMCEPopup.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tinyMCEPopup.execCommand("mceBeginUndoLevel");
|
|
|
|
|
|
|
|
if (e == null) {
|
|
|
|
ed.getDoc().execCommand("unlink", false, null);
|
|
|
|
|
|
|
|
// If no selection exists, create a new link from scratch.
|
|
|
|
if ( ed.selection.isCollapsed() ) {
|
2010-11-16 05:11:41 +00:00
|
|
|
el = ed.dom.create('a', { href: "#mce_temp_url#" }, defaultContent);
|
2010-11-15 18:42:49 +00:00
|
|
|
ed.selection.setNode(el);
|
|
|
|
// If a selection exists, wrap it in a link.
|
|
|
|
} else {
|
|
|
|
tinyMCEPopup.execCommand("CreateLink", 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ed.dom.setAttribs(e, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't move caret if selection was image
|
|
|
|
if (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') {
|
|
|
|
ed.focus();
|
|
|
|
ed.selection.select(e);
|
|
|
|
ed.selection.collapse(0);
|
|
|
|
tinyMCEPopup.storeSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
tinyMCEPopup.execCommand("mceEndUndoLevel");
|
|
|
|
tinyMCEPopup.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
selectInternalLink : function() {
|
|
|
|
var t = $(this);
|
|
|
|
if ( t.hasClass('unselectable') )
|
|
|
|
return;
|
|
|
|
t.siblings('.selected').removeClass('selected');
|
|
|
|
t.addClass('selected');
|
2010-11-15 23:48:24 +00:00
|
|
|
inputs.url.val( t.children('.item-permalink').val() );
|
|
|
|
inputs.title.val( t.children('.item-title').text() );
|
2010-11-15 18:42:49 +00:00
|
|
|
},
|
|
|
|
|
2010-11-16 05:11:41 +00:00
|
|
|
maybeLoadRiver : function() {
|
|
|
|
var t = $(this),
|
|
|
|
ul = t.children('ul'),
|
|
|
|
river = t.data('river'),
|
2010-11-16 18:30:34 +00:00
|
|
|
bottom = t.scrollTop() + t.height();
|
2010-11-15 18:42:49 +00:00
|
|
|
|
2010-11-16 18:30:34 +00:00
|
|
|
if ( bottom != ul.height() || river.active || river.allLoaded )
|
2010-11-16 05:11:41 +00:00
|
|
|
return;
|
2010-11-15 18:42:49 +00:00
|
|
|
|
2010-11-16 18:30:34 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
var newTop = t.scrollTop(),
|
|
|
|
newBottom = newTop + t.height(),
|
|
|
|
waiting = t.find('.river-waiting');
|
|
|
|
|
|
|
|
if ( bottom != newBottom || newBottom != ul.height() || river.active || river.allLoaded )
|
|
|
|
return;
|
|
|
|
|
|
|
|
river.active = true;
|
|
|
|
waiting.show();
|
|
|
|
t.scrollTop( newTop + waiting.outerHeight() );
|
|
|
|
|
|
|
|
wpLink.linkAJAX( t, { page : river.page }, function(r) {
|
|
|
|
river.page++;
|
|
|
|
river.active = false;
|
|
|
|
river.allLoaded = !r;
|
|
|
|
waiting.hide();
|
|
|
|
}, {
|
|
|
|
append : true,
|
|
|
|
delay : wpLink.minRiverAJAXDuration
|
|
|
|
});
|
|
|
|
}, wpLink.timeToTriggerRiver );
|
2010-11-15 18:42:49 +00:00
|
|
|
},
|
|
|
|
searchInternalLinks : function() {
|
2010-11-15 23:48:24 +00:00
|
|
|
var t = $(this), waiting,
|
|
|
|
title = t.val();
|
|
|
|
|
2010-11-16 05:11:41 +00:00
|
|
|
if ( title.length > 2 ) {
|
2010-11-15 23:48:24 +00:00
|
|
|
results.recent.hide();
|
|
|
|
results.search.show();
|
2010-11-16 05:11:41 +00:00
|
|
|
|
|
|
|
// Don't search if the keypress didn't change the title.
|
|
|
|
if ( wpLink.lastSearch == title )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wpLink.lastSearch = title;
|
2010-11-15 18:42:49 +00:00
|
|
|
waiting = t.siblings('img.waiting').show();
|
2010-11-16 05:11:41 +00:00
|
|
|
|
|
|
|
results.search.data('river', wpLink.riverDefaults() );
|
|
|
|
results.search.scrollTop(0);
|
2010-11-15 23:48:24 +00:00
|
|
|
wpLink.linkAJAX( results.search, { title : title }, function(){ waiting.hide(); });
|
|
|
|
} else {
|
|
|
|
results.search.hide();
|
|
|
|
results.recent.show();
|
|
|
|
}
|
2010-11-15 18:42:49 +00:00
|
|
|
},
|
|
|
|
|
2010-11-16 18:30:34 +00:00
|
|
|
linkAJAX : function( $panel, params, callback, opts ) {
|
|
|
|
var response;
|
|
|
|
opts = opts || {};
|
|
|
|
|
2010-11-15 23:48:24 +00:00
|
|
|
if ( ! $panel.hasClass('query-results') )
|
|
|
|
$panel = $panel.parents('.query-results');
|
|
|
|
|
|
|
|
if ( ! $panel.length )
|
|
|
|
return;
|
|
|
|
|
2010-11-16 18:30:34 +00:00
|
|
|
response = wpLink.delayedCallback( function( results ) {
|
|
|
|
wpLink.processAJAXResponse( $panel, results, callback, opts );
|
|
|
|
}, opts.delay );
|
|
|
|
|
2010-11-15 18:42:49 +00:00
|
|
|
$.post( ajaxurl, $.extend({
|
2010-11-15 23:48:24 +00:00
|
|
|
action : 'wp-link-ajax'
|
2010-11-16 18:30:34 +00:00
|
|
|
}, params ), response, "json" );
|
|
|
|
},
|
|
|
|
|
|
|
|
processAJAXResponse: function( $panel, results, callback, opts ) {
|
|
|
|
var list = '';
|
|
|
|
|
|
|
|
if ( !results ) {
|
|
|
|
if ( !opts.append ) {
|
|
|
|
list += '<li class="no-matches-found unselectable"><span class="item-title"><em>'
|
|
|
|
+ wpLinkL10n.noMatchesFound
|
|
|
|
+ '</em></span></li>';
|
2010-11-16 05:11:41 +00:00
|
|
|
}
|
2010-11-16 18:30:34 +00:00
|
|
|
} else {
|
|
|
|
$.each( results, function() {
|
|
|
|
list += '<li><input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
|
|
|
|
list += '<span class="item-title">';
|
|
|
|
list += this['title'] ? this['title'] : '<em>'+ wpLinkL10n.untitled + '</em>';
|
|
|
|
list += '</span><span class="item-info">' + this['info'] + '</span></li>';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set results
|
|
|
|
$panel.children('ul')[ opts.append ? 'append' : 'html' ]( list );
|
|
|
|
|
|
|
|
// Run callback
|
|
|
|
if ( callback )
|
|
|
|
callback( results );
|
|
|
|
},
|
|
|
|
|
|
|
|
delayedCallback : function( func, delay ) {
|
|
|
|
var timeoutTriggered, funcTriggered, funcArgs, funcContext;
|
|
|
|
|
|
|
|
if ( ! delay )
|
|
|
|
return func;
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
if ( funcTriggered )
|
|
|
|
return func.apply( funcContext, funcArgs );
|
|
|
|
// Otherwise, wait.
|
|
|
|
timeoutTriggered = true;
|
|
|
|
}, delay);
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
if ( timeoutTriggered )
|
|
|
|
return func.apply( this, arguments );
|
|
|
|
// Otherwise, wait.
|
|
|
|
funcArgs = arguments;
|
|
|
|
funcContext = this;
|
|
|
|
funcTriggered = true;
|
|
|
|
};
|
2010-11-15 18:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready( wpLink.init );
|
|
|
|
})(jQuery);
|