class QuickSearch::Generators::TypeaheadGenerator
Public Instance Methods
generate_quick_search_typeahead_js()
click to toggle source
# File lib/generators/quick_search/typeahead_generator.rb, line 8 def generate_quick_search_typeahead_js unless QuickSearch::Engine::APP_CONFIG.has_key? 'typeahead' puts 'No typeahead configuration found in quick_search_config.yml' else QuickSearch::Engine::APP_CONFIG['typeahead'].each do |searcher_name, searcher_config| puts insert_bloodhound_config(searcher_name, searcher_config) end end end
Private Instance Methods
insert_bloodhound_config(searcher_name, searcher_config)
click to toggle source
# File lib/generators/quick_search/typeahead_generator.rb, line 121 def insert_bloodhound_config(searcher_name, searcher_config) url = searcher_config.has_key?('url') ? searcher_config['url'] : File.join(quick_search_path, "/typeahead?searcher=#{searcher_name}") if searcher_config['remote_type'] == 'prefetch' remote = "prefetch: '#{url}'," else remote = <<-REMOTE remote: { url: #{url} + '%QUERY', prepare: function(query, settings) { settings.dataType = 'jsonp'; settings.url = settings.url.replace('%QUERY', query); return settings; }, filter: function(response){ return response.#{searcher_config['response']} %>; }, rateLimitWait: 100, }, REMOTE end bloodhound_config = <<-BLOODHOUND // TYPEAHEAD CONFIG FOR #{searcher_name} // AUTOMATICALLY INSERTED BY QUICK_SEARCH // DO NOT EDIT THIS MANUALLY! USE quick_search:typeahead GENERATOR! // SEE http://github.com/ncsu-libraries/quick_search/ FOR DOCS var #{searcher_name} = new Bloodhound ({ queryTokenizer: Bloodhound.tokenizers.whitespace, datumTokenizer: function (datum) { return datum.#{searcher_config['value']}.split(" "); }, valueKey: '#{searcher_config['value']}', #{remote} }); BLOODHOUND end
insert_typeahead_head()
click to toggle source
# File lib/generators/quick_search/typeahead_generator.rb, line 19 def insert_typeahead_head <<-TYPEAHEAD_HEAD /* * QuickSearch Typeahead front-end configuration and setup * ** INSERTED AUTOMATICALLY BY QuickSearch ** * ** DO NOT EDIT MANUALLY! USE quick_search:typeahead RAILS GENERATOR * ** SEE DOCS @ https://www.github.com/ncsu-libraries/quick_search */ jQuery(document).ready(function() { TYPEAHEAD_HEAD end
insert_typeahead_utility()
click to toggle source
# File lib/generators/quick_search/typeahead_generator.rb, line 40 def insert_typeahead_utility log_event_path = File.join(quick_search_path, '/log_event') <<-TYPEAHEAD_UTILITY /* * Typeahead utility functions, currently handles: known vs. unknown item clicks, URL construction, logging. */ var typeaheadUtility = (function () { function listenForTypeaheadClicks() { jQuery('.quicksearch-typeahead').on('typeahead:selected', function (e, datum, dataset) { var link = { href: handleTypeaheadClick(datum, dataset) }; var eventValuesDefined = { category: 'typeahead-' + dataset, action: datum.value, label: window.location.href } sendEventData(eventValuesDefined, link); setTimeout(function() { document.location.href = link.href; }, 200); }); } function sendEventData (eventValues, link) { jQuery.when(logEventToDatabase(eventValues)).then(function () { sendDataToGoogleAnalytics(eventValues, link) }); } function sendDataToGoogleAnalytics (eventValues, link) { _gaq.push(['_set','hitCallback',function() { if (link !== '') { document.location = link; } }]); ga('send', eventValues.category, eventValues.action, eventValues.label); } function logEventToDatabase(eventValues) { var url = '#{log_event_path}'; return jQuery.ajax({ url: url, data: { category: eventValues.category, event_action: eventValues.action, label: eventValues.label }, jsonp: 'callback', dataType: 'jsonp' }); } function handleTypeaheadClick(datum, dataset) { if (datum.url || datum.path) { return datum.url || datum.path; } else if (datum.question_id) { return "//www.lib.ncsu.edu/faq/faq.php?id=" + datum.question_id; } else { // Fire off a search when an unknown element is selected return '/?q=' + datum.ac; } } return { listenForTypeaheadClicks: listenForTypeaheadClicks }; })(); TYPEAHEAD_UTILITY end