/** * jQuery WP-SmartCrop v2.0.6 * Copyright (c) 2017 Greg Schoppe * License: http://www.opensource.org/licenses/mit-license.php **/ !function(t){t.fn.extend({wpsmartcrop:function(e){e=t.extend({focal_point:null,compatibility:null,focus_mode:"power-lines"},e);for(var o=0,i=["webkit","moz"],n=window.requestAnimationFrame,a=window.cancelAnimationFrame,r=0;rr&&(n=r),n<0&&(n=0),-1*n},i=t.data("smartcrop-focus"),n=t.data("smartcrop-mode"),a=[t[0].naturalWidth?t[0].naturalWidth:t[0].getAttribute("width"),t[0].naturalHeight?t[0].naturalHeight:t[0].getAttribute("height")],r=[t.width(),t.height()],s=function(t,e){var o=t[0]/t[1];return o>e[0]/e[1]?[Math.round(e[1]*o),e[1]]:[e[0],Math.round(e[0]/o)]}(a,r),c=[0,0];return i||(i=e.focal_point),!(!i||i.length<2||!a[0]||!a[1]||!r[0]||!r[1])&&(n||(n=void 0!==window.wpsmartcrop_focus_mode?e.focus_mode:"power-lines"),r[0]/r[1]").addClass("wpsmartcrop-overlay").append(s).insertAfter(o)}d(o,r);var p=!1;t(window).resize(function(){a(p),p=n(function(){d(o,r)})}),t(window).on("load",function(){d(o,r)}),o.on("wpsmartcrop-redraw",function(){d(o,r)})}}})}})}(jQuery),jQuery(document).ready(function(t){"undefined"==typeof wpsmartcrop_options&&(wpsmartcrop_options={});var e=function(){t("img.wpsmartcrop-image").wpsmartcrop(wpsmartcrop_options)};e();var o=o||navigator.userAgent,i=-1!==o.indexOf("Trident/7");"MutationObserver"in window&&!i&&new MutationObserver(function(t){e()}).observe(document.documentElement,{attributes:!1,characterData:!1,childList:!0,subtree:!0})}); ; (function ($) { const brookingsApp = { hubspot: { init: function () { // view subscriptions jQuery(document).on("submit", "form#hs-view-subscriptions", function (e) { e.preventDefault(); brookingsApp.hubspot.trigger('brookingsViewSubscriptions'); }); jQuery(window).on('brookingsViewSubscriptions', function () { brookingsApp.hubspot.viewSubscriptions(); }); // subscribe to newsletter(s) from main newsletter landing page jQuery(document).on("submit", "form#hs-subscribe-all", function (e) { e.preventDefault(); brookingsApp.hubspot.trigger('brookingsSubscribeAll'); }); jQuery(document).on("submit", "form#hs-subscribe-all-mobile", function (e) { e.preventDefault(); brookingsApp.hubspot.trigger('brookingsSubscribeAll'); }); jQuery(window).on('brookingsSubscribeAll', function () { brookingsApp.hubspot.subscribeToAll('subscribe', $('form#hs-subscribe-all').serialize()); }); // unsubscribe from all newsletter(s) on main newsletter landing page jQuery(document).on("submit", "form#hs-unsubscribe-all", function (e) { e.preventDefault(); brookingsApp.hubspot.trigger('brookingsUnsubscribeAll'); }); jQuery(window).on('brookingsUnsubscribeAll', function () { const checkboxes = document.querySelectorAll('.cb-newsletter-subscribe'); checkboxes.forEach(function (checkbox) { checkbox.checked = false; }); brookingsApp.hubspot.subscribeToAll('unsubscribe', $('form#hs-unsubscribe-all').serialize()); $('#unsubscribe-modal .uk-modal-close').click(); }); // subscribe via component jQuery(document).on("submit", "form.subscribe-component", function (e) { e.preventDefault(); var formID = jQuery(this).attr('id'); brookingsApp.hubspot.trigger('subscribeFromComponent', formID); }); jQuery(window).on('subscribeFromComponent', function (e) { brookingsApp.hubspot.subscribeFromComponent(e); }); }, trigger: function (tmAction, id=null) { var customAction = tmAction; var formId = id; grecaptcha.execute(tmscripts.recaptchaKey, {action: 'forms'} ).then(function (token) { var recaptchaElements = document.getElementsByName('g-recaptcha-response'); for ( var i = 0; i < recaptchaElements.length; i++ ) { recaptchaElements[i].value = token; } window.dispatchEvent(new CustomEvent(customAction, {'detail': { 'formId': formId }})); }); }, viewSubscriptions: function () { const validationCheck = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const email = jQuery('form#hs-view-subscriptions').find('input[type="email"]').val(); if ( email && String(email) .toLowerCase() .match( validationCheck ) ) { jQuery.ajax({ url: tmscripts.ajaxurl, type: "POST", data: { action: "brookings_hubspot_get_lists", email: email, formData: jQuery('form#hs-view-subscriptions').serialize(), nonce: tmscripts.security, }, success: function (response) { if (response.success && response.data && typeof response.data.lists === 'object' && Object.keys(response.data.lists).length > 0) { // check and checkboxes with an ID that matches a list ID jQuery.each(response.data.lists, function (index, value) { if ( 'true' == value ) { jQuery(`input#${index}`).prop('checked', true); } }); // populate email address on subscribe and unsubscribe forms document.querySelector('#hs-subscribe-all input[type="email"]').value = response.data.email; document.querySelector('#hs-unsubscribe-all input[type="email"]').value = response.data.email; } else { console.log('no lists found for email address'); } $('#subscribe-modal .uk-modal-close').click(); }, error: function (error) { console.log(error); }, }); } else { // invalid email address } }, subscribeToAll: function (type, formData) { const checkboxes = document.querySelectorAll('.cb-newsletter-subscribe'); const properties = {}; var subscribeType = type; var newsletterPrograms = []; var newsletterNames = []; checkboxes.forEach(function (checkbox) { if (checkbox.checked) { properties[checkbox.getAttribute('name')] = true; newsletterPrograms.push(checkbox.getAttribute('data-program')); newsletterNames.push(checkbox.getAttribute('data-name')); } else { properties[checkbox.getAttribute('name')] = false; } }); // add to window.subscriptionData for consumption in DataLayer event. window.subscriptionData = { 'newsletterProgram': newsletterPrograms.join(','), 'newsletterName': newsletterNames.join(',') }; jQuery.ajax({ url: tmscripts.ajaxurl, type: "POST", data: { action: "brookings_newsletters_subscribe", properties: properties, formData: formData, nonce: tmscripts.security, }, success: function (response) { if ( 'subscribe' == subscribeType ) { if ( response.success ) { document.querySelector('#hs-subscribe-all .email-and-submit').style.display = 'none'; document.querySelector('#hs-subscribe-all .disclaimer-text').style.display = 'none'; document.querySelector('#hs-subscribe-all .subscriber-disclaimer').style.display = 'none'; document.querySelector('#hs-subscribe-all .form-message').innerHTML = response.data; window.dispatchEvent(new CustomEvent('brookings_newsletter_subscribe_success')); } else { console.log( response ); document.querySelector('#hs-subscribe-all .form-message').innerHTML = 'There was an error subscribing to the newsletter. Please try again later.'; } } else { console.log( response ); } }, error: function (error) { console.log(error); if ( 'subscribe' == subscribeType ) { document.querySelector('#hs-subscribe-all .form-message').innerHTML = "There was a problem subscribing to the newsletter(s). Please try again later."; } }, }); }, subscribeFromComponent: function (e) { // check whether there are any checkboxes checked var checkboxes = jQuery(`#${e.detail.formId}`).find('input[type="checkbox"]'); var checked = false; var newsletterPrograms = []; var newsletterNames = []; checkboxes.each(function () { if (jQuery(this).is(':checked')) { checked = true; newsletterPrograms.push(jQuery(this).data('program')); newsletterNames.push(jQuery(this).data('name')); } }); // add to window.subscriptionData for consumption in DataLayer event. window.subscriptionData = { 'newsletterProgram': newsletterPrograms.join(','), 'newsletterName': newsletterNames.join(',') }; if ( false == checked ) { jQuery(`#${e.detail.formId} .form-message`).text('Please select at least one newsletter to subscribe to.'); return; } var formData = jQuery(`#${e.detail.formId}`).serialize(); var formMessage = jQuery(`#${e.detail.formId}`).find('.form-message'); var formFields = jQuery(`#${e.detail.formId}`).find('.subscribe-form-fields'); jQuery.ajax({ url: tmscripts.ajaxurl, type: "POST", data: { action: "brookings_newsletter_subscribe", formData: formData, nonce: tmscripts.security, }, success: function (response) { if (response.success) { formMessage.html(response.data); $(formFields).hide(); window.dispatchEvent(new CustomEvent('brookings_newsletter_subscribe_success')); } else { console.log(response); formMessage.html(response.data); } }, error: function (error) { console.log(error); // formMessage.html(error.data); }, }); }, }, filters: { init: function () { jQuery(document).ready(function () { var url = new URL(window.location.href); var expertTopics = url.searchParams.get("ext"); var expertPrograms = url.searchParams.get("exp"); if (expertTopics) { expertTopics = expertTopics.replace(/[^a-zA-Z0-9-]/g, ''); jQuery(`#topicFilters .form-checkbox[data-slug="${expertTopics}"]`).click(); } if ( expertPrograms ) { expertPrograms = expertPrograms.replace(/[^a-zA-Z0-9-]/g, ''); jQuery(`#researchProgramFilters .form-checkbox[data-slug="${expertPrograms}"]`).click(); } // jQuery(document).on('click', '.form-checkbox', function () { // const slug = jQuery(this).data('slug'); // if (slug) { // // var name = jQuery(this).attr('name'); // brookingsApp.filters.updateUrl({ // 'ext': slug // }); // } // }); }); }, updateUrl: function(params) { var url = new URL(window.location.href); var search_params = url.searchParams; Object.entries(params).map(item => { search_params.set(item[0], item[1]); }); window.history.replaceState(null, null, url) } }, document: { init: function () { jQuery(document).ready(function () { setTimeout(function () { // scroll to interactive component if share link if (window.location.href.indexOf("share") > -1) { var interactiveId = window.location.href.split("share=")[1]; var interactiveComponent = document.getElementById(interactiveId); if (interactiveComponent) { const yOffset = -140; const y = interactiveComponent.getBoundingClientRect().top + window.pageYOffset + yOffset; window.scrollTo({top: y, behavior: 'instant'}); } } }, 500); // add support for ol 'start' attribute const lists = document.querySelectorAll('ol[start]'); lists.forEach(function(el, index) { let count = el.getAttribute('start') - 1; el.style.counterReset = 'ordered ' + count; }); // responsive iframes iFrameResize( { log: false, scrolling: false, checkOrigin: false, autoResize: false, }, "iframe.brookings-interactive:not(.autoresize)" ); iFrameResize( { log: false, scrolling: false, checkOrigin: false, autoResize: true, }, "iframe.brookings-interactive.autoresize" ); // add target="_blank" to external links const allLinks = document.querySelectorAll('a'); if ( allLinks.length ) { allLinks.forEach(link => { var href = link.getAttribute('href'); if ( ! brookingsApp.document.isValidUrl(href) ) { return false; } var url = new URL(href); if (! url.hostname.includes('brookings') ) { link.setAttribute('target', '_blank'); } }); } }); }, isValidUrl: urlString => { try { return Boolean(new URL(urlString)); } catch(e){ return false; } } }, dataLayer: { init: function () { // page load started window.addEventListener('DOMContentLoaded', (event) => { brookingsApp.dataLayer.pageLoadStarted(); }); // page load completed window.addEventListener('load', (event) => { brookingsApp.dataLayer.pageLoadCompleted(); }); // Content Interaction -- when users click on any CTA or links to download, print, or share content posts jQuery(document).on('click', '.share-and-print a.btn-social, .slideout-nav .social-icons a.btn-social', function (e) { // post share click brookingsApp.dataLayer.contentInteraction('social share'); }); jQuery(document).on('click', '.print-btn', function (e) { // post print click brookingsApp.dataLayer.contentInteraction('print'); }); jQuery(document).on('click', '.sidebar-block.downloads a.btn', function (e) { // download click brookingsApp.dataLayer.contentInteraction('download'); }); jQuery(document).on('click', '.sidebar-block.print-sidebar a.btn', function (e) { // print click brookingsApp.dataLayer.contentInteraction('print'); }); jQuery(document).on('click', '.brookings-interactive .share-links a.btn', function (e) { // interactive share click brookingsApp.dataLayer.contentInteraction('interactive share'); }); // register for event jQuery(document).on('click', '.event-actions a.btn', function (e) { // get href const href = jQuery(this).attr('href'); brookingsApp.dataLayer.eventRegistration(href); }); // interactive view window.addEventListener('load', (event) => { const elements = document.querySelectorAll('.brookings-interactive'); if ( elements.length ) { // callback function for when interactive is in view const callback = (entries, observer) => { entries.forEach((entry) => { if (entry.isIntersecting) { const interactiveId = entry.target.getAttribute('data-identifier'); const interactiveName = entry.target.getAttribute('data-name'); const interactiveType = entry.target.getAttribute('data-type'); brookingsApp.dataLayer.interactiveView({ 'interactiveId': interactiveId, 'interactiveName': interactiveName, 'interactiveType': interactiveType }); observer.unobserve(entry.target); } }); }; // Create new observer instance const observer = new IntersectionObserver(callback, { root: null, rootMargin: '0px', threshold: 0.1 }); // Observe each interactive elements.forEach((element) => { observer.observe(element); }); } }); // algoliaInteraction jQuery(document).on('algolia:search:init', function (e) { brookingsApp.dataLayer.algoliaInteraction({key: 'algolia_search_init', label: 'Algolia Search Initialization', value: null}); }); jQuery('#contentStream, .algolia-modal').on('click', function (e) { if ( jQuery(e.target).is('input[type="checkbox"]') ) { brookingsApp.dataLayer.algoliaInteraction({key: 'algolia_filter', label: 'Algolia Filter', value: jQuery(e.target).val()}); } else if ( jQuery(e.target).is('input[type="radio"]') ) { brookingsApp.dataLayer.algoliaInteraction({key: 'algolia_filter', label: 'Algolia Filter', value: jQuery(e.target).next().text()}); } }); // algolia search input on blur jQuery('#contentStream').on('blur', 'input[type="search"], #overlay-search .search-field, #searchFacet .search-field', function (e) { console.log( 'Algolia Search' ); brookingsApp.dataLayer.algoliaInteraction({key: 'algolia_search', label: 'Algolia Search', value: jQuery(this).val()}); }); // home page link click events if ( document.querySelector('body').classList.contains( 'home' ) ) { const homepageClickEvents = { '.byo-block.featured-content a' : 'Featured Content block', '.byo-block.home--about a' : 'About Brookings block', '.byo-block.home--experts a' : 'Experts block', '.byo-block .home-events-block a' : 'Events block', '.byo-block.home--programs a' : 'Programs block', '.byo-block .explain-swiper a' : 'Brookings Explains block', '.byo-block .podcast2-swiper a' : 'Podcasts block', }; Object.entries(homepageClickEvents).forEach(( [key, value] ) => { if ( document.querySelectorAll(key) ) { jQuery(document).on('click', key, function (e) { brookingsApp.dataLayer.navigationClick(value, e); }); } }); } // global component click events const componentClickEvents = { '.byo-block .related-content-feed a' : 'Related Content block', '.sidebar-block.read-the-series a' : 'Read More From widget', '.byo-block.impact a' : 'Impact block', }; Object.entries(componentClickEvents).forEach(( [key, value] ) => { if ( document.querySelector(key) ) { jQuery(document).on('click', key, function (e) { brookingsApp.dataLayer.navigationClick(value, e); }); } }); // navigation click jQuery(document).on('click', 'nav li.menu-item', function (e) { const navName = jQuery(this).closest('nav').attr('data-nav'); brookingsApp.dataLayer.navigationClick(navName, e); }); // sign up jQuery(window).on('brookings_newsletter_subscribe_success', function (e) { brookingsApp.dataLayer.newsletterSignup(); }); // Contact Us Completed jQuery(document).on('gform_confirmation_loaded', function(event, formId) { brookingsApp.dataLayer.contactUsCompleted(formId); }); }, pageLoadStarted: function () { window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ page_data: null }); // Clear the previous page_data object. dataLayer.push({ "event": "page_load_started", "detailed_event": "Page Load Started", "event_data": { "archived": this.getData('archived'), "author": this.getData('author'), "author_id": this.getData('author_id'), "author_type": this.getData('author_type'), "center": this.getData('center'), "content_type": this.getData('content_type'), "historical_url": this.getData('historical_url'), "original_post_id": this.getData('original_post_id'), "parent": this.getData('parent'), "post_id": this.getData('post_id'), "primary_topic": this.getData('primary_topic'), "program": this.getData('program'), "project": this.getData('project'), "publish_date": this.getData('publish_date'), "region": this.getData('region'), "subtype": this.getData('subtype'), "tag_id": this.getData('tag_id'), "tags": this.getData('tags'), "template": this.getData('template'), "topic": this.getData('topic'), "type": this.getData('type'), "word_count": this.getData('word_count'), }, "page_data": { "language": this.getData('language'), "name": this.getData('title'), "page_location": window.location.href, } }); }, pageLoadCompleted: function () { window.dataLayer = window.dataLayer || []; dataLayer.push({ "event": "page_view", "detailed_event": "Page Load Completed" }); }, contentInteraction: function (interactionType) { window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "content_interaction", "detailed_event": "Content Interaction", "event_data": { "archived": this.getData('archived'), "author": this.getData('author'), "author_id": this.getData('author_id'), "author_type": this.getData('author_type'), "center": this.getData('center'), "content_type": this.getData('content_type'), "historical_url": this.getData('historical_url'), "original_post_id": this.getData('original_post_id'), "parent": this.getData('parent'), "post_id": this.getData('post_id'), "primary_topic": this.getData('primary_topic'), "program": this.getData('program'), "project": this.getData('project'), "publish_date": this.getData('publish_date'), "region": this.getData('region'), "subtype": this.getData('subtype'), "tag_id": this.getData('tag_id'), "tags": this.getData('tags'), "template": this.getData('template'), "method": interactionType, "topic": this.getData('topic'), "type": this.getData('type'), "word_count": this.getData('word_count'), "language": this.getData('language'), "interactive": interactionType === 'interactive share' ? 'Yes' : 'No', } }); }, navigationClick: function (navName, event) { let navHref = null; let navText = null; if ( 'SPAN' == event.target.nodeName ) { event.target = event.target.parentNode; } if ( 'A' === event.target.nodeName ) { navHref = jQuery(event.target).attr('href'); navText = jQuery(event.target).text().trim(); } let campaign = null; let campaignElement = jQuery(event.target).closest('.block-campaign'); if ( campaignElement.length ) { campaign = campaignElement.data('campaign'); } window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "navigation_click", "detailed_event": "Navigation Click", "event_data": { "archived": this.getData('archived'), "author": this.getData('author'), "author_id": this.getData('author_id'), "author_type": this.getData('author_type'), "center": this.getData('center'), "historical_url": this.getData('historical_url'), "language": this.getData('language'), "module_name": navName, "module_link": navHref, "module_text": navText, "module_campaign": campaign, "original_post_id": this.getData('original_post_id'), "parent": this.getData('parent'), "post_id": this.getData('post_id'), "primary_topic": this.getData('primary_topic'), "project": this.getData('project'), "publish_date": this.getData('publish_date'), "region": this.getData('region'), "subtype": this.getData('subtype'), "tag_id": this.getData('tag_id'), "tags": this.getData('tags'), "template": this.getData('template'), "topic": this.getData('topic'), "type": this.getData('type'), "word_count": this.getData('word_count'), } }); }, newsletterSignup: function () { var newsletterPrograms = ''; var newsletterNames = ''; if ( 'undefined' !== typeof window.subscriptionData.newsletterProgram ) { newsletterPrograms = window.subscriptionData.newsletterProgram; } if ( 'undefined' !== typeof window.subscriptionData.newsletterName ) { newsletterNames = window.subscriptionData.newsletterName; } window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "sign_up", "detailed_event": "Sign Up", "event_data": { // "event_count": "", "newsletter_name": newsletterNames, "program": newsletterPrograms } }); }, interactiveView: function (data) { window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "interactive_view", "detailed_event": "Interactive View", "event_data": { "archived": this.getData('archived'), "author": this.getData('author'), "author_id": this.getData('author_id'), "center": this.getData('center'), "chart_name": data.interactiveName, "chart_id": data.interactiveId, "content_type": this.getData('content_type'), "historical_url": this.getData('historical_url'), "interactive_name": data.interactiveName, "interactive_type": data.interactiveType, "language": this.getData('language'), "original_post_id": this.getData('original_post_id'), "parent": this.getData('parent'), "post_id": this.getData('post_id'), "primary_topic": this.getData('primary_topic'), "program": this.getData('program'), "project": this.getData('project'), "publish_date": this.getData('publish_date'), "region": this.getData('region'), "subtype": this.getData('subtype'), "tag_id": this.getData('tag_id'), "tags": this.getData('tags'), "template": this.getData('template'), "topic": this.getData('topic'), "type": this.getData('type'), "word_count": this.getData('word_count'), } }); }, eventRegistration: function (href) { window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "register_event", "detailed_event": "Register Event", "event_data": { // "event_count": "", "event_name": this.getData('title'), "identifier": href } }); }, contactUsCompleted: function (formId) { var contactPurpose = ''; var author = ''; if ( formId == 1 ) { // experts form author = this.getData('title'); } else if ( formId == 2 ) { // Donation contactPurpose = 'Donation'; } else if ( formId == 3 ) { // General contact contactPurpose = 'General Contact'; // TODO: get purpose from contact form. } else { contactPurpose = 'Other'; } window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": "contact_us_complete", "detailed_event": "Contact Us Completed", "event_data": { "author": author, "contact_purpose": contactPurpose, // "method": "" } }); }, algoliaInteraction: function ( event ) { window.dataLayer = window.dataLayer || []; dataLayer.push({ event_data: null }); // Clear the previous event_data object. dataLayer.push({ "event": event.key, "detailed_event": event.label, "event_data": { "archived": this.getData('archived'), "author": this.getData('author'), "author_id": this.getData('author_id'), "center": this.getData('center'), "content_type": this.getData('content_type'), "historical_url": this.getData('historical_url'), "language": this.getData('language'), "original_post_id": this.getData('original_post_id'), "parent": this.getData('parent'), "post_id": this.getData('post_id'), "primary_topic": this.getData('primary_topic'), "program": this.getData('program'), "project": this.getData('project'), "publish_date": this.getData('publish_date'), "region": this.getData('region'), "subtype": this.getData('subtype'), "tag_id": this.getData('tag_id'), "tags": this.getData('tags'), "template": this.getData('template'), "topic": this.getData('topic'), "type": this.getData('type'), "word_count": this.getData('word_count'), "algolia_value": event.value } }); }, getData: function( key ) { if (typeof window.brookings.dataLayer[key] !== 'undefined') { return window.brookings.dataLayer[key]; } return ''; } }, init: function () { brookingsApp.hubspot.init(); brookingsApp.filters.init(); brookingsApp.document.init(); brookingsApp.dataLayer.init(); }, }; brookingsApp.init(); })(jQuery);;