toolkit/components/satchel/nsInputListAutoComplete.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

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 const Ci = Components.interfaces;
michael@0 6
michael@0 7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 8 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 9 Components.utils.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
michael@0 10
michael@0 11 function InputListAutoComplete() {}
michael@0 12
michael@0 13 InputListAutoComplete.prototype = {
michael@0 14 classID : Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}"),
michael@0 15 QueryInterface: XPCOMUtils.generateQI([Ci.nsIInputListAutoComplete]),
michael@0 16
michael@0 17 autoCompleteSearch : function (formHistoryResult, aUntrimmedSearchString, aField) {
michael@0 18 let comments = []; // "comments" column values for suggestions
michael@0 19 let [values, labels] = this.getListSuggestions(aField);
michael@0 20 let historyResults = [];
michael@0 21 let historyComments = [];
michael@0 22
michael@0 23 // formHistoryResult will be null if form autocomplete is disabled.
michael@0 24 // We still want the list values to display.
michael@0 25 if (formHistoryResult) {
michael@0 26 entries = formHistoryResult.wrappedJSObject.entries;
michael@0 27 for (let i = 0; i < entries.length; ++i) {
michael@0 28 historyResults.push(entries[i].text);
michael@0 29 historyComments.push("");
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 // fill out the comment column for the suggestions
michael@0 34 // if we have any suggestions, put a label at the top
michael@0 35 if (values.length) {
michael@0 36 comments[0] = "separator";
michael@0 37 }
michael@0 38 for (let i = 1; i < values.length; ++i) {
michael@0 39 comments.push("");
michael@0 40 }
michael@0 41
michael@0 42 // now put the history results above the suggestions
michael@0 43 let finalValues = historyResults.concat(values);
michael@0 44 let finalLabels = historyResults.concat(labels);
michael@0 45 let finalComments = historyComments.concat(comments);
michael@0 46
michael@0 47 return new FormAutoCompleteResult(aUntrimmedSearchString,
michael@0 48 Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
michael@0 49 0, "", finalValues, finalLabels,
michael@0 50 finalComments, formHistoryResult);
michael@0 51 },
michael@0 52
michael@0 53 getListSuggestions : function (aField) {
michael@0 54 let values = [];
michael@0 55 let labels = [];
michael@0 56
michael@0 57 if (aField) {
michael@0 58 let filter = !aField.hasAttribute("mozNoFilter");
michael@0 59 let lowerFieldValue = aField.value.toLowerCase();
michael@0 60
michael@0 61 if (aField.list) {
michael@0 62 let options = aField.list.options;
michael@0 63 let length = options.length;
michael@0 64 for (let i = 0; i < length; i++) {
michael@0 65 let item = options.item(i);
michael@0 66 let label = "";
michael@0 67 if (item.label) {
michael@0 68 label = item.label;
michael@0 69 } else if (item.text) {
michael@0 70 label = item.text;
michael@0 71 } else {
michael@0 72 label = item.value;
michael@0 73 }
michael@0 74
michael@0 75 if (filter && label.toLowerCase().indexOf(lowerFieldValue) == -1) {
michael@0 76 continue;
michael@0 77 }
michael@0 78
michael@0 79 labels.push(label);
michael@0 80 values.push(item.value);
michael@0 81 }
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 return [values, labels];
michael@0 86 }
michael@0 87 };
michael@0 88
michael@0 89 let component = [InputListAutoComplete];
michael@0 90 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);

mercurial