From c09b2e5b227f49f74eeb1268aadb30e0fac1321b Mon Sep 17 00:00:00 2001 From: nacin Date: Tue, 16 Nov 2010 18:30:34 +0000 Subject: [PATCH] Improve internal linking river feedback. props koopersmith, see #11420. git-svn-id: http://svn.automattic.com/wordpress/trunk@16407 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../tinymce/plugins/wplink/js/wplink.dev.js | 120 ++++++++++++------ .../js/tinymce/plugins/wplink/js/wplink.js | 2 +- wp-includes/js/tinymce/wp-mce-link.php | 4 +- 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/wp-includes/js/tinymce/plugins/wplink/js/wplink.dev.js b/wp-includes/js/tinymce/plugins/wplink/js/wplink.dev.js index 4ca58323aa..e2a4389897 100644 --- a/wp-includes/js/tinymce/plugins/wplink/js/wplink.dev.js +++ b/wp-includes/js/tinymce/plugins/wplink/js/wplink.dev.js @@ -1,6 +1,8 @@ (function($){ var inputs = {}, results = {}, ed, wpLink = { + timeToTriggerRiver: 150, + minRiverAJAXDuration: 200, lastSearch: '', riverDefaults: function() { return { @@ -130,22 +132,34 @@ var t = $(this), ul = t.children('ul'), river = t.data('river'), - waiting = t.find('.river-waiting'); + bottom = t.scrollTop() + t.height(); - if( t.scrollTop() + t.height() != ul.height() || river.active || river.allLoaded ) + if ( bottom != ul.height() || river.active || river.allLoaded ) return; - river.active = true; - waiting.show(); - - wpLink.linkAJAX( t, { page : river.page }, function(r) { - river.page++; - river.active = false; - river.allLoaded = !r; - waiting.hide(); - }, true); + 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 ); }, - searchInternalLinks : function() { var t = $(this), waiting, title = t.val(); @@ -170,40 +184,72 @@ } }, - linkAJAX : function( $panel, params, callback, append ) { + linkAJAX : function( $panel, params, callback, opts ) { + var response; + opts = opts || {}; + if ( ! $panel.hasClass('query-results') ) $panel = $panel.parents('.query-results'); if ( ! $panel.length ) return; + response = wpLink.delayedCallback( function( results ) { + wpLink.processAJAXResponse( $panel, results, callback, opts ); + }, opts.delay ); + $.post( ajaxurl, $.extend({ action : 'wp-link-ajax' - }, params ), function( results ) { - var list = ''; - - if ( !results ) { - if ( !append ) { - list += '
  • ' - + wpLinkL10n.noMatchesFound - + '
  • '; - } - } else { - $.each( results, function() { - list += '
  • '; - list += ''; - list += this['title'] ? this['title'] : ''+ wpLinkL10n.untitled + ''; - list += '' + this['info'] + '
  • '; - }); + }, params ), response, "json" ); + }, + + processAJAXResponse: function( $panel, results, callback, opts ) { + var list = ''; + + if ( !results ) { + if ( !opts.append ) { + list += '
  • ' + + wpLinkL10n.noMatchesFound + + '
  • '; } - - // Set results - $panel.children('ul')[ append ? 'append' : 'html' ]( list ); - - // Run callback - if ( callback ) - callback( results ); - }, "json" ); + } else { + $.each( results, function() { + list += '
  • '; + list += ''; + list += this['title'] ? this['title'] : ''+ wpLinkL10n.untitled + ''; + list += '' + this['info'] + '
  • '; + }); + } + + // 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; + }; } } diff --git a/wp-includes/js/tinymce/plugins/wplink/js/wplink.js b/wp-includes/js/tinymce/plugins/wplink/js/wplink.js index 5e4b559b3d..2098ff7fa5 100644 --- a/wp-includes/js/tinymce/plugins/wplink/js/wplink.js +++ b/wp-includes/js/tinymce/plugins/wplink/js/wplink.js @@ -1 +1 @@ -(function(d){var a={},c={},b,e={lastSearch:"",riverDefaults:function(){return{page:2,allLoaded:false,active:false}},init:function(){var f;b=tinyMCEPopup.editor;a.url=d("#url-field");a.title=d("#link-title-field");a.openInNewTab=d("#link-target-checkbox");a.search=d("#search-field");c.search=d("#search-results");c.recent=d("#most-recent-results");c.search.data("river",e.riverDefaults());c.recent.data("river",e.riverDefaults());d("#wp-update").click(e.update);d("#wp-cancel").click(function(){tinyMCEPopup.close()});d(".query-results").delegate("li","click",e.selectInternalLink);d(".query-results").scroll(e.maybeLoadRiver);a.search.keyup(e.searchInternalLinks);if(f=b.dom.getParent(b.selection.getNode(),"A")){a.url.val(f.href);a.title.val(b.dom.getAttrib(f,"title"));if("_blank"==b.dom.getAttrib(f,"target")){a.openInNewTab.attr("checked","checked")}}a.url.focus()},update:function(){var j,g=tinyMCEPopup.editor,h={href:a.url.val(),title:a.title.val(),target:a.openInNewTab.attr("checked")?"_blank":""},k,f,i=h.title?h.title:h.href;tinyMCEPopup.restoreSelection();k=g.dom.getParent(g.selection.getNode(),"A");if(!h.href){if(g.selection.isCollapsed()){tinyMCEPopup.close();return}else{if(k){tinyMCEPopup.execCommand("mceBeginUndoLevel");f=g.selection.getBookmark();g.dom.remove(k,1);g.selection.moveToBookmark(f);tinyMCEPopup.execCommand("mceEndUndoLevel");tinyMCEPopup.close();return}}}tinyMCEPopup.execCommand("mceBeginUndoLevel");if(k==null){g.getDoc().execCommand("unlink",false,null);if(g.selection.isCollapsed()){j=g.dom.create("a",{href:"#mce_temp_url#"},i);g.selection.setNode(j)}else{tinyMCEPopup.execCommand("CreateLink",false,"#mce_temp_url#",{skip_undo:1})}tinymce.each(g.dom.select("a"),function(l){if(g.dom.getAttrib(l,"href")=="#mce_temp_url#"){k=l;g.dom.setAttribs(k,h)}})}else{g.dom.setAttribs(k,h)}if(k.childNodes.length!=1||k.firstChild.nodeName!="IMG"){g.focus();g.selection.select(k);g.selection.collapse(0);tinyMCEPopup.storeSelection()}tinyMCEPopup.execCommand("mceEndUndoLevel");tinyMCEPopup.close()},selectInternalLink:function(){var f=d(this);if(f.hasClass("unselectable")){return}f.siblings(".selected").removeClass("selected");f.addClass("selected");a.url.val(f.children(".item-permalink").val());a.title.val(f.children(".item-title").text())},maybeLoadRiver:function(){var g=d(this),f=g.children("ul"),h=g.data("river"),i=g.find(".river-waiting");if(g.scrollTop()+g.height()!=f.height()||h.active||h.allLoaded){return}h.active=true;i.show();e.linkAJAX(g,{page:h.page},function(j){h.page++;h.active=false;h.allLoaded=!j;i.hide()},true)},searchInternalLinks:function(){var f=d(this),h,g=f.val();if(g.length>2){c.recent.hide();c.search.show();if(e.lastSearch==g){return}e.lastSearch=g;h=f.siblings("img.waiting").show();c.search.data("river",e.riverDefaults());c.search.scrollTop(0);e.linkAJAX(c.search,{title:g},function(){h.hide()})}else{c.search.hide();c.recent.show()}},linkAJAX:function(h,g,i,f){if(!h.hasClass("query-results")){h=h.parents(".query-results")}if(!h.length){return}d.post(ajaxurl,d.extend({action:"wp-link-ajax"},g),function(j){var k="";if(!j){if(!f){k+='
  • '+wpLinkL10n.noMatchesFound+"
  • "}}else{d.each(j,function(){k+='
  • ';k+='';k+=this["title"]?this["title"]:""+wpLinkL10n.untitled+"";k+=''+this["info"]+"
  • "})}h.children("ul")[f?"append":"html"](k);if(i){i(j)}},"json")}};d(document).ready(e.init)})(jQuery); \ No newline at end of file +(function(d){var a={},c={},b,e={timeToTriggerRiver:150,minRiverAJAXDuration:200,lastSearch:"",riverDefaults:function(){return{page:2,allLoaded:false,active:false}},init:function(){var f;b=tinyMCEPopup.editor;a.url=d("#url-field");a.title=d("#link-title-field");a.openInNewTab=d("#link-target-checkbox");a.search=d("#search-field");c.search=d("#search-results");c.recent=d("#most-recent-results");c.search.data("river",e.riverDefaults());c.recent.data("river",e.riverDefaults());d("#wp-update").click(e.update);d("#wp-cancel").click(function(){tinyMCEPopup.close()});d(".query-results").delegate("li","click",e.selectInternalLink);d(".query-results").scroll(e.maybeLoadRiver);a.search.keyup(e.searchInternalLinks);if(f=b.dom.getParent(b.selection.getNode(),"A")){a.url.val(f.href);a.title.val(b.dom.getAttrib(f,"title"));if("_blank"==b.dom.getAttrib(f,"target")){a.openInNewTab.attr("checked","checked")}}a.url.focus()},update:function(){var j,g=tinyMCEPopup.editor,h={href:a.url.val(),title:a.title.val(),target:a.openInNewTab.attr("checked")?"_blank":""},k,f,i=h.title?h.title:h.href;tinyMCEPopup.restoreSelection();k=g.dom.getParent(g.selection.getNode(),"A");if(!h.href){if(g.selection.isCollapsed()){tinyMCEPopup.close();return}else{if(k){tinyMCEPopup.execCommand("mceBeginUndoLevel");f=g.selection.getBookmark();g.dom.remove(k,1);g.selection.moveToBookmark(f);tinyMCEPopup.execCommand("mceEndUndoLevel");tinyMCEPopup.close();return}}}tinyMCEPopup.execCommand("mceBeginUndoLevel");if(k==null){g.getDoc().execCommand("unlink",false,null);if(g.selection.isCollapsed()){j=g.dom.create("a",{href:"#mce_temp_url#"},i);g.selection.setNode(j)}else{tinyMCEPopup.execCommand("CreateLink",false,"#mce_temp_url#",{skip_undo:1})}tinymce.each(g.dom.select("a"),function(l){if(g.dom.getAttrib(l,"href")=="#mce_temp_url#"){k=l;g.dom.setAttribs(k,h)}})}else{g.dom.setAttribs(k,h)}if(k.childNodes.length!=1||k.firstChild.nodeName!="IMG"){g.focus();g.selection.select(k);g.selection.collapse(0);tinyMCEPopup.storeSelection()}tinyMCEPopup.execCommand("mceEndUndoLevel");tinyMCEPopup.close()},selectInternalLink:function(){var f=d(this);if(f.hasClass("unselectable")){return}f.siblings(".selected").removeClass("selected");f.addClass("selected");a.url.val(f.children(".item-permalink").val());a.title.val(f.children(".item-title").text())},maybeLoadRiver:function(){var h=d(this),g=h.children("ul"),i=h.data("river"),f=h.scrollTop()+h.height();if(f!=g.height()||i.active||i.allLoaded){return}setTimeout(function(){var j=h.scrollTop(),k=j+h.height(),l=h.find(".river-waiting");if(f!=k||k!=g.height()||i.active||i.allLoaded){return}i.active=true;l.show();h.scrollTop(j+l.outerHeight());e.linkAJAX(h,{page:i.page},function(m){i.page++;i.active=false;i.allLoaded=!m;l.hide()},{append:true,delay:e.minRiverAJAXDuration})},e.timeToTriggerRiver)},searchInternalLinks:function(){var f=d(this),h,g=f.val();if(g.length>2){c.recent.hide();c.search.show();if(e.lastSearch==g){return}e.lastSearch=g;h=f.siblings("img.waiting").show();c.search.data("river",e.riverDefaults());c.search.scrollTop(0);e.linkAJAX(c.search,{title:g},function(){h.hide()})}else{c.search.hide();c.recent.show()}},linkAJAX:function(i,h,j,g){var f;g=g||{};if(!i.hasClass("query-results")){i=i.parents(".query-results")}if(!i.length){return}f=e.delayedCallback(function(k){e.processAJAXResponse(i,k,j,g)},g.delay);d.post(ajaxurl,d.extend({action:"wp-link-ajax"},h),f,"json")},processAJAXResponse:function(i,f,j,g){var h="";if(!f){if(!g.append){h+='
  • '+wpLinkL10n.noMatchesFound+"
  • "}}else{d.each(f,function(){h+='
  • ';h+='';h+=this["title"]?this["title"]:""+wpLinkL10n.untitled+"";h+=''+this["info"]+"
  • "})}i.children("ul")[g.append?"append":"html"](h);if(j){j(f)}},delayedCallback:function(h,f){var k,j,i,g;if(!f){return h}setTimeout(function(){if(j){return h.apply(g,i)}k=true},f);return function(){if(k){return h.apply(this,arguments)}i=arguments;g=this;j=true}}};d(document).ready(e.init)})(jQuery); \ No newline at end of file diff --git a/wp-includes/js/tinymce/wp-mce-link.php b/wp-includes/js/tinymce/wp-mce-link.php index 6c830d52fc..3eed49db6d 100644 --- a/wp-includes/js/tinymce/wp-mce-link.php +++ b/wp-includes/js/tinymce/wp-mce-link.php @@ -23,7 +23,7 @@ var ajaxurl = '',