toutpratique/js/admin/search.js
2015-08-13 09:59:25 +02:00

144 lines
4.0 KiB
JavaScript

$(document).ready(function(){
var that = this;
that.AjaxCurrent = [];
that.language = {
"NO_RESULT": "Aucun resultat",
"ERROR": "Une erreur est survenue"
}
that.completeObject = function(object)
{
//EXEMPLE
if(object.action == 'getProducts') {
}
return object;
}
$('body').on('click', function(e){
var target = $( e.target );
if ( target.is( 'li.input-search-result' )
|| target.is('.input-search-js')) {
return;
}
$('.searchResult').hide();
});
$('.input-search-js').each(function(){
var object = {};
object.urlAjax = searchUrl;
object.id = $(this).attr('id');
object.$input = $(this);
object.$idContentResultList = $('#content-result-list-'+object.id);
object.$idContentSelected = $('#content-selected-'+object.id);
object.action = object.$input.data('action');
object.typeDisplay = object.action;
object.currentAjax = null;
object.data_query = {};
object.inputNameElement = 'input-'+object.id+'[]';
object.classNameElement = 'class-'+object.id;
object.keys = [];
object = that.completeObject(object);
if(object.$idContentResultList.size() == 0)
console.log('#content-result-list-'+object.id+' not exist');
if(object.$idContentSelected.size() == 0)
console.log('#content-selected-'+object.id+' not exist');
object.$idContentResultList.on('click', 'li', function(){
object.addKey($(this).data('id'), $(this).html());
object.$idContentResultList.html('');
object.$input.val('').focus();
});
object.$idContentSelected.on('click', 'li .removeItem', function(){
object.removeKey($(this).parent().data('id'));
});
object.$idContentSelected.find('li').each(function(){
var id = $(this).data('id')
object.keys[id] = id;
});
object.addKey = function(id, html){
if(typeof(object.keys[id]) == 'undefined') {
var content = '<input type="hidden" name="'+object.inputNameElement+'" value="'+id+'">';
content += '<li class="'+object.classNameElement+' input-search-result-selected" data-id="'+id+'"><button class="removeItem btn btn-default" type="button"><i class="icon-remove"></i></button><span class="content">'+html+'</span></li>';
object.$idContentSelected.append(content);
object.keys[id] = id;
}
};
object.removeKey = function(id){
if(typeof(object.keys[id]) != 'undefined') {
object.$idContentSelected.find('input[name="'+object.inputNameElement+'"][value='+id+']').remove();
object.$idContentSelected.find('.'+object.classNameElement+'[data-id='+id+']').remove();
object.keys[id] = undefined;
}
};
$(this).on('click keyup keydown', function(){
$('.searchResult').show();
that.loadElem(object, $(this).val());
});
});
that.loadElem = function(object, filter)
{
if(filter == '')
return;
if(typeof(that.AjaxCurrent[object.id]) == 'undefined') {
that.AjaxCurrent[object.id] = null;
}
if(that.AjaxCurrent[object.id] !== null)
that.AjaxCurrent[object.id].abort();
datas = {
"q": filter,
"ajax": 1,
"action": object.action
};
if(typeof(datas_) != 'undefined')
datas = $.extend( datas, object.data_query );
that.AjaxCurrent[object.id] = $.ajax({
type: 'POST',
url: object.urlAjax,
dataType : "json",
data : datas,
success:function(data){
var list = data.results;
var content = '';
if(typeof(list) != 'undefined' && list !== false) {
content += '<ul>';
for(var g = 0; g < list.length; g++) {
var elem = list[g];
content += '<li data-id="'+elem.id+'" class="input-search-result">'+elem.content+'</li>';
}
content += '</ul>';
}
else {
content += that.language.NO_RESULT;
}
$(object.$idContentResultList).html(content);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
if (textStatus != "abort") {
$(object.$idContentResultList).html('<div class="alert alert-error">'+that.language.ERROR+'</div>');
}
}
});
}
});