OSDN Git Service

fix search for offline docs. again.
[android-x86/build.git] / tools / droiddoc / templates / assets / search_autocomplete.js
1 var gSelectedIndex = -1;
2 var gSelectedID = -1;
3 var gMatches = new Array();
4 var gLastText = "";
5 var ROW_COUNT = 30;
6 var gInitialized = false;
7 var DEFAULT_TEXT = "search developer docs";
8
9 function set_row_selected(row, selected)
10 {
11     var c1 = row.cells[0];
12   //  var c2 = row.cells[1];
13     if (selected) {
14         c1.className = "jd-autocomplete jd-selected";
15   //      c2.className = "jd-autocomplete jd-selected jd-linktype";
16     } else {
17         c1.className = "jd-autocomplete";
18   //      c2.className = "jd-autocomplete jd-linktype";
19     }
20 }
21
22 function set_row_values(toroot, row, match)
23 {
24     var link = row.cells[0].childNodes[0];
25     link.innerHTML = match.label;
26     link.href = toroot + match.link
27   //  row.cells[1].innerHTML = match.type;
28 }
29
30 function sync_selection_table(toroot)
31 {
32     var filtered = document.getElementById("search_filtered");
33     var r; //TR DOM object
34     var i; //TR iterator
35     gSelectedID = -1;
36
37     filtered.onmouseover = function() { 
38         if(gSelectedIndex >= 0) {
39           set_row_selected(this.rows[gSelectedIndex], false);
40           gSelectedIndex = -1;
41         }
42     }
43
44     //initialize the table; draw it for the first time (but not visible).
45     if (!gInitialized) {
46         for (i=0; i<ROW_COUNT; i++) {
47             var r = filtered.insertRow(-1);
48             var c1 = r.insertCell(-1);
49         //    var c2 = r.insertCell(-1);
50             c1.className = "jd-autocomplete";
51          //   c2.className = "jd-autocomplete jd-linktype";
52             var link = document.createElement("a");
53             c1.onmousedown = function() {
54                 window.location = this.firstChild.getAttribute("href");
55             }
56             c1.onmouseover = function() {
57                 this.className = this.className + " jd-selected";
58             }
59             c1.onmouseout = function() {
60                 this.className = "jd-autocomplete";
61             }
62             c1.appendChild(link);
63         }
64   /*      var r = filtered.insertRow(-1);
65         var c1 = r.insertCell(-1);
66         c1.className = "jd-autocomplete jd-linktype";
67         c1.colSpan = 2; */
68         gInitialized = true;
69     }
70
71     //if we have results, make the table visible and initialize result info
72     if (gMatches.length > 0) {
73         document.getElementById("search_filtered_div").className = "showing";
74         var N = gMatches.length < ROW_COUNT ? gMatches.length : ROW_COUNT;
75         for (i=0; i<N; i++) {
76             r = filtered.rows[i];
77             r.className = "show-row";
78             set_row_values(toroot, r, gMatches[i]);
79             set_row_selected(r, i == gSelectedIndex);
80             if (i == gSelectedIndex) {
81                 gSelectedID = gMatches[i].id;
82             }
83         }
84         //start hiding rows that are no longer matches
85         for (; i<ROW_COUNT; i++) {
86             r = filtered.rows[i];
87             r.className = "no-display";
88         }
89         //if there are more results we're not showing, so say so.
90 /*      if (gMatches.length > ROW_COUNT) {
91             r = filtered.rows[ROW_COUNT];
92             r.className = "show-row";
93             c1 = r.cells[0];
94             c1.innerHTML = "plus " + (gMatches.length-ROW_COUNT) + " more"; 
95         } else {
96             filtered.rows[ROW_COUNT].className = "hide-row";
97         }*/
98     //if we have no results, hide the table
99     } else {
100         document.getElementById("search_filtered_div").className = "no-display";
101     }
102 }
103
104 function search_changed(e, kd, toroot)
105 {
106     var search = document.getElementById("search_autocomplete");
107     var text = search.value;
108
109     // 13 = enter
110     if (!kd && (e.keyCode == 13)) {
111         document.getElementById("search_filtered_div").className = "no-display";
112         if (gSelectedIndex >= 0) {
113             window.location = toroot + gMatches[gSelectedIndex].link;
114             return false;
115         }
116     }
117     // 38 -- arrow up
118     else if (kd && (e.keyCode == 38)) {
119         if (gSelectedIndex >= 0) {
120             gSelectedIndex--;
121         }
122         sync_selection_table(toroot);
123         return false;
124     }
125     // 40 -- arrow down
126     else if (kd && (e.keyCode == 40)) {
127         if (gSelectedIndex < gMatches.length-1
128                         && gSelectedIndex < ROW_COUNT-1) {
129             gSelectedIndex++;
130         }
131         sync_selection_table(toroot);
132         return false;
133     }
134     else if (!kd) {
135         gMatches = new Array();
136         matchedCount = 0;
137         gSelectedIndex = -1;
138         for (i=0; i<DATA.length; i++) {
139             var s = DATA[i];
140             if (text.length != 0 && s.label.indexOf(text) != -1) {
141                 gMatches[matchedCount] = s;
142                 if (gSelectedID == s.id) {
143                     gSelectedIndex = matchedCount;
144                 }
145                 matchedCount++;
146             }
147         }
148         sync_selection_table(toroot);
149         return true; // allow the event to bubble up to the search api
150     }
151 }
152
153 function search_focus_changed(obj, focused)
154 {
155     if (focused) {
156         if(obj.value == DEFAULT_TEXT){
157             obj.value = "";
158             obj.style.color="#000000";
159         }
160     } else {
161         if(obj.value == ""){
162           obj.value = DEFAULT_TEXT;
163           obj.style.color="#aaaaaa";
164         }
165         document.getElementById("search_filtered_div").className = "no-display";
166     }
167 }
168
169 function submit_search() {
170   var query = document.getElementById('search_autocomplete').value;
171   document.location = toRoot + 'search.html#q=' + query + '&t=0';
172   return false;
173 }