share/collection.js: Improve the search suggestion behavior.
authorFlorian Forster <ff@octo.it>
Mon, 21 Jun 2010 13:29:23 +0000 (15:29 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 21 Jun 2010 13:29:23 +0000 (15:29 +0200)
share/collection.js
share/style.css

index 026d434..9968452 100644 (file)
@@ -1,6 +1,8 @@
 function format_instance(inst)
 {
-  return ("<li class=\"instance\"><a href=\"" + location.pathname + "?action=graph;" + inst.params + "\">" + inst.description + "</a></li>");
+  return ("<li class=\"instance\"><a href=\"" + location.pathname
+      + "?action=show_graph;" + inst.params + "\">" + inst.description
+      + "</a></li>");
 }
 
 function format_instance_list(instances)
@@ -24,22 +26,57 @@ function format_graph(graph)
   return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
 }
 
+function update_search_suggestions ()
+{
+  var term = $("#search-input").val ();
+  if (term.length < 2)
+  {
+    $("#search-suggest").hide ();
+    return (true);
+  }
+
+  $("#search-suggest").show ();
+  $.getJSON ("collection.fcgi",
+    { "action": "search_json", "q": term},
+    function(data)
+    {
+      var i;
+      $("#search-suggest").html ("");
+      for (i = 0; i < data.length; i++)
+      {
+        var graph = data[i];
+        $("#search-suggest").append (format_graph (graph));
+      }
+    }
+  );
+} /* update_search_suggestions */
+
 $(document).ready(function() {
-    $("#search-input").keyup (function()
+    /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
+    $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
+    $("#search-suggest").hide ();
+
+    $("#search-input").blur (function()
+    {
+      $("#search-suggest").hide ();
+    });
+
+    $("#search-input").focus (function()
     {
       var term = $("#search-input").val ();
-      $.getJSON ("collection.fcgi",
-        { "action": "search_json", "q": term},
-        function(data)
-        {
-          var i;
-          $("#search-output").html ("");
-          for (i = 0; i < data.length; i++)
-          {
-            var graph = data[i];
-            $("#search-output").append (format_graph (graph));
-          }
-        });
+      if (term.length < 2)
+      {
+        $("#search-suggest").hide ();
+      }
+      else
+      {
+        $("#search-suggest").show ();
+      }
+    });
+
+    $("#search-input").keyup (function()
+    {
+      update_search_suggestions ();
     });
 });
 
index d5b1161..740ae33 100644 (file)
@@ -35,13 +35,23 @@ body
        vertical-align: top;
 }
 
-ul.graph_list
+#search-form
+{
+       position: relative;
+}
+
+#search-suggest
+{
+       position: absolute;
+}
+
+#search-output ul.graph_list
 {
        margin: 0;
        padding: 1ex;
 }
 
-li.graph
+#search-output li.graph
 {
        display: block;
        border: 1px solid gray;
@@ -50,7 +60,7 @@ li.graph
        background-color: rgb(245,245,255);
 }
 
-ul.instance_list
+#search-output ul.instance_list
 {
        margin: 0;
        padding: 1ex;
@@ -59,7 +69,7 @@ ul.instance_list
        background-color: rgb(252,252,252);
 }
 
-li.instance
+#search-output li.instance
 {
        display: block;