search.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. (function (compodoc) {
  2. var usePushState = typeof history.pushState !== 'undefined',
  3. // DOM Elements
  4. $body = $('body'),
  5. $searchResults,
  6. $searchInput,
  7. $searchList,
  8. $searchTitle,
  9. $searchResultsCount,
  10. $searchQuery,
  11. $mainContainer,
  12. $xsMenu;
  13. // Throttle search
  14. function throttle(fn, wait) {
  15. var timeout;
  16. return function () {
  17. var ctx = this,
  18. args = arguments;
  19. if (!timeout) {
  20. timeout = setTimeout(function () {
  21. timeout = undefined;
  22. fn.apply(ctx, args);
  23. }, wait);
  24. }
  25. };
  26. }
  27. function displayResults(res) {
  28. var noResults = res.count == 0;
  29. var groups = {};
  30. $searchResults.toggleClass('no-results', noResults);
  31. // Clear old results
  32. $searchList.empty();
  33. // Display title for research
  34. $searchResultsCount.text(res.count);
  35. $searchQuery.text(res.query);
  36. // Group result by context
  37. res.results.forEach(function (res) {
  38. var context = res.title.split(' - ')[0];
  39. if (typeof groups[context] === 'undefined') {
  40. groups[context] = {
  41. results: [res]
  42. };
  43. } else {
  44. groups[context].results.push(res);
  45. }
  46. });
  47. var sortedGroups = Object.keys(groups).sort();
  48. for (var i = 0; i < sortedGroups.length; i++) {
  49. var property = sortedGroups[i];
  50. var $li = $('<li>', {
  51. class: 'search-results-group'
  52. });
  53. var finalPropertyLabel = '';
  54. var propertyLabels = property.split('-');
  55. if (
  56. propertyLabels.length === 2 &&
  57. propertyLabels[0] !== 'miscellaneous' &&
  58. propertyLabels[0] !== 'additional'
  59. ) {
  60. finalPropertyLabel =
  61. propertyLabels[0].charAt(0).toUpperCase() +
  62. propertyLabels[0].substring(1) +
  63. ' - ' +
  64. propertyLabels[1].charAt(0).toUpperCase() +
  65. propertyLabels[1].substring(1) +
  66. ' (' +
  67. groups[property].results.length +
  68. ')';
  69. } else if (propertyLabels[0] === 'additional') {
  70. finalPropertyLabel =
  71. 'Additional pages' + ' (' + groups[property].results.length + ')';
  72. } else {
  73. finalPropertyLabel =
  74. propertyLabels[0].charAt(0).toUpperCase() +
  75. propertyLabels[0].substring(1) +
  76. ' (' +
  77. groups[property].results.length +
  78. ')';
  79. }
  80. var $groupTitle = $('<h3>', {
  81. text: finalPropertyLabel
  82. });
  83. $groupTitle.appendTo($li);
  84. var $ulResults = $('<ul>', {
  85. class: 'search-results-list'
  86. });
  87. groups[property].results.forEach(function (res) {
  88. var link = '';
  89. var $liResult = $('<li>', {
  90. class: 'search-results-item'
  91. });
  92. switch (COMPODOC_CURRENT_PAGE_DEPTH) {
  93. case 0:
  94. link = './';
  95. break;
  96. case 1:
  97. case 2:
  98. case 3:
  99. case 4:
  100. case 5:
  101. link = '../'.repeat(COMPODOC_CURRENT_PAGE_DEPTH);
  102. break;
  103. }
  104. var finalResLabel =
  105. res.title.split(' - ')[1].charAt(0).toUpperCase() +
  106. res.title.split(' - ')[1].substring(1);
  107. var $link = $('<a>', {
  108. href: link + res.url,
  109. text: finalResLabel
  110. });
  111. $link.appendTo($liResult);
  112. $liResult.appendTo($ulResults);
  113. });
  114. $ulResults.appendTo($li);
  115. $li.appendTo($searchList);
  116. }
  117. }
  118. function launchSearch(q) {
  119. $body.addClass('with-search');
  120. if ($xsMenu.css('display') === 'block') {
  121. $mainContainer.css('height', 'calc(100% - 100px)');
  122. $mainContainer.css('margin-top', '100px');
  123. }
  124. throttle(
  125. compodoc.search.query(q, 0, MAX_SEARCH_RESULTS).then(function (results) {
  126. displayResults(results);
  127. }),
  128. 1000
  129. );
  130. }
  131. function closeSearch() {
  132. $body.removeClass('with-search');
  133. if ($xsMenu.css('display') === 'block') {
  134. $mainContainer.css('height', 'calc(100% - 50px)');
  135. }
  136. }
  137. function bindMenuButton() {
  138. document.getElementById('btn-menu').addEventListener('click', function () {
  139. if ($xsMenu.css('display') === 'none') {
  140. $body.removeClass('with-search');
  141. $mainContainer.css('height', 'calc(100% - 50px)');
  142. }
  143. $.each($searchInputs, function (index, item) {
  144. var item = $(item);
  145. item.val('');
  146. });
  147. });
  148. }
  149. function bindSearch() {
  150. // Bind DOM
  151. $searchInputs = $('#book-search-input input');
  152. $searchResults = $('.search-results');
  153. $searchList = $searchResults.find('.search-results-list');
  154. $searchTitle = $searchResults.find('.search-results-title');
  155. $searchResultsCount = $searchTitle.find('.search-results-count');
  156. $searchQuery = $searchTitle.find('.search-query');
  157. $mainContainer = $('.container-fluid');
  158. $xsMenu = $('.xs-menu');
  159. // Launch query based on input content
  160. function handleUpdate(item) {
  161. var q = item.val();
  162. if (q.length == 0) {
  163. closeSearch();
  164. window.location.href = window.location.href.replace(window.location.search, '');
  165. } else {
  166. launchSearch(q);
  167. }
  168. }
  169. // Detect true content change in search input
  170. var propertyChangeUnbound = false;
  171. $.each($searchInputs, function (index, item) {
  172. var item = $(item);
  173. // HTML5 (IE9 & others)
  174. item.on('input', function (e) {
  175. // Unbind propertychange event for IE9+
  176. if (!propertyChangeUnbound) {
  177. $(this).unbind('propertychange');
  178. propertyChangeUnbound = true;
  179. }
  180. handleUpdate($(this));
  181. });
  182. // Workaround for IE < 9
  183. item.on('propertychange', function (e) {
  184. if (e.originalEvent.propertyName == 'value') {
  185. handleUpdate($(this));
  186. }
  187. });
  188. // Push to history on blur
  189. item.on('blur', function (e) {
  190. // Update history state
  191. if (usePushState) {
  192. var uri = updateQueryString('q', $(this).val());
  193. if ($(this).val() !== '') {
  194. history.pushState({ path: uri }, null, uri);
  195. }
  196. }
  197. });
  198. });
  199. }
  200. function launchSearchFromQueryString() {
  201. var q = getParameterByName('q');
  202. if (q && q.length > 0) {
  203. // Update search inputs
  204. $.each($searchInputs, function (index, item) {
  205. var item = $(item);
  206. item.val(q);
  207. });
  208. // Launch search
  209. launchSearch(q);
  210. }
  211. }
  212. compodoc.addEventListener(compodoc.EVENTS.SEARCH_READY, function (event) {
  213. bindSearch();
  214. bindMenuButton();
  215. launchSearchFromQueryString();
  216. });
  217. function getParameterByName(name) {
  218. var url = window.location.href;
  219. name = name.replace(/[\[\]]/g, '\\$&');
  220. var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)', 'i'),
  221. results = regex.exec(url);
  222. if (!results) return null;
  223. if (!results[2]) return '';
  224. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  225. }
  226. function updateQueryString(key, value) {
  227. value = encodeURIComponent(value);
  228. var url = window.location.href;
  229. var re = new RegExp('([?&])' + key + '=.*?(&|#|$)(.*)', 'gi'),
  230. hash;
  231. if (re.test(url)) {
  232. if (typeof value !== 'undefined' && value !== null)
  233. return url.replace(re, '$1' + key + '=' + value + '$2$3');
  234. else {
  235. hash = url.split('#');
  236. url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
  237. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
  238. return url;
  239. }
  240. } else {
  241. if (typeof value !== 'undefined' && value !== null) {
  242. var separator = url.indexOf('?') !== -1 ? '&' : '?';
  243. hash = url.split('#');
  244. url = hash[0] + separator + key + '=' + value;
  245. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
  246. return url;
  247. } else return url;
  248. }
  249. }
  250. })(compodoc);