diff --git a/app/assets/javascripts/application/search.js b/app/assets/javascripts/application/search.js index d3f0b7fb03ddab9a56d06371ab61e962ce840789..16d286a245fd942327d0507d010d26360d28d0f2 100644 --- a/app/assets/javascripts/application/search.js +++ b/app/assets/javascripts/application/search.js @@ -3,12 +3,17 @@ var search = function() { var intervalId = 0; var params = {}; //// initialize search parameters - params.query = ""; - params.order = ""; - params.subject = []; - params.type = []; - params.school_year = []; - params.year = ""; + resetParam(); + + function resetParam() { + params.query = ""; + params.page = 1; + params.order = ""; + params.subject = []; + params.type = []; + params.school_year = []; + params.year = ""; + } $(document).ready(function () { checkUrl(); @@ -49,6 +54,7 @@ var search = function() { function watchSearchInput() { $('.search-input form').on('submit', function (event) { event.preventDefault(); + resetParam(); params.query = $('input[name="query"]', this).val(); fetch(); }); @@ -56,11 +62,19 @@ var search = function() { function watchOrder() { $('select[name="order"]').on('change', function (event) { - params.order = $(this).val() + params.order = $(this).val(); fetch(); }); } + function watchMoreResults() { + $('.search-more').on('click', function (event) { + event.preventDefault(); + params.page += 1; + fetch(true); + }); + } + function watchFilters() { $('.search-link').on('change', function(e) { // cancel previous function call @@ -135,22 +149,46 @@ var search = function() { } // get objects html to embed - function fetch() { + function fetch(append) { + // append result - see more + if (append === undefined || append === false) { + append = false; + + // reset pagination + params.page = 1; + } + + // change url with new parameters var url = createUrlFromParameters(); history.pushState(state, url, url); + + $.ajax({ url:"/search/fetch", type: 'get', data: params, beforeSend: function() { - $("#search-fetch").empty(); - $("#search-fetch").append('<br><br><div style="text-align:center;"><div class="pong-loader">Carregando...</div></div>'); + if (append) { + $('.search-more').remove(); + } + else { + $("#search-fetch").empty(); + } + $("#search-fetch").append('<div class="search-loading" style="text-align:center;"><br><div class="pong-loader">Carregando...</div></div>'); } }) .done(function (data) { - $("#search-fetch").empty(); + if (append) { + $('.search-more, .search-loading').remove(); + } + else { + $("#search-fetch").empty(); + } + $("#search-fetch").append(data); + + watchMoreResults(); }); } }; diff --git a/app/views/search/_fetch.html.erb b/app/views/search/_fetch.html.erb index 3f09466eba8779b1c955e4ea7eb24e8653d009ef..64b030930660086189807e91d6c1a8e53d69b59a 100644 --- a/app/views/search/_fetch.html.erb +++ b/app/views/search/_fetch.html.erb @@ -1,2 +1,2 @@ <%= render @objects, orientation: "horizontal" %> -<%= paginate @result %> +<a href="javascript:void(0);" class="btn btn-primary search-more">Ver mais</a>