Added click-to-refine-search feature in search.js.
authorDavid Humphrey (:humph) <david.humphrey@senecac.on.ca>
Mon, 07 Feb 2011 22:38:15 -0500
changeset 36 7c4d0cd619e8
parent 35 a32498bf2edb
child 37 4d8b63c55e4c
push id27
push userdavid.humphrey@senecac.on.ca
push dateTue, 08 Feb 2011 03:43:40 +0000
Added click-to-refine-search feature in search.js.
server/static/search.js
--- a/server/static/search.js
+++ b/server/static/search.js
@@ -38,13 +38,47 @@ var results;
       sendmsg();
   }});
 
   if (location.hash) {
       $("#keywords").val(decodeURIComponent(location.hash.substring(1)));
       sendmsg();
   }
 
+  var findWord = (function() {
+    function isChar(c){
+      return 'A' <= c && c <= 'z';
+    }
+
+    return function(text, offset) {
+      if (!text || text.length <= offset) {
+        return;
+      }
+
+      var preoffset = offset;
+      while(preoffset && isChar(text[preoffset - 1])) {
+        preoffset--;
+      }
+
+      var postoffset = offset;
+      while(postoffset + 1 < text.length && isChar(text[postoffset + 1])) {
+        postoffset++;
+      }
+
+      return text.substring(preoffset, postoffset + 1) || '';
+    };
+  })();
+
+  $('body').bind('click', function() {
+    var s = window.getSelection();
+    if (s.anchorNode && s.anchorNode.nodeValue) {
+      var word = findWord(s.anchorNode.nodeValue, s.focusOffset);
+      $("#keywords").val(function(index, value) {
+        return word.length ? value + ' ' + word : value;
+      });
+    }
+  });
+
   function log(data){
     $("#matchingbugs").html(data);
   };
 
 });