[Bps-public-commit] RT-Extension-rt_cpan_org branch, rt4, updated. c09522db1b9f04f96fd57cfc48d7b477bf74bd9c

Thomas Sibley trs at bestpractical.com
Fri Mar 29 21:24:36 EDT 2013


The branch, rt4 has been updated
       via  c09522db1b9f04f96fd57cfc48d7b477bf74bd9c (commit)
      from  f542b92c653dfbbeddb261809d399b2e36ae0696 (commit)

Summary of changes:
 html/Helpers/Autocomplete/Queues |  2 +-
 html/NoAuth/js/rt.cpan.org.js    | 73 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 69 insertions(+), 6 deletions(-)

- Log -----------------------------------------------------------------
commit c09522db1b9f04f96fd57cfc48d7b477bf74bd9c
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Fri Mar 29 18:19:07 2013 -0700

    Adaptively autocomplete distribution or module names when finding a queue
    
    If the user types a colon, we assume they're looking for a module name
    and switch to mapping modules to distributions via api.metacpan.org.
    
    If the user hasn't yet typed a colon, we preserve the long-standing
    expectation of distribution names and autocomplete against rt.cpan.org
    queue names.
    
    This means that typing "App::Prove" finds you Test-Harness and typing
    "LWP::U" finds you libwww-perl.  It also ensures that typing "libwww"
    autocompletes to libwww-perl for anyone familiar with rt.cpan.org's
    historical expectations.

diff --git a/html/Helpers/Autocomplete/Queues b/html/Helpers/Autocomplete/Queues
index afaeff4..6637ec9 100644
--- a/html/Helpers/Autocomplete/Queues
+++ b/html/Helpers/Autocomplete/Queues
@@ -29,6 +29,6 @@ $queues->Limit(
 my @suggestions;
 while (my $q = $queues->Next) {
     next if $right and not $q->CurrentUserHasRight($right);
-    push @suggestions, $q->Name;
+    push @suggestions, { label => $q->Name, value => $q->Name };
 }
 </%INIT>
diff --git a/html/NoAuth/js/rt.cpan.org.js b/html/NoAuth/js/rt.cpan.org.js
index 0b1ac6c..b9a3d05 100644
--- a/html/NoAuth/js/rt.cpan.org.js
+++ b/html/NoAuth/js/rt.cpan.org.js
@@ -13,15 +13,15 @@ jQuery(function(){
 
     jQuery("input[data-autocomplete=Queues]").each(function() {
         var input = jQuery(this);
-        var opts  = {
-            source: <% RT->Config->Get('WebPath') |n,j%>
-                    + "/Helpers/Autocomplete/Queues?max=20",
+        var opts = {
             minLength: 2,
             delay: 100
         };
+        var rt_source = <% RT->Config->Get('WebPath') |n,j%>
+                      + "/Helpers/Autocomplete/Queues?max=20";
 
         if (input.attr("data-autocomplete-params") != null)
-            opts.source = opts.source + "&" + input.attr("data-autocomplete-params");
+            rt_source += "&" + input.attr("data-autocomplete-params");
 
         // Auto-submit once a queue is chosen
         if (input.attr("data-autocomplete-autosubmit")) {
@@ -31,7 +31,70 @@ jQuery(function(){
             };
         }
 
-        input.autocomplete(opts);
+        var current_request;
+        opts.source = function( request, response ) {
+            var this_request;
+
+            if (current_request)
+                current_request.abort();
+
+            if (/:/.test(request.term)) {
+                // Adaptive autocomplete!  If the term contains a colon, look
+                // for modules and map to distributions via metacpan.
+                this_request = current_request = jQuery.ajax({
+                    url: "http://api.metacpan.org/v0/search/autocomplete",
+                    dataType: "json",
+                    data: {
+                        q: request.term
+                    },
+                    success: function( data ) {
+                        if (!data || data.timed_out || !data.hits || this_request !== current_request)
+                            return;
+
+                        response( jQuery.map( data.hits.hits, function( item ) {
+                            return {
+                                label: item.fields.documentation,
+                                value: item.fields.distribution,
+                                module: true
+                            }
+                        }));
+                    }
+                });
+            } else {
+                // Otherwise, look for distributions (queues) on rt.cpan.org.
+                this_request = current_request = jQuery.ajax({
+                    url: rt_source,
+                    dataType: "json",
+                    data: {
+                        term: request.term
+                    },
+                    success: function( data ) {
+                        if (data && this_request === current_request)
+                            response(data);
+                    }
+                });
+            }
+        };
+
+        input.autocomplete(opts).data("autocomplete")._renderItem = function(ul, item) {
+            var rendered = jQuery("<a/>");
+
+            if (item.module) {
+                rendered.html(
+                      rendered.text(item.label).html()
+                    + "<small> in "
+                    + rendered.text(item.value).html()
+                    + "</small>"
+                );
+            } else {
+                rendered.text( item.label );
+            }
+
+            return jQuery("<li/>")
+                .data( "item.autocomplete", item )
+                .append( rendered )
+                .appendTo( ul );
+        };
     });
 
     // XXX TODO: Support users as well

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list