richremotearraymatcher.js 5.4 KB

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