Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autocomplete-highlighters-debug.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('autocomplete-highlighters', function(Y) {
  9. /**
  10. * Provides pre-built result highlighters for AutoComplete.
  11. *
  12. * @module autocomplete
  13. * @submodule autocomplete-highlighters
  14. * @class AutoCompleteHighlighters
  15. * @static
  16. */
  17. var YArray = Y.Array,
  18. Highlight = Y.Highlight,
  19. Highlighters = Y.mix(Y.namespace('AutoCompleteHighlighters'), {
  20. // -- Public Methods -------------------------------------------------------
  21. /**
  22. * Highlights any individual query character that occurs anywhere in a
  23. * result. Case-insensitive.
  24. *
  25. * @method charMatch
  26. * @param {String} query Query to match
  27. * @param {Array} results Results to highlight
  28. * @return {Array} Highlighted results
  29. * @static
  30. */
  31. charMatch: function (query, results, caseSensitive) {
  32. // The caseSensitive parameter is only intended for use by
  33. // charMatchCase(). It's intentionally undocumented.
  34. var queryChars = YArray.unique((caseSensitive ? query :
  35. query.toLowerCase()).split(''));
  36. return YArray.map(results, function (result) {
  37. return Highlight.all(result.text, queryChars, {
  38. caseSensitive: caseSensitive
  39. });
  40. });
  41. },
  42. /**
  43. * Case-sensitive version of <code>charMatch()</code>.
  44. *
  45. * @method charMatchCase
  46. * @param {String} query Query to match
  47. * @param {Array} results Results to highlight
  48. * @return {Array} Highlighted results
  49. * @static
  50. */
  51. charMatchCase: function (query, results) {
  52. return Highlighters.charMatch(query, results, true);
  53. },
  54. /**
  55. * Highlights the complete query as a phrase anywhere within a result.
  56. * Case-insensitive.
  57. *
  58. * @method phraseMatch
  59. * @param {String} query Query to match
  60. * @param {Array} results Results to highlight
  61. * @return {Array} Highlighted results
  62. * @static
  63. */
  64. phraseMatch: function (query, results, caseSensitive) {
  65. // The caseSensitive parameter is only intended for use by
  66. // phraseMatchCase(). It's intentionally undocumented.
  67. return YArray.map(results, function (result) {
  68. return Highlight.all(result.text, [query], {
  69. caseSensitive: caseSensitive
  70. });
  71. });
  72. },
  73. /**
  74. * Case-sensitive version of <code>phraseMatch()</code>.
  75. *
  76. * @method phraseMatchCase
  77. * @param {String} query Query to match
  78. * @param {Array} results Results to highlight
  79. * @return {Array} Highlighted results
  80. * @static
  81. */
  82. phraseMatchCase: function (query, results) {
  83. return Highlighters.phraseMatch(query, results, true);
  84. },
  85. /**
  86. * Highlights the complete query as a phrase at the beginning of a result.
  87. * Case-insensitive.
  88. *
  89. * @method startsWith
  90. * @param {String} query Query to match
  91. * @param {Array} results Results to highlight
  92. * @return {Array} Highlighted results
  93. * @static
  94. */
  95. startsWith: function (query, results, caseSensitive) {
  96. // The caseSensitive parameter is only intended for use by
  97. // startsWithCase(). It's intentionally undocumented.
  98. return YArray.map(results, function (result) {
  99. return Highlight.all(result.text, [query], {
  100. caseSensitive: caseSensitive,
  101. startsWith : true
  102. });
  103. });
  104. },
  105. /**
  106. * Case-sensitive version of <code>startsWith()</code>.
  107. *
  108. * @method startsWithCase
  109. * @param {String} query Query to match
  110. * @param {Array} results Results to highlight
  111. * @return {Array} Highlighted results
  112. * @static
  113. */
  114. startsWithCase: function (query, results) {
  115. return Highlighters.startsWith(query, results, true);
  116. },
  117. /**
  118. * Highlights portions of results in which words from the query match either
  119. * whole words or parts of words in the result. Non-word characters like
  120. * whitespace and certain punctuation are ignored. Case-insensitive.
  121. *
  122. * @method subWordMatch
  123. * @param {String} query Query to match
  124. * @param {Array} results Results to highlight
  125. * @return {Array} Highlighted results
  126. * @static
  127. */
  128. subWordMatch: function (query, results, caseSensitive) {
  129. // The caseSensitive parameter is only intended for use by
  130. // subWordMatchCase(). It's intentionally undocumented.
  131. var queryWords = Y.Text.WordBreak.getUniqueWords(query, {
  132. ignoreCase: !caseSensitive
  133. });
  134. return YArray.map(results, function (result) {
  135. return Highlight.all(result.text, queryWords, {
  136. caseSensitive: caseSensitive
  137. });
  138. });
  139. },
  140. /**
  141. * Case-sensitive version of <code>subWordMatch()</code>.
  142. *
  143. * @method subWordMatchCase
  144. * @param {String} query Query to match
  145. * @param {Array} results Results to highlight
  146. * @return {Array} Highlighted results
  147. * @static
  148. */
  149. subWordMatchCase: function (query, results) {
  150. return Highlighters.subWordMatch(query, results, true);
  151. },
  152. /**
  153. * Highlights individual words in results that are also in the query.
  154. * Non-word characters like punctuation are ignored. Case-insensitive.
  155. *
  156. * @method wordMatch
  157. * @param {String} query Query to match
  158. * @param {Array} results Results to highlight
  159. * @return {Array} Highlighted results
  160. * @static
  161. */
  162. wordMatch: function (query, results, caseSensitive) {
  163. // The caseSensitive parameter is only intended for use by
  164. // wordMatchCase(). It's intentionally undocumented.
  165. return YArray.map(results, function (result) {
  166. return Highlight.words(result.text, query, {
  167. caseSensitive: caseSensitive
  168. });
  169. });
  170. },
  171. /**
  172. * Case-sensitive version of <code>wordMatch()</code>.
  173. *
  174. * @method wordMatchCase
  175. * @param {String} query Query to match
  176. * @param {Array} results Results to highlight
  177. * @return {Array} Highlighted results
  178. * @static
  179. */
  180. wordMatchCase: function (query, results) {
  181. return Highlighters.wordMatch(query, results, true);
  182. }
  183. });
  184. }, '3.4.0' ,{requires:['array-extras', 'highlight-base']});