1jQuery(function($) { 2 3 'use strict'; 4 5 6 /** 7 * jQuery Tiny Pub/Sub 8 * https://github.com/cowboy/jquery-tiny-pubsub 9 * 10 * Copyright (c) 2013 "Cowboy" Ben Alman 11 * Licensed under the MIT license. 12 **********************************************************************/ 13 var o = $({}); 14 $.subscribe = function() {o.on.apply(o, arguments);}; 15 $.unsubscribe = function() {o.off.apply(o, arguments);}; 16 $.publish = function() {o.trigger.apply(o, arguments);}; 17 18 /** 19 * Save the main building block of DOM elements; for the 20 * sake of succinctness 21 **********************************************************************/ 22 var DOM = (function ( dom ) { 23 24 var dom = dom || {} 25 , cleverwaFlag = $( '.cleverwa-flag' ) 26 ; 27 28 dom.body = $('body:eq(0)'); 29 dom.isSmallScreen = ( window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == '"small"' || window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == 'small' ) ? true : false; 30 dom.isMobileScreen = ( window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == '"mobile"' || window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == 'mobile' ) ? true : false; 31 dom.isLargeScreen = ( window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == '"desktop"' || window.getComputedStyle( cleverwaFlag.get(0), ':after' ).content == 'desktop' ) ? true : false; 32 33 return dom; 34 35 }( DOM ) ); 36 37 /**
38 * Simple cookie utilities 39 **********************************************************************/ 40 var COOKIES = (function ( cookies ) { 41 42 var cookies = cookies || {}; 43 44 cookies.setItem = function ( name, value, durationInDays ) { 45 var d = new Date(); 46 d.setTime( d.getTime() + ( durationInDays * 24 * 60 * 60 * 1000 ) ); 47 var expires = 'expires=' + d.toUTCString(); 48 document.cookie = name + '=' + value + ';' + expires + ';path=/'; 49 }; 50 51 cookies.getItem = function ( name ) { 52 var name = name + '=', 53 decodedCookie = decodeURIComponent( document.cookie ), 54 ca = decodedCookie.split(';'); 55 56 for ( var i = 0; i < ca.length; i++ ) { 57 58 var c = ca[ i ]; 59 60 while ( c.charAt( 0 ) == ' ' ) { 61 c = c.substring(1); 62 } 63 64 if ( c.indexOf( name ) == 0 ) { 65 return c.substring( name.length, c.length ); 66 } 67 } 68 return null; 69 } 70 71 cookies.deleteItem = function ( name ) { 72 document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; 73 }; 74 75 return cookies; 76 77 }( COOKIES ) ); 78 79 /** 80 * Set widget height on mobile 81 **********************************************************************/ 82 (function () { 83
84 $.subscribe('cleverwa-widget-ready', function () { 85 var container = $( '.cleverwa-container' ) 86 , box = container.find( '.cleverwa-box' ) 87 , description = container.find( '.cleverwa-description' ) 88 , toggle = container.find( '.cleverwa-toggle' ) 89 , peopleList = container.find( '.cleverwa-people' ) 90 ; 91 92 peopleList.css({ 93 maxHeight: $( window ).height() - ( description.outerHeight() + parseInt( box.css( 'bottom' ) ) + 20 ) 94 }); 95 96 }); 97 98 }()); 99 100 /** 101 * Centralize the process of hide/show of the box. 102 **********************************************************************/ 103 (function () { 104 var neverBeenResized = true; 105 $.subscribe('cleverwa-widget-ready', function () { 106 var cleverwa = DOM.body.find( '.cleverwa-container' ), 107 toggleBox = function ( e ) { 108 cleverwa.toggleClass( 'cleverwa-show' ); 109 110 if ( ! COOKIES.getItem( 'cleverwa' ) ) { 111 COOKIES.setItem( 'cleverwa', 'toggled', 1 ); 112 } 113 }; 114 $.subscribe('cleverwa-toggle-box', toggleBox); 115 }); 116 117 }()); 118 119 /** 120 * Show and hide the box. 121 **********************************************************************/ 122 (function () { 123
124 $.subscribe('cleverwa-widget-ready', function () { 125 126 var cleverwaFlag = DOM.body.find( '.cleverwa-flag' ), 127 cleverwa = DOM.body.find( '.cleverwa-container' ), 128 delayTime = parseInt( cleverwa.data( 'delay-time' ) ), 129 inactiveTime = parseInt( cleverwa.data( 'inactive-time' ) ), 130 scrollLength = parseInt( cleverwa.data( 'scroll-length' ) ), 131 autoDisplayOnMobile = cleverwa.data( 'auto-display-on-mobile' ), 132 box = cleverwa.find( '.cleverwa-box' ), 133 toggle = cleverwa.find( '.cleverwa-toggle' ), 134 close = cleverwa.find( '.cleverwa-close' ), 135 bottomClose = cleverwa.find( '.cleverwa-mobile-close' ), 136 autoShow 137 ; 138 139 if ( ! cleverwa.length || ! cleverwaFlag.length ) { 140 return; 141 } 142 143 /* Set the box's display to block. Its visibility still hidden though. */ 144 box.addClass( 'cleverwa-js-ready' ); 145 146 /* Toggle box on toggle's (or close's) click */ 147 toggle.add( close ).add( bottomClose ).on( 'click', function () { 148 $.publish('cleverwa-toggle-box'); 149 } ); 150 151 /* We're done here if the screen is small. */ 152 if ( DOM.isSmallScreen && cleverwa.is( '.cleverwa-disable-auto-display-on-small-screen' ) ) { 153 return; 154 } 155 156 /* Show box after a delay time on page load and only if it has 157 not been shown before. 158 */ 159 if ( delayTime > 0 ) { 160 autoShow = setTimeout( function () { 161 if ( ! box.is( '.cleverwa-show' ) && ! COOKIES.getItem( 'cleverwa' ) ) { 162 $.publish('cleverwa-toggle-box'); 163 } 164 }, delayTime * 1000 ); 165 } 166 167 /* Trigger after inactivity and only if it has not been shown 168 before. 169 */ 170 var cb, 171 executed = false, 172 events = 'mousemove mousedown mouseup onkeydown onkeyup focus scroll', 173 showAfterInactivity = function () { 174 clearTimeout( cb ); 175 if ( ! executed ) { 176 cb = setTimeout(function () { 177 if ( ! COOKIES.getItem( 'cleverwa' ) && ! box.is( '.cleverwa-show' ) ) { 178 $.publish('cleverwa-toggle-box'); 179 } 180 $( document ).off( events, showAfterInactivity ); 181 }, inactiveTime * 1000 ); 182 } 183 }; 184 185 if ( inactiveTime > 0 ) { 186 $( document ).on( events, showAfterInactivity ); 187 } 188 189 /* Trigger after scrolling. 190 Accessing DOM on-scroll is a bad idea. Let's execute the function 191 every half a second during/post scroll instead. 192 */ 193 var percentage = Math.abs( scrollLength ) / 100, 194 scrolling, 195 timing = true, 196 scrollHandler = function() { 197 198 scrolling = true; 199 200 if ( timing ) { 201 202 setTimeout(function () { 203 if ( $( window ).scrollTop() >= ( $( document ).height() - $( window ).height() ) * percentage ){ 204 if ( ! COOKIES.getItem( 'cleverwa' ) && ! box.is( '.cleverwa-show' ) ) { 205 $.publish('cleverwa-toggle-box'); 206 } 207 $( window ).off( 'scroll', scrollHandler ); 208 } 209 timing = true; 210 scrolling = false; 211 }, 500 ); 212 213 if ( scrolling ) { 214 timing = false; 215 } 216 } 217 } 218 ; 219 220 if ( scrollLength > 0 ) { 221 $( window ).on( 'scroll', scrollHandler ); 222 } 223 }); 224 225 }()); 226 227 /** 228 * If avatar is not provided or provided but error, add a hint to 229 * .cleverwa-face so we can show a default image. 230 **********************************************************************/ 231 (function () { 232
233 $.subscribe('cleverwa-widget-ready', function () { 234 DOM.body.find( '.cleverwa-container .cleverwa-face' ).each(function () { 235 var el = $( this ), 236 img = el.find( 'img' ), 237 noImage = true 238 ; 239 240 if ( img.length ) { 241 var url = img.attr( 'src' ), 242 tester = new Image(); 243 tester.src = url; 244 245 tester.onerror = function () { 246 el.addClass( 'no-image' ); 247 }; 248 249 } 250 else { 251 el.addClass( 'no-image' ); 252 } 253 254 }); 255 }); 256 257 }()); 258 259 /** 260 * If we're on desktop, use web.whatsapp.com instead. But if not, 261 * remove the target attribute because it will simply open the app. 262 **********************************************************************/ 263 (function () { 264 265 var alterURL = function () { 266 var cleverwaAccounts = DOM.body.find( 'a.cleverwa-account' ), 267 cleverwaFlag = DOM.body.find( '.cleverwa-flag' ) 268 ; 269 270 if ( ! cleverwaFlag.length ) { 271 return; 272 } 273 274 /* Change URL to web.whatsapp.com if the user is using a desktop. */ 275 if ( DOM.isLargeScreen ) { 276 277 cleverwaAccounts.each(function () { 278 var el = $( this ), 279 number = el.data( 'number' ), 280 text = el.data( 'auto-text' ) 281 ; 282 283 if ( '' === number ) { 284 return true; 285 } 286 el.attr( 'href', 'https://web.whatsapp.com/send?phone=' + number + '&text=' + text ); 287 }); 288 289 } 290 else { 291 if ( window.location === window.parent.location ) { 292 cleverwaAccounts.removeAttr( 'target' ); 293 } 294 } 295 }; 296 297 alterURL(); 298
299 $.subscribe('cleverwa-widget-ready', function () { 300 alterURL(); 301 }); 302 303 $.subscribe('cleverwa-woo-ready', function () { 304 alterURL(); 305 }); 306 307 $.subscribe('cleverwa-button-ready', function () { 308 alterURL(); 309 }); 310 311 }()); 312 313 /** 314 * Get widget via AJAX 315 **********************************************************************/ 316 (function () { 317 318 var cleverwasw = DOM.body.find( '#cleverwa-show-widget' ); 319 if ( cleverwasw.length < 1 ) { 320 return; 321 } 322 323 var data = { 324 'action': 'cleverwa_display_widget', 325 'when': Date.now(), 326 'current-language': cleverwasw.data( 'current-language' ), 327 'ids': cleverwasw.data( 'ids' ), 328 'page-title': cleverwasw.data( 'page-title' ), 329 'page-url': cleverwasw.data( 'page-url' ) 330 }; 331 332 $.post( ajax_object.ajax_url, data, function( response ) { 333 334 if ( 'no-show' === response ) { 335 return; 336 } 337 338 var hideOnLargeScreen = true 339 , hideOnSmallScreen = true 340 ; 341 342 $( response ).find( '.cleverwa-account' ).each(function () { 343 var el = $( this ); 344 345 if ( DOM.isLargeScreen && ! el.is( '.cleverwa-hide-on-large-screen' ) ) { 346 hideOnLargeScreen = false; 347 } 348 349 if ( ( DOM.isSmallScreen || DOM.isMobileScreen ) && ! el.is( '.cleverwa-hide-on-small-screen' ) ) { 350 hideOnSmallScreen = false; 351 } 352 }); 353 354 if ( ( DOM.isLargeScreen && ! hideOnLargeScreen ) || ( ( DOM.isMobileScreen || DOM.isSmallScreen ) && ! hideOnSmallScreen ) ) { 355 $( response ).appendTo( DOM.body ); 356 } 357 358 setTimeout( function () { 359 $.publish('cleverwa-widget-ready'); 360 }, 100 ); 361 }); 362 363 }()); 364 365 /** 366 * Send event to Google Analytics 367 **********************************************************************/ 368 (function () { 369 370 /* Remove gdpr container if user already gave her consent. */
371 $.subscribe('cleverwa-widget-ready', function () { 372 if ( COOKIES.getItem( 'consent' ) ) { 373 DOM.body.find( '.cleverwa-gdpr' ).remove(); 374 } 375 }); 376 377 DOM.body.on( 'click', 'a.cleverwa-account', function ( e ) { 378 379 var el = $( this ) 380 , number = parseInt( el.data( 'number' ) ) 381 , gaLabel = el.data( 'ga-label' ) 382 , parent = el.parents( '.cleverwa-people' ) 383 , gdpr = parent.find( '.cleverwa-gdpr' ) 384 , confirmation = gdpr.find( '.cleverwa-confirmation' ) 385 , consent = gdpr.find( '#cleverwa-consent' ) 386 , to 387 ; 388 389 /* If consent needed and is not given yet. */ 390 if ( consent.length && ! consent.is( ':checked' ) ) { 391 confirmation.addClass( 'cleverwa-blink' ); 392 393 confirmation.one( 'animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function () { 394 confirmation.removeClass( 'cleverwa-blink' ); 395 } ); 396 397 e.preventDefault(); 398 return false; 399 } 400 401 /* If the script reaches here, the user gave her consent. */ 402 if ( el.closest( '.cleverwa-container' ).length ) { 403 COOKIES.setItem( 'consent', 'acquired', 1 ); 404 gdpr.remove(); 405 } 406 407 /* Click analytics. Try to send data to Google or Facebook. */ 408 try { 409 gtag( 'event', 'Button Clicked', { 410 'event_category': 'WhatsApp Click to Chat', 411 'event_label': 'WhatsApp Account: ' + gaLabel 412 } ); 413 } 414 catch ( error ) { 415 /* It seems like the site doesn't have Google Analytics installed. */ 416 window.console && console.log( 'Catched from WhatsApp Click to Chat: ' + error.message ); 417 } 418 419 try { 420 ga( 'send', 'event', 'WhatsApp Click to Chat', 'Button Clicked', 'WhatsApp Account: ' + gaLabel ); 421 } 422 catch ( error ) { 423 /* It seems like the site doesn't have Google Analytics installed. */ 424 window.console && console.log( 'Catched from WhatsApp Click to Chat: ' + error.message ); 425 } 426 427 try { 428 _gaq.push([ '_trackEvent', 'WhatsApp Click to Chat', 'Button Clicked', 'WhatsApp Account: ' + gaLabel ]); 429 } 430 catch ( error ) { 431 /* It seems like the site doesn't have Google Analytics installed. */ 432 window.console && console.log( 'Catched from WhatsApp Click to Chat: ' + error.message ); 433 } 434 435 try { 436 dataLayer.push({ 437 'event': 'customEvent', 438 'eventCategory': 'WhatsApp Click to Chat', 439 'eventAction': 'Button Clicked', 440 'eventLabel': 'WhatsApp Account: ' + gaLabel 441 }); 442 } 443 catch ( error ) { 444 /* It seems like the site doesn't have Google Analytics installed. */ 445 window.console && console.log( 'Catched from WhatsApp Click to Chat: ' + error.message ); 446 } 447 448 var pixeled = false; 449 try { 450 /* Facebook pixel yields warning when we initiate fbq multiple times. */ 451 if ( ! pixeled ) { 452 fbq( 'trackCustom', 'ClickToChat', { 453 event: 'Chat started', 454 account: gaLabel 455 }); 456 pixeled = true; 457 } 458 } 459 catch ( error ) { 460 /* It seems like the site doesn't have Google Analytics installed. */ 461 window.console && console.log( 'Catched from WhatsApp Click to Chat: ' + error.message ); 462 } 463 464 } ); 465 466 }()); 467 468 /** 469 * Button uses ajax to fetch data so we can have an accurate display 470 * even when the page is cached. 471 **********************************************************************/ 472 (function () { 473 474 var ids = [] 475 , pageURL = '' 476 , pageTitle = '' 477 ; 478 479 DOM.body.find( '.cleverwa-button-container' ).each(function () { 480 var el = $( this ) 481 , id = el.data( 'target-id' ) 482 ; 483 484 pageURL = el.data( 'page-url' ); 485 pageTitle = el.data( 'page-title' ); 486 487 ids.push( id ); 488 }); 489 490 if ( ids.length < 1 ) { 491 return; 492 } 493 494 var data = { 495 'action': 'cleverwa_display_buttons', 496 'when': Date.now(), 497 'ids': ids.join( ',' ), 498 'page-title': pageTitle, 499 'page-url': pageURL 500 }; 501 502 $.post( ajax_object.ajax_url, data, function( response ) { 503 504 if ( response === 'none' ) { 505 return false; 506 } 507 508 var data = JSON.parse( response ); 509 510 for ( var key in data ) { 511 if ( data.hasOwnProperty( key ) ) { 512 var val = data[ key ]; 513 DOM.body.find( '.cleverwa-button-container#cleverwa-button-' + val.id ).append( val.content ); 514 515 /* Remove this item from the ids. Those which are not removed means 516 that it doesn't have the button to show. */ 517 var index = ids.indexOf( val.id ); 518 if ( index > -1 ) { 519 ids.splice( index, 1 ); 520 } 521 } 522 } 523 524 /* If there's an offline account which doesn't have an offline description, 525 then remove the container. */ 526 for ( var i = 0, limit = ids.length; i < limit; i++ ) { 527 DOM.body.find( '.cleverwa-button-container#cleverwa-button-' + ids[i] ).remove(); 528 } 529 530 setTimeout( function () { 531 $.publish('cleverwa-button-ready'); 532 }, 100 ); 533 534 }); 535 536 }()); 537 538 /** 539 * WooCommerce buttons are loaded using AJAX and treated differently 540 * from the other buttons because it has randomize and limit features. 541 **********************************************************************/ 542 (function () { 543 544 var ids = '' 545 , pageURL = '' 546 , pageTitle = '' 547 ; 548 549 DOM.body.find( '.cleverwa-wc-buttons-container' ).each(function () { 550 var el = $( this ); 551 552 pageURL = el.data( 'page-url' ); 553 pageTitle = el.data( 'page-title' ); 554 ids = el.data( 'ids' ); 555 }); 556 557 if ( ids.length < 1 ) { 558 return; 559 } 560 561 var data = { 562 'action': 'cleverwa_display_buttons', 563 'when': Date.now(), 564 'ids': ids, 565 'type' : 'woocommerce_button', 566 'page-title': pageTitle, 567 'page-url': pageURL 568 }; 569 570 $.post( ajax_object.ajax_url, data, function( response ) { 571 572 if ( response === 'none' ) { 573 return false; 574 } 575 576 var data = JSON.parse( response ); 577 578 for ( var key in data ) { 579 if ( data.hasOwnProperty( key ) ) { 580 var val = data[ key ]; 581 DOM.body.find( '.cleverwa-wc-buttons-container' ).append( $( '<p>' + val.content + '</p>' ) ); 582 } 583 } 584 585 setTimeout( function () { 586 $.publish('cleverwa-woo-ready'); 587 }, 100 ); 588 589 }); 590 591 }()); 592 593 /** 594 *
595 **********************************************************************/ 596 (function () { 597 598 599 600 }()); 601 602});
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.