autocompleterichremote.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2007 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <!--
  9. -->
  10. <head>
  11. <title>goog.ui.ac.RichRemote</title>
  12. <script src="../base.js"></script>
  13. <script>
  14. goog.require("goog.array");
  15. goog.require("goog.dom");
  16. goog.require("goog.ui.ac.RichRemote");
  17. goog.require('goog.dom.TagName');
  18. </script>
  19. <link rel="stylesheet" href="css/demo.css">
  20. <link rel="stylesheet" href="../css/autocomplete.css">
  21. <style type="text/css">
  22. .apple {
  23. background-color: #990033;
  24. color: #FFFFFF;
  25. margin: 3px;
  26. font-style: italic;
  27. }
  28. .citrus {
  29. background-color: #CCCC33;
  30. color: #FFFFFF;
  31. margin: 3px;
  32. font-style: italic;
  33. }
  34. .berry {
  35. background-color: #009933;
  36. color: #FFFFFF;
  37. margin: 3px;
  38. font-style: italic;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h1>goog.ui.ac.RichRemote</h1>
  44. <p>
  45. Fruit Selector:<br>
  46. <input type="text" id="txtInput" style="width:500px"/>
  47. <input type="checkbox" id="berryAllergy" onclick="setFilter()"/>
  48. <label for="berryAllergy">berry allergy</label>
  49. <input type="checkbox" id="setHighlighting"
  50. onclick="setHighlighting()"/>
  51. <label for="setHighlighting">standard highlighting</label>
  52. <p>
  53. This data is being pulled from the server at
  54. <a href="autocompleterichremotedata.js">autocompleterichremotedata.js</a>.
  55. </p>
  56. <p>
  57. Ideally the server would perform a search on the query and would only
  58. return relevant results; however, this response is static.
  59. </p>
  60. </p>
  61. <iframe id="wikipedia" width="900" height="600"></iframe>
  62. <script>
  63. var makeRichRow_ = function(item, itemType, itemClassName) {
  64. item.type = itemType;
  65. item.render = function(node, token) {
  66. var dom_ = goog.dom.getDomHelper(node);
  67. var typeNode = dom_.createDom(goog.dom.TagName.SPAN, itemClassName);
  68. dom_.appendChild(typeNode, dom_.createTextNode(itemType));
  69. var nameNode = dom_.createDom(goog.dom.TagName.SPAN);
  70. dom_.appendChild(nameNode, dom_.createTextNode(item.name));
  71. dom_.appendChild(node, typeNode);
  72. dom_.appendChild(node, nameNode);
  73. };
  74. item.select = function(target) {
  75. target.value = item.name;
  76. wikipedia.src = item.url;
  77. };
  78. return item;
  79. };
  80. var apple = function(item) {
  81. return makeRichRow_(item, "Apple", "apple");
  82. };
  83. var citrus = function(item) {
  84. return makeRichRow_(item, "Citrus", "citrus");
  85. };
  86. var berry = function(item) {
  87. return makeRichRow_(item, "Berry", "berry");
  88. };
  89. var input = document.getElementById("txtInput");
  90. var wikipedia = document.getElementById("wikipedia");
  91. var ac = new goog.ui.ac.RichRemote("autocompleterichremotedata.js",
  92. input);
  93. // Set the autocomplete"s rowFilter appropriately
  94. var setFilter = function() {
  95. var checkbox = document.getElementById("berryAllergy");
  96. if (checkbox.checked) {
  97. ac.setRowFilter(filterOutBerries);
  98. } else {
  99. ac.setRowFilter();
  100. }
  101. };
  102. // Set the autocomplete"s standard highlighting
  103. var setHighlighting = function() {
  104. var checkbox = document.getElementById("setHighlighting");
  105. ac.setUseStandardHighlighting(checkbox.checked);
  106. };
  107. // Do not include berries in the search results
  108. var filterOutBerries = function(rows) {
  109. var filterFunction = function(item) { return item.type != "Berry"; };
  110. return goog.array.filter(rows, filterFunction);
  111. };
  112. // When the page loads, make sure to set the filter appropriately
  113. window.onload = function() {
  114. setFilter();
  115. setHighlighting();
  116. };
  117. </script>
  118. </body>
  119. </html>