Skip to content
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

New searchbar design #12

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ps_searchbar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#header, #header .header-top {
background-color: white;
}

#search_widget {
margin-bottom: .625rem;
overflow: auto;
}
#search_widget form {
position: relative;
}
#search_widget form i {
position: absolute;
padding: 8px;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use rem rather than px

}
#search_widget form i.clear {
right: 15px;
display: none;
}
#search_widget form input {
width: 100%;
padding: 10px 40px;
outline: none;
background-color: #f1f1f1;
border: none;
border-radius: 5px;
}

.ui-autocomplete.searchbar-autocomplete {
width: 100%;
min-height: 100%;
border: none;
}

.ui-autocomplete.searchbar-autocomplete li a, .ui-autocomplete.searchbar-autocomplete li a.ui-state-focus {
padding: 8px 15px;
overflow: auto;
border: none;
background: none;
margin: auto;
border-radius: 0;
}

.ui-autocomplete.searchbar-autocomplete li a:hover {
background-color: #f1f1f1;
cursor: pointer;
}

.ui-autocomplete.searchbar-autocomplete li a .autocomplete-thumbnail {
float: left;
width: 50px;
height: 50px;
margin-right: 8px;
}

.ui-autocomplete.searchbar-autocomplete li a .autocomplete-thumbnail img {
width: 100%;
height: auto;
}

@media only screen and (min-width: 768px) {
#search_widget {
float: right;
margin-bottom: 0;
}

.ui-autocomplete.searchbar-autocomplete {
width: 400px;
min-height: auto;
left: auto;
}
}

@media only screen and (min-width: 992px) {
#search_widget {
min-width: 15.63rem;
}
}
42 changes: 40 additions & 2 deletions ps_searchbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@ $(document).ready(function () {
var $searchWidget = $('#search_widget');
var $searchBox = $searchWidget.find('input[type=text]');
var searchURL = $searchWidget.attr('data-search-controller-url');
var $clearButton = $searchWidget.find('i.clear');

$.widget('prestashop.psBlockSearchAutocomplete', $.ui.autocomplete, {
_renderItem: function (ul, product) {
var $img = $('<div class="autocomplete-thumbnail">');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var $img actually references a <div>

if (product.cover) {
$img.append('<img src="'+product.cover.bySize.small_default.url+'">')
} else {
$img.append('<img src="'+prestashop.urls.no_picture_image.bySize.small_default.url+'">')
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this could be made simpler:

const coverImage = (product.cover) ? product.cover : prestashop.urls.no_picture_image;

return $('<li>')
  .append($('<a>'))
     .append($('<img>').attr('src', coverImage.bySize.small_default.url))
     .append(/* ... */)

return $("<li>")
.append($("<a>")
.append($("<span>").html(product.category_name).addClass("category"))
.append($("<span>").html(' > ').addClass("separator"))
.append($img)
.append($("<span>").html(product.name).addClass("product"))
).appendTo(ul)
;
}
});

var isMobile = function() {
return $(window).width() < 768;
};
var autocompletePosition = function() {
return {
my: 'right top',
at: 'right bottom',
of: isMobile() ? '.header-top' : '#search_widget',
};
};

$searchBox.psBlockSearchAutocomplete({
position: autocompletePosition(),
source: function (query, response) {
$.post(searchURL, {
s: query.term,
Expand All @@ -31,5 +49,25 @@ $(document).ready(function () {
var url = ui.item.url;
window.location.href = url;
},
}).psBlockSearchAutocomplete("widget").addClass('searchbar-autocomplete');

$(window).resize(function() {
$searchBox.psBlockSearchAutocomplete({
position: autocompletePosition(),
});
$searchBox.keyup();
});

$clearButton.click(function() {
$searchBox.val("");
$clearButton.hide();
});

$searchBox.keyup(function() {
if ($searchBox.val() !== "" && isMobile()) {
$clearButton.show();
} else {
$clearButton.hide();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify using .toggle():

$clearButton.toggle($searchBox.val() !== "" && isMobile());

});
});
1 change: 1 addition & 0 deletions ps_searchbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function install()
public function hookHeader()
{
$this->context->controller->addJqueryUI('ui.autocomplete');
$this->context->controller->registerStylesheet('modules-searchbar', 'modules/' . $this->name . '/ps_searchbar.css');
$this->context->controller->registerJavascript('modules-searchbar', 'modules/'.$this->name.'/ps_searchbar.js', ['position' => 'bottom', 'priority' => 150]);
}

Expand Down
15 changes: 7 additions & 8 deletions ps_searchbar.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
* International Registered Trademark & Property of PrestaShop SA
*}

<div id="search_widget" data-search-controller-url="{$search_controller_url}">
<form method="get" action="{$search_controller_url}">
<input type="hidden" name="controller" value="search">
<input type="text" name="s" value="{$search_string}">
<button type="submit">
{l s='Search' d='Modules.Searchbar.Shop'}
</button>
</form>
<div id="search_widget" class="search-widgets" data-search-controller-url="{$search_controller_url}">
<form method="get" action="{$search_controller_url}">
<input type="hidden" name="controller" value="search">
eternoendless marked this conversation as resolved.
Show resolved Hide resolved
<i class="material-icons search">&#xE8B6;</i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to use aria-hidden="true" for icons so that they are hidden from screen readers.

Suggested change
<i class="material-icons search">&#xE8B6;</i>
<i class="material-icons search" aria-hidden="true">&#xE8B6;</i>

Also, you can change &#xE8B6; to the actual icon name from material icons (eg. "search")

<input type="text" name="s" value="{$search_string}" placeholder="{l s='Search our catalog' d='Modules.Searchbar.Catalog'}" aria-label="{l s='Search' d='Shop.Theme.Catalog'}">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules.Searchbar.Catalog

I don't think this domain is right, wdyt @LouiseBonnard ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<input type="text" name="s" value="{$search_string}" placeholder="{l s='Search our catalog' d='Modules.Searchbar.Catalog'}" aria-label="{l s='Search' d='Shop.Theme.Catalog'}">
<input type="text" name="s" value="{$search_string}" placeholder="{l s='Search our catalog' d='Shop.Theme.Catalog'}" aria-label="{l s='Search' d='Shop.Theme.Catalog'}">

<i class="material-icons clear">&#xe14c;</i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as with the previous icon

</form>
</div>