diff --git a/app/assets/javascripts/application/search.js b/app/assets/javascripts/application/search.js index f99cd6b2ecc185fa24e74db2dcc690ce22ec233e..4046990f11b7b1b1a11f4595c8e0f47053419d08 100644 --- a/app/assets/javascripts/application/search.js +++ b/app/assets/javascripts/application/search.js @@ -468,3 +468,42 @@ var runDropdown = function(element) { $(element).next().data('dropdown', 'closed'); } }; + + +var getElementsNumber = function(className) { + + $.ajax({ + url: '/search/get_class_size', + data: { class: className }, + dataType: "json", + success: function(data) { + switch(className) { + case 'LearningObject': + $('.search-input input').attr('placeholder', "Buscar em " + data.number + " conteúdos"); + break; + case 'Collection': + $('.search-input input').attr('placeholder', "Buscar em " + data.number + " coleções"); + break; + case 'User': + $('.search-input input').attr('placeholder', "Buscar em " + data.number + " usuários"); + break; + default: + $('.search-input input').attr('placeholder', "Buscar conteúdo"); + } + }, + error: function() { + $('.search-input input').attr('placeholder', "Buscar conteúdo"); + } + }); +}; + + +$(document).ready (function(){ + + getElementsNumber('LearningObject'); + + $('.search-input select').change( function(){ + getElementsNumber( $(this).val()); + }) + +}); diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 98ffbd562839ba7e581120ad7b1f237f74286657..dc7d2368f4456c1a5387cde2ce22b284694fab18 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -51,6 +51,11 @@ class SearchController < ApplicationController render json: autocomplete_search(Object.const_get(params[:search_class]), params_hash, get_thumbnail) end + def get_class_size + className = params[:class] + render json: { number: Object.const_get(className).count } + end + private def autocomplete_search(search_class, params_hash={}, get_thumbnail) @@ -126,6 +131,7 @@ class SearchController < ApplicationController end end + private def validate_object diff --git a/app/views/shared/application/_search.erb b/app/views/shared/application/_search.erb index 42612350832dd6f2886afb3e1b5d87c30b297786..98bd06af1ea2bd07eb93300c5d9d383f0327aeb9 100644 --- a/app/views/shared/application/_search.erb +++ b/app/views/shared/application/_search.erb @@ -1,7 +1,7 @@ <div class="search-input"> <form action="/search" method="get"> <div class="input-group" id="search"> - <input type="text" placeholder="buscar em <%= number_with_delimiter(LearningObject.count, delimiter: ".") %> conteúdos" class="form-control" name="query" id="main_search"> + <input type="text" placeholder="" class="form-control" name="query" id="main_search"> <select name="search_class" id="search_class" class="form-control"> <option value="LearningObject" selected>Conteúdos Educacionais</option> <option value="Collection">Coleções</option> @@ -13,4 +13,4 @@ </div> </form> <input type="hidden" id="search_class" value=""/> -</div> \ No newline at end of file +</div> diff --git a/config/routes.rb b/config/routes.rb index dc72339b3f249719b33df56c3bb4ee58d4337cfd..47da59df4fc0cc5d82eb98d0e445f283c7980015 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -130,6 +130,7 @@ Rails.application.routes.draw do get '/search' => 'search#index', as: 'search' get '/search/fetch' => 'search#fetch', as: 'search_fetch' get '/autocomplete' => 'search#autocomplete' + get '/search/get_class_size' => 'search#get_class_size', as: :get_class_size get '/subject/:id' => 'welcome#subject', as: 'subject_index' get '/add_file' => 'welcome#add_file'