Debian Codegraph @

closure-library/closure/goog/ui/ac/richremote.js

raw
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 rich autocomplete that will match
17 * from an array of data provided via ajax. The server returns a complex data
18 * structure that is used with client-side javascript functions to render the
19 * results.
20 *
21 * The server sends a list of the form:
22 * [["type1", {...}, {...}, ...], ["type2", {...}, {...}, ...], ...]
23 * The first element of each sublist is a string designating the type of the
24 * hashes in the sublist, each of which represents one match. The type string
25 * must be the name of a function(item) which converts the hash into a rich
26 * row that contains both a render(node, token) and a select(target) method.
27 * The render method is called by the renderer when rendering the rich row,
28 * and the select method is called by the RichInputHandler when the rich row is
29 * selected.
30 *
31 * @see ../../demos/autocompleterichremote.html
32 */
33
34 goog.provide('goog.ui.ac.RichRemote');
35
36 goog.require('goog.ui.ac.AutoComplete');
37 goog.require('goog.ui.ac.Remote');
38 goog.require('goog.ui.ac.Renderer');
39 goog.require('goog.ui.ac.RichInputHandler');
40 goog.require('goog.ui.ac.RichRemoteArrayMatcher');
41
42
43
44 /**
45 * Factory class to create a rich autocomplete widget that autocompletes an
46 * inputbox or textarea from data provided via ajax. The server returns a
47 * complex data structure that is used with client-side javascript functions to
48 * render the results.
49 *
50 * @param {string} url The Uri which generates the auto complete matches.
51 * @param {Element} input Input element or text area.
52 * @param {boolean=} opt_multi Whether to allow multiple entries; defaults
53 * to false.
54 * @param {boolean=} opt_useSimilar Whether to use similar matches; e.g.
55 * "gost" => "ghost".
56 * @constructor
57 * @extends {goog.ui.ac.Remote}
58 */
59 goog.ui.ac.RichRemote = function(url, input, opt_multi, opt_useSimilar) {
60 // Create a custom renderer that renders rich rows. The renderer calls
61 // row.render(node, token) for each row.
62 var customRenderer = {};
63 customRenderer.renderRow = function(row, token, node) {
64 return row.data.render(node, token);
65 };
66
67 /**
68 * A standard renderer that uses a custom row renderer to display the
69 * rich rows generated by this autocomplete widget.
70 * @type {goog.ui.ac.Renderer}
71 * @private
72 */
73 var renderer = new goog.ui.ac.Renderer(null, customRenderer);
74
75 /**
76 * A remote matcher that parses rich results returned by the server.
77 * @type {goog.ui.ac.RichRemoteArrayMatcher}
78 * @private
79 */
80 var matcher = new goog.ui.ac.RichRemoteArrayMatcher(url, !opt_useSimilar);
81
82 /**
83 * An input handler that calls select on a row when it is selected.
84 * @type {goog.ui.ac.RichInputHandler}
85 * @private
86 */
87 var inputhandler =
88 new goog.ui.ac.RichInputHandler(null, null, !!opt_multi, 300);
89
90 // Create the widget and connect it to the input handler.
91 goog.ui.ac.AutoComplete.call(this, matcher, renderer, inputhandler);
92 inputhandler.attachAutoComplete(this);
93 inputhandler.attachInputs(input);
94 };
95 goog.inherits(goog.ui.ac.RichRemote, goog.ui.ac.Remote);
96
97
98 /**
99 * Set the filter that is called before the array matches are returned.
100 * @param {Function} rowFilter A function(rows) that returns an array of rows as
101 * a subset of the rows input array.
102 */
103 goog.ui.ac.RichRemote.prototype.setRowFilter = function(rowFilter) {
104 this.matcher_.setRowFilter(rowFilter);
105 };
106
107
108 /**
109 * Sets the function building the rows.
110 * @param {goog.ui.ac.RichRemoteArrayMatcher.RowBuilder} rowBuilder
111 * A function(type, response) converting the type and the server response to
112 * an object with two methods: render(node, token) and select(target).
113 */
114 goog.ui.ac.RichRemote.prototype.setRowBuilder = function(rowBuilder) {
115 this.matcher_.setRowBuilder(rowBuilder);
116 };