ac.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility methods supporting the autocomplete package.
  16. *
  17. * @author adamwos@google.com (Adam Wos)
  18. * @see ../../demos/autocomplete-basic.html
  19. */
  20. goog.provide('goog.ui.ac');
  21. goog.require('goog.ui.ac.ArrayMatcher');
  22. goog.require('goog.ui.ac.AutoComplete');
  23. goog.require('goog.ui.ac.InputHandler');
  24. goog.require('goog.ui.ac.Renderer');
  25. /**
  26. * Factory function for building a basic autocomplete widget that autocompletes
  27. * an inputbox or text area from a data array.
  28. * @param {Array<?>} data Data array.
  29. * @param {Element} input Input element or text area.
  30. * @param {boolean=} opt_multi Whether to allow multiple entries separated with
  31. * semi-colons or commas.
  32. * @param {boolean=} opt_useSimilar use similar matches. e.g. "gost" => "ghost".
  33. * @return {!goog.ui.ac.AutoComplete} A new autocomplete object.
  34. */
  35. goog.ui.ac.createSimpleAutoComplete = function(
  36. data, input, opt_multi, opt_useSimilar) {
  37. var matcher = new goog.ui.ac.ArrayMatcher(data, !opt_useSimilar);
  38. var renderer = new goog.ui.ac.Renderer();
  39. var inputHandler = new goog.ui.ac.InputHandler(null, null, !!opt_multi);
  40. var autoComplete =
  41. new goog.ui.ac.AutoComplete(matcher, renderer, inputHandler);
  42. inputHandler.attachAutoComplete(autoComplete);
  43. inputHandler.attachInputs(input);
  44. return autoComplete;
  45. };