searcher.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {% if config.crowi['searcher:url'] %}
  2. <form id="headerSearch" class="navbar-form navbar-left form-inline" role="search">
  3. <div class="form-group">
  4. <input id="searchQuery" name="q" type="text" class="form-control" placeholder="検索文字...">
  5. <button type="submit" class="btn btn-default">検索</button>
  6. <script>
  7. function Searcher () {
  8. };
  9. Searcher.prototype = {
  10. baseUrl: "{{ config.crowi['searcher:url'] }}",
  11. currentQuery: "",
  12. searchData: [],
  13. setData: function (data) {
  14. this.searchData = data;
  15. },
  16. search: function (query) {
  17. var self = this;
  18. self.currentQuery = query;
  19. if (query == "") {
  20. return false;
  21. }
  22. $("#searchQuery").addClass('searching');
  23. $.ajax({
  24. url: self.baseUrl + '/search?type=json&callback=?',
  25. data: {q: query},
  26. cache: false,
  27. dataType: 'jsonp'
  28. }).done(function (data) {
  29. self.setData(data);
  30. self.showSearchWidget();
  31. $("#searchQuery").removeClass('searching');
  32. });
  33. },
  34. showSearchWidget: function () {
  35. if (this.searchData.length > 0) {
  36. $('#headerSearch').popover('show');
  37. }
  38. $("#searchQuery").removeClass('searching');
  39. },
  40. createSearchContent: function () {
  41. var self = this;
  42. var contentHtml = $('<ul class=\"search-list\"></ul>');
  43. $.each(self.searchData.slice(0, 6), function (i, d) {
  44. var $li = $("<li class=\"list-link\"></li>");
  45. var $a = $("<a></a>");
  46. $a.attr('href', d.path).html(d.path + "<br>");
  47. var $span = $("<span class=\"search-description\"></span>");
  48. $span.text(d.body.substr(0, 50) + "...");
  49. $a.append($span);
  50. $li.append($a);
  51. contentHtml.append($li);
  52. });
  53. var $li = $("<li class=\"divider\"></li>");
  54. contentHtml.append($li);
  55. $li = $("<li class=\"next-link\"></li>");
  56. $li.html("<a href=\"" + self.baseUrl + "/search?q=" + encodeURIComponent(self.currentQuery) + "\">もっと見る</a>");
  57. contentHtml.append($li);
  58. return contentHtml;
  59. },
  60. jump: function (query) {
  61. self = this;
  62. top.location.href = self.baseUrl + '/search?q=' + query;
  63. }
  64. };
  65. var SearcherObject = new Searcher();
  66. $('#headerSearch').popover({
  67. placement: 'bottom',
  68. trigger: 'manual',
  69. html: 'true',
  70. content: function () {
  71. return SearcherObject.createSearchContent();
  72. }
  73. });
  74. $('#searchQuery').on('focus', function(e) {
  75. SearcherObject.showSearchWidget();
  76. });
  77. $('#searchQuery').on('blur', function(e) {
  78. $('#headerSearch').popover('hide');
  79. });
  80. $('#headerSearch').on('submit', function(e) {
  81. SearcherObject.jump($("#searchQuery").val());
  82. return false;
  83. });
  84. var previousText = "";
  85. setInterval(function (e) {
  86. var text = $("#searchQuery").val();
  87. if (text != previousText) {
  88. SearcherObject.search(text);
  89. }
  90. previousText = text;
  91. }, 1000);
  92. </script>
  93. </div>
  94. </form>
  95. {% endif %}