1 2 3 4function dump(obj) { 5 var out = ''; 6 for (var i in obj) { 7 out += i + ": " + obj[i] + "<br/>"; 8 } 9 return out; 10} 11 12 13 14 15function getParamOpzionaleODefault(paramOpzionale, paramDefault){ 16 17 if (typeof paramOpzionale === 'undefined') return paramDefault; 18 19 return paramOpzionale; 20 21} 22 23 24function getJQueryElemDaTemplate(selettoreTemplate){ 25 26 //template: <script type="text/html" id="id-del-template"> ...HTML... </script> 27 28 return jQuery( jQuery.parseHTML( jQuery(selettoreTemplate).html() ) ); 29 30} 31 32 33 34function stringaContienePattern(stringa,pattern){ 35 36 return stringa.indexOf(pattern) !== -1; 37 38} 39function stringaIniziaConPattern(stringa,pattern){ 40 41 return stringa.indexOf(pattern) === 0; 42 43} 44 45 46function appendParamsToURL(url, parametri){ 47 48 if(parametri != ""){ 49 50 var simbolo = stringaContienePattern(url, "?") ? "&" : "?"; 51 52 return url+simbolo+parametri; 53 54 }else{ 55 56 return url; 57 58 } 59 60} 61 62 63 64 65 66function getVariabileQueryString(variabile){ 67 var query = window.location.search.substring(1); 68 var vars = query.split("&"); 69 for (var i=0;i<vars.length;i++) { 70 var pair = vars[i].split("="); 71 if(pair[0] == variabile){return pair[1];} 72 } 73 return(false); 74} 75 76 77 78 79function roundNumDec(val, numdecimali){ 80 81 if(numdecimali<=0){ 82 return Math.round(val); 83 } 84 85 var coeff = Math.pow(10, numdecimali); 86 87 return Math.round(val*coeff)/coeff; 88 89} 90 91 92 93function setCookieLS(chiave, valore, giorniDurata){ 94 var d = new Date(); 95 d.setTime(d.getTime() + (giorniDurata*24*60*60*1000)); 96 var expires = "expires="+ d.toUTCString(); 97 document.cookie = chiave + "=" + valore + ";" + expires + ";path=/"; 98} 99 100function getCookieLS(chiave){ 101 var name = chiave + "="; 102 var decodedCookie = decodeURIComponent(document.cookie); 103 var ca = decodedCookie.split(';'); 104 for(var i = 0; i <ca.length; i++){ 105 var c = ca[i]; 106 while (c.charAt(0) == ' ') { 107 c = c.substring(1); 108 } 109 if (c.indexOf(name) == 0) { 110 return c.substring(name.length, c.length); 111 } 112 } 113 return ""; 114} 115 116 117 118 119function ripetiFunzioneInitPagina(funzione){ 120 121 funzione(); 122 setTimeout(function(){funzione();}, 100); 123 setTimeout(function(){funzione();}, 200); 124 setTimeout(function(){funzione();}, 500); 125 setTimeout(function(){funzione();}, 750); 126 setTimeout(function(){funzione();}, 1000); 127 setTimeout(function(){funzione();}, 1500); 128 setTimeout(function(){funzione();}, 2000); 129 setTimeout(function(){funzione();}, 3000); 130 setTimeout(function(){funzione();}, 5000); 131 setTimeout(function(){funzione();}, 10000); 132 133} 134 135function ripetiFunzioneAIntervalli(arrayIntervalli, funzione){ 136 137 funzione(); 138 for(var i=0; i<arrayIntervalli.length; i++){ 139 setTimeout(function(){funzione();}, arrayIntervalli[i]); 140 } 141 142} 143 144 145 146 147 148function initAncoreAnimate(selettoreCssAncore, offsetTopScorrimento, usaAnameInveceDiDivId){ 149 150 usaAnameInveceDiDivId = getParamOpzionaleODefault(usaAnameInveceDiDivId, false); 151 152 var listaAncore = $(selettoreCssAncore); 153 154 if(listaAncore.length <= 0){ 155 return; 156 } 157 158 listaAncore.click(function(e){ 159 160 e.preventDefault(); 161 162 $(selettoreCssAncore).removeClass("selez"); 163 164 $(this).addClass("selez"); 165 166 var hrefAncora = $(this).attr('href'); 167 168 // se è un link a un'altra pagina: 169 if(hrefAncora.indexOf("#") === -1){ 170 window.location = hrefAncora; 171 return; 172 } 173 174 175 var hrefAncoraHash = hrefAncora.replace(/.*#/,"#"); 176 177 var elemDestinazione; 178 179 var usaDivId = !usaAnameInveceDiDivId; 180 181 182 if(usaDivId){ 183 184 // caso con "div id" 185 elemDestinazione = $(hrefAncoraHash); 186 187 }else{ 188 189 // caso con "a name" 190 191 var hrefAncoraNoHash = hrefAncoraHash.replace("#",""); 192 193 elemDestinazione = $('a[name="'+hrefAncoraNoHash+'"]'); 194 195 } 196 197 198 // se l'elemento dell'ancora si trova in un'altra pagina: 199 if(elemDestinazione.length <= 0){ 200 201 window.location = hrefAncora; 202 return; 203 204 } 205 206 207 var offsetScorr = (elemDestinazione.offset().top)+offsetTopScorrimento; 208 209 if(offsetScorr<0) offsetScorr=0; 210 211 $('html,body').animate({scrollTop: offsetScorr},'fast','swing',function(){ 212 213 // ora eseguire questa riga equivale a un ulteriore click dell'ancora, 214 // che quindi fa uno scatto indesiderato se c'e` un offset: 215 //window.location.hash = hrefAncoraHash; 216 217 }); 218 219 }); 220 221} 222 223 224 225 226 227 228 229function scrollToTop(){ 230 $('html,body').animate({scrollTop: 0},'normal'); 231} 232 233function scrollToId(id){ 234 $('html,body').animate({scrollTop: $("#"+id).offset().top},'normal'); 235} 236 237 238 239function isDataValida(g,m,a) { 240 241 // Assume not leap year by default (note zero index for Jan) 242 var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; 243
244 // If evenly divisible by 4 and not evenly divisible by 100, 245 // or is evenly divisible by 400, then a leap year 246 if ( (!(a % 4) && a % 100) || !(a % 400)) { 247 daysInMonth[1] = 29; 248 } 249 250 return g <= daysInMonth[m-1]; 251 252} 253 254 255 256 257function isInIframe(){ 258 try{ 259 if(window.location != window.parent.location){ 260 return true; 261 } 262 }catch(err) { 263 return true; 264 } 265 return false; 266} 267 268 269 270/** 271 * USO: eseguiFunzioneInIframe(".classe iframe.iframe-xyx").funzioneDefinitaInIframe(parametri); 272 */ 273function eseguiFunzioneInIframe(selettoreCssIframe){ 274 275 return $(selettoreCssIframe).get(0).contentWindow; 276 277} 278 279 280 281 282// USO: registraListenerMessaggioDaIframe(function(msg){ /* gestire messaggio */ }); 283function registraListenerMessaggioDaIframe(callback_param_msg){ 284 285 if(!window.addEventListener) { 286 287 window.attachEvent("onmessage", function(event) { 288 callback_param_msg(event.data); 289 }); 290 291 }else{ 292 293 window.addEventListener("message", function(event) { 294 callback_param_msg(event.data); 295 }); 296 297 } 298 299} 300 301function inviaMessaggioDaIframe(msg){ 302 303 parent.postMessage(""+msg, "*"); 304 305} 306 307/*********** iframe con altezza autoaggiornante **************** 308 309- codice da mettere nella pagina INTERNA all'iframe: 310 311<script type="text/javascript"> 312var altezzaFramePrecedente = -1; 313function aggiornaAltezzaFrame(){ 314 var body = document.body, 315 html = document.documentElement; 316 var docHeight = Math.max(body.offsetHeight, html.scrollHeight, html.offsetHeight ); 317 if(altezzaFramePrecedente != docHeight){ 318 parent.postMessage("altezzaframe_"+docHeight, "*"); 319 } 320 altezzaFramePrecedente = docHeight; 321} 322window.onload=function(){ aggiornaAltezzaFrame(); }; 323setTimeout(function(){ aggiornaAltezzaFrame(); },500); 324setTimeout(function(){ aggiornaAltezzaFrame(); },1000); 325setTimeout(function(){ aggiornaAltezzaFrame(); },2000); 326setTimeout(function(){ aggiornaAltezzaFrame(); },6000); 327</script> 328 329 330- codice da mettere nella pagina ESTERNA all'iframe: 331 332<script type="text/javascript"> 333registraListenerMessaggioDaIframe(function(msg){ 334 if(msg.match(/altezzaframe_[0-9]+/)){ 335 var altezza = parseInt(msg.replace("altezzaframe_","")); 336 $('#id-dell-iframe').css("height",altezza+"px"); 337 } 338}); 339</script> 340 341***************************************************************/ 342 343function initIframeAutoridimensionanti(selettore){ 344 345 // per ora funziona solo con un iframe per pagina 346 347 selettore = getParamOpzionaleODefault(selettore, ".iframe-autoridimensionante"); 348 349 var elementi = $(selettore); 350 351 if(elementi.length>0){ 352 353 registraListenerMessaggioDaIframe(function(msg){ 354 if(msg.match(/altezzaframe_[0-9]+/)){ 355 var altezza = parseInt(msg.replace("altezzaframe_","")); 356 $(selettore).css("height",altezza+"px"); 357 } 358 }); 359 360 } 361 362} 363 364 365 366 367 368 369 370 371 372 373 374 375/** 376 * 377 * @param string selettoreCssBlocchi selettore css dei blocchi da sincronizzare 378 * @param int margineSotto opzionale - eventuale margine in px da aggiungere all'altezza massima rilevata 379 * @param int sogliaDisattivaSuMobile opzionale - soglia in px sotto la quale disattivare la sincronizzazione 380 */ 381function sincronizzaAltezzeBlocchi(selettoreCssBlocchi, margineSotto, sogliaDisattivaSuMobile){ 382 383 margineSotto = getParamOpzionaleODefault(margineSotto,0); 384 sogliaDisattivaSuMobile = getParamOpzionaleODefault(sogliaDisattivaSuMobile,750); 385 386 $(function(){ 387 sincronizzaAltezzeBlocchiPrivate(selettoreCssBlocchi, margineSotto, sogliaDisattivaSuMobile); 388 }); 389 390 $(window).on("load",function(){ 391 sincronizzaAltezzeBlocchiPrivate(selettoreCssBlocchi, margineSotto, sogliaDisattivaSuMobile); 392 }); 393 394 $(window).resize(function(){ 395 sincronizzaAltezzeBlocchiPrivate(selettoreCssBlocchi, margineSotto, sogliaDisattivaSuMobile); 396 }); 397 398} 399 400 401function sincronizzaAltezzeBlocchiPrivate(selettoreCssBlocchi, margineSotto, sogliaDisattivaSuMobile){ 402 403 var elementi = $(selettoreCssBlocchi); 404 405 if(elementi.length <= 0) 406 return; 407 408 elementi.css("height", "auto"); 409 410 sogliaDisattivaSuMobile = getParamOpzionaleODefault(sogliaDisattivaSuMobile,750); 411 412 if($(window).width()<sogliaDisattivaSuMobile) return; // non fare su mobile 413 414 var altMax = 0; 415 416 elementi.each(function(){ 417 418 altMax = Math.max(altMax, $(this).outerHeight()); 419 420 }); 421 422 margineSotto = getParamOpzionaleODefault(margineSotto,0); 423 424 elementi.css("height", (altMax+margineSotto)+"px"); 425 426} 427 428 429 430 431/** 432 * 433 * @param string selettoreCssBlocchi selettore css dei blocchi da sincronizzare 434 * @param int margineDx opzionale - eventuale margine in px da aggiungere alla larghezza massima rilevata 435 * @param int sogliaDisattivaSuMobile opzionale - soglia in px sotto la quale disattivare la sincronizzazione 436 */ 437function sincronizzaLarghezzeBlocchi(selettoreCssBlocchi, margineDx, sogliaDisattivaSuMobile){ 438 439 margineDx = getParamOpzionaleODefault(margineDx,0); 440 sogliaDisattivaSuMobile = getParamOpzionaleODefault(sogliaDisattivaSuMobile,750); 441 442 $(function(){ 443 sincronizzaLarghezzeBlocchiPrivate(selettoreCssBlocchi, margineDx, sogliaDisattivaSuMobile); 444 }); 445 446 $(window).on("load",function(){ 447 sincronizzaLarghezzeBlocchiPrivate(selettoreCssBlocchi, margineDx, sogliaDisattivaSuMobile); 448 }); 449 450 $(window).resize(function(){ 451 sincronizzaLarghezzeBlocchiPrivate(selettoreCssBlocchi, margineDx, sogliaDisattivaSuMobile); 452 }); 453 454} 455 456 457function sincronizzaLarghezzeBlocchiPrivate(selettoreCssBlocchi, margineDx, sogliaDisattivaSuMobile){ 458 459 var elementi = $(selettoreCssBlocchi); 460 461 if(elementi.length <= 0) 462 return; 463 464 elementi.css("width", "auto"); 465 466 sogliaDisattivaSuMobile = getParamOpzionaleODefault(sogliaDisattivaSuMobile,750); 467 468 if($(window).width()<sogliaDisattivaSuMobile) return; // non fare su mobile 469 470 var larghMax = 0; 471 472 elementi.each(function(){ 473 474 larghMax = Math.max(larghMax, $(this).outerWidth()); 475 476 }); 477 478 margineDx = getParamOpzionaleODefault(margineDx,0); 479 480 elementi.css("width", (larghMax+margineDx)+"px"); 481 482} 483 484 485 486 487 488function isIosUA(){ 489 490 var ua = navigator.userAgent; 491 492 return (ua.match(/iphone/i) || ua.match(/ipad/i)) && !ua.match(/android/i); 493 494} 495 496function isAndroidUA(){ 497 498 var ua = navigator.userAgent; 499 500 return ua.match(/android/i) && !(ua.match(/iphone/i) || ua.match(/ipad/i)); 501 502} 503 504
505function isNotAndroidIosUA(){ 506 507 return (!isIosUA()) && (!isAndroidUA()); 508 509} 510 511 512 513 514 515function autoscalaTestoInContenitorePrivate(selettoreContenitore, larghezzaContenitoreDefault, fontSizeDefaultPx){ 516 517 var w = $(selettoreContenitore).width(); 518 519 var fontSize = (fontSizeDefaultPx*w)/larghezzaContenitoreDefault; 520 521 $(selettoreContenitore).css('font-size', fontSize+'px'); 522 523} 524 525function autoscalaTestoInContenitore(selettoreContenitore, larghezzaContenitoreDefault, fontSizeDefault){ 526 527 $(function(){ 528 529 if( $(selettoreContenitore).length <= 0 ){ 530 return; 531 } 532 533 autoscalaTestoInContenitorePrivate(selettoreContenitore, larghezzaContenitoreDefault, fontSizeDefault); 534 535 $(window).on("load",function(){ 536 autoscalaTestoInContenitorePrivate(selettoreContenitore, larghezzaContenitoreDefault, fontSizeDefault); 537 }); 538 539 $(window).resize(function(){ 540 autoscalaTestoInContenitorePrivate(selettoreContenitore, larghezzaContenitoreDefault, fontSizeDefault); 541 }); 542 543 }); 544 545} 546 547 548 549 550 551 552 553/** 554 * Il contenitore interno deve essere display inline-block, whitespace:nowrap 555 * CoefficienteScala serve per avere un po' di margine (con valori <1), è opzionale e di default è 1 556 */ 557function autoscalaTestoRiempiContenitoreAutomaticoPrivate(selettoreContenitoreEsterno, selettoreContenitoreInterno, coefficienteScala){ 558 559 coefficienteScala = getParamOpzionaleODefault(coefficienteScala,1); 560 561 var wExt = $(selettoreContenitoreEsterno).width() * coefficienteScala; 562 563 var maxWinterna = 0; 564 565 $(selettoreContenitoreInterno).each(function(){ 566 if($(this).width()>maxWinterna){ 567 568 maxWinterna = $(this).width(); 569 570 } 571 572 }); 573 574 575 var fontSizeCorrentePx = parseFloat($(selettoreContenitoreInterno).css('font-size')); 576 577 var fontSize = (fontSizeCorrentePx*wExt)/maxWinterna; 578 579 $(selettoreContenitoreInterno).css({ 580 fontSize:fontSize+'px', 581 opacity:1 582 }); 583 584} 585 586function autoscalaTestoRiempiContenitoreAutomatico(selettoreContenitoreEsterno, selettoreContenitoreInterno, coefficienteScala){ 587 588 $(function(){ 589 590 if( $(selettoreContenitoreEsterno).length <= 0 ){ 591 return; 592 } 593 if( $(selettoreContenitoreInterno).length <= 0 ){ 594 return; 595 } 596 597 $(selettoreContenitoreInterno).css({ 598 display:"inline-block", 599 whiteSpace:"nowrap" 600 }); 601 602 autoscalaTestoRiempiContenitoreAutomaticoPrivate(selettoreContenitoreEsterno, selettoreContenitoreInterno, coefficienteScala); 603 604 $(window).on("load",function(){ 605 autoscalaTestoRiempiContenitoreAutomaticoPrivate(selettoreContenitoreEsterno, selettoreContenitoreInterno, coefficienteScala); 606 }); 607 608 $(window).resize(function(){ 609 autoscalaTestoRiempiContenitoreAutomaticoPrivate(selettoreContenitoreEsterno, selettoreContenitoreInterno, coefficienteScala); 610 }); 611 612 }); 613 614} 615 616 617 618 619 620 621 622 623/** 624 * Il form deve avere un bottone type=submit 625 */ 626function eseguiCallbackOnFormSubmit(selettoreForm, callback){ 627 628 var form = $(selettoreForm); 629 630 var bottSubmit = form.find('[type="submit"]'); 631 632 if(bottSubmit.length>0){ 633 634 bottSubmit.click(function(e){ 635 636 e.preventDefault(); 637 638 callback(); 639 640 }); 641 642 }else{ 643 alert("Errore: bottone submit non presente"); 644 } 645 646} 647 648 649 650/* 651// USO: 652 653new LSValidatoreFormJs('id_form', function(){ 654 655 if ( this.isNonDefinito("idinput") ) { 656 this.segnalaErroreForm("idinput", "Il campo non deve essere vuoto"); 657 } 658 659 if ( this.isNonDefinito('email') || (!this.email_reg_exp.test($('#email').val())) ) { 660 this.segnalaErroreForm("email", "Inserire un indirizzo email corretto"); 661 } 662 663 if ( ! $('#idinput').val().match(/^regexformatodato$/) ){ 664 this.segnalaErroreForm("idinput", "Il dato inserito non è nel formato corretto" ); 665 } 666 667 if (this.isNonDefinito('data') || ! this.isDataDisplayStringValida($('#data').val()) ){ 668 this.segnalaErroreForm("data", "Inserire una data valida nel formato GG/MM/AAAA"); 669 } 670 671 // se input[type=date]: 672 if (this.isNonDefinito('data') || ! this.isDataDisplayStringValidaInputTypeDate($('#data').val()) ){ 673 this.segnalaErroreForm("data", "Inserire una data valida nel formato GG/MM/AAAA"); 674 } 675 676 if(!( $('#privacy').prop('checked') )){ 677 this.segnalaErroreForm("privacy", "Devi selezionare la checkbox" ); 678 } 679 680}); 681 682*/ 683function LSValidatoreFormJs(idForm, metodoControlliValidazione){ 684 685 this.idForm = idForm; 686 687 this.erroriForm = false;
688 this.scrollTopErrori = -1; 689 690 this.email_reg_exp = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 691 692 this.telefono_reg_exp = /^\+?[0-9\ \-]+$/; 693 694 this.identifier_reg_exp = /^([a-zA-Z0-9_\.\-\ ])+$/; 695 696 this.data_reg_exp = /^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/; 697 this.data_reg_exp_input_type_date = /^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/; 698 699 700 if(typeof metodoControlliValidazione === 'function'){ 701 702 this.controlliValidazione = metodoControlliValidazione; 703 704 } 705 706 707 var thisValidatore = this; 708 709 $(function(){ 710 711 var bottSubmit = $('#'+idForm+' [type="submit"]'); 712 713 if(bottSubmit.length>0){ 714 715 bottSubmit.click(function(e){ 716 717 e.preventDefault(); 718 719 thisValidatore.inviaForm(); 720 721 }); 722 723 } 724 725 }); 726 727} 728 729LSValidatoreFormJs.prototype.isDataValida = function(g,m,a){ 730 731 // Assume not leap year by default (note zero index for Jan) 732 var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; 733 734 // If evenly divisible by 4 and not evenly divisible by 100, 735 // or is evenly divisible by 400, then a leap year 736 if ( (!(a % 4) && a % 100) || !(a % 400)) { 737 daysInMonth[1] = 29; 738 } 739 740 return g <= daysInMonth[m-1]; 741 742}; 743 744LSValidatoreFormJs.prototype.isDataDisplayStringValida = function(data){ 745 746 if(!this.data_reg_exp.test(data)){ 747 return false; 748 } 749 750 var arr = data.split("/"); 751 752 return this.isDataValida( parseInt(arr[0]), parseInt(arr[1]), parseInt(arr[2]) ); 753 754}; 755 756LSValidatoreFormJs.prototype.isDataDisplayStringValidaInputTypeDate = function(data){ 757 758 if(!this.data_reg_exp_input_type_date.test(data)){ 759 return false; 760 } 761 762 var arr = data.split("-"); 763 764 return this.isDataValida( parseInt(arr[2]), parseInt(arr[1]), parseInt(arr[0]) ); 765 766}; 767 768LSValidatoreFormJs.prototype.segnalaErroreForm = function(id, messaggio){ 769 770 console.log("errore form campo "+id); 771 772 var primoerrore = this.erroriForm == false; 773 774 this.erroriForm = true; 775 776 777 if ($('#msg-errore-' + id).length > 0){ 778
779 $('#msg-errore-' + id).html(messaggio); 780 781 } else { 782 783 $('#' + id).after('<div class="msg-errore-lsform" id="msg-errore-' + id + '">' + messaggio + '</div>'); 784 785 } 786 787 788 if(primoerrore){ 789 790 this.scrollTopErrori = Math.max(0, ($('#msg-errore-' + id ).offset().top - 200) ); 791 792 } 793 794}; 795 796LSValidatoreFormJs.prototype.isNonDefinito = function(id){ 797 798 return ($('#'+id).val() == "") || ($('#'+id).val() == "undefined") || ($('#'+id).val() == null); 799 800}; 801 802LSValidatoreFormJs.prototype.controlliValidazione = function(){ 803 804 // da implementare in override 805 806}; 807 808LSValidatoreFormJs.prototype.inviaForm = function(){ 809 810 this.erroriForm = false; 811 812 this.scrollTopErrori = -1; 813 814 $('.msg-errore-lsform').html(""); 815 816 817 this.controlliValidazione(); 818 819 820 if (!this.erroriForm){ 821 822 this.funzioneSubmit(); 823 824 }else{ 825 826 $('html,body').animate({scrollTop: this.scrollTopErrori},'normal'); 827 828 } 829 830}; 831 832LSValidatoreFormJs.prototype.funzioneSubmit = function(){ 833 834 document.getElementById(this.idForm).submit(); 835 836}; 837 838 839 840 841 842 843 844 845 846 847 848function rilevaScrollPaginaCambiaClasseBody(sogliaPxCambioClasse){ 849 850 $(window).scroll(function(event){ 851 852 var scroll = $(window).scrollTop(); 853 854 if(scroll > sogliaPxCambioClasse){ 855 856 if( ! $("body").hasClass('ls-scrollato') ){ 857 $("body").addClass('ls-scrollato'); 858 } 859 860 }else{ 861 862 if( $("body").hasClass('ls-scrollato') ){ 863 $("body").removeClass('ls-scrollato'); 864 } 865 866 } 867 868 869 870 }); 871 872 873} 874 875 876 877 878 879 880 881function getScriptParentNode(){ 882 883 return document.currentScript.parentNode; 884 885} 886 887/** 888 * Se selettoreScrittaOpzionale è vuoto, viene preso il parent dello script 889 */ 890function setColoreScrittaPerImgSfondo(urlImg, selettoreScrittaOpzionale){ 891 892 var selettoreScritta = getParamOpzionaleODefault(selettoreScrittaOpzionale, null); 893 894 if(selettoreScritta === null){ 895 selettoreScritta = getScriptParentNode(); 896 } 897 898 var img = new Image(); 899 900 img.onload = function () { 901 902 var rgb = getAverageRGB(this); 903 904 var media = (rgb.r + rgb.g + rgb.b) / 3; 905 906 if(media < (255/2)){ 907 $(selettoreScritta).addClass("ls-scritta-chiara"); 908 }else{ 909 $(selettoreScritta).addClass("ls-scritta-scura"); 910 } 911 912 } 913 914 img.src = urlImg; 915 916} 917 918 919 920function getAverageRGB(imgEl){ 921 922 var blockSize = 5, // only visit every 5 pixels 923 defaultRGB = {r:0,g:0,b:0}, // for non-supporting envs 924 canvas = document.createElement('canvas'), 925 context = canvas.getContext && canvas.getContext('2d'), 926 data, width, height, 927 i = -4, 928 length, 929 rgb = {r:0,g:0,b:0}, 930 count = 0; 931 932 if (!context) { 933 return defaultRGB; 934 } 935 936 height = canvas.height = imgEl.naturalHeight || imgEl.offsetHeight || imgEl.height; 937 width = canvas.width = imgEl.naturalWidth || imgEl.offsetWidth || imgEl.width; 938 939 context.drawImage(imgEl, 0, 0); 940 941 try { 942 data = context.getImageData(0, 0, width, height); 943 } catch(e) { 944 /* security error, img on diff domain */ 945 return defaultRGB; 946 } 947 948 length = data.data.length; 949 950 while ( (i += blockSize * 4) < length ) { 951 ++count; 952 rgb.r += data.data[i]; 953 rgb.g += data.data[i+1]; 954 rgb.b += data.data[i+2]; 955 } 956 957 // ~~ used to floor values 958 rgb.r = ~~(rgb.r/count); 959 rgb.g = ~~(rgb.g/count); 960 rgb.b = ~~(rgb.b/count); 961 962 return rgb; 963 964} 965 966 967 968/* 969 * USO: 970 * eseguiQuandoElemCompareInViewport($(".selettore").get(0),function(elem){ 971 $(elem). ... 972 }); 973 974 threshold è un float tra 0 e 1: 975 - 0 appena visibile 976 - 0.x via di mezzo 977 - 1 completamente visibile 978 */ 979function eseguiQuandoElemCompareInViewport(elemento, threshold, callback){ 980 981 var funzioneObserver = function(entries, observer){ 982 983 entries.forEach(entry => { 984 985 if(entry.isIntersecting){ 986 987 callback(entry.target); 988 989 } 990 991 }); 992 993 }; 994 995 var observer = new IntersectionObserver(funzioneObserver, { 996 root: null, 997 rootMargin: '0px', 998 threshold: threshold 999 }); 1000 1001 observer.observe(elemento); 1002 1003} 1004 1005 1006 1007 1008 1009 1010var LSGlob_initSwipeTouchMobile_xDown = null; 1011var LSGlob_initSwipeTouchMobile_yDown = null; 1012 1013function initSwipeTouchMobile(callbackSx, callbackDx){ 1014 1015 document.addEventListener('touchstart', handlerTouchStart, false); 1016 document.addEventListener('touchmove', handlerTouchMove, false); 1017 1018 function getTouches(evt) { 1019 return evt.touches || // browser API 1020 evt.originalEvent.touches; // jQuery 1021 } 1022 1023 function handlerTouchStart(evt) { 1024 const firstTouch = getTouches(evt)[0]; 1025 LSGlob_initSwipeTouchMobile_xDown = firstTouch.clientX; 1026 LSGlob_initSwipeTouchMobile_yDown = firstTouch.clientY; 1027 }; 1028 1029 function handlerTouchMove(evt) { 1030 if ( ! LSGlob_initSwipeTouchMobile_xDown || ! LSGlob_initSwipeTouchMobile_yDown ) { 1031 return; 1032 } 1033 1034 var xUp = evt.touches[0].clientX; 1035 var yUp = evt.touches[0].clientY; 1036 1037 var xDiff = LSGlob_initSwipeTouchMobile_xDown - xUp; 1038 var yDiff = LSGlob_initSwipeTouchMobile_yDown - yUp; 1039 1040 if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/ 1041 if ( xDiff > 0 ) { 1042 /* right swipe */ 1043 callbackDx(); 1044 } else { 1045 /* left swipe */ 1046 callbackSx(); 1047 } 1048 } else { 1049 if ( yDiff > 0 ) { 1050 /* down swipe */ 1051 } else { 1052 /* up swipe */ 1053 } 1054 } 1055 /* reset values */ 1056 LSGlob_initSwipeTouchMobile_xDown = null; 1057 LSGlob_initSwipeTouchMobile_yDown = null; 1058 }; 1059 1060} 1061 1062 1063 1064 1065function attivaFocusTrap(selettoreCssContenitore, callbackIsFocusTrapAttiva){ 1066 1067 var element = $(selettoreCssContenitore).get(0); 1068 1069 var focusableEls = element.querySelectorAll('a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])'); 1070 var firstFocusableEl = focusableEls[0]; 1071 var lastFocusableEl = focusableEls[focusableEls.length - 1]; 1072 var KEYCODE_TAB = 9; 1073 1074 element.addEventListener('keydown', function (e) { 1075 var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB); 1076 1077 if (!isTabPressed) { 1078 return; 1079 } 1080 1081 if (!callbackIsFocusTrapAttiva()) { 1082 return; 1083 } 1084 1085 if (e.shiftKey) /* shift + tab */ { 1086 if (document.activeElement === firstFocusableEl) {
1087 lastFocusableEl.focus(); 1088 e.preventDefault(); 1089 } 1090 } else /* tab */ { 1091 if (document.activeElement === lastFocusableEl) { 1092 firstFocusableEl.focus(); 1093 e.preventDefault(); 1094 } 1095 } 1096 }); 1097} 1098 1099 1100 1101 1102function spostaElementoAppend(selettoreElemento, selettoreDestinazione){ 1103 1104 $(selettoreElemento).detach().appendTo(selettoreDestinazione); 1105 1106} 1107 1108function spostaElementoPrepend(selettoreElemento, selettoreDestinazione){ 1109 1110 $(selettoreElemento).detach().prependTo(selettoreDestinazione); 1111 1112} 1113 1114 1115 1116/** 1117 * se si usa senza parametro mostra la data corrente (new Date()) 1118 */ 1119function getDataOraDisplay(dateObj){ 1120 1121 var dateObj = getParamOpzionaleODefault(dateObj,new Date()); 1122 1123 var year = dateObj.getFullYear(); 1124 var month = dateObj.getMonth()+1; 1125 var day = dateObj.getDate(); 1126 var hour = dateObj.getHours(); 1127 var minute = dateObj.getMinutes(); 1128 var second = dateObj.getSeconds(); 1129 1130 if(month.toString().length == 1) { 1131 month = '0'+month; 1132 } 1133 if(day.toString().length == 1) { 1134 day = '0'+day; 1135 } 1136 if(hour.toString().length == 1) { 1137 hour = '0'+hour; 1138 } 1139 if(minute.toString().length == 1) { 1140 minute = '0'+minute; 1141 } 1142 if(second.toString().length == 1) { 1143 second = '0'+second; 1144 } 1145 1146 var dataStr = day+'/'+month+'/'+year+' '+hour+':'+minute+':'+second; 1147 1148 return dataStr; 1149 1150} 1151 1152
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.