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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Ci = Components.interfaces;
     7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     8 Components.utils.import("resource://gre/modules/Services.jsm");
     9 Components.utils.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
    11 function InputListAutoComplete() {}
    13 InputListAutoComplete.prototype = {
    14   classID       : Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}"),
    15   QueryInterface: XPCOMUtils.generateQI([Ci.nsIInputListAutoComplete]),
    17   autoCompleteSearch : function (formHistoryResult, aUntrimmedSearchString, aField) {
    18     let comments = []; // "comments" column values for suggestions
    19     let [values, labels] = this.getListSuggestions(aField);
    20     let historyResults = [];
    21     let historyComments = [];
    23     // formHistoryResult will be null if form autocomplete is disabled.
    24     // We still want the list values to display.
    25     if (formHistoryResult) {
    26       entries = formHistoryResult.wrappedJSObject.entries;
    27       for (let i = 0; i < entries.length; ++i) {
    28         historyResults.push(entries[i].text);
    29         historyComments.push("");
    30       }
    31     }
    33     // fill out the comment column for the suggestions
    34     // if we have any suggestions, put a label at the top
    35     if (values.length) {
    36       comments[0] = "separator";
    37     }
    38     for (let i = 1; i < values.length; ++i) {
    39       comments.push("");
    40     }
    42     // now put the history results above the suggestions
    43     let finalValues = historyResults.concat(values);
    44     let finalLabels = historyResults.concat(labels);
    45     let finalComments = historyComments.concat(comments);
    47     return new FormAutoCompleteResult(aUntrimmedSearchString,
    48                                       Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
    49                                       0, "", finalValues, finalLabels,
    50                                       finalComments, formHistoryResult);
    51   },
    53   getListSuggestions : function (aField) {
    54     let values = [];
    55     let labels = [];
    57     if (aField) {
    58       let filter = !aField.hasAttribute("mozNoFilter");
    59       let lowerFieldValue = aField.value.toLowerCase();
    61       if (aField.list) {
    62         let options = aField.list.options;
    63         let length = options.length;
    64         for (let i = 0; i < length; i++) {
    65           let item = options.item(i);
    66           let label = "";
    67           if (item.label) {
    68             label = item.label;
    69           } else if (item.text) {
    70             label = item.text;
    71           } else {
    72             label = item.value;
    73           }
    75           if (filter && label.toLowerCase().indexOf(lowerFieldValue) == -1) {
    76             continue;
    77           }
    79           labels.push(label);
    80           values.push(item.value);
    81         }
    82       }
    83     }
    85     return [values, labels];
    86   }
    87 };
    89 let component = [InputListAutoComplete];
    90 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);

mercurial