@@ -3,21 +3,16 @@
|
||||
(function($) {
|
||||
|
||||
MaterioSearchApiAjax = function(){
|
||||
|
||||
var _History = window.History,
|
||||
_isloadingresults = false;
|
||||
_$content = $('#content'),
|
||||
isExplore = $('.search-results', _$content).length,
|
||||
isActuality = $('.actuality-items', _$content).length;
|
||||
var _isloadingresults = false;
|
||||
var _$content = $('#content');
|
||||
// TODO: define $content by module settings
|
||||
/**
|
||||
* init()
|
||||
*/
|
||||
function init(){
|
||||
trace('init module');
|
||||
trace('init MaterioSearchApiAjax');
|
||||
|
||||
initSearchAjax();
|
||||
initHistoryNav();
|
||||
initViewMode();
|
||||
|
||||
};
|
||||
@@ -27,7 +22,6 @@ MaterioSearchApiAjax = function(){
|
||||
*/
|
||||
function initSearchAjax(){
|
||||
// trace('initSearchAjax');
|
||||
// $('#edit-searchfield').focus();
|
||||
|
||||
$('#materio-search-api-search-form').bind('submit', function(event) {
|
||||
// trace('search submited', event);
|
||||
@@ -36,36 +30,28 @@ MaterioSearchApiAjax = function(){
|
||||
loadResults(getSearchKeys());
|
||||
},10);
|
||||
return false;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686
|
||||
$("#edit-searchfield").bind('autocompleteSelect', function(event) {
|
||||
$(this).parents('.form').trigger('submit');
|
||||
});
|
||||
|
||||
_$content
|
||||
.bind('jsp-initialised', function(event, isScrollable){
|
||||
trace('isScrollable = '+isScrollable);
|
||||
// TODO: better to check scroll-y than isscrollable, load next page before the end of scroll
|
||||
if(!isScrollable)
|
||||
infinitScrollPager();
|
||||
// TODO: what happend when there is no more page
|
||||
})
|
||||
.bind('jsp-scroll-y', function(event, scrollPositionY, isAtTop, isAtBottom){
|
||||
if(isAtBottom)
|
||||
infinitScrollPager();
|
||||
});
|
||||
|
||||
$(document)
|
||||
.bind('init-scroller-pager', onInitScrollerPager)
|
||||
.bind('load-scroller-pager', onLoadScrollerPager)
|
||||
.bind('view-mode-changed', onViewModeChanged)
|
||||
.bind('history-state-change', onHistoryStateChange);
|
||||
|
||||
// trigger updated event for direct html loading
|
||||
setTimeout(function(){
|
||||
var event = jQuery.Event('resultschanged');
|
||||
|
||||
if(isExplore)
|
||||
event.container = $('.search-results', _$content);
|
||||
if(isExplore())
|
||||
event.container = '#content .search-results';
|
||||
|
||||
if(isActuality)
|
||||
event.container = $('.actuality-items', _$content);
|
||||
if(isActuality())
|
||||
event.container = '#content .actuality-items';
|
||||
|
||||
$.event.trigger(event);
|
||||
}, 10);
|
||||
@@ -93,6 +79,7 @@ MaterioSearchApiAjax = function(){
|
||||
|
||||
|
||||
if(!_isloadingresults){
|
||||
$.event.trigger('loading-content');
|
||||
_isloadingresults = true;
|
||||
$('#materio-search-api-search-form').addClass('loading');
|
||||
// TODO: record ajax path in a variable from materio_search_api_ajax_init
|
||||
@@ -100,57 +87,75 @@ MaterioSearchApiAjax = function(){
|
||||
{'types':types},
|
||||
function(json){
|
||||
trace('json', json);
|
||||
$.event.trigger('resultsloaded');
|
||||
$.event.trigger('loaded-content');
|
||||
_isloadingresults = false;
|
||||
$('#materio-search-api-search-form').removeClass('loading');
|
||||
updateContent(json);
|
||||
|
||||
var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.search_path + '/' + json.keys;
|
||||
|
||||
var event = jQuery.Event('new-history-page');
|
||||
event.path = path;
|
||||
event.title = json.keys;
|
||||
event.content = json.return;
|
||||
$.event.trigger(event);
|
||||
|
||||
for (language in Drupal.settings.materio_search_api_ajax.languages) {
|
||||
var l = Drupal.settings.materio_search_api_ajax.languages[language];
|
||||
$('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys)
|
||||
};
|
||||
|
||||
changeContent(json);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function updateContent(json){
|
||||
function loadActuality(){
|
||||
if(!_isloadingresults){
|
||||
$.event.trigger('loading-content');
|
||||
_isloadingresults = true;
|
||||
$('#materio-search-api-search-form').addClass('loading');
|
||||
|
||||
var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/actuality';
|
||||
$.getJSON(url,
|
||||
function(json){
|
||||
trace('json', json);
|
||||
$.event.trigger('resultsloaded');
|
||||
_isloadingresults = false;
|
||||
$('#materio-search-api-search-form').removeClass('loading');
|
||||
changeContent(json);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function changeContent(json){
|
||||
if(json.return){
|
||||
|
||||
$.event.trigger('loaded-content');
|
||||
$('.inner-content',_$content).html(json.return);
|
||||
|
||||
var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.search_path + '/' + json.keys;
|
||||
|
||||
_History.pushState({content:json.return}, json.keys, path);
|
||||
$('input[name=location]','#feedback-form').attr('value', path);
|
||||
|
||||
for (language in Drupal.settings.materio_search_api_ajax.languages) {
|
||||
var l = Drupal.settings.materio_search_api_ajax.languages[language];
|
||||
$('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys)
|
||||
};
|
||||
|
||||
var event = jQuery.Event('resultschanged');
|
||||
event.container = $('.search-results', _$content);
|
||||
$.event.trigger(event);
|
||||
|
||||
triggerContentChanged();
|
||||
}else{
|
||||
trace('no results');
|
||||
}
|
||||
};
|
||||
|
||||
function triggerContentChanged(){
|
||||
var event = jQuery.Event('resultschanged');
|
||||
event.container = '#content .search-results, #content .actuality-items';
|
||||
$.event.trigger(event);
|
||||
};
|
||||
|
||||
/**
|
||||
* infinit scroll
|
||||
*/
|
||||
function infinitScrollPager(){
|
||||
var $nextpage = $('ul.pager .pager-current', _$content).next(),
|
||||
href = $('a', $nextpage).attr('href');
|
||||
|
||||
if(href){
|
||||
if (isExplore)
|
||||
loadNextResultsPage(href);
|
||||
|
||||
if(isActuality)
|
||||
loadNextActualityPage(href);
|
||||
}
|
||||
function onInitScrollerPager(event){
|
||||
if(isActuality() || isExplore())
|
||||
event.pager.hide();
|
||||
};
|
||||
|
||||
function onLoadScrollerPager(event){
|
||||
if (isExplore())
|
||||
loadNextResultsPage(event.href);
|
||||
|
||||
if(isActuality())
|
||||
loadNextActualityPage(event.href);
|
||||
};
|
||||
|
||||
function loadNextResultsPage(href){
|
||||
@@ -192,8 +197,9 @@ MaterioSearchApiAjax = function(){
|
||||
$newpager = $('ul.pager', $newcontent);
|
||||
|
||||
$(container_class, _$content).append($newitems);
|
||||
$('ul.pager', _$content).replaceWith($newpager);
|
||||
$('ul.pager', _$content).replaceWith($newpager.hide());
|
||||
|
||||
// TODO: animation, this should be on theme side
|
||||
$(container_class, _$content).children('.just-added').each(function(i){
|
||||
// $(this).delay(5000*i).removeClass('just-added');
|
||||
var $this = $(this);
|
||||
@@ -207,28 +213,14 @@ MaterioSearchApiAjax = function(){
|
||||
$.event.trigger(event);
|
||||
};
|
||||
|
||||
/**
|
||||
* history navigation
|
||||
*/
|
||||
function initHistoryNav(){
|
||||
var state = _History.getState();
|
||||
_History.log('initial:', state.data, state.title, state.url);
|
||||
|
||||
_History.Adapter.bind(window,'statechange',function(){
|
||||
var state = _History.getState();
|
||||
_History.log('statechange:', state.data, state.title, state.url);
|
||||
// TODO: History : empty content if we go back to the homepage
|
||||
$('.inner-content',_$content).html(state.data.content);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* viewmode
|
||||
*/
|
||||
function initViewMode(){
|
||||
$('.viewmode-link').click(function(event){
|
||||
event.preventDefault();
|
||||
changeViewMode($(this).attr('rel'), $(this));
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -236,7 +228,8 @@ MaterioSearchApiAjax = function(){
|
||||
$.getJSON(Drupal.settings.basePath+'materio_search_api_ajax/viewmode/change/'+vm, function(json){
|
||||
trace('viewmode json', json);
|
||||
if (json.statut == "saved"){
|
||||
loadResults(getSearchKeys());
|
||||
// loadResults(getSearchKeys());
|
||||
$.event.trigger('view-mode-changed');
|
||||
$('.viewmode-link').removeClass('active');
|
||||
$btn.addClass('active');
|
||||
}
|
||||
@@ -244,6 +237,34 @@ MaterioSearchApiAjax = function(){
|
||||
});
|
||||
};
|
||||
|
||||
function onViewModeChanged(event){
|
||||
if (isExplore())
|
||||
loadResults(getSearchKeys());
|
||||
|
||||
if(isActuality())
|
||||
loadActuality();
|
||||
};
|
||||
|
||||
/**
|
||||
* history
|
||||
*/
|
||||
function onHistoryStateChange(event){
|
||||
if(isExplore() || isActuality())
|
||||
triggerContentChanged();
|
||||
};
|
||||
|
||||
/**
|
||||
* helpers
|
||||
*/
|
||||
function isExplore(){
|
||||
return $('.search-results', '#content').length;
|
||||
};
|
||||
|
||||
function isActuality(){
|
||||
return $('.actuality-items', '#content').length;
|
||||
};
|
||||
|
||||
|
||||
|
||||
init();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user