materio-base-legacy/js/materio_search_api_ajax.js
bachy 3b4f1e024f big big big update
Signed-off-by: bachy <git@g-u-i.net>
2012-10-09 21:30:27 +02:00

206 lines
5.5 KiB
JavaScript

// @codekit-prepend "gui.js"
(function($) {
MaterioBaseMod = function(){
var _History = window.History,
_isloadingresults = false;
_$content = $('#content');
// TODO: define $content by module settings
/**
* init()
*/
function init(){
trace('init module');
// $('#root').bind('resultsupdated', function(event) {
// alert('HO');
// });
$(window).trigger('resultsupdated');
initSearchAjax();
initHistoryNav();
initViewMode();
};
/**
* searchAjax
*/
function initSearchAjax(){
// trace('initSearchAjax');
$('#edit-searchfield').focus();
$('#materio-search-api-search-form').bind('submit', function(event) {
// trace('search submited', event);
var $this = $(this);
setTimeout(function(){
// trace('submit load');
loadResults(getSearchKeys());
},10);
return false;
});
// /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686
$("#edit-searchfield").bind('autocompleteSelect', function(event) {
// trace('autocompleteSelect - event', event);
// trace('val', $(this).val());
// var $form = $(this).parents('form');
// setTimeout(function(){
// $form.submit();
// }, 10);
loadResults($(this).val());
});
_$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)
loadNextResultsPage();
// TODO: what happend when there is no more page
})
.bind('jsp-scroll-y', function(event, scrollPositionY, isAtTop, isAtBottom){
if(isAtBottom)
loadNextResultsPage();
});
};
function loadResults(keys){
// trace('keys', keys);
if(keys !== undefined){
keys = keys.replace('/', ' ');
if(!_isloadingresults){
_isloadingresults = true;
// TODO: record ajax path in a variable from materio_search_api_ajax_init
$.getJSON('/materio_search_api_ajax/search/'+keys, function(json){
// trace('json', json);
$(window).trigger('resultsloaded');
_isloadingresults = false;
updateContent(json);
});
}
}
};
function updateContent(json){
if(json.return){
// TODO: set jscrollpane to top
$('.inner-content',_$content).html(json.return);
// TODO: this a DIRTY sheet of what could be refresh results transitions
// var $newContent = $(json.return);
// trace('newContent', $newContent);
// $('.node-materiau',$newContent).hide();
// $('.node-materiau','#content').fadeOut(300);
// setTimeout(function(){
// $('#content').html($newContent);
// $('.node-materiau','#content').fadeIn(500);
// },300);
var event = jQuery.Event('resultsupdated');
event.container = $('.search-results', _$content);
$(window).trigger(event);
_History.pushState({content:json.return}, json.keys, '/'+json.search_path + '/' +json.keys);
// navigate({content:json.return}, '', '/'+json.page.path + '/' +json.keys);
}else{
alert('no results');
}
};
function getSearchKeys(){
return $('#materio-search-api-search-form').find('input[name*="searchfield"]').val();
};
/**
* infinit scroll
*/
function loadNextResultsPage(){
var $nextpage = $('ul.pager .pager-current', _$content).next(),
href = $('a', $nextpage).attr('href');
if(href){
var keys = href.match(/explore\/([^\/|\?]+)/);
var page = href.match(/\?page=([0-9]+)/);
if(!_isloadingresults){
_isloadingresults = true;
$.getJSON('/materio_search_api_ajax/search/'+keys[1]+'/'+page[1], function(json){
// trace('json', json);
_isloadingresults = false;
addNextpage(json);
});
}
}
};
function addNextpage(json){
var $newcontent = $(json.return),
$newresults = $('.search-results article', $newcontent),
$newpager = $('ul.pager', $newcontent);
$('.search-results', _$content).append($newresults);
$('ul.pager', _$content).replaceWith($newpager);
var event = jQuery.Event('resultscompleted');
event.container = $('.search-results', _$content);
$(window).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){
changeViewMode($(this).attr('rel'), $(this));
});
};
function changeViewMode(vm, $btn){
$.getJSON('/materiosearchapi/viewmode/change/'+vm, function(json){
// trace('viewmode json', json);
if (json.statut == "saved"){
loadResults(getSearchKeys());
$('.viewmode-link').removeClass('active');
$btn.addClass('active');
}
});
};
init();
};
var materiobasemod = new MaterioBaseMod();
})(jQuery);