_binary From 8bf1ebf61b9efc5a743726b052e931190475a7db Mon Sep 17 00:00:00 2001
From: Sandhya Vissapragada <sandyvissa@gmail.com>
Date: Thu, 1 Nov 2012 23:05:59 -0700
Subject: [PATCH] git add scripts/suggest.js git add views/search_view.php

---
 scripts/suggest.js    |  171 +++++++++++++++++++++++++++++++++++++++++++------
 views/search_view.php |    2 +-
 2 files changed, 153 insertions(+), 20 deletions(-)

diff --git a/scripts/suggest.js b/scripts/suggest.js
index dc5275f..c3a0e3c 100644
--- a/scripts/suggest.js
+++ b/scripts/suggest.js
@@ -51,7 +51,6 @@ FONT_HEIGHT = 24;
  * what the trie object loaded says in loadTrie
  */
 END_OF_TERM_MARKER = " ";
-
 /**
  * Steps to follow every time a key is up from the user end
  * Handles up/dowm arrow keys
@@ -60,6 +59,8 @@ END_OF_TERM_MARKER = " ";
  * @return String text_field Current value from the search box
  *
  */
+
+
 function onTypeTerm(event, text_field) 
 {
     var key_code_pressed;
@@ -69,7 +70,19 @@ function onTypeTerm(event, text_field)
     var suggest_dropdown = elt("suggest-dropdown");
     var scroll_pos = 0;
     var tmp_pos = 0;
-
+    localeTerms = new Object();
+    localTermsPresent = false;
+    localSuggest = true;
+    search_list_array = new Object();
+ 
+    //To find the length of an associative array
+    Object.size = function(obj) {
+    var size = 0, key;
+    for (key in obj) {
+        if (obj.hasOwnProperty(key)) size++;
+    }
+    return size;
+    };
     concat_term = "";
     if(window.event) { // IE8 and earlier
         key_code_pressed = event.keyCode;
@@ -90,7 +103,41 @@ function onTypeTerm(event, text_field)
     if (key_code_pressed != KeyCodes.DOWN_ARROW && 
             key_code_pressed != KeyCodes.UP_ARROW) {
         search_list = "";
-        termSuggest(dictionary, input_term);
+
+    // First search the local storage to fetch the suggestions
+    if (localStorage) {
+         if (localStorage[locale] != null) {
+            split = localStorage[locale].split(" ");
+            localeTerms = JSON.parse(split[1]);
+            local_dict = JSON.parse(localStorage[locale].split(" ", 1));
+            if (local_dict != null) {
+                termSuggest(local_dict, input_term);
+                localTermsPresent = true;
+            }
+         }
+         else  count =0; 
+    }
+    var sortedLocal = SortLocalTerms();
+    if (Object.size(search_list_array) > 0) {
+        search_list = "";         
+         for(var i = 0; i < sortedLocal.length ; i++) {         
+            var splitArray = sortedLocal[i].split('*');     
+            
+            if(search_list_array[splitArray[1]] != null)
+            search_list += search_list_array[splitArray[1]];
+         }
+        
+     }
+     
+     // Now search the actual dictionary trie
+     localSuggest = false;
+     termSuggest(dictionary, input_term);
+
+     // insert nbsp of the number of suggestions are less than MAX_DISPLAY
+     short_max = MAX_DISPLAY - count;
+     for(var i = 0; i < short_max; i++) {
+        search_list += "<li><span class='unselected'>&nbsp;</span></li>";
+    }
         if(count < 1) {
             search_list = "";
         }
@@ -148,6 +195,90 @@ function onTypeTerm(event, text_field)
 }
 
 /**
+ * To update the local storage with the previous query terms and
+ * create a trie on those terms
+ *
+ * @param None * 
+ */
+function updateLocalStorage() 
+{
+     var trie_to_store;
+     trie_storage = {};
+     var store_term = elt("query-field").value;
+     var freq, k=0;
+     var sortedLocaleTerms = new Array();
+     if(localStorage) {                
+         if (localeTerms[store_term] == null) {
+            localeTerms[store_term] = 1;
+         } else {
+            freq = parseInt(localeTerms[store_term]);
+            freq++;
+            localeTerms[store_term] = freq;             
+         }     
+         for(var key in localeTerms) {
+            sortedLocaleTerms[k] = key;
+            k++;
+         }        
+        sortedLocaleTerms.sort();
+
+        // Build the trie
+        for(var i=0; i< sortedLocaleTerms.length; i++) {
+        var trie_word = sortedLocaleTerms[i];        
+        var letters = trie_word.split(""),cur = trie_storage;
+    	for (var j=0;j<letters.length;j++) {
+	        var letter = encode(letters[j]);
+            var pos = cur[ letter ];
+            if (pos == null) {        
+                if (j === letters.length - 1) { 
+                    cur = cur[ letter ] = {'$' : '$'};
+                } else {
+                    cur = cur[ letter ] = {}; 
+                }
+            } else if (pos === 0) {
+		        cur = cur[ letter ] = { '$' : '$' };
+            } else {
+		        cur = cur[ letter ];
+		    }
+        }            	
+    }
+    }
+    trie_to_store = JSON.stringify( trie_storage );
+    localStorage.setItem(locale,trie_to_store + " " + JSON.stringify(localeTerms)); 
+}
+
+/**
+ * Sort the local storage words based of number of times they are queried
+ */
+
+function SortLocalTerms()
+{
+    var localStorageArray = new Array(); 
+    if(Object.size(localeTerms) > 0) {
+        var j=0;
+        for(var key in localeTerms) {
+            localStorageArray[j] = localeTerms[key] +"*"+  key;
+            j++;
+        }
+    }
+    localStorageArray.sort(sortFunction);
+    localStorageArray.reverse();
+    return localStorageArray;
+}
+
+/**
+ * Sort function to sort the numbers in ascending order
+ */
+
+function sortFunction(a, b)
+{
+    var splitArray1 = a.split('*');
+    var splitArray2 = b.split('*'); 
+    var val1 = parseInt(splitArray1[0]);
+    var val2 = parseInt(splitArray2[0]);
+    return (val1 - val2);
+}
+
+/**
  * To select an suggest value while up/down arrow keys are being used
  * and place in the search box
  *
@@ -196,21 +327,24 @@ function getTrieTerms(trie_array, parent_word, highlighted_word)
                 getTrieTerms(trie_array[key], parent_word + key, 
                         highlighted_word + key);
             } else {
-                search_terms = concat_term.trim() + " " + decode(parent_word);
-                search_terms = search_terms.trim();
-                highlighted_terms = concat_term.trim() + " " 
-                    + decode(highlighted_word) + "</b>";
-                search_list += "<li><span id='term" +count+
+                if (localTermsPresent == false || (localeTerms[decode(parent_word)] == null && localTermsPresent == true)) {
+                    search_terms = concat_term.trim() + " " + decode(parent_word);
+                    search_terms = search_terms.trim();
+                    highlighted_terms = concat_term.trim() + " " + decode(highlighted_word) + "</b>";
+                    search_string =  "<li><span id='term" +count+
                     "' class='unselected' onclick = 'void(0)' " +
                     "title='"+search_terms+"' " +
                     "onmouseover='setSelectedTerm(\""+count+"\",\"selected\")'" +
                     "onmouseout='setSelectedTerm(\""+count+"\",\"unselected\")'" +
                     "onmouseup='termClick(\""+search_terms+"\",this.id)'"+
                     ">" + highlighted_terms + "</span></li>";
-                count++;
+                    search_list += search_string;           
+                    search_list_array[decode(parent_word)] = search_string;
+                    count++;                     
+                }
+                }
             }
         }
-    }
 }
 
 /**
@@ -230,7 +364,7 @@ function exist(trie_array, term)
         tmp = getUnicodeCharAndNextOffset(term, i);
         if(tmp == false) return false;
         next_char = tmp[0];
-        i = tmp[1];
+        i = tmp[1]; 
         enc_char = encode(next_char);
         trie_array = trie_array[enc_char];
         if(trie_array == null) {
@@ -252,11 +386,14 @@ function exist(trie_array, term)
 function termSuggest(trie_array, term)
 {
     last_word = false;
-    count = 0;
-    search_list = "";
+    if (localSuggest == true) {
+        count = 0;
+        search_list = ""; 
+    
+    }
     // For US english ignore the case
     if(locale == 'en-US') {
-    term = term.toLowerCase();
+        term = term.toLowerCase();
     }
     var tmp;
     if(trie_array == null) {
@@ -275,10 +412,6 @@ function termSuggest(trie_array, term)
         trie_array = trie_array[term];
     }
     getTrieTerms(trie_array, term, term + "<b>");
-    short_max = MAX_DISPLAY - count;
-    for(var i = 0; i < short_max; i++) {
-        search_list += "<li><span class='unselected'>&nbsp;</span></li>";
-    }
 }
 
 /* wrappers to save typing */
@@ -291,7 +424,7 @@ function encode(str)
 {
     str = encodeURIComponent(str);
     str = str.replace(/\'/g, '%27'); // encodeURIComponent doesnt convert '
-    return str;
+    return str;    
 }
 
 /**
diff --git a/views/search_view.php b/views/search_view.php
index ce7f432..f6935f5 100755
--- a/views/search_view.php
+++ b/views/search_view.php
@@ -112,7 +112,7 @@ class SearchView extends View implements CrawlConstants
         ?>
 
         <div class="searchbox">
-            <form id="search-form" method="get" action='?'>
+            <form id="search-form" method="get" action='?' onsubmit="updateLocalStorage()">
             <p>
             <?php if(isset($data["SUBSEARCH"]) && $data["SUBSEARCH"] != "") { ?>
             <input type="hidden" name="s" value="<?php 
-- 
1.7.9.4

