/**
* options: {
* enableLogs: Boolean,
* intervalFrequency: Number,
* logIdentifier: String,
* maxIterations: Number,
* onSuccess: () => void,
* successCheck: () => Boolean,
* }
*/
function waitForX(options) {
var log = function log() {
if (options.enableLogs)
console.log.apply(console, arguments)
}
var logSuffix = options.logIdentifier ? ' -- [' + options.logIdentifier + ']' : ''
var intervalFrequency = options.intervalFrequency || 100
var maxIterations = options.maxIterations || 100
// Do 1 immediate check before the interval starts.
if (options.successCheck()) {
log('SUCCESS (immediate), no interval needed' + logSuffix)
options.onSuccess()
return;
}
log('every ' + intervalFrequency + 'ms, for ' + intervalFrequency * maxIterations + 'ms' + logSuffix)
var iterations = 0
var waitTimer = window.setInterval(function () {
if (options.successCheck.apply(options)) {
log('SUCCESS, waited: ' + ((iterations + 1) * intervalFrequency) + 'ms (' + (iterations + 1) + ' iterations)' + logSuffix)
options.onSuccess.apply(options)
window.clearInterval(waitTimer);
log('cleared interval' + logSuffix)
}
if (iterations >= maxIterations) {
log('TIMED OUT' + logSuffix)
// Safety net to ensure this interval won't run forever.
window.clearInterval(waitTimer)
log('cleared interval' + logSuffix)
}
iterations++
}, options.intervalFrequency)
}
var autocompleteScript = document.createElement('script')
autocompleteScript.src = 'https://api.demandbase.com/autocomplete/widget.js'
document.body.appendChild(autocompleteScript)
var DEMANDBASE_FORM_SCRIPT_ID = 'demandbaseFormScript'
if (data.page === 'attendeeInfo') {
waitForX({
intervalFrequency: 100,
maxIterations: 150,
onSuccess: function () {
if (!document.querySelector('#' + DEMANDBASE_FORM_SCRIPT_ID)) {
var formScript = document.createElement('script')
formScript.src = 'https://events.rainfocus.com/pages/redhat/global/demandbaseform'
formScript.id = DEMANDBASE_FORM_SCRIPT_ID
document.body.appendChild(formScript)
} else {
// This is terrible, but it's the only way to re-initialize the autocomplete on a SPA.
// Fortunately, it is an uncommon use case.
window.Demandbase.CompanyAutocomplete.setup = false
window.Demandbase.CompanyAutocomplete.initialized = false
window.DemandbaseForm.formConnector.form = document.querySelector('#myDynamicForm')
window.DemandbaseForm.formConnector.init()
}
},
successCheck: function () {
return document.querySelector('#formAttendee-companyname');
},
});
}
if (data.page !== null) {
//Modified so this will run for all places in which the Global Opt-In attribute will run.
//(data.page === 'contactInfo' || data.page === 'createaccount' || data.page === 'attendeeInfo' ||data.page ==='attendeepage2') {
waitForX({
intervalFrequency: INTERVAL_DELAY,
maxIterations: MAX_INTERVAL_ITERATIONS,
onSuccess: function () {
var countrySelectBox = document.querySelector('#formAttendee-countryId');
if (countrySelectBox) {
if (!initialNCValueSet) {
initialNCValueSet = true
applyNoticeChoice({ target: countrySelectBox })
}
countrySelectBox.addEventListener('change', applyNoticeChoice);
}
},
successCheck: function () {
return document.querySelector('#' + FORM_ID);
},
});
}