Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | this.EXPORTED_SYMBOLS = [ "FormAutoCompleteResult" ]; |
michael@0 | 6 | |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | const Cr = Components.results; |
michael@0 | 9 | |
michael@0 | 10 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | this.FormAutoCompleteResult = |
michael@0 | 13 | function FormAutoCompleteResult(searchString, |
michael@0 | 14 | searchResult, |
michael@0 | 15 | defaultIndex, |
michael@0 | 16 | errorDescription, |
michael@0 | 17 | values, |
michael@0 | 18 | labels, |
michael@0 | 19 | comments, |
michael@0 | 20 | prevResult) { |
michael@0 | 21 | this.searchString = searchString; |
michael@0 | 22 | this._searchResult = searchResult; |
michael@0 | 23 | this._defaultIndex = defaultIndex; |
michael@0 | 24 | this._errorDescription = errorDescription; |
michael@0 | 25 | this._values = values; |
michael@0 | 26 | this._labels = labels; |
michael@0 | 27 | this._comments = comments; |
michael@0 | 28 | this._formHistResult = prevResult; |
michael@0 | 29 | |
michael@0 | 30 | if (prevResult) { |
michael@0 | 31 | this.entries = prevResult.wrappedJSObject.entries; |
michael@0 | 32 | } else { |
michael@0 | 33 | this.entries = []; |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | FormAutoCompleteResult.prototype = { |
michael@0 | 38 | |
michael@0 | 39 | // The user's query string |
michael@0 | 40 | searchString: "", |
michael@0 | 41 | |
michael@0 | 42 | // The result code of this result object, see |get searchResult| for possible values. |
michael@0 | 43 | _searchResult: 0, |
michael@0 | 44 | |
michael@0 | 45 | // The default item that should be entered if none is selected |
michael@0 | 46 | _defaultIndex: 0, |
michael@0 | 47 | |
michael@0 | 48 | //The reason the search failed |
michael@0 | 49 | _errorDescription: "", |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * A reference to the form history nsIAutocompleteResult that we're wrapping. |
michael@0 | 53 | * We use this to forward removeEntryAt calls as needed. |
michael@0 | 54 | */ |
michael@0 | 55 | _formHistResult: null, |
michael@0 | 56 | |
michael@0 | 57 | entries: null, |
michael@0 | 58 | |
michael@0 | 59 | get wrappedJSObject() { |
michael@0 | 60 | return this; |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * @return the result code of this result object, either: |
michael@0 | 65 | * RESULT_IGNORED (invalid searchString) |
michael@0 | 66 | * RESULT_FAILURE (failure) |
michael@0 | 67 | * RESULT_NOMATCH (no matches found) |
michael@0 | 68 | * RESULT_SUCCESS (matches found) |
michael@0 | 69 | */ |
michael@0 | 70 | get searchResult() { |
michael@0 | 71 | return this._searchResult; |
michael@0 | 72 | }, |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * @return the default item that should be entered if none is selected |
michael@0 | 76 | */ |
michael@0 | 77 | get defaultIndex() { |
michael@0 | 78 | return this._defaultIndex; |
michael@0 | 79 | }, |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * @return the reason the search failed |
michael@0 | 83 | */ |
michael@0 | 84 | get errorDescription() { |
michael@0 | 85 | return this._errorDescription; |
michael@0 | 86 | }, |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * @return the number of results |
michael@0 | 90 | */ |
michael@0 | 91 | get matchCount() { |
michael@0 | 92 | return this._values.length; |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | _checkIndexBounds : function (index) { |
michael@0 | 96 | if (index < 0 || index >= this._values.length) { |
michael@0 | 97 | throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE); |
michael@0 | 98 | } |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Retrieves a result |
michael@0 | 103 | * @param index the index of the result requested |
michael@0 | 104 | * @return the result at the specified index |
michael@0 | 105 | */ |
michael@0 | 106 | getValueAt: function(index) { |
michael@0 | 107 | this._checkIndexBounds(index); |
michael@0 | 108 | return this._values[index]; |
michael@0 | 109 | }, |
michael@0 | 110 | |
michael@0 | 111 | getLabelAt: function(index) { |
michael@0 | 112 | this._checkIndexBounds(index); |
michael@0 | 113 | return this._labels[index]; |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Retrieves a comment (metadata instance) |
michael@0 | 118 | * @param index the index of the comment requested |
michael@0 | 119 | * @return the comment at the specified index |
michael@0 | 120 | */ |
michael@0 | 121 | getCommentAt: function(index) { |
michael@0 | 122 | this._checkIndexBounds(index); |
michael@0 | 123 | return this._comments[index]; |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | /** |
michael@0 | 127 | * Retrieves a style hint specific to a particular index. |
michael@0 | 128 | * @param index the index of the style hint requested |
michael@0 | 129 | * @return the style hint at the specified index |
michael@0 | 130 | */ |
michael@0 | 131 | getStyleAt: function(index) { |
michael@0 | 132 | this._checkIndexBounds(index); |
michael@0 | 133 | if (!this._comments[index]) { |
michael@0 | 134 | return null; // not a category label, so no special styling |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | if (index == 0) { |
michael@0 | 138 | return "suggestfirst"; // category label on first line of results |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | return "suggesthint"; // category label on any other line of results |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Retrieves an image url. |
michael@0 | 146 | * @param index the index of the image url requested |
michael@0 | 147 | * @return the image url at the specified index |
michael@0 | 148 | */ |
michael@0 | 149 | getImageAt: function(index) { |
michael@0 | 150 | this._checkIndexBounds(index); |
michael@0 | 151 | return ""; |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Retrieves a result |
michael@0 | 156 | * @param index the index of the result requested |
michael@0 | 157 | * @return the result at the specified index |
michael@0 | 158 | */ |
michael@0 | 159 | getFinalCompleteValueAt: function(index) { |
michael@0 | 160 | return this.getValueAt(index); |
michael@0 | 161 | }, |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Removes a result from the resultset |
michael@0 | 165 | * @param index the index of the result to remove |
michael@0 | 166 | */ |
michael@0 | 167 | removeValueAt: function(index, removeFromDatabase) { |
michael@0 | 168 | this._checkIndexBounds(index); |
michael@0 | 169 | // Forward the removeValueAt call to the underlying result if we have one |
michael@0 | 170 | // Note: this assumes that the form history results were added to the top |
michael@0 | 171 | // of our arrays. |
michael@0 | 172 | if (removeFromDatabase && this._formHistResult && |
michael@0 | 173 | index < this._formHistResult.matchCount) { |
michael@0 | 174 | // Delete the history result from the DB |
michael@0 | 175 | this._formHistResult.removeValueAt(index, true); |
michael@0 | 176 | } |
michael@0 | 177 | this._values.splice(index, 1); |
michael@0 | 178 | this._labels.splice(index, 1); |
michael@0 | 179 | this._comments.splice(index, 1); |
michael@0 | 180 | }, |
michael@0 | 181 | |
michael@0 | 182 | // nsISupports |
michael@0 | 183 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]) |
michael@0 | 184 | }; |