michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = [ "FormAutoCompleteResult" ]; michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: this.FormAutoCompleteResult = michael@0: function FormAutoCompleteResult(searchString, michael@0: searchResult, michael@0: defaultIndex, michael@0: errorDescription, michael@0: values, michael@0: labels, michael@0: comments, michael@0: prevResult) { michael@0: this.searchString = searchString; michael@0: this._searchResult = searchResult; michael@0: this._defaultIndex = defaultIndex; michael@0: this._errorDescription = errorDescription; michael@0: this._values = values; michael@0: this._labels = labels; michael@0: this._comments = comments; michael@0: this._formHistResult = prevResult; michael@0: michael@0: if (prevResult) { michael@0: this.entries = prevResult.wrappedJSObject.entries; michael@0: } else { michael@0: this.entries = []; michael@0: } michael@0: } michael@0: michael@0: FormAutoCompleteResult.prototype = { michael@0: michael@0: // The user's query string michael@0: searchString: "", michael@0: michael@0: // The result code of this result object, see |get searchResult| for possible values. michael@0: _searchResult: 0, michael@0: michael@0: // The default item that should be entered if none is selected michael@0: _defaultIndex: 0, michael@0: michael@0: //The reason the search failed michael@0: _errorDescription: "", michael@0: michael@0: /** michael@0: * A reference to the form history nsIAutocompleteResult that we're wrapping. michael@0: * We use this to forward removeEntryAt calls as needed. michael@0: */ michael@0: _formHistResult: null, michael@0: michael@0: entries: null, michael@0: michael@0: get wrappedJSObject() { michael@0: return this; michael@0: }, michael@0: michael@0: /** michael@0: * @return the result code of this result object, either: michael@0: * RESULT_IGNORED (invalid searchString) michael@0: * RESULT_FAILURE (failure) michael@0: * RESULT_NOMATCH (no matches found) michael@0: * RESULT_SUCCESS (matches found) michael@0: */ michael@0: get searchResult() { michael@0: return this._searchResult; michael@0: }, michael@0: michael@0: /** michael@0: * @return the default item that should be entered if none is selected michael@0: */ michael@0: get defaultIndex() { michael@0: return this._defaultIndex; michael@0: }, michael@0: michael@0: /** michael@0: * @return the reason the search failed michael@0: */ michael@0: get errorDescription() { michael@0: return this._errorDescription; michael@0: }, michael@0: michael@0: /** michael@0: * @return the number of results michael@0: */ michael@0: get matchCount() { michael@0: return this._values.length; michael@0: }, michael@0: michael@0: _checkIndexBounds : function (index) { michael@0: if (index < 0 || index >= this._values.length) { michael@0: throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Retrieves a result michael@0: * @param index the index of the result requested michael@0: * @return the result at the specified index michael@0: */ michael@0: getValueAt: function(index) { michael@0: this._checkIndexBounds(index); michael@0: return this._values[index]; michael@0: }, michael@0: michael@0: getLabelAt: function(index) { michael@0: this._checkIndexBounds(index); michael@0: return this._labels[index]; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieves a comment (metadata instance) michael@0: * @param index the index of the comment requested michael@0: * @return the comment at the specified index michael@0: */ michael@0: getCommentAt: function(index) { michael@0: this._checkIndexBounds(index); michael@0: return this._comments[index]; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieves a style hint specific to a particular index. michael@0: * @param index the index of the style hint requested michael@0: * @return the style hint at the specified index michael@0: */ michael@0: getStyleAt: function(index) { michael@0: this._checkIndexBounds(index); michael@0: if (!this._comments[index]) { michael@0: return null; // not a category label, so no special styling michael@0: } michael@0: michael@0: if (index == 0) { michael@0: return "suggestfirst"; // category label on first line of results michael@0: } michael@0: michael@0: return "suggesthint"; // category label on any other line of results michael@0: }, michael@0: michael@0: /** michael@0: * Retrieves an image url. michael@0: * @param index the index of the image url requested michael@0: * @return the image url at the specified index michael@0: */ michael@0: getImageAt: function(index) { michael@0: this._checkIndexBounds(index); michael@0: return ""; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieves a result michael@0: * @param index the index of the result requested michael@0: * @return the result at the specified index michael@0: */ michael@0: getFinalCompleteValueAt: function(index) { michael@0: return this.getValueAt(index); michael@0: }, michael@0: michael@0: /** michael@0: * Removes a result from the resultset michael@0: * @param index the index of the result to remove michael@0: */ michael@0: removeValueAt: function(index, removeFromDatabase) { michael@0: this._checkIndexBounds(index); michael@0: // Forward the removeValueAt call to the underlying result if we have one michael@0: // Note: this assumes that the form history results were added to the top michael@0: // of our arrays. michael@0: if (removeFromDatabase && this._formHistResult && michael@0: index < this._formHistResult.matchCount) { michael@0: // Delete the history result from the DB michael@0: this._formHistResult.removeValueAt(index, true); michael@0: } michael@0: this._values.splice(index, 1); michael@0: this._labels.splice(index, 1); michael@0: this._comments.splice(index, 1); michael@0: }, michael@0: michael@0: // nsISupports michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]) michael@0: };