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: const Ci = Components.interfaces; michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/nsFormAutoCompleteResult.jsm"); michael@0: michael@0: function InputListAutoComplete() {} michael@0: michael@0: InputListAutoComplete.prototype = { michael@0: classID : Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIInputListAutoComplete]), michael@0: michael@0: autoCompleteSearch : function (formHistoryResult, aUntrimmedSearchString, aField) { michael@0: let comments = []; // "comments" column values for suggestions michael@0: let [values, labels] = this.getListSuggestions(aField); michael@0: let historyResults = []; michael@0: let historyComments = []; michael@0: michael@0: // formHistoryResult will be null if form autocomplete is disabled. michael@0: // We still want the list values to display. michael@0: if (formHistoryResult) { michael@0: entries = formHistoryResult.wrappedJSObject.entries; michael@0: for (let i = 0; i < entries.length; ++i) { michael@0: historyResults.push(entries[i].text); michael@0: historyComments.push(""); michael@0: } michael@0: } michael@0: michael@0: // fill out the comment column for the suggestions michael@0: // if we have any suggestions, put a label at the top michael@0: if (values.length) { michael@0: comments[0] = "separator"; michael@0: } michael@0: for (let i = 1; i < values.length; ++i) { michael@0: comments.push(""); michael@0: } michael@0: michael@0: // now put the history results above the suggestions michael@0: let finalValues = historyResults.concat(values); michael@0: let finalLabels = historyResults.concat(labels); michael@0: let finalComments = historyComments.concat(comments); michael@0: michael@0: return new FormAutoCompleteResult(aUntrimmedSearchString, michael@0: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, michael@0: 0, "", finalValues, finalLabels, michael@0: finalComments, formHistoryResult); michael@0: }, michael@0: michael@0: getListSuggestions : function (aField) { michael@0: let values = []; michael@0: let labels = []; michael@0: michael@0: if (aField) { michael@0: let filter = !aField.hasAttribute("mozNoFilter"); michael@0: let lowerFieldValue = aField.value.toLowerCase(); michael@0: michael@0: if (aField.list) { michael@0: let options = aField.list.options; michael@0: let length = options.length; michael@0: for (let i = 0; i < length; i++) { michael@0: let item = options.item(i); michael@0: let label = ""; michael@0: if (item.label) { michael@0: label = item.label; michael@0: } else if (item.text) { michael@0: label = item.text; michael@0: } else { michael@0: label = item.value; michael@0: } michael@0: michael@0: if (filter && label.toLowerCase().indexOf(lowerFieldValue) == -1) { michael@0: continue; michael@0: } michael@0: michael@0: labels.push(label); michael@0: values.push(item.value); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return [values, labels]; michael@0: } michael@0: }; michael@0: michael@0: let component = [InputListAutoComplete]; michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);