Debian Codegraph @

closure-library/closure/goog/ui/ac/richremotearraymatcher.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 Class that retrieves rich autocomplete matches, represented as
17 * a structured list of lists, via an ajax call. The first element of each
18 * sublist is the name of a client-side javascript function that converts the
19 * remaining sublist elements into rich rows.
20 *
21 */
22
23 goog.provide('goog.ui.ac.RichRemoteArrayMatcher');
24
25 goog.require('goog.dom');
26 goog.require('goog.ui.ac.RemoteArrayMatcher');
27
28
29
30 /**
31 * An array matcher that requests rich matches via ajax and converts them into
32 * rich rows.
33 *
34 * @param {string} url The Uri which generates the auto complete matches. The
35 * search term is passed to the server as the 'token' query param.
36 * @param {boolean=} opt_noSimilar If true, request that the server does not do
37 * similarity matches for the input token against the dictionary.
38 * The value is sent to the server as the 'use_similar' query param which is
39 * either "1" (opt_noSimilar==false) or "0" (opt_noSimilar==true).
40 * @constructor
41 * @extends {goog.ui.ac.RemoteArrayMatcher}
42 */
43 goog.ui.ac.RichRemoteArrayMatcher = function(url, opt_noSimilar) {
44 goog.ui.ac.RemoteArrayMatcher.call(this, url, opt_noSimilar);
45
46 /**
47 * A function(rows) that is called before the array matches are returned.
48 * It runs client-side and filters the results given by the server before
49 * being rendered by the client.
50 * @type {Function}
51 * @private
52 */
53 this.rowFilter_ = null;
54
55 /**
56 * A function(type, response) converting the type and the server response to
57 * an object with two methods: render(node, token) and select(target).
58 * @private {goog.ui.ac.RichRemoteArrayMatcher.RowBuilder}
59 */
60 this.rowBuilder_ = function(type, response) {
61 return /** @type {!Object} */ (response);
62 };
63 };
64 goog.inherits(goog.ui.ac.RichRemoteArrayMatcher, goog.ui.ac.RemoteArrayMatcher);
65
66
67 /**
68 * Set the filter that is called before the array matches are returned.
69 * @param {Function} rowFilter A function(rows) that returns an array of rows as
70 * a subset of the rows input array.
71 */
72 goog.ui.ac.RichRemoteArrayMatcher.prototype.setRowFilter = function(rowFilter) {
73 this.rowFilter_ = rowFilter;
74 };
75
76
77 /**
78 * @typedef {function(string, *): {
79 * render: (function(!Element, string)|undefined),
80 * select: (function(!Element)|undefined)
81 * }}
82 */
83 goog.ui.ac.RichRemoteArrayMatcher.RowBuilder;
84
85
86 /**
87 * Sets the function building the rows.
88 * @param {goog.ui.ac.RichRemoteArrayMatcher.RowBuilder} rowBuilder
89 * A function(type, response) converting the type and the server response to
90 * an object with two methods: render(node, token) and select(target).
91 */
92 goog.ui.ac.RichRemoteArrayMatcher.prototype.setRowBuilder = function(
93 rowBuilder) {
94 this.rowBuilder_ = rowBuilder;
95 };
96
97
98 /**
99 * Retrieve a set of matching rows from the server via ajax and convert them
100 * into rich rows.
101 * @param {string} token The text that should be matched; passed to the server
102 * as the 'token' query param.
103 * @param {number} maxMatches The maximum number of matches requested from the
104 * server; passed as the 'max_matches' query param. The server is
105 * responsible for limiting the number of matches that are returned.
106 * @param {Function} matchHandler Callback to execute on the result after
107 * matching.
108 * @override
109 */
110 goog.ui.ac.RichRemoteArrayMatcher.prototype.requestMatchingRows = function(
111 token, maxMatches, matchHandler) {
112 // The RichRemoteArrayMatcher must map over the results and filter them
113 // before calling the request matchHandler. This is done by passing
114 // myMatchHandler to RemoteArrayMatcher.requestMatchingRows which maps,
115 // filters, and then calls matchHandler.
116 var myMatchHandler = goog.bind(function(token, matches) {
117
118 try {
119 var rows = [];
120 for (var i = 0; i < matches.length; i++) {
121 for (var j = 1; j < matches[i].length; j++) {
122 var richRow = this.rowBuilder_(matches[i][0], matches[i][j]);
123 rows.push(richRow);
124
125 // If no render function was provided, set the node's textContent.
126 if (typeof richRow.render == 'undefined') {
127 richRow.render = function(node, token) {
128 goog.dom.setTextContent(node, richRow.toString());
129 };
130 }
131
132 // If no select function was provided, set the text of the input.
133 if (typeof richRow.select == 'undefined') {
134 richRow.select = function(target) {
135 target.value = richRow.toString();
136 };
137 }
138 }
139 }
140 if (this.rowFilter_) {
141 rows = this.rowFilter_(rows);
142 }
143 matchHandler(token, rows);
144 } catch (exception) {
145 // TODO(user): Is this what we want?
146 matchHandler(token, []);
147 }
148 }, this);
149
150 // Call the super's requestMatchingRows with myMatchHandler
151 goog.ui.ac.RichRemoteArrayMatcher.superClass_.requestMatchingRows.call(
152 this, token, maxMatches, myMatchHandler);
153 };