$(function () {
boxSearch_init();
//SEARCH DESTINATION
$('#search-destination').off();
$('#search-destination').on('click', function (e) {
e.preventDefault();
$('#search-destination-wrapper').toggleClass('active');
$('.search-destination-input').focus();
});
$('#search-destination-wrapper .modal-backdrop').off();
$('#search-destination-wrapper .modal-backdrop').on('click', function (e) {
e.preventDefault();
$('#search-destination-wrapper').removeClass('active');
});
//SEARCH WEB
$('#search-web').off();
$('#search-web').on('click', function (e) {
e.preventDefault();
$('#search-web-wrapper').toggleClass('active');
$('.search-web-input').focus();
});
//SEARCH WEB
$('#navbarSearchWrapper').off();
$('#navbarSearchWrapper').on('click', function (e) {
e.preventDefault();
$('#search-web-wrapper').toggleClass('active');
$('.search-web-input').focus();
});
$('#search-web-wrapper .modal-backdrop, .search-web-close').off();
$('#search-web-wrapper .modal-backdrop, .search-web-close').on('click', function (e) {
e.preventDefault();
$('#search-web-wrapper').removeClass('active');
});
$('#search-web-category li').off();
$('#search-web-category li').on('click', function (e) {
e.preventDefault();
$('#search-web-category li').removeClass('active');
$(this).addClass('active');
$('.search-web-input').focus();
//console.log($(this).data('id'));
});
/* SEARCH DESTINATION */
if ($('.search-destination-input').length) {
var client_node = $('#client-data');
var client = '0';
if (client_node.length) {
client = client_node.data('type') + '|' + client_node.data('id');
}
$.typeahead({
input: '.search-destination-input',
minLength: 1,
order: "asc",
dynamic: true,
delay: 300,
accent: true,
hint: true,
searchOnFocus: true,
template: function (query, item) {
return '
' +
'{{name}} {{type}}' +
'{{country}}' +
"
"
},
emptyTemplate: "no se encontraron sugerencias para {{query}}, presione ENTER para buscar más resultados.",
source: {
destination: {
display: "name",
href: "/es-{{country}}/{{name|slugify}}/d{{hash}}",
ajax: function (query) {
return {
type: "GET",
url: GR_URL_API + "destination/search/",
dataType: "jsonp",
path: "data.destination",
data: {
q: "{{query}}",
c: client,
cat: $('#search-web-category li.active').data('id'),
},
callback: {
done: function (data) {
for (var i = 0; i < data.data.destination.length; i++) {
switch (data.data.destination[i].type) {
case '0':
data.data.destination[i].type = 'País';
break;
case '1':
data.data.destination[i].type = 'Provincia';
break;
case '2':
data.data.destination[i].type = 'Cantón';
break;
case '3':
data.data.destination[i].type = 'Parroquia';
break;
case '4':
data.data.destination[i].type = 'Pueblo';
break;
case '5':
data.data.destination[i].type = 'Comunidad Autónoma';
break;
case '6':
data.data.destination[i].type = 'Municipio';
break;
case '7':
data.data.destination[i].type = 'Ciudad Autónoma';
break;
}
}
return data;
}
}
}
}
}
},
callback: {
onClick: function (node, a, item, event) {
// You can do a simple window.location of the item.href
//alert(JSON.stringify(item));
},
onSendRequest: function (node, query) {
//console.log('request is sent')
},
onReceiveRequest: function (node, query) {
//console.log('request is received')
}
},
debug: true
});
}
/* SEARCH WEB */
if ($('.search-web-input').length) {
var client_node = $('#client-data');
var client = '0';
if (client_node.length) {
client = client_node.data('type') + '|' + client_node.data('id');
}
var category_node = $('#search-web-category li.active').data('id');
var category = 0;
if (category_node.length) {
category = client_node.data('id');
}
$.typeahead({
input: '.search-web-input',
minLength: 3,
maxItem: 20,
order: "asc",
dynamic: true,
delay: 300,
accent: true,
hint: false,
group: {
template: "{{group}}"
},
maxItemPerGroup: 10,
//dropdownFilter: "all beers",
searchOnFocus: true,
emptyTemplate: "no se encontraron sugerencias para {{query}}, presione ENTER para buscar más resultados.",
//groupTemplate: "",
//groupTemplate: "{{group}}
",
//correlativeTemplate: true,
source: {
'Destinos': {
display: "name",
href: "/" + GR_LANG + "/-{{country}}/{{name|slugify}}/d{{hash}}",
ajax: function (query) {
return {
type: "GET",
url: "/search/destination/",
path: "data.destination",
data: {
lang: GR_LANG,
q: "{{query}}",
c: client,
cat: $('#search-web-category li.active').data('id'),
},
callback: {
done: function (data) {
if (typeof data.data.destination === 'undefined') {
return;
}
for (var i = 0; i < data.data.destination.length; i++) {
switch (data.data.destination[i].type) {
case '0':
data.data.destination[i].type = 'País';
break;
case '1':
data.data.destination[i].type = 'Provincia';
break;
case '2':
data.data.destination[i].type = 'Cantón';
break;
case '3':
data.data.destination[i].type = 'Parroquia';
break;
case '4':
data.data.destination[i].type = 'Pueblo';
break;
case '5':
data.data.destination[i].type = 'Comunidad Autónoma';
break;
case '6':
data.data.destination[i].type = 'Municipio';
break;
case '7':
data.data.destination[i].type = 'Ciudad Autónoma';
break;
}
switch (data.data.destination[i].country) {
case 'ae':
data.data.destination[i].country_name = 'Emiratos Árabes Unidos';
break;
case 'ec':
data.data.destination[i].country_name = 'Ecuador';
break;
case 'ar':
data.data.destination[i].country_name = 'Argentina';
break;
case 'es':
data.data.destination[i].country_name = 'España';
break;
}
}
return data;
}
}
}
},
template: sourceTemplate_destination()
},
'Explora': {
display: "name",
href: "/" + GR_LANG + "-ec/{{name|slugify}}/e{{hash}}" + GR_DESTINATION_DEFAULT, //Ecuador: 58f5d
ajax: function (query) {
return {
type: "GET",
url: "/search/category/",
path: "data.category",
data: {
lang: GR_LANG,
q: "{{query}}",
c: client,
cat: $('#search-web-category li.active').data('id'),
},
callback: {
done: function (data) {
}
}
}
},
template: sourceTemplate_explorer()
},
'Publicaciones': {
display: "name",
href: "/" + GR_LANG + "-{{country}}/{{name|slugify}}-a{{hash}}",
ajax: function (query) {
return {
type: "GET",
url: "/search/item/",
path: "data.item",
data: {
lang: GR_LANG,
q: "{{query}}",
c: client,
cat: $('#search-web-category li.active').data('id'),
},
callback: {
done: function (data) {
if (typeof data.data.item === 'undefined') {
return;
}
for (var i = 0; i < data.data.item.length; i++) {
/*
switch (data.data.item[i].type) {
case '0':
data.data.item[i].type = 'País';
break;
case '1':
data.data.item[i].type = 'Provincia';
break;
case '2':
data.data.item[i].type = 'Cantón';
break;
case '3':
data.data.item[i].type = 'Parroquia';
break;
case '4':
data.data.item[i].type = 'Pueblo';
break;
case '5':
data.data.item[i].type = 'Comunidad Autónoma';
break;
case '6':
data.data.item[i].type = 'Municipio';
break;
case '7':
data.data.item[i].type = 'Ciudad Autónoma';
break;
}
*/
/*
switch (data.data.item[i].country) {
case 'ae':
data.data.item[i].country_name = 'Emiratos Árabes Unidos';
break;
case 'ec':
data.data.item[i].country_name = 'Ecuador';
break;
case 'ar':
data.data.item[i].country_name = 'Argentina';
break;
case 'es':
data.data.item[i].country_name = 'España';
break;
}
*/
}
return data;
}
}
}
},
template: sourceTemplate_item()
}
},
callback: {
onClick: function (node, a, item, event) {
//console.log('result selected')
// You can do a simple window.location of the item.href
//alert(JSON.stringify(item));
},
onSendRequest: function (node, query) {
/*
console.log($('.search-web-input').length);
$('#search-web-category').addClass('active');
*/
//console.log('request is sent')
},
onReceiveRequest: function (node, query) {
//console.log('request is received')
}
},
debug: true
});
}
/*
//C V1
$.typeahead({
input: '.js-typeahead-user_v1',
minLength: 1,
order: "asc",
dynamic: true,
delay: 500,
backdrop: {
"background-color": "#fff"
},
template: function (query, item) {
var color = "#777";
if (item.status === "owner") {
color = "#ff1493";
}
return '' +
'' +
'
' +
"" +
'{{username}} ({{status}})' +
'({{id}})' +
""
},
emptyTemplate: "no result for {{query}}",
source: {
user: {
display: "username",
href: "https://www.github.com/{{username|slugify}}",
data: [{
"id": 415849,
"username": "an inserted user that is not inside the database",
"avatar": "https://avatars3.githubusercontent.com/u/415849",
"status": "contributor"
}],
ajax: function (query) {
return {
type: "GET",
url: "/cache/destinations/",
path: "data.user",
data: {
q: "{{query}}"
},
callback: {
done: function (data) {
for (var i = 0; i < data.data.user.length; i++) {
if (data.data.user[i].username === 'running-coder') {
data.data.user[i].status = 'owner';
} else {
data.data.user[i].status = 'contributor';
}
}
return data;
}
}
}
}
},
project: {
display: "project",
href: function (item) {
return '/' + item.project.replace(/\s+/g, '').toLowerCase() + '/documentation/'
},
ajax: [{
type: "GET",
url: "/cache/destinations/",
data: {
q: "{{query}}"
}
}, "data.project"],
template: '' +
'' +
'
' +
'' +
'' +
'{{project}} {{version}}' +
'' +
'- {{demo}} Demos
' +
'- {{option}}+ Options
' +
'- {{callback}}+ Callbacks
' +
'
' +
'' +
''
}
},
callback: {
onClick: function (node, a, item, event) {
// You can do a simple window.location of the item.href
alert(JSON.stringify(item));
},
onSendRequest: function (node, query) {
console.log('request is sent')
},
onReceiveRequest: function (node, query) {
console.log('request is received')
}
},
debug: true
});
*/
});
/* SEARCH TEMPLATES */
function sourceTemplate_destination() {
return '' +
'{{name}} {{type}}' +
'{{path}}' +
'
';
}
function sourceTemplate_explorer() {
return '' +
'{{name}} {{type}}' +
'
';
}
function sourceTemplate_item() {
return '' +
'{{name}} {{type}}' +
'{{country_name}}' +
'
';
}
/* SEARCH BOX */
function boxSearch_init() {
$('.search-web-input').on('keyup', function (evt) {
switch (evt.keyCode) {
case 13: //ENTER
if ($(this).val().length) {
//header_search_get();
}
break;
case 27: //ESC
header_search_close();
break;
default:
boxSearch_delay(function () {
header_search_get();
}, 1000); //600
break;
}
});
/*
$('.header-search').on('keyup', function (evt) {
switch (evt.keyCode) {
case 13: //ENTER
if ($(this).val().length) {
header_search_get();
}
break;
case 27: //ESC
header_search_close();
break;
default:
boxSearch_delay(function () {
header_search_get();
}, 1000); //600
break;
}
});
*/
}
function header_search_get() {
if ($('.header-search').val().length >= 4) {
$('.header-search').toggleClass('loading');
$.ajax({
type: 'POST',
url: GR_URL_API,
data: {
option: 'search',
action: 'boxSearch',
search: $('.header-search').val()
},
success: function (data) {
if (data.errorCode) {
$('#register_error').html(data.errorMessage);
return;
}
$('.boxSearch').html(data.source);
// $('.boxSearch').show();
$('.header-search').toggleClass('loading');
},
dataType: 'json',
async: true
});
} else {
// $('#boxSearch').modal('hide');
// $('.boxSearch").hide();
}
}
function header_search_close() {
$('.header-search').val('');
// $('.header-search').attr('placeholder','Buscar');
$('body').removeClass('search-focus');
$('.header-search').parent(this).removeClass('focus');
// $('.header-search').blur();
$("#boxSearch").modal('hide');
// $('.boxSearch').hide();
$('.boxSearch').html('');
// isTranslated();
}
var boxSearch_delay = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
});