-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add an instant search (via AJAX) #173
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
// Exposes jQuery as a global variable | ||
global.$ = global.jQuery = require('jquery'); | ||
|
||
// loads the Bootstrap jQuery plugins | ||
import 'bootstrap-sass/assets/javascripts/bootstrap/dropdown.js'; | ||
import 'bootstrap-sass/assets/javascripts/bootstrap/modal.js'; | ||
import 'bootstrap-sass/assets/javascripts/bootstrap/transition.js'; | ||
|
||
// loads the code syntax highlighting library | ||
import './highlight.js'; | ||
|
||
// loads the instant search library | ||
import './jquery.instantSearch.js'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* 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 items 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.result); | ||
var $li = $('<li class="list-group-item">').append($link); | ||
|
||
$preview.append($li); | ||
} | ||
|
||
var noItemsFound = function() { | ||
var $li = $('<li class="list-group-item">').text(config.noItemsFoundMessage); | ||
|
||
$preview.empty(); | ||
$preview.append($li); | ||
} | ||
|
||
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,4 +59,22 @@ private function createPaginator(Query $query, $page) | |
|
||
return $paginator; | ||
} | ||
|
||
public function findByTerms(array $terms, $limit = Post::NUM_ITEMS) | ||
{ | ||
$queryBuilder = $this->createQueryBuilder('p'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about to shorten it with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its really a bad practice to get variable names under 4 characters so IMO that's definitely not something that should be encouraged in this project There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Symfony Best Practices don't mention about short variable's names, so I prefer to base on Symfony docs. If it'll be changed - so we could change it in Symfony demo too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a Symfony best practice, it's a PHP one. Having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theofidry, @bocharsky-bw I think it's better to create a particular issue to discuss code style for this project. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @voronkovich true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I think now we should following styles already used in this project. |
||
|
||
foreach ($terms as $key => $term) { | ||
$queryBuilder | ||
->orWhere('p.title LIKE :t_'.$key) | ||
->setParameter('t_'.$key, '%'.$term.'%') | ||
; | ||
} | ||
|
||
return $queryBuilder | ||
->orderBy('p.publishedAt', 'DESC') | ||
->setMaxResults($limit) | ||
->getQuery() | ||
->getResult(); | ||
} | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
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.
why not just
$results[] = xxx
?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.
@Koc, I don't know :) Anyway, this code was changed in #613.
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.
We finally used
$results[] = xxx
... mostly because PHPStorm displayed a pretty annoying message suggesting that 😄