diff --git a/Gemfile b/Gemfile index 6f59602a..8c1b1b9a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' if RUBY_PLATFORM == 'java' - gem 'gollum-rjgit_adapter', :git => 'https://github.com/repotag/gollum-lib_rjgit_adapter' # For development purposes + gem 'gollum-rjgit_adapter', :git => 'https://github.com/gollum/rjgit_adapter' # For development purposes gem 'warbler' else gem 'gollum-rugged_adapter', :git => 'https://github.com/gollum/rugged_adapter.git' # For development purposes diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 81192701..08179589 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -461,11 +461,17 @@ module Precious end get '/search' do - @query = params[:q] || '' - wiki = wiki_new - # Sort wiki search results by count (desc) and then by name (asc) - @results = wiki.search(@query).sort { |a, b| (a[:count] <=> b[:count]).nonzero? || b[:name] <=> a[:name] }.reverse - @name = @query + @query = params[:q] + @name = @query + if @query.empty? + @results = [] + @search_terms = [] + else + @page_num = [params[:page_num].to_i, 1].max + @max_count = 10 + wiki = wiki_new + @results, @search_terms = wiki.search(@query) + end mustache :search end diff --git a/lib/gollum/public/gollum/javascript/gollum.js.erb b/lib/gollum/public/gollum/javascript/gollum.js.erb index b8ac53b8..57893108 100755 --- a/lib/gollum/public/gollum/javascript/gollum.js.erb +++ b/lib/gollum/public/gollum/javascript/gollum.js.erb @@ -508,36 +508,39 @@ $(document).ready(function() { var clickPageNav = function (e) { e.preventDefault(); - $.ajax({ - url: $(this).attr('href'), - type: 'GET', - success: function(data) { - var rowDiv = $('#page-history-list', data); - var new_pagination = $('#pagination', data); + if ( !$(this).hasClass('disabled') ) { + $.ajax({ + url: $(this).attr('href'), + type: 'GET', + success: function(data) { + var rowDiv = $('#page-history-list', data); + var new_pagination = $('#pagination', data); - next = $('#pagination #next'); - prev = $('#pagination #prev'); - new_next = new_pagination.find('#next'); - new_prev = new_pagination.find('#prev'); + ['#next', '#prev'].forEach( function (nav_item) { + old_btn = $('#pagination ' + nav_item); + new_btn = new_pagination.find(nav_item); + old_btn.attr('href', new_btn.attr('href')); + if (new_btn.hasClass('disabled')) { + old_btn.addClass('disabled'); + } else { + old_btn.removeClass('disabled'); + } + }); - next[0].hidden = new_next[0].hidden; - prev[0].hidden = new_prev[0].hidden; + $('#page-history-list').replaceWith(rowDiv); - next.children('a').attr('href', new_next.children('a').attr('href')); - prev.children('a').attr('href', new_prev.children('a').attr('href')); - - $('#page-history-list').replaceWith(rowDiv); - - setCheckboxEvents(); - toggleInputs(); - }, - error: function(data, textStatus, errorThrown) { - console.log('something went wrong: ' + textStatus + errorThrown) - } - }); + setCheckboxEvents(); + toggleInputs(); + }, + error: function(data, textStatus, errorThrown) { + console.log('something went wrong: ' + textStatus + errorThrown) + } + }); + } + this.blur(); }; - $("#pagination #next a, #pagination #prev a").each(function(index, element) { + $("#pagination #next, #pagination #prev").each(function(index, element) { $(element).on("click", clickPageNav); }); } @@ -566,6 +569,30 @@ $(document).ready(function() { $.GollumEditor({ NewFile: true, MarkupType: default_markup, commands: editorHotkeys }); } + if($('#search-results').length ){ + $('.toggle-context').each(function () { + var hiddenContext = $(this).parent().next('div.search-context').find('li:hidden'); + if ( !hiddenContext.length ) { + $(this).toggle(); + } else { + $(this).click(function () { + hiddenContext.toggle(); + $(this).toggle(); + }); + } + }); + + var searchQuery = new RegExp(searchTerms.join('|'), 'gi'); //searchTerms provided by the layout template. + $('div.search-context li span').each(function () { + var curText = $(this).html().replace(/"/g, '"').replace(/'/g, '''); + var newText = curText.replace(searchQuery, function (match) { + return '' + match + ''; + }); + + $(this).html(newText); + }); + } + if($('.markdown-body').length ){ // Set text direction (LTR or RTL) preparePage(); diff --git a/lib/gollum/public/gollum/stylesheets/template.scss.erb b/lib/gollum/public/gollum/stylesheets/template.scss.erb index 2a52f2ca..55ee554c 100644 --- a/lib/gollum/public/gollum/stylesheets/template.scss.erb +++ b/lib/gollum/public/gollum/stylesheets/template.scss.erb @@ -650,4 +650,14 @@ a { color: #A31515; } } +} + +div.pagination a.disabled { + pointer-events: none; +} + +.search-results { + .search-context li:nth-child(n+4) { + display: none; + } } \ No newline at end of file diff --git a/lib/gollum/templates/layout.mustache b/lib/gollum/templates/layout.mustache index ae178bb8..dbcb2340 100644 --- a/lib/gollum/templates/layout.mustache +++ b/lib/gollum/templates/layout.mustache @@ -24,6 +24,10 @@ var pageFullPath = '{{escaped_url_path}}'; var pageFormat = '{{format}}'; {{/page}} + {{#has_search_terms}} + var searchTerms = [{{#search_terms}}'{{.}}', {{/search_terms}} + ]; + {{/has_search_terms}} {{#is_create_page}} var default_markup = '{{default_markup}}'; {{/is_create_page}} diff --git a/lib/gollum/templates/pagination.mustache b/lib/gollum/templates/pagination.mustache index cfcc5e78..a100f97f 100644 --- a/lib/gollum/templates/pagination.mustache +++ b/lib/gollum/templates/pagination.mustache @@ -1,11 +1,6 @@ - + \ No newline at end of file diff --git a/lib/gollum/templates/search.mustache b/lib/gollum/templates/search.mustache index 52d45aae..b2c30b05 100644 --- a/lib/gollum/templates/search.mustache +++ b/lib/gollum/templates/search.mustache @@ -1,26 +1,33 @@
- -
+ {{#has_results}} + + {{>pagination}} + +
    - {{#results}} -
  • - {{name}} - ({{count}} matches) +
    + {{#results}} +
  • + {{filename_count}} - {{count}}  + + {{name}} +
  • - {{/results}} + +
    + {{#context}} +
  • {{.}}
  • + {{/context}} +
    + + {{/results}}
+
{{/has_results}} {{#no_results}} @@ -29,11 +36,10 @@

{{/no_results}} + -
diff --git a/lib/gollum/views/pagination.rb b/lib/gollum/views/pagination.rb index 2b60a1b1..c7b50720 100644 --- a/lib/gollum/views/pagination.rb +++ b/lib/gollum/views/pagination.rb @@ -1,7 +1,9 @@ module Precious - module Pagination + module Pagination + def next_page - @versions.length < @max_count ? nil : (@page_num + 1).to_s + result_set = @versions || results + result_set.length < @max_count ? nil : (@page_num + 1).to_s end def previous_page diff --git a/lib/gollum/views/search.rb b/lib/gollum/views/search.rb index 20f5c2a7..ca075f70 100644 --- a/lib/gollum/views/search.rb +++ b/lib/gollum/views/search.rb @@ -1,12 +1,38 @@ module Precious module Views class Search < Layout - attr_reader :content, :page, :footer, :results, :query + attr_reader :query, :search_terms + include Pagination + + def results + @results.sort do |a, b| + if b.nil? + b_filename_count = 0 + b_count = 0 + else + b_filename_count = b[:filename_count] + b_count = b[:count] + end + [a[:filename_count], a[:count]] <=> [b_filename_count, b_count] + end.reverse.slice((@page_num - 1) * @max_count, @max_count) + end + + def query_string + "&q=#{@query}" + end def title "Search results for " + @query end + def search + true # View has searchbar + end + + def has_search_terms + !@search_terms.empty? + end + def has_results !@results.empty? end