-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add an instant post search #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f45c31c
Add an instant post search
voronkovich da43022
Fix cs issues
voronkovich 0c236ed
Refactored the feature
javiereguiluz f219db9
Changes requested by reviewers
javiereguiluz 51ab96a
Added missing search.js file
javiereguiluz fe2e2ab
Fixed CS issues
javiereguiluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block javascripts %} | ||
{{ parent() }} | ||
<script src="{{ asset('build/js/search.js') }}"></script> | ||
{% endblock %} | ||
|
||
{% block main %} | ||
<form action="{{ path('blog_search') }}" method="get"> | ||
<div class="form-group"> | ||
<input name="q" type="text" class="form-control search-field" placeholder="{{ 'post.search_for'|trans }}" autocomplete="off" autofocus> | ||
</div> | ||
</form> | ||
|
||
<div id="results"> | ||
</div> | ||
{% endblock %} | ||
|
||
{% block sidebar %} | ||
{{ parent() }} | ||
|
||
{{ show_source_code(_self) }} | ||
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* jQuery plugin for an instant searching. | ||
* | ||
* @author Oleg Voronkovich <[email protected]> | ||
*/ | ||
(function($) { | ||
$.fn.instantSearch = function(config) { | ||
return this.each(function() { | ||
initInstantSearch(this, $.extend(true, defaultConfig, config || {})); | ||
}); | ||
}; | ||
|
||
var defaultConfig = { | ||
minQueryLength: 2, | ||
maxPreviewItems: 10, | ||
previewDelay: 500, | ||
noItemsFoundMessage: 'No results found.' | ||
}; | ||
|
||
function debounce(fn, delay) { | ||
var timer = null; | ||
return function () { | ||
var context = this, args = arguments; | ||
clearTimeout(timer); | ||
timer = setTimeout(function () { | ||
fn.apply(context, args); | ||
}, delay); | ||
}; | ||
} | ||
|
||
var initInstantSearch = function(el, config) { | ||
var $input = $(el); | ||
var $form = $input.closest('form'); | ||
var $preview = $('<ul class="search-preview list-group">').appendTo($form); | ||
|
||
var setPreviewItems = function(items) { | ||
$preview.empty(); | ||
|
||
$.each(items, function(index, item) { | ||
if (index > config.maxPreviewItems) { | ||
return; | ||
} | ||
|
||
addItemToPreview(item); | ||
}); | ||
} | ||
|
||
var addItemToPreview = function(item) { | ||
var $link = $('<a>').attr('href', item.url).text(item.title); | ||
var $title = $('<h3>').attr('class', 'm-b-0').append($link); | ||
var $summary = $('<p>').text(item.summary); | ||
var $result = $('<div>').append($title).append($summary); | ||
|
||
$preview.append($result); | ||
} | ||
|
||
var noItemsFound = function() { | ||
var $result = $('<div>').text(config.noItemsFoundMessage); | ||
|
||
$preview.empty(); | ||
$preview.append($result); | ||
} | ||
|
||
var updatePreview = function() { | ||
var query = $.trim($input.val()).replace(/\s{2,}/g, ' '); | ||
|
||
if (query.length < config.minQueryLength) { | ||
$preview.empty(); | ||
return; | ||
} | ||
|
||
$.getJSON($form.attr('action') + '?' + $form.serialize(), function(items) { | ||
if (items.length === 0) { | ||
noItemsFound(); | ||
return; | ||
} | ||
|
||
setPreviewItems(items); | ||
}); | ||
} | ||
|
||
$input.focusout(function(e) { | ||
$preview.fadeOut(); | ||
}); | ||
|
||
$input.focusin(function(e) { | ||
$preview.fadeIn(); | ||
updatePreview(); | ||
}); | ||
|
||
$input.keyup(debounce(updatePreview, config.previewDelay)); | ||
} | ||
})(window.jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import './jquery.instantSearch.js'; | ||
|
||
$(function() { | ||
$('.search-field').instantSearch({ | ||
previewDelay: 100, | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiereguiluz, you forgot to add a widget with the
Show source
button.