remote.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2007 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 Factory class to create a simple autocomplete that will match
  16. * from an array of data provided via ajax.
  17. *
  18. * @see ../../demos/autocompleteremote.html
  19. */
  20. goog.provide('goog.ui.ac.Remote');
  21. goog.require('goog.ui.ac.AutoComplete');
  22. goog.require('goog.ui.ac.InputHandler');
  23. goog.require('goog.ui.ac.RemoteArrayMatcher');
  24. goog.require('goog.ui.ac.Renderer');
  25. /**
  26. * Factory class for building a remote autocomplete widget that autocompletes
  27. * an inputbox or text area from a data array provided via ajax.
  28. * @param {string} url The Uri which generates the auto complete matches.
  29. * @param {Element} input Input element or text area.
  30. * @param {boolean=} opt_multi Whether to allow multiple entries; defaults
  31. * to false.
  32. * @param {boolean=} opt_useSimilar Whether to use similar matches; e.g.
  33. * "gost" => "ghost".
  34. * @constructor
  35. * @extends {goog.ui.ac.AutoComplete}
  36. */
  37. goog.ui.ac.Remote = function(url, input, opt_multi, opt_useSimilar) {
  38. var matcher = new goog.ui.ac.RemoteArrayMatcher(url, !opt_useSimilar);
  39. this.matcher_ = matcher;
  40. var renderer = new goog.ui.ac.Renderer();
  41. var inputhandler = new goog.ui.ac.InputHandler(null, null, !!opt_multi, 300);
  42. goog.ui.ac.AutoComplete.call(this, matcher, renderer, inputhandler);
  43. inputhandler.attachAutoComplete(this);
  44. inputhandler.attachInputs(input);
  45. };
  46. goog.inherits(goog.ui.ac.Remote, goog.ui.ac.AutoComplete);
  47. /**
  48. * Set whether or not standard highlighting should be used when rendering rows.
  49. * @param {boolean} useStandardHighlighting true if standard highlighting used.
  50. */
  51. goog.ui.ac.Remote.prototype.setUseStandardHighlighting = function(
  52. useStandardHighlighting) {
  53. this.renderer_.setUseStandardHighlighting(useStandardHighlighting);
  54. };
  55. /**
  56. * Gets the attached InputHandler object.
  57. * @return {goog.ui.ac.InputHandler} The input handler.
  58. */
  59. goog.ui.ac.Remote.prototype.getInputHandler = function() {
  60. return /** @type {goog.ui.ac.InputHandler} */ (this.selectionHandler_);
  61. };
  62. /**
  63. * Set the send method ("GET", "POST") for the matcher.
  64. * @param {string} method The send method; default: GET.
  65. */
  66. goog.ui.ac.Remote.prototype.setMethod = function(method) {
  67. this.matcher_.setMethod(method);
  68. };
  69. /**
  70. * Set the post data for the matcher.
  71. * @param {string} content Post data.
  72. */
  73. goog.ui.ac.Remote.prototype.setContent = function(content) {
  74. this.matcher_.setContent(content);
  75. };
  76. /**
  77. * Set the HTTP headers for the matcher.
  78. * @param {Object|goog.structs.Map} headers Map of headers to add to the
  79. * request.
  80. */
  81. goog.ui.ac.Remote.prototype.setHeaders = function(headers) {
  82. this.matcher_.setHeaders(headers);
  83. };
  84. /**
  85. * Set the timeout interval for the matcher.
  86. * @param {number} interval Number of milliseconds after which an
  87. * incomplete request will be aborted; 0 means no timeout is set.
  88. */
  89. goog.ui.ac.Remote.prototype.setTimeoutInterval = function(interval) {
  90. this.matcher_.setTimeoutInterval(interval);
  91. };