_binary From 06c7ad829f6d9bcea44f84239618cf19c1561590 Mon Sep 17 00:00:00 2001
From: Forrest Sun <sxh19911230@gmail.com>
Date: Wed, 25 Sep 2019 01:17:57 -0700
Subject: [PATCH] Added Chinese suggestion words. Fixed a bug where the js will
 not encode if there is only one char in the search bar. Now the suggestion
 words are frequency ordered instead of lexicon ordered. r=chris

---
 src/scripts/suggest.js | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/scripts/suggest.js b/src/scripts/suggest.js
index 76b5199c..3b52ab53 100644
--- a/src/scripts/suggest.js
+++ b/src/scripts/suggest.js
@@ -191,6 +191,16 @@ function onTypeTerm(event, text_field)
         }
         suggest_dropdown.scrollTop =  0;
         suggest_results.innerHTML = search_list;
+        //sort the list
+        var count_tmp=0;
+        Array.from(suggest_results.getElementsByTagName("LI"))
+        .sort((a, b) => b.getAttribute("data-frequency")
+            -a.getAttribute("data-frequency"))
+        .forEach(li => {
+            li.firstChild.id=("term"+count_tmp);
+            count_tmp+=1;
+            suggest_results.appendChild(li);
+        });
         cursor_pos = -1;
         num_items = count;
         if (num_items == 0 || search_list == "") {
@@ -513,13 +523,13 @@ function getTrieTerms(trie_array, parent_word, highlighted_word)
                     search_terms = search_terms.trim();
                     highlighted_terms = concat_term.trim() + " " +
                     decode(highlighted_word) + "</b>";
-                    search_list +=  "<li><span id='term" +count+
-                        "' class='unselected' onclick = 'void(0)' " +
+                    search_list +=  "<li data-frequency="+trie_array[key]+
+                        "><span class='unselected' onclick = 'void(0)' " +
                         "title='"+search_terms+"' " +
-                        "onmouseover='setSelectedTerm(\""
-                        +count+"\",\"selected\")'" +
-                        "onmouseout='setSelectedTerm(\""+count
-                        +"\",\"unselected\")'" +
+                        "onmouseover='setSelectedTerm(this.id.substr(4),"+
+                        "\"selected\")'" +
+                        "onmouseout='setSelectedTerm(this.id.substr(4),"+
+                        "\"unselected\")'" +
                         "onmouseup='termClick(\""+search_terms
                         +"\",this.id)'"+
                         ">" + highlighted_terms + "</span></li>";
@@ -593,13 +603,11 @@ function termSuggest(trie_array, term)
     if (trie_array == null) {
         return false;
     }
-    if ((term.length) > 1) {
+    if ((term.length) > 0) {
         trie_array = exist(trie_array, term);
-        if (trie_array == false) {
-            return false;
-        }
-    } else {
-        trie_array = trie_array[term];
+    }
+    else {
+        return false;
     }
     getTrieTerms(trie_array, term, term + "<b>");
 }
@@ -763,4 +771,4 @@ if (ip_field != null) {
                 onTypeTerm(e,ip_field);
                 }, 0);
     }
-}
+}
\ No newline at end of file
-- 
2.21.0.windows.1

