toolkit/components/satchel/nsInputListAutoComplete.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/nsInputListAutoComplete.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.11 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.12 +Components.utils.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
    1.13 +
    1.14 +function InputListAutoComplete() {}
    1.15 +
    1.16 +InputListAutoComplete.prototype = {
    1.17 +  classID       : Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}"),
    1.18 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIInputListAutoComplete]),
    1.19 +
    1.20 +  autoCompleteSearch : function (formHistoryResult, aUntrimmedSearchString, aField) {
    1.21 +    let comments = []; // "comments" column values for suggestions
    1.22 +    let [values, labels] = this.getListSuggestions(aField);
    1.23 +    let historyResults = [];
    1.24 +    let historyComments = [];
    1.25 +
    1.26 +    // formHistoryResult will be null if form autocomplete is disabled.
    1.27 +    // We still want the list values to display.
    1.28 +    if (formHistoryResult) {
    1.29 +      entries = formHistoryResult.wrappedJSObject.entries;
    1.30 +      for (let i = 0; i < entries.length; ++i) {
    1.31 +        historyResults.push(entries[i].text);
    1.32 +        historyComments.push("");
    1.33 +      }
    1.34 +    }
    1.35 +
    1.36 +    // fill out the comment column for the suggestions
    1.37 +    // if we have any suggestions, put a label at the top
    1.38 +    if (values.length) {
    1.39 +      comments[0] = "separator";
    1.40 +    }
    1.41 +    for (let i = 1; i < values.length; ++i) {
    1.42 +      comments.push("");
    1.43 +    }
    1.44 +
    1.45 +    // now put the history results above the suggestions
    1.46 +    let finalValues = historyResults.concat(values);
    1.47 +    let finalLabels = historyResults.concat(labels);
    1.48 +    let finalComments = historyComments.concat(comments);
    1.49 +
    1.50 +    return new FormAutoCompleteResult(aUntrimmedSearchString,
    1.51 +                                      Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
    1.52 +                                      0, "", finalValues, finalLabels,
    1.53 +                                      finalComments, formHistoryResult);
    1.54 +  },
    1.55 +
    1.56 +  getListSuggestions : function (aField) {
    1.57 +    let values = [];
    1.58 +    let labels = [];
    1.59 +
    1.60 +    if (aField) {
    1.61 +      let filter = !aField.hasAttribute("mozNoFilter");
    1.62 +      let lowerFieldValue = aField.value.toLowerCase();
    1.63 +
    1.64 +      if (aField.list) {
    1.65 +        let options = aField.list.options;
    1.66 +        let length = options.length;
    1.67 +        for (let i = 0; i < length; i++) {
    1.68 +          let item = options.item(i);
    1.69 +          let label = "";
    1.70 +          if (item.label) {
    1.71 +            label = item.label;
    1.72 +          } else if (item.text) {
    1.73 +            label = item.text;
    1.74 +          } else {
    1.75 +            label = item.value;
    1.76 +          }
    1.77 +
    1.78 +          if (filter && label.toLowerCase().indexOf(lowerFieldValue) == -1) {
    1.79 +            continue;
    1.80 +          }
    1.81 +
    1.82 +          labels.push(label);
    1.83 +          values.push(item.value);
    1.84 +        }
    1.85 +      }
    1.86 +    }
    1.87 +
    1.88 +    return [values, labels];
    1.89 +  }
    1.90 +};
    1.91 +
    1.92 +let component = [InputListAutoComplete];
    1.93 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);

mercurial