searcher.html 3.3 KB

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