The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 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 | filtered.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 | filtered.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 | if (gSelectedIndex >= 0) { |
| 112 | window.location = toroot + gMatches[gSelectedIndex].link; |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | // 38 -- arrow up |
| 117 | else if (kd && (e.keyCode == 38)) { |
| 118 | if (gSelectedIndex >= 0) { |
| 119 | gSelectedIndex--; |
| 120 | } |
| 121 | sync_selection_table(toroot); |
| 122 | return false; |
| 123 | } |
| 124 | // 40 -- arrow down |
| 125 | else if (kd && (e.keyCode == 40)) { |
| 126 | if (gSelectedIndex < gMatches.length-1 |
| 127 | && gSelectedIndex < ROW_COUNT-1) { |
| 128 | gSelectedIndex++; |
| 129 | } |
| 130 | sync_selection_table(toroot); |
| 131 | return false; |
| 132 | } |
| 133 | else if (!kd) { |
| 134 | gMatches = new Array(); |
| 135 | matchedCount = 0; |
| 136 | gSelectedIndex = -1; |
| 137 | for (i=0; i<DATA.length; i++) { |
| 138 | var s = DATA[i]; |
| 139 | if (text.length != 0 && s.label.toLowerCase().indexOf(text.toLowerCase()) != -1) { |
| 140 | gMatches[matchedCount] = s; |
| 141 | if (gSelectedID == s.id) { |
| 142 | gSelectedIndex = matchedCount; |
| 143 | } |
| 144 | matchedCount++; |
| 145 | } |
| 146 | } |
| 147 | sync_selection_table(toroot); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | function search_focus_changed(obj, focused) |
| 152 | { |
| 153 | if (focused) { |
| 154 | if(obj.value == DEFAULT_TEXT){ |
| 155 | obj.value = ""; |
| 156 | obj.style.color="#000000"; |
| 157 | } |
| 158 | } else { |
| 159 | obj.value = DEFAULT_TEXT; |
| 160 | obj.style.color="#aaaaaa"; |
| 161 | document.getElementById("search_filtered").className = "no-display"; |
| 162 | } |
| 163 | } |