meilisearch/Resources/Public/JavaScript/search_controller.js

55 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
function SearchController() {
var _this = this;
_this.ajaxType = 7383;
this.init = function() {
2021-04-17 21:20:54 +02:00
jQuery("body").delegate("a.meilisearch-ajaxified", "click", _this.handleClickOnAjaxifiedUri);
2021-04-17 00:26:33 +02:00
};
this.handleClickOnAjaxifiedUri = function() {
var clickedLink = jQuery(this);
2021-04-17 21:20:54 +02:00
var meilisearchContainer = clickedLink.closest(".tx_meilisearch");
var meilisearchParent = meilisearchContainer.parent();
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
var loader = jQuery("<div class='tx-meilisearch-loader'></div>");
2021-04-17 00:26:33 +02:00
var uri = clickedLink.uri();
2021-04-17 21:20:54 +02:00
meilisearchParent.append(loader);
2021-04-17 00:26:33 +02:00
uri.addQuery("type", _this.ajaxType);
jQuery.get(
uri.href(),
function(data) {
2021-04-17 21:20:54 +02:00
meilisearchContainer = meilisearchContainer.replaceWith(data);
_this.scrollToTopOfElement(meilisearchParent, 50);
2021-04-17 00:26:33 +02:00
jQuery("body").trigger("tx_meilisearch_updated");
loader.fadeOut().remove();
history.replaceState({}, null, uri.removeQuery("type").href());
}
);
return false;
};
this.scrollToTopOfElement = function(element, deltaTop) {
jQuery('html, body').animate({
scrollTop: (element.offset().top - deltaTop) + 'px'
}, 'slow');
};
this.setAjaxType = function(ajaxType) {
_this.ajaxType = ajaxType;
};
}
jQuery(document).ready(function() {
2021-04-17 21:20:54 +02:00
var meilisearchSearchController = new SearchController();
meilisearchSearchController.init();
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
if(typeof meilisearchSearchAjaxType !== "undefined") {
meilisearchSearchController.setAjaxType(meilisearchSearchAjaxType);
2021-04-17 00:26:33 +02:00
}
});