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