1 2function sanitize(cadena) { 3 cadena = cadena.replace(/[^A-Za-z0-9 áéÃóúüçñÃÃÃÃÃÃÃÃ-]/g, ''); 4 return cadena; 5} 6 7module('dynamic-search', function ($, el, opts) { 8 9 var result = $(opts.dynamicSearch), 10 isMobile = opts.mobile; 11 12 13 var locale = { 14 'Páginas': 'Pages', 15 'Documentos': 'Documents', 16 'Ir a la página': 'Go to page', 17 'Ver más': 'See more', 18 'Resultados de la búsqueda': 'Search results', 19 'Se han encontrado': 'We found', 20 'resultados que coinciden con la palabra': ' results that match the word', 21 }; 22 23 24 Twig.extendFilter("sanitize", function (value) { 25 return value 26 .toLowerCase() 27 .replace(/[^A-Za-z0-9 ]/g, '') 28 .replace(/ +/g, ' ') 29 .trim() 30 .replace(/ /g, '_'); 31 }); 32 33 Twig.extendFilter("t", function (value) { 34 if (accionistas_options.language == 'es') { 35 return value; 36 } 37 38 if (value in locale) { 39 return locale[value]; 40 } 41 42 return value; 43 }); 44 45 46 /* Form */ 47 48 var form = el.parents('form'); 49 // var searchUrl = form.attr('data-search'); 50 51 var isSearching = !!window.location.href.match(/(search)/); 52 var attrMode = !!window.location.href.match(/(bbvaSearch|query|s|q)=/); 53 54 form.submit(function (e) { 55 56 var val = sanitize(el.val()); 57 58 if (!isSearching && !isMobile) { 59 location = accionistas_options.site_url + 'search/?q=' + encodeURI(val); 60 } 61 62 63 performSearch(val); 64 if (typeof s_gi !== 'undefined') //si s_gi no está definido es que no se han aceptado las cookies de analiÃtica. 65 { 66 //código omniture para buscador en sucesivas llamadas 67 var s; 68 var inicial = $(this).attr('data-omniture-event'); 69 70 s = s_gi(accionistas_options.cuenta_omniture); 71 72 73 var q = val; 74 75 s.hier2 = "search:?q=" + q; 76 s.pageName = "search:?q=" + q; 77 s.server = ""; 78 s.channel = "search"; 79 s.pageType = ""; 80 s.prop1 = "?q=" + q; 81 s.prop2 = ""; 82 s.prop3 = ""; 83 s.prop4 = ""; 84 s.prop5 = ""; 85 s.prop6 = ""; 86 s.prop7 = ""; 87 s.prop8 = ""; 88 if (accionistas_options.language == 'es') s.prop16 = "Castellano"; else s.prop16 = "Ingles"; 89 s.prop17 = ""; 90 s.state = ""; 91 s.zip = ""; 92 s.events = ""; 93 s.products = ""; 94 s.purchaseID = ""; 95 s.eVar1 = ""; 96 s.eVar2 = ""; 97 s.eVar3 = ""; 98 s.eVar4 = ""; 99 s.eVar5 = ""; 100 s.eVar16 = s.prop16; 101 s.eVar17 = s.prop17; 102 var s_code = s.t(); if (s_code) document.write(s_code); 103 } 104 e.preventDefault(); 105 return false; 106 }); 107 108 var performSearch = function (query) { 109 110 111 isSearching = true; 112 113 result.css('opacity', 0.5); 114 result.find('.loader').show(); 115 result.find('ul').hide(); 116 117 var newUrl = window.location.href; 118 119 var cleanQuery = query 120 .toLowerCase() 121 .replace(/á/g, 'a') 122 .replace(/é/g, 'e') 123 .replace(/Ã/g, 'i') 124 .replace(/ó/g, 'o') 125 .replace(/ú/g, 'u') 126 .replace(/[^A-Za-z0-9 ]/g, '') 127 .replace(/ +/g, ' ') 128 .trim() 129 .replace(/ /g, '+'); 130 131 132 133 if (attrMode) { 134 newUrl = newUrl.replace(/(bbvaSearch|query|s|q)=.+(&?)/, '$1=' + cleanQuery + '$2'); 135 136 } else { 137 newUrl = newUrl.replace(/\/[^\/]+\/$/, '/' + cleanQuery + '/'); 138 } 139 140 141 window.history.replaceState(null, null, newUrl); 142 143 var displayResultsMobile = function (vars) { 144 145 var output = ''; 146 147 vars.tabs.documentos.items.forEach(function (item) { 148 149 var str = item.document_links[0].url; 150 var res = str.replace("http://accionistaseinversores.bbva.com", ""); 151 152 output += '<li><a href="' + res + '">' + item.title + '</a></li>'; 153 }); 154
155 vars.tabs.page.items.forEach(function (item) { 156 output += '<li><a href="' + item.link + '">' + item.title + '</a></li>'; 157 }); 158 159 result.find('ul').html(output).show(); 160 }; 161 162 var compileTwig = function (vars) { 163 164 $('[type="text/twig-template"]').each(function () { 165 166 var tplScript = $(this); 167 var tplResult = tplScript.next('.script-result'); 168 169 if (tplResult.length < 1) { 170 tplScript.after('<div class="script-result"></div>'); 171 tplResult = tplScript.next('.script-result'); 172 } 173 174 var data = tplScript.text(); 175 var template = Twig.twig({ 176 data: data 177 }); 178 179 var html = template.render(vars); 180 tplResult.html(html); 181 }); 182 183 $('body').trigger('search-load'); 184 //console.log('search-load'); 185 }; 186 187 $.ajax({
188 url: accionistas_options.components_api + '/search', 189 timeout: 10000, 190 data: { 191 'project': accionistas_options.project, 192 'q.parser': 'lucene', 193 /*'return': 'title,resourcename,description,image_src,category,date,wp_double,wp_text_array,content_language',*/ 194 'q': '*' + query + '*', 195 'size': isMobile ? 10 : 400, 196 'start': 0, 197 /*'fq': '(and content_language:\'' + accionistas_options.language + '\')'*/ 198 }, 199 success: function (res) { 200 result.css('opacity', 1); 201 result.find('.loader').hide(); 202 203 204 totalIdioma = 0; 205 206 207 208 var vars = { 209 query_string: cleanQuery, 210 total: totalIdioma, //res.data.hits.found, 211 tabs: { 212 213 page: { 214 _self: { 215 label: 'Páginas' 216 }, 217 items: [] 218 }, 219 'documentos': { 220 _self: { 221 label: 'Documentos' 222 }, 223 items: [] 224 } 225 } 226 }; 227 res.data.hits.hit.forEach(function (hit) { 228 var links = []; 229 230 if (hit.fields.content_language == accionistas_options.language) { 231 232 totalIdioma = totalIdioma + 1; 233 234 (hit.fields.wp_text_array || []).forEach(function (tag) { 235 var parts = tag.split(/\|/); 236 237 238 239 var str = parts[1]; 240 var resurl = str.replace("http://accionistaseinversores.bbva.com", ""); 241 242 links.push({ 243 name: parts[0], 244 url: resurl, 245 class: parts[2] 246 }); 247 }); 248 249 var category = null; 250 251 var postType = 'page'; 252 253 if (hit.fields.category == 'documento') { 254 postType = 'documento'; 255 } 256 257 if (hit.fields.resourcename.indexOf("/es/") == 0 || hit.fields.resourcename.indexOf("/en/") == 0) { 258 var enlaceAux = hit.fields.resourcename.substring("3"); 259 } else { 260 var enlaceAux = hit.fields.resourcename; 261 } 262 263 var post = { 264 'title': hit.fields.title, 265 'link': enlaceAux, 266 'thumbnail': hit.fields.image_src, 267 'document_links': links 268 }; 269 270 271 if (postType == 'documento') { 272 vars.tabs.documentos._self.label = 'Documentos'; 273 vars.tabs.documentos.items.push(post); 274 } else { 275 vars.tabs.page._self.label = 'Páginas'; 276 vars.tabs.page.items.push(post); 277 } 278 } 279 280 }); 281 282 vars.total = totalIdioma; 283 284 285 286 if (!isMobile) { 287 compileTwig(vars); 288 } else { 289 displayResultsMobile(vars); 290 } 291 292 }, 293 error: function () { 294 result.css('opacity', 1); 295 296 var vars = { 297 query_string: cleanQuery, 298 groups: { 299 'Todas': [] 300 } 301 }; 302 303 compileTwig(vars); 304 } 305 }); 306 }; 307 308 if (isSearching) { 309 // QUITAR LA BARRA FINAL 310 var uri = (document.location.href + "").replace(/\/+$/, ''); 311 312 if (!uri) { 313 console.error('can\'t load uri'); 314 return; 315 } 316 317 var query = ''; 318 if (attrMode) { 319 var params = window.location.search.slice(1) 320 .split('&') 321 .reduce(function _reduce(a, b) { 322 b = b.split('='); 323 const rawKey = b[0]; 324 const safeKey = "$" + rawKey; 325 a[safeKey] = sanitize(decodeURIComponent(b[1])); 326 return a; 327 }, {}); 328 329 query = params['bbvaSearch'] || params['query'] || params['s'] || params['q']; 330 331 } else { 332 query = uri.split('/').pop().replace(/-|%20|\+/g, ' '); 333 } 334 335 336 337 query = query.replace(/\+/g, ' '); 338 339 340 if (query == '#' || query == 'search') { 341 $(document).ready(function () { 342 setTimeout(function () {$('.searchbox-ico').click()}, 100); 343 }); 344 } else { 345 el.val(query); 346 performSearch(query); 347 } 348 } 349 350}); 351 352//Muestra los paneles de una tab a través de un selector 353module('show-selected-async', function ($, el, opt) { 354 355 $('body').on('search-load', function () { 356 357 el = el.find('select'); 358 359 var items = []; 360 361 el.find('option').each(function () { 362 363 var itemEl = $(this).attr('value'); 364 var item = $(itemEl); 365 366 if (item && item.length > 0) { 367 items.push(item); 368 } 369 }); 370 371 el.change(function () { 372 373 var itemEl = $(this).val(); 374 var item = $(itemEl); 375 376 if (!item || item.length <= 0) { 377 return; 378 } 379
380 items.forEach(function (node) { 381 node.hide(); 382 }); 383 384 item.show(); 385 }); 386 387 el.trigger('change'); 388 389 }); 390 391}); 392 393/** 394 * Directiva que crea una paginación de resultados 395 * 396 * @var data-paginate 397 * @require Elemento .result para almacenar los resultados cargados 398 * @require Elemento .load-more que carga mas resultados 399 * 400 */ 401 402module('paginate-async', function ($, el, opt) { 403 404 $('body').on('search-load', function () { 405 //console.log('paginate async'); 406 407 var idx = 9; 408 $('body').on('click', '[data-paginate-async] .load-more', function (e) { 409 e.preventDefault(); 410 displayNexts(idx += 9); 411 }); 412 413 function displayNexts(quantity) { 414 415 var lastItem = el.find('.result').last(); 416 417 el.find('.result').each(function (index) { 418 419 if (index <= idx) { 420 $(this).fadeIn(); 421 422 if ($(this).get(0) == lastItem.get(0)) { 423 el.find('.load-more').hide(); 424 } 425 } 426 }); 427 } 428 }); 429}); 430
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.